From efa2eed4c5377ab15a8d50b1ef6d82bdd0269f8d Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Wed, 28 Dec 2022 12:44:19 +0000
Subject: [PATCH] cleanup

---
 shareholder_register_bonds.links.menu.yml     |  9 ++-
 src/BondsInterest.php                         |  4 +-
 src/BondsTransactionValidation.php            | 62 +++++++++++--------
 .../ShareholderRegisterBondsSubscriber.php    | 14 ++++-
 tests/src/Kernel/YearlyTest.php               |  8 ++-
 5 files changed, 62 insertions(+), 35 deletions(-)

diff --git a/shareholder_register_bonds.links.menu.yml b/shareholder_register_bonds.links.menu.yml
index d2f66fa..69da745 100644
--- a/shareholder_register_bonds.links.menu.yml
+++ b/shareholder_register_bonds.links.menu.yml
@@ -17,13 +17,14 @@ entity.shareholder_register_bond.collection:
   description: 'Manage Bonds.'
   route_name: entity.shareholder_register_bond.collection
   parent: entity.bond_transaction.collection
-  weight: 150
+  weight: 100
 
 entity.bond_payment.collection:
   title: Bond Payments
   description: 'Bond payments'
   route_name: entity.bond_payment.collection
   parent: entity.bond_transaction.collection
+  weight: 150
 
 entity.shareholder_register_bond_type.collection:
   title: 'Bond types'
@@ -36,10 +37,12 @@ entity.bond_payment.settings:
   title: Bond Payment
   description: 'Configure a Bond Payment entity type'
   route_name:  entity.bond_payment.settings
-  parent: entity.shareholder_register.config
+  parent: entity.shareholder_register_bond_type.collection
+  weight: 100
 
 entity.bond_transaction.settings:
   title: Bond Transaction
   description: Configure a Bond Transaction entity type
   route_name:  entity.bond_transaction.settings
-  parent: entity.shareholder_register.config
+  parent: entity.shareholder_register_bond_type.collection
+  weight: 150
\ No newline at end of file
diff --git a/src/BondsInterest.php b/src/BondsInterest.php
index 4afa39a..b202f5b 100644
--- a/src/BondsInterest.php
+++ b/src/BondsInterest.php
@@ -102,7 +102,9 @@ class BondsInterest {
    */
   public function createPayments($date = NULL) {
     if ($date === NULL) {
-      $date = new DrupalDateTime();
+        $date = new DrupalDateTime('midnight', new \DateTimeZone('UTC'));
+    } else {
+        $date = new DrupalDatetime("midnight " . $date->format("Y-m-d"), new \DateTimeZone('UTC'));
     }
     $date->modify('first day of this month');
     $result = [];
diff --git a/src/BondsTransactionValidation.php b/src/BondsTransactionValidation.php
index 7584b0c..2035b25 100644
--- a/src/BondsTransactionValidation.php
+++ b/src/BondsTransactionValidation.php
@@ -13,6 +13,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
 
 use Drupal\shareholder_register\Entity\ShareTransactionGroupType;
+use Drupal\shareholder_register\Exception\ShareholderRegisterInvalidSharesException;
 use Drupal\shareholder_register\Exception\ShareholderRegisterMissingSharesException;
 use Drupal\shareholder_register_bonds\Entity\ShareholderRegisterBond;
 use Drupal\shareholder_register_bonds\Entity\BondTransaction;
@@ -301,6 +302,38 @@ class BondsTransactionValidation {
     return $transactions;
   }
 
+  /**
+   * Create repurchase transaction for bond.
+   */
+  public function createRepurchaseTransactionForBond(ShareholderRegisterBond $bond, $validate = TRUE) {
+    $transaction = BondTransaction::create(
+      [
+        'shareholder_id' => $bond->getShareholderId(),
+        'quantity' => -1,
+      ]
+    );
+    $transaction->save();
+
+    $context = [
+      'bond_ids' => [$bond->id()],
+    ];
+
+    $this->attachBondsToTransaction(
+      $transaction,
+      $bond->getMaturityDate(),
+      $context
+    );
+
+    if ($validate) {
+      $this->validateBondTransaction(
+        $transaction,
+        $bond->getMaturityDate()
+        // $context
+      );
+    }
+    return $transaction;
+  }
+
   /**
    * Handle automatic maturity.
    */
@@ -323,33 +356,8 @@ class BondsTransactionValidation {
 
     if ($matured_bond_ids) {
       foreach (ShareholderRegisterBond::loadMultiple($matured_bond_ids) as $bond) {
-        $transaction = BondTransaction::create(
-          [
-            'shareholder_id' => $bond->getShareholderId(),
-            'quantity' => -1,
-          ]
-        );
-        $transaction->save();
-
-        $context = [
-          'bond_ids' => [$bond->id()],
-        ];
-
-        $this->attachBondsToTransaction(
-          $transaction,
-          $bond->getMaturityDate(),
-          $context
-        );
-
-        if ($validate) {
-          $this->validateBondTransaction(
-            $transaction,
-            $bond->getMaturityDate()
-            // $context
-          );
-        }
-
-        $transactions[] = $transaction;
+        $transactions[] = $this->createRepurchaseTransactionForBond(
+          $bond, $validate);
       }
     }
 
diff --git a/src/EventSubscriber/ShareholderRegisterBondsSubscriber.php b/src/EventSubscriber/ShareholderRegisterBondsSubscriber.php
index 9629920..105e849 100644
--- a/src/EventSubscriber/ShareholderRegisterBondsSubscriber.php
+++ b/src/EventSubscriber/ShareholderRegisterBondsSubscriber.php
@@ -77,12 +77,24 @@ class ShareholderRegisterBondsSubscriber implements EventSubscriberInterface {
       $email = $shareholder->get('mail')->value;
       $ctx = [
         'entity' => $transaction,
+        'template_variants' => [
+          "mail_bond_{$transaction->getGroupBaseType()}_transaction_validated",
+        ],
       ];
       $params = [
         'context' => $ctx,
       ];
       $langcode = $shareholder->getPreferredLangcode();
-      $this->mailManager->mail('shareholder_register_bonds', 'mail_bond_validated', $email, $langcode, $params, NULL, TRUE);
+
+      $this->mailManager->mail(
+        'shareholder_register_bonds',
+        'mail_bond_transaction_validated',
+        $email,
+        $langcode,
+        $params,
+        NULL,
+        TRUE
+      );
     }
     catch (\Exception $e) {
       $this->messenger->addError("Could not send bond transaction mail!");
diff --git a/tests/src/Kernel/YearlyTest.php b/tests/src/Kernel/YearlyTest.php
index ea7a443..d41b040 100644
--- a/tests/src/Kernel/YearlyTest.php
+++ b/tests/src/Kernel/YearlyTest.php
@@ -217,10 +217,12 @@ class YearlyTest extends KernelTestBase {
       ]);
     $bond->save();
 
+    $payment_date = DrupalDateTime::createFromFormat(
+      DateTimeItemInterface::DATE_STORAGE_FORMAT,
+      '2023-01-05');
+
     $payments = \Drupal::service('shareholder_register_bonds.interest')->createPayments(
-      DrupalDateTime::createFromFormat(
-        DateTimeItemInterface::DATE_STORAGE_FORMAT,
-        '2023-01-05')
+      $payment_date
     );
 
     $payments_for_bond = array_filter(
-- 
GitLab