From 1cc9f9fff0a81c4c37483f1fd173c0ef2b587f11 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Sat, 20 Jan 2024 06:37:15 +0100
Subject: [PATCH] imp: I-7936 twig filter for deposito balance

---
 .../DsrDepositoTwigExtension.php              | 37 ++++++++++++++++++-
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/modules/dsr_deposito/src/TwigExtension/DsrDepositoTwigExtension.php b/modules/dsr_deposito/src/TwigExtension/DsrDepositoTwigExtension.php
index 52edc77..729c8ae 100644
--- a/modules/dsr_deposito/src/TwigExtension/DsrDepositoTwigExtension.php
+++ b/modules/dsr_deposito/src/TwigExtension/DsrDepositoTwigExtension.php
@@ -29,6 +29,7 @@ class DsrDepositoTwigExtension extends \Twig_Extension {
   public function getFilters() {
     return [
       new \Twig_SimpleFilter('deposito_ogm', [$this, 'depositoOgmFilter']),
+      new \Twig_SimpleFilter('deposito_balance', [$this, 'depositoBalanceFilter']),
     ];
   }
 
@@ -65,7 +66,7 @@ class DsrDepositoTwigExtension extends \Twig_Extension {
   /**
    * {@inheritdoc}
    */
-  public function getDepositoOgm($object) {
+  public function getDepositoOgms($object) {
     if (empty($object)) {
       return FALSE;
     }
@@ -78,10 +79,25 @@ class DsrDepositoTwigExtension extends \Twig_Extension {
       ->execute();
 
     if (!$ogm_ids) {
+      return [];
+    }
+
+    return BankingOGM::loadMultiple(($ogm_ids));
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDepositoOgm($object) {
+    if (empty($object)) {
       return FALSE;
     }
 
-    return BankingOGM::load(reset($ogm_ids));
+    $ogms = $this->getDepositoOgms($object);
+    if ($ogms) {
+        return reset($ogms);
+    }
+    return FALSE;
   }
 
   /**
@@ -95,4 +111,21 @@ class DsrDepositoTwigExtension extends \Twig_Extension {
     return $ogm->getFormattedOGM();
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function depositoBalanceFilter($object) {
+    $ogms = $this->getDepositoOgms($object);
+    $balance = '0';
+    foreach ($ogms as $ogm) {
+      error_log('adding balance ' . $ogm->get('account')->entity->getBalance()['balance_sign']);
+      $balance = bcadd(
+        $balance,
+        $ogm->get('account')->entity ? $ogm->get('account')->entity->getBalance()['balance_sign'] : '0',
+        2
+      );
+    }
+    return $balance;
+  }
+
 }
-- 
GitLab