From 5c3504e254453af7892a48a5450e2070e9e62a55 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Fri, 4 Jun 2021 12:20:40 +0200
Subject: [PATCH] feat: currency exchange rating

---
 bin/backend/controls/panels/Journal.css       |  6 +++++
 src/QUI/ERP/Accounting/Invoice/Invoice.php    | 25 ++++++++++++++++---
 .../Accounting/Invoice/InvoiceTemporary.php   | 20 +++++++++++++++
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/bin/backend/controls/panels/Journal.css b/bin/backend/controls/panels/Journal.css
index dd7a96d..4aaf89d 100644
--- a/bin/backend/controls/panels/Journal.css
+++ b/bin/backend/controls/panels/Journal.css
@@ -214,3 +214,9 @@
 .invoices-invoice-details .articles-sum td {
     font-size: 14px !important;
 }
+
+.invoices-invoice-details .articles-sum-exchange-rate {
+    float: left;
+    font-size: 10px;
+    margin: 10px 10px 0 10px;
+}
diff --git a/src/QUI/ERP/Accounting/Invoice/Invoice.php b/src/QUI/ERP/Accounting/Invoice/Invoice.php
index b3363d9..97a1dd8 100644
--- a/src/QUI/ERP/Accounting/Invoice/Invoice.php
+++ b/src/QUI/ERP/Accounting/Invoice/Invoice.php
@@ -45,10 +45,10 @@ class Invoice extends QUI\QDOM
     //    const PAYMENT_STATUS_STORNO = 3; // Alias for cancel
     //    const PAYMENT_STATUS_CREATE_CREDIT = 5;
 
-    const DUNNING_LEVEL_OPEN       = 0; // No Dunning -> Keine Mahnung
-    const DUNNING_LEVEL_REMIND     = 1; // Payment reminding -> Zahlungserinnerung
-    const DUNNING_LEVEL_DUNNING    = 2; // Dunning -> Erste Mahnung
-    const DUNNING_LEVEL_DUNNING2   = 3; // Second dunning -> Zweite Mahnung
+    const DUNNING_LEVEL_OPEN = 0; // No Dunning -> Keine Mahnung
+    const DUNNING_LEVEL_REMIND = 1; // Payment reminding -> Zahlungserinnerung
+    const DUNNING_LEVEL_DUNNING = 2; // Dunning -> Erste Mahnung
+    const DUNNING_LEVEL_DUNNING2 = 3; // Second dunning -> Zweite Mahnung
     const DUNNING_LEVEL_COLLECTION = 4; // Collection -> Inkasso
 
     /**
@@ -249,6 +249,23 @@ public function getArticles(): ArticleListUnique
         $List = new ArticleListUnique($articles, $this->getCustomer());
         $List->setLocale($this->getCustomer()->getLocale());
 
+
+        // accounting currency, if exists
+        $accountingCurrencyData = $this->getCustomDataEntry('accountingCurrencyData');
+
+        try {
+            if ($accountingCurrencyData) {
+                $List->setExchangeCurrency(
+                    new QUI\ERP\Currency\Currency(
+                        $accountingCurrencyData['accountingCurrency']
+                    )
+                );
+
+                $List->setExchangeRate($accountingCurrencyData['rate']);
+            }
+        } catch (QUI\Exception $Exception) {
+        }
+
         return $List;
     }
 
diff --git a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
index 1ac9a52..c2e9f9e 100644
--- a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
+++ b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
@@ -238,6 +238,26 @@ public function __construct($id, Handler $Handler)
         if (\is_numeric($data['shipping_id'])) {
             $this->shippingId = (int)$data['shipping_id'];
         }
+
+        // accounting currency, if exists
+        $accountingCurrencyData = $this->getCustomDataEntry('accountingCurrencyData');
+
+        try {
+            if ($accountingCurrencyData) {
+                $this->Articles->setExchangeCurrency(
+                    new QUI\ERP\Currency\Currency(
+                        $accountingCurrencyData['accountingCurrency']
+                    )
+                );
+
+                $this->Articles->setExchangeRate($accountingCurrencyData['rate']);
+            } elseif (QUI\ERP\Currency\Conf::accountingCurrencyEnabled()) {
+                $this->Articles->setExchangeCurrency(
+                    QUI\ERP\Currency\Conf::getAccountingCurrency()
+                );
+            }
+        } catch (QUI\Exception $Exception) {
+        }
     }
 
     //region Getter
-- 
GitLab