diff --git a/src/BondsInterest.php b/src/BondsInterest.php index ee1a32828e50cb0251fb84f0824c6d300d4d3deb..88f4fb3c22303e0a5ea5f45e3a1cf68983f98475 100644 --- a/src/BondsInterest.php +++ b/src/BondsInterest.php @@ -55,39 +55,33 @@ class BondsInterest { } /** - * Method description. + * Create a bond payment. */ - public function createPayment($bond, $date) { - if ($bond->getDate()->format('%m-%d') === $date->format('%m-%d')) { - $gross = bcmul($bond->getParValue(), '0.02', 2); - $payment = BondPayment::create([ + public function createPayment( + ShareholderRegisterBondInterface $bond, + string $payment_type, + DrupalDateTime $payment_date, + string $gross, + string $net + ) { + if (!bccomp('0', $gross, 2)) { + return; + } + + $payment = BondPayment::create( + [ 'bond_id' => $bond->id(), - 'payment_type' => 'interest', - 'date' => $date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), + 'payment_type' => $payment_type, + 'date' => $payment_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), 'gross' => $gross, - 'net' => bcsub( - $gross, - bcmul( - $gross, - '0.3', - 2 - ), - 2 - ), + 'net' => $net, ]); - $payment->save(); - - $this->logger->info( - "Create payment for bond '@bond'", - [ - '@bond' => $bond->getTitle(), - ] - ); - } + $payment->save(); + return $payment; } /** - * Method description. + * Create all bond payments for date. */ public function createPayments($date = NULL) { if ($date === NULL) { diff --git a/src/Plugin/BondIndenture/Yearly.php b/src/Plugin/BondIndenture/Yearly.php index 6e6fc4a7504418f5c074421e582142a05ac6f1d6..93d01077a0ec528e3796ef4f51b1495e3791e854 100644 --- a/src/Plugin/BondIndenture/Yearly.php +++ b/src/Plugin/BondIndenture/Yearly.php @@ -9,9 +9,9 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; -use Drupal\shareholder_register_bonds\ShareholderRegisterBondInterface; use Drupal\shareholder_register_bonds\BondPaymentInterface; -use Drupal\shareholder_register_bonds\Entity\BondPayment; +use Drupal\shareholder_register_bonds\BondsInterest; +use Drupal\shareholder_register_bonds\ShareholderRegisterBondInterface; use Drupal\shareholder_register_bonds\Plugin\BondIndenturePluginBase; /** @@ -32,6 +32,13 @@ class Yearly extends BondIndenturePluginBase { */ protected $entityTypeManager; + /** + * The bonds interest service. + * + * @var \Drupal\shareholder_register_bonds\BondsInterest + */ + protected $bondsInterest; + /** * Constructs a new Yearly BondIndenture plugin. * @@ -43,10 +50,13 @@ class Yearly extends BondIndenturePluginBase { * The plugin implementation definition. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. + * @param \Drupal\shareholder_register_bonds\BondsInterest $bonds_interest + * The bonds interest service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, BondsInterest $bonds_interest) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; + $this->bondsInterest = $bonds_interest; } /** @@ -58,6 +68,7 @@ class Yearly extends BondIndenturePluginBase { $plugin_id, $plugin_definition, $container->get('entity_type.manager'), + $container->get('shareholder_register_bonds.interest') ); } @@ -102,24 +113,26 @@ class Yearly extends BondIndenturePluginBase { 2 ); - $payment = BondPayment::create([ - 'bond_id' => $bond->id(), - 'payment_type' => BondPaymentInterface::BOND_PAYMENT_TYPE_INTEREST, - 'date' => $payment_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), - 'gross' => $gross, - 'net' => bcsub( + $payment = $this->bondsInterest->createPayment( + $bond, + BondPaymentInterface::BOND_PAYMENT_TYPE_INTEREST, + $payment_date, + $gross, + bcsub( $gross, bcmul( $gross, - // FIXME: configurable. + // FIXME: configurable tax rate. '0.3', 2 ), 2 ), - ]); - $payment->save(); - $payments[] = $payment; + ); + + if ($payment) { + $payments[] = $payment; + } } return $payments; }