From ed07384f5d3b7da26cd37fec5e6e9ae2038ea0d4 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Fri, 3 Mar 2023 09:06:05 +0100
Subject: [PATCH 1/8] imp: use external_id on bank_transaction for uniqueness

---
 banking.install                               | 30 ++++++++++++++++++
 banking.post_update.php                       | 10 ++++++
 .../src/BankingImportService.php              |  6 ++--
 .../Plugin/BankingImportFormat/CAMT053.php    | 12 +++----
 src/BankTransactionStorageSchema.php          | 31 +++++++++++++++++++
 src/Entity/BankAccount.php                    |  1 +
 src/Entity/BankTransaction.php                | 15 +++++++++
 7 files changed, 95 insertions(+), 10 deletions(-)
 create mode 100644 src/BankTransactionStorageSchema.php

diff --git a/banking.install b/banking.install
index b03c261..d0c5ca8 100644
--- a/banking.install
+++ b/banking.install
@@ -271,3 +271,33 @@ function banking_update_8009() {
 function banking_update_8010() {
  banking_db_change_varchar_field('bank_transaction', 'account_holder_name', 100);
 }
+
+/**
+ * Install the external_id field.
+ */
+function banking_update_8011() {
+  if (!array_key_exists(
+      'external_id',
+      \Drupal::service('entity.last_installed_schema.repository')
+        ->getLastInstalledFieldStorageDefinitions(
+          'bank_transaction'))) {
+
+    $storage_definition = BaseFieldDefinition::create('string')
+      ->setLabel(t('External ID'))
+      ->setDescription(t('The ID of the Bank transaction entity from the import source (prefix with plugin ID).'))
+      ->setSettings([
+        'max_length' => 255,
+        'text_processing' => 0,
+      ])
+      ->setDefaultValue('')
+      ->setDisplayOptions('view', [
+        'label' => 'above',
+        'type' => 'string',
+        'weight' => -4,
+      ])
+      ->setDisplayConfigurable('view', TRUE);
+
+    \Drupal::entityDefinitionUpdateManager()
+      ->installFieldStorageDefinition('external_id', 'bank_transaction', 'bank_transaction', $storage_definition);
+  }
+}
diff --git a/banking.post_update.php b/banking.post_update.php
index 0e9f5c8..eff81a4 100644
--- a/banking.post_update.php
+++ b/banking.post_update.php
@@ -36,3 +36,13 @@ function banking_post_update_add_unique_transaction_account_name_key(&$sandbox)
     $schema->addUniqueKey('bank_transaction', 'bank_transaction_name_account', ['name', 'bank_account_id']);
   }
 }
+
+/**
+ * Add unique key for bank_transaction external_id / account combination.
+ */
+function banking_post_update_add_unique_transaction_account_external_id_key(&$sandbox) {
+  $schema = \Drupal::database()->schema();
+  if (!$schema->indexExists('bank_transaction', 'bank_transaction_external_id_account')) {
+    $schema->addUniqueKey('bank_transaction', 'bank_transaction_name_account', ['external_id', 'bank_account_id']);
+  }
+}
diff --git a/modules/banking_import/src/BankingImportService.php b/modules/banking_import/src/BankingImportService.php
index 5b815fe..9d5bc8c 100644
--- a/modules/banking_import/src/BankingImportService.php
+++ b/modules/banking_import/src/BankingImportService.php
@@ -51,7 +51,7 @@ class BankingImportService implements BankingImportServiceInterface {
 
     // adjust max string length
     $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions('bank_transaction');
-    foreach (['account_holder_name', 'account_holder_address', 'account_number', 'information'] as $str_field) {
+    foreach (['name', 'external_id', 'account_holder_name', 'account_holder_address', 'account_number', 'information'] as $str_field) {
         if (
             isset($transaction_data[$str_field]) &&
             isset($field_definitions[$str_field]->getSettings()['max_length']) &&
@@ -65,7 +65,7 @@ class BankingImportService implements BankingImportServiceInterface {
     }
 
     $transaction = BankTransaction::create($transaction_data);
-    
+
     try {
         $transaction->save();
     }
@@ -75,7 +75,7 @@ class BankingImportService implements BankingImportServiceInterface {
         // allow to continue to next transaction if error is duplicate key.
         return NULL;
       }
-      throw $exception;      
+      throw $exception;
     }
     $event = new BankTransactionEvent($transaction);
     $event_dispatcher = \Drupal::service('event_dispatcher');
diff --git a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
index 834b6ee..ef395d9 100644
--- a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
+++ b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
@@ -168,8 +168,8 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
         // continue;
       }
 
+      $transaction_idx = 0;
       foreach ($batch_entry->getTransactionDetails() as $entry) {
-        $transaction_idx = 0;
         if ($entry->getAmountDetails() && $entry->getAmountDetails()->getAmount()) {
           $amount = $entry->getAmountDetails()->getAmount();
         }
@@ -177,16 +177,14 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
           $amount = $batch_entry->getAmount();
         }
 
+        $external_id = NULL;
         if ($batch_entry->getReference()) {
-          $name = $batch_entry->getReference() . '/' . $transaction_idx;
-        }
-        else {
-          // Banks who do not provide NtryRef.
-          $name = $statement->getId() . '/' . $entry_idx . '/' . $transaction_idx;
+          $external_id = 'camt053:' . $batch_entry->getReference() . '/' . $transaction_idx;
         }
 
         $transaction_data = [
-          'name' => $name,
+          'name' => $statement->getId() . '/' . $entry_idx . '/' . $transaction_idx,
+          'external_id' => $external_id,
           'bank_account_id' => $bank_account,
           'amount' => $moneyFormatter->format($amount),
           'date' => $batch_entry->getBookingDate()->format('Y-m-d'),
diff --git a/src/BankTransactionStorageSchema.php b/src/BankTransactionStorageSchema.php
new file mode 100644
index 0000000..5743460
--- /dev/null
+++ b/src/BankTransactionStorageSchema.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Drupal\comment;
+
+use Drupal\Core\Entity\ContentEntityTypeInterface;
+use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\Core\Field\RequiredFieldStorageDefinitionInterface;
+
+/**
+ * Defines the bank transaction schema handler.
+ */
+class BankTransactionStorageSchema extends SqlContentEntityStorageSchema {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
+    $schema = parent::getEntitySchema($entity_type, $reset);
+
+    if ($base_table = $this->storage->getBaseTable()) {
+      $schema[$base_table]['indexes'] += [
+        'bank_transaction_name_account' => ['name', 'bank_account_id'],
+      ];
+      $schema[$base_table]['indexes'] += [
+        'bank_transaction_external_id_account' => ['external_id', 'bank_account_id'],
+      ];
+    }
+
+    return $schema;
+  }
diff --git a/src/Entity/BankAccount.php b/src/Entity/BankAccount.php
index 9893993..fbb1b41 100644
--- a/src/Entity/BankAccount.php
+++ b/src/Entity/BankAccount.php
@@ -90,6 +90,7 @@ class BankAccount extends ContentEntityBase implements BankAccountInterface {
    */
   public static function loadByAccountNumber($acc_number) {
     $ids = \Drupal::entityQuery('bank_account')
+      ->accessCheck(FALSE)
       ->condition('number', $acc_number)
       ->execute();
     if (!$ids) {
diff --git a/src/Entity/BankTransaction.php b/src/Entity/BankTransaction.php
index cc954ed..01cb2cd 100644
--- a/src/Entity/BankTransaction.php
+++ b/src/Entity/BankTransaction.php
@@ -305,6 +305,21 @@ class BankTransaction extends ContentEntityBase implements BankTransactionInterf
       ->setDisplayConfigurable('form', TRUE)
       ->setDisplayConfigurable('view', TRUE);
 
+    $fields['external_id'] = BaseFieldDefinition::create('string')
+      ->setLabel(t('External ID'))
+      ->setDescription(t('The ID of the Bank transaction entity from the import source (prefix with plugin ID).'))
+      ->setSettings([
+        'max_length' => 255,
+        'text_processing' => 0,
+      ])
+      ->setDefaultValue('')
+      ->setDisplayOptions('view', [
+        'label' => 'above',
+        'type' => 'string',
+        'weight' => -4,
+      ])
+      ->setDisplayConfigurable('view', TRUE);
+
     $fields['date'] = BaseFieldDefinition::create('datetime')
       ->setLabel(t('Transaction Date'))
       ->setDescription(t('The Date of the transaction.'))
-- 
GitLab


From e460953646e5aae49959392198a3cb114d627369 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Fri, 3 Mar 2023 14:53:43 +0000
Subject: [PATCH 2/8] phpcs

---
 banking.install                               |  4 +--
 .../install/views.view.bank_transactions.yml  |  1 -
 config/optional/core.date_format.date.yml     |  1 -
 .../banking_accounting.install                |  2 +-
 .../src/Entity/PaymentOrderLine.php           |  2 +-
 .../src/BankingImportService.php              | 22 ++++++-------
 .../Plugin/BankingImportFormat/CAMT053.php    |  2 +-
 .../tests/src/Kernel/BankingImportTest.php    |  7 ++--
 src/BankTransactionStorageSchema.php          | 32 +++++++++----------
 src/Entity/BankTransaction.php                |  2 +-
 .../Action/ChangeTransactionStateAction.php   |  1 -
 11 files changed, 34 insertions(+), 42 deletions(-)

diff --git a/banking.install b/banking.install
index d0c5ca8..3a2ac40 100644
--- a/banking.install
+++ b/banking.install
@@ -8,7 +8,6 @@
 use Drupal\Core\Config\FileStorage;
 use Drupal\Core\Field\BaseFieldDefinition;
 
-
 /**
  * Change length of a varchar entity field with data, safe with entity-updates.
  *
@@ -62,7 +61,6 @@ function banking_db_change_varchar_field($entity_type_id, $field_name, $field_le
   }
 }
 
-
 /**
  * Implements hook_install().
  */
@@ -269,7 +267,7 @@ function banking_update_8009() {
  * Update account holder field length to 100 chars.
  */
 function banking_update_8010() {
- banking_db_change_varchar_field('bank_transaction', 'account_holder_name', 100);
+  banking_db_change_varchar_field('bank_transaction', 'account_holder_name', 100);
 }
 
 /**
diff --git a/config/install/views.view.bank_transactions.yml b/config/install/views.view.bank_transactions.yml
index f309db2..78610d4 100644
--- a/config/install/views.view.bank_transactions.yml
+++ b/config/install/views.view.bank_transactions.yml
@@ -895,4 +895,3 @@ display:
         - url.query_args
         - user.permissions
       tags: {  }
-
diff --git a/config/optional/core.date_format.date.yml b/config/optional/core.date_format.date.yml
index 839cf9c..eb79946 100644
--- a/config/optional/core.date_format.date.yml
+++ b/config/optional/core.date_format.date.yml
@@ -4,4 +4,3 @@ dependencies: {  }
 id: date
 label: 'Date'
 pattern: Y-m-d
-
diff --git a/modules/banking_accounting/banking_accounting.install b/modules/banking_accounting/banking_accounting.install
index 19627f6..2b0d689 100644
--- a/modules/banking_accounting/banking_accounting.install
+++ b/modules/banking_accounting/banking_accounting.install
@@ -21,6 +21,6 @@ function banking_accounting_update_8010(&$sandbox) {
   \Drupal::entityDefinitionUpdateManager()
     ->updateFieldStorageDefinition(
       \Drupal::service('entity_field.manager')
-      ->getBaseFieldDefinitions('banking_ogm')['account']
+        ->getBaseFieldDefinitions('banking_ogm')['account']
     );
 }
diff --git a/modules/banking_export/src/Entity/PaymentOrderLine.php b/modules/banking_export/src/Entity/PaymentOrderLine.php
index 95d97b1..1110b86 100644
--- a/modules/banking_export/src/Entity/PaymentOrderLine.php
+++ b/modules/banking_export/src/Entity/PaymentOrderLine.php
@@ -150,7 +150,7 @@ class PaymentOrderLine extends ContentEntityBase implements PaymentOrderLineInte
     if (!$this->get('extra')->isEmpty()) {
       $data = $this->get('extra')->first()->getValue();
     }
-    return isset($data[$key]) ? $data[$key] : $default;
+    return $data[$key] ?? $default;
   }
 
   /**
diff --git a/modules/banking_import/src/BankingImportService.php b/modules/banking_import/src/BankingImportService.php
index 9d5bc8c..84b0199 100644
--- a/modules/banking_import/src/BankingImportService.php
+++ b/modules/banking_import/src/BankingImportService.php
@@ -36,7 +36,7 @@ class BankingImportService implements BankingImportServiceInterface {
    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
    *   The entity field manager.
    */
-    public function __construct(MessengerInterface $messenger, EntityFieldManagerInterface $entity_field_manager) {
+  public function __construct(MessengerInterface $messenger, EntityFieldManagerInterface $entity_field_manager) {
     $this->messenger = $messenger;
     $this->entityFieldManager = $entity_field_manager;
   }
@@ -49,30 +49,30 @@ class BankingImportService implements BankingImportServiceInterface {
       $transaction_data['imported'] = TRUE;
     }
 
-    // adjust max string length
+    // Adjust max string length.
     $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions('bank_transaction');
     foreach (['name', 'external_id', 'account_holder_name', 'account_holder_address', 'account_number', 'information'] as $str_field) {
-        if (
+      if (
             isset($transaction_data[$str_field]) &&
             isset($field_definitions[$str_field]->getSettings()['max_length']) &&
             strlen($transaction_data[$str_field]) > $field_definitions[$str_field]->getSettings()['max_length']) {
-            $transaction_data[$str_field] = substr(
-              $transaction_data[$str_field],
-              0,
-              $field_definitions[$str_field]->getSettings()['max_length']
-            );
-        }
+        $transaction_data[$str_field] = substr(
+            $transaction_data[$str_field],
+            0,
+            $field_definitions[$str_field]->getSettings()['max_length']
+          );
+      }
     }
 
     $transaction = BankTransaction::create($transaction_data);
 
     try {
-        $transaction->save();
+      $transaction->save();
     }
     catch (EntityStorageException $exception) {
       $duplicate_key = "SQLSTATE[23000]";
       if (substr($exception->getMessage(), 0, strlen($duplicate_key)) === $duplicate_key) {
-        // allow to continue to next transaction if error is duplicate key.
+        // Allow to continue to next transaction if error is duplicate key.
         return NULL;
       }
       throw $exception;
diff --git a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
index ef395d9..9f068f4 100644
--- a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
+++ b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
@@ -165,7 +165,7 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
               '@account' => $statement->getAccount()->getIban(),
             ]),
           'warning');
-        // continue;
+        // continue;.
       }
 
       $transaction_idx = 0;
diff --git a/modules/banking_import/tests/src/Kernel/BankingImportTest.php b/modules/banking_import/tests/src/Kernel/BankingImportTest.php
index a238a1d..744d1b1 100644
--- a/modules/banking_import/tests/src/Kernel/BankingImportTest.php
+++ b/modules/banking_import/tests/src/Kernel/BankingImportTest.php
@@ -3,7 +3,6 @@
 namespace Drupal\Tests\banking_import\Kernel;
 
 use Drupal\KernelTests\KernelTestBase;
-use Drupal\banking\Entity\BankTransaction;
 use Drupal\banking\Entity\BankAccount;
 
 /**
@@ -71,7 +70,7 @@ class BankingImportTest extends KernelTestBase {
     $batch = $definition->importFileBatch(
       file_save_data(
         file_get_contents(
-          drupal_get_path('module', 'banking_import') .'/tests/data/mutations.xml'),
+          drupal_get_path('module', 'banking_import') . '/tests/data/mutations.xml'),
         'public://' . 'testcamt.xml')
     );
 
@@ -108,7 +107,7 @@ class BankingImportTest extends KernelTestBase {
     $batch = $definition->importFileBatch(
       file_save_data(
         file_get_contents(
-          drupal_get_path('module', 'banking_import') .'/tests/data/mutations.xml'),
+          drupal_get_path('module', 'banking_import') . '/tests/data/mutations.xml'),
         'public://' . 'testcamt.xml')
     );
 
@@ -127,7 +126,7 @@ class BankingImportTest extends KernelTestBase {
     $batch = $definition->importFileBatch(
       file_save_data(
         file_get_contents(
-          drupal_get_path('module', 'banking_import') .'/tests/data/mutations.xml'),
+          drupal_get_path('module', 'banking_import') . '/tests/data/mutations.xml'),
         'public://' . 'testcamt.xml')
     );
 
diff --git a/src/BankTransactionStorageSchema.php b/src/BankTransactionStorageSchema.php
index 5743460..a95ee49 100644
--- a/src/BankTransactionStorageSchema.php
+++ b/src/BankTransactionStorageSchema.php
@@ -4,28 +4,26 @@ namespace Drupal\comment;
 
 use Drupal\Core\Entity\ContentEntityTypeInterface;
 use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
-use Drupal\Core\Field\FieldStorageDefinitionInterface;
-use Drupal\Core\Field\RequiredFieldStorageDefinitionInterface;
 
 /**
  * Defines the bank transaction schema handler.
  */
 class BankTransactionStorageSchema extends SqlContentEntityStorageSchema {
 
-  /**
-   * {@inheritdoc}
-   */
-  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
-    $schema = parent::getEntitySchema($entity_type, $reset);
-
-    if ($base_table = $this->storage->getBaseTable()) {
-      $schema[$base_table]['indexes'] += [
-        'bank_transaction_name_account' => ['name', 'bank_account_id'],
-      ];
-      $schema[$base_table]['indexes'] += [
-        'bank_transaction_external_id_account' => ['external_id', 'bank_account_id'],
-      ];
-    }
+/**
+ * {@inheritdoc}
+ */
+protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
+  $schema = parent::getEntitySchema($entity_type, $reset);
 
-    return $schema;
+  if ($base_table = $this->storage->getBaseTable()) {
+    $schema[$base_table]['indexes'] += [
+      'bank_transaction_name_account' => ['name', 'bank_account_id'],
+    ];
+    $schema[$base_table]['indexes'] += [
+      'bank_transaction_external_id_account' => ['external_id', 'bank_account_id'],
+    ];
   }
+
+  return $schema;
+}
diff --git a/src/Entity/BankTransaction.php b/src/Entity/BankTransaction.php
index 01cb2cd..25b9b7e 100644
--- a/src/Entity/BankTransaction.php
+++ b/src/Entity/BankTransaction.php
@@ -229,7 +229,7 @@ class BankTransaction extends ContentEntityBase implements BankTransactionInterf
     if (!$this->get('extra')->isEmpty()) {
       $data = $this->get('extra')->first()->getValue();
     }
-    return isset($data[$key]) ? $data[$key] : $default;
+    return $data[$key] ?? $default;
   }
 
   /**
diff --git a/src/Plugin/Action/ChangeTransactionStateAction.php b/src/Plugin/Action/ChangeTransactionStateAction.php
index 7cc0775..469a6a5 100644
--- a/src/Plugin/Action/ChangeTransactionStateAction.php
+++ b/src/Plugin/Action/ChangeTransactionStateAction.php
@@ -6,7 +6,6 @@ use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Access\AccessResult;
-use Drupal\workflows\Entity\Workflow;
 
 /**
  * Change Transaction State.
-- 
GitLab


From ecceb65e0b929c904d36dc681371e4825cc686b4 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Sat, 4 Mar 2023 06:11:53 +0100
Subject: [PATCH 3/8] add: .gitignore

---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b25c15b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*~
-- 
GitLab


From f47e23ede30611da836a553352421db7927540d0 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Sat, 4 Mar 2023 06:13:04 +0100
Subject: [PATCH 4/8] fix: revert naming scheme to 1.0.23 for uniqueness check

---
 .../src/Plugin/BankingImportFormat/CAMT053.php              | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
index 9f068f4..e7f4b8a 100644
--- a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
+++ b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
@@ -154,7 +154,7 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
     $currencies = new ISOCurrencies();
     $moneyFormatter = new DecimalMoneyFormatter($currencies);
     $bank_account = BankAccount::loadByAccountNumber($statement->getAccount()->getIban());
-    $entry_idx = 0;
+    $all_transaction_for_statement_idx = 0;
 
     foreach ($statement->getEntries() as $batch_entry) {
       if (in_array($batch_entry->getBookingDate()->format('Y-m-d'), $dates)) {
@@ -183,7 +183,7 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
         }
 
         $transaction_data = [
-          'name' => $statement->getId() . '/' . $entry_idx . '/' . $transaction_idx,
+          'name' => $statement->getId() . '/' . $all_transaction_for_statement_idx;
           'external_id' => $external_id,
           'bank_account_id' => $bank_account,
           'amount' => $moneyFormatter->format($amount),
@@ -252,6 +252,7 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
 
         \Drupal::service('banking_import.default')->createImportedTransaction($transaction_data);
         $transaction_idx++;
+        $all_transaction_for_statement_idx++;
       }
 
       if (!isset($context['results']['count'])) {
@@ -259,7 +260,6 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
       }
 
       $context['results']['count'] += 1;
-      $entry_idx++;
     }
   }
 
-- 
GitLab


From c7e1433ea9e4ccdce23d6b7a5680304baa477369 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Sat, 4 Mar 2023 06:13:42 +0100
Subject: [PATCH 5/8] ref: unique constraint definition using storagescheme

---
 banking.install                      |  8 --------
 src/BankTransactionStorageSchema.php | 30 +++++++++++++++-------------
 src/Entity/BankTransaction.php       |  1 +
 3 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/banking.install b/banking.install
index 3a2ac40..702bc6f 100644
--- a/banking.install
+++ b/banking.install
@@ -61,14 +61,6 @@ function banking_db_change_varchar_field($entity_type_id, $field_name, $field_le
   }
 }
 
-/**
- * Implements hook_install().
- */
-function banking_install() {
-  $schema = \Drupal::database()->schema();
-  $schema->addUniqueKey('bank_transaction', 'bank_transaction_name_account', ['name', 'bank_account_id']);
-}
-
 /**
  * V1.0.0-alpha1 -> v1.0.0-alpha2.
  */
diff --git a/src/BankTransactionStorageSchema.php b/src/BankTransactionStorageSchema.php
index a95ee49..79954bd 100644
--- a/src/BankTransactionStorageSchema.php
+++ b/src/BankTransactionStorageSchema.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\comment;
+namespace Drupal\banking;
 
 use Drupal\Core\Entity\ContentEntityTypeInterface;
 use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
@@ -10,20 +10,22 @@ use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
  */
 class BankTransactionStorageSchema extends SqlContentEntityStorageSchema {
 
-/**
- * {@inheritdoc}
- */
-protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
-  $schema = parent::getEntitySchema($entity_type, $reset);
+  /**
+   * {@inheritdoc}
+   */
+  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
+    $schema = parent::getEntitySchema($entity_type, $reset);
+
+    if ($base_table = $this->storage->getBaseTable()) {
+      $schema[$base_table]['indexes'] += [
+        'bank_transaction_name_account' => ['name', 'bank_account_id'],
+      ];
+      $schema[$base_table]['indexes'] += [
+        'bank_transaction_external_id_account' => ['external_id', 'bank_account_id'],
+      ];
+    }
 
-  if ($base_table = $this->storage->getBaseTable()) {
-    $schema[$base_table]['indexes'] += [
-      'bank_transaction_name_account' => ['name', 'bank_account_id'],
-    ];
-    $schema[$base_table]['indexes'] += [
-      'bank_transaction_external_id_account' => ['external_id', 'bank_account_id'],
-    ];
+    return $schema;
   }
 
-  return $schema;
 }
diff --git a/src/Entity/BankTransaction.php b/src/Entity/BankTransaction.php
index 25b9b7e..08947bd 100644
--- a/src/Entity/BankTransaction.php
+++ b/src/Entity/BankTransaction.php
@@ -20,6 +20,7 @@ use Drupal\banking\Exception\BankingIntegrityException;
  *   id = "bank_transaction",
  *   label = @Translation("Bank transaction"),
  *   handlers = {
+ *     "storage_schema" = "Drupal\banking\BankTransactionStorageSchema",
  *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
  *     "list_builder" = "Drupal\banking\BankTransactionListBuilder",
  *     "views_data" = "Drupal\banking\Entity\BankTransactionViewsData",
-- 
GitLab


From 75e97d159a71a028c29dcf83be7b27d11f354481 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Sat, 4 Mar 2023 07:51:33 +0100
Subject: [PATCH 6/8] imp: add post_update hook to move import files to private
 filesystem

---
 .../banking_import.post_update.php            | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/modules/banking_import/banking_import.post_update.php b/modules/banking_import/banking_import.post_update.php
index f035d4b..bda601b 100644
--- a/modules/banking_import/banking_import.post_update.php
+++ b/modules/banking_import/banking_import.post_update.php
@@ -6,6 +6,7 @@
  */
 
 use Drupal\banking\Entity\BankTransaction;
+use Drupal\file\Entity\File;
 
 /**
  * Set imported field on bank_transactions.
@@ -17,3 +18,33 @@ function banking_import_post_update_imported(&$sandbox) {
     }
   }
 }
+
+/**
+ * Move imported files to private filesystem.
+ */
+function banking_import_post_update_move_import_file() {
+  $fids = \Drupal::database()->query("select distinct(import_file__target_id) from bank_transaction where import_file__target_id is not null")->fetchCol(0);
+
+  foreach(File::loadMultiple($fids) as $file) {
+    $temp = "temporary://";
+    if (substr($file->getFileUri(), 0, strlen($temp)) === $temp) {
+
+      $camt_file = \Drupal::service('file_system')->realpath($file->GetFileUri());
+
+      if (! file_exists($camt_file)) {
+        \Drupal::loggger('update')->error(
+          "Missing file %camt_file (%fid)",
+          [
+            '%file' => $camt_file,
+            '%fid' => $file->id(),
+          ]
+        );
+        continue;
+      }
+
+      \Drupal::service('file.repository')->move($file, 'private://' . $file->getFilename());
+      \Drupal::notice('update')->notice(
+        "Moved file %file" ["%file" => $file->getFilename()]);
+    }
+  }
+}
-- 
GitLab


From 0331d1ccffea242fcf632c389c8ee2df999c91f4 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Sat, 4 Mar 2023 07:54:10 +0100
Subject: [PATCH 7/8] fix: typo

---
 banking.post_update.php                                   | 2 +-
 modules/banking_import/banking_import.post_update.php     | 8 ++++----
 .../src/Plugin/BankingImportFormat/CAMT053.php            | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/banking.post_update.php b/banking.post_update.php
index eff81a4..20ef711 100644
--- a/banking.post_update.php
+++ b/banking.post_update.php
@@ -43,6 +43,6 @@ function banking_post_update_add_unique_transaction_account_name_key(&$sandbox)
 function banking_post_update_add_unique_transaction_account_external_id_key(&$sandbox) {
   $schema = \Drupal::database()->schema();
   if (!$schema->indexExists('bank_transaction', 'bank_transaction_external_id_account')) {
-    $schema->addUniqueKey('bank_transaction', 'bank_transaction_name_account', ['external_id', 'bank_account_id']);
+    $schema->addUniqueKey('bank_transaction', 'bank_transaction_external_id_account', ['external_id', 'bank_account_id']);
   }
 }
diff --git a/modules/banking_import/banking_import.post_update.php b/modules/banking_import/banking_import.post_update.php
index bda601b..2b75dbb 100644
--- a/modules/banking_import/banking_import.post_update.php
+++ b/modules/banking_import/banking_import.post_update.php
@@ -32,8 +32,8 @@ function banking_import_post_update_move_import_file() {
       $camt_file = \Drupal::service('file_system')->realpath($file->GetFileUri());
 
       if (! file_exists($camt_file)) {
-        \Drupal::loggger('update')->error(
-          "Missing file %camt_file (%fid)",
+        \Drupal::logger('update')->error(
+          "Missing file %file (%fid)",
           [
             '%file' => $camt_file,
             '%fid' => $file->id(),
@@ -43,8 +43,8 @@ function banking_import_post_update_move_import_file() {
       }
 
       \Drupal::service('file.repository')->move($file, 'private://' . $file->getFilename());
-      \Drupal::notice('update')->notice(
-        "Moved file %file" ["%file" => $file->getFilename()]);
+      \Drupal::logger('update')->notice(
+        "Moved file %file", ["%file" => $file->getFilename()]);
     }
   }
 }
diff --git a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
index e7f4b8a..0a72aef 100644
--- a/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
+++ b/modules/banking_import/src/Plugin/BankingImportFormat/CAMT053.php
@@ -183,7 +183,7 @@ class CAMT053 extends BankingImportFormatDefinitionBase {
         }
 
         $transaction_data = [
-          'name' => $statement->getId() . '/' . $all_transaction_for_statement_idx;
+          'name' => $statement->getId() . '/' . $all_transaction_for_statement_idx,
           'external_id' => $external_id,
           'bank_account_id' => $bank_account,
           'amount' => $moneyFormatter->format($amount),
-- 
GitLab


From 21e2a226e113ce58d7f7ad50fff719002e4109de Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Sat, 4 Mar 2023 13:15:28 +0100
Subject: [PATCH 8/8] fix: unit test

---
 modules/banking_import/tests/src/Kernel/BankingImportTest.php | 3 ---
 src/BankTransactionStorageSchema.php                          | 4 ++--
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/modules/banking_import/tests/src/Kernel/BankingImportTest.php b/modules/banking_import/tests/src/Kernel/BankingImportTest.php
index 744d1b1..2e67c58 100644
--- a/modules/banking_import/tests/src/Kernel/BankingImportTest.php
+++ b/modules/banking_import/tests/src/Kernel/BankingImportTest.php
@@ -91,9 +91,6 @@ class BankingImportTest extends KernelTestBase {
   public function testImportDuplicateCamt053() {
     $format = 'camt053';
 
-    module_load_include('install', 'banking');
-    banking_install();
-
     $account = BankAccount::create([
       'name' => 'test',
       'number' => 'BE68111171790734',
diff --git a/src/BankTransactionStorageSchema.php b/src/BankTransactionStorageSchema.php
index 79954bd..71ac806 100644
--- a/src/BankTransactionStorageSchema.php
+++ b/src/BankTransactionStorageSchema.php
@@ -17,10 +17,10 @@ class BankTransactionStorageSchema extends SqlContentEntityStorageSchema {
     $schema = parent::getEntitySchema($entity_type, $reset);
 
     if ($base_table = $this->storage->getBaseTable()) {
-      $schema[$base_table]['indexes'] += [
+      $schema[$base_table]['unique keys'] += [
         'bank_transaction_name_account' => ['name', 'bank_account_id'],
       ];
-      $schema[$base_table]['indexes'] += [
+      $schema[$base_table]['unique keys'] += [
         'bank_transaction_external_id_account' => ['external_id', 'bank_account_id'],
       ];
     }
-- 
GitLab