From 316a7209ea47ece56ec23cb5563a96ee6b03765d Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Mon, 31 May 2021 10:48:42 +0200
Subject: [PATCH] feat: currency select for invoices

---
 ajax/invoices/temporary/save.php              |  4 +++
 .../panels/TemporaryInvoice.Data.html         | 23 ++++++++++++++++
 .../controls/panels/TemporaryInvoice.js       | 14 +++++++++-
 locale.xml                                    |  8 ++++++
 .../Accounting/Invoice/InvoiceTemporary.php   | 26 +++++++++++++++++--
 5 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/ajax/invoices/temporary/save.php b/ajax/invoices/temporary/save.php
index 84d3483..29893d4 100644
--- a/ajax/invoices/temporary/save.php
+++ b/ajax/invoices/temporary/save.php
@@ -44,6 +44,10 @@ function ($invoiceId, $data) {
             unset($data['articles']);
         }
 
+        if (isset($data['currency'])) {
+            $Invoice->setCurrency($data['currency']);
+        }
+
         $Invoice->setAttribute('invoice_address', false); // needed because of address reset
         $Invoice->setAttributes($data);
         $Invoice->save();
diff --git a/bin/backend/controls/panels/TemporaryInvoice.Data.html b/bin/backend/controls/panels/TemporaryInvoice.Data.html
index 59bc2b6..4b059b0 100644
--- a/bin/backend/controls/panels/TemporaryInvoice.Data.html
+++ b/bin/backend/controls/panels/TemporaryInvoice.Data.html
@@ -139,6 +139,29 @@
                 </label>
             </td>
         </tr>
+        <tr class="currency-row">
+            <td>
+                <label class="field-container">
+                    <span class="field-container-item" title="{{textCurrency}}">
+                        {{textCurrency}}
+                    </span>
+                    <input type="hidden"
+                           name="currency"
+                           data-qui="package/quiqqer/currency/bin/settings/CurrencyList"
+                    />
+                </label>
+            </td>
+        </tr>
+        <tr class="currency-row">
+            <td>
+                <label class="field-container">
+                    <span class="field-container-item" title="{{textCurrencyRate}}">
+                        {{textCurrencyRate}}
+                    </span>
+                    <input type="text" name="currencyRate" class="field-container-field"/>
+                </label>
+            </td>
+        </tr>
         </tbody>
     </table>
 
diff --git a/bin/backend/controls/panels/TemporaryInvoice.js b/bin/backend/controls/panels/TemporaryInvoice.js
index c93ebb3..08ffb6b 100644
--- a/bin/backend/controls/panels/TemporaryInvoice.js
+++ b/bin/backend/controls/panels/TemporaryInvoice.js
@@ -277,6 +277,7 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                 time_for_payment       : this.getAttribute('time_for_payment'),
                 payment_method         : this.getAttribute('payment_method'),
                 additional_invoice_text: this.getAttribute('additional_invoice_text'),
+                currency               : this.getAttribute('currency'),
                 addressDelivery        : deliveryAddress,
                 processing_status      : this.getAttribute('processing_status')
             };
@@ -326,6 +327,9 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                         textStatus        : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textStatus'),
                         textContactPerson : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textContactPerson'),
 
+                        textCurrency    : QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textCurrency'),
+                        textCurrencyRate: QUILocale.get(lg, 'erp.panel.temporary.invoice.category.data.textCurrencyRate'),
+
                         textInvoiceDeliveryAddress: QUILocale.get(lg, 'deliveryAddress'),
                     })
                 });
@@ -346,7 +350,8 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                     project_name     : self.getAttribute('project_name'),
                     editor_id        : self.getAttribute('editor_id'),
                     processing_status: self.getAttribute('processing_status'),
-                    contact_person   : self.getAttribute('contact_person')
+                    contact_person   : self.getAttribute('contact_person'),
+                    currency         : self.getAttribute('currency')
                 }, Form);
 
                 Form.elements.date.set('disabled', true);
@@ -396,10 +401,12 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
 
                 var editorIdQUIId    = Content.getElement('[name="editorId"]').get('data-quiid');
                 var orderedByIdQUIId = Content.getElement('[name="orderedBy"]').get('data-quiid');
+                var currencyIdQUIId  = Content.getElement('[name="currency"]').get('data-quiid');
 
                 var Data      = QUI.Controls.getById(quiId);
                 var EditorId  = QUI.Controls.getById(editorIdQUIId);
                 var OrderedBy = QUI.Controls.getById(orderedByIdQUIId);
+                var Currency  = QUI.Controls.getById(currencyIdQUIId);
 
                 OrderedBy.setAttribute('showAddressName', false);
 
@@ -465,6 +472,11 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                     });
                 });
 
+                // currency
+                Currency.addEvent('change', function (Instance, value) {
+                    self.setAttribute('currency', value);
+                });
+
                 // editor
                 EditorId.addEvent('onChange', function () {
                     self.setAttribute('editor_id', EditorId.getValue());
diff --git a/locale.xml b/locale.xml
index b8f69dc..28e7411 100644
--- a/locale.xml
+++ b/locale.xml
@@ -1032,6 +1032,14 @@
             <de><![CDATA[Ansprechpartner]]></de>
             <en><![CDATA[Contact person]]></en>
         </locale>
+        <locale name="erp.panel.temporary.invoice.category.data.textCurrency">
+            <de><![CDATA[Währung]]></de>
+            <en><![CDATA[Currency]]></en>
+        </locale>
+        <locale name="erp.panel.temporary.invoice.category.data.textCurrencyRate">
+            <de><![CDATA[Wechselkurs]]></de>
+            <en><![CDATA[Exchange rate]]></en>
+        </locale>
         <locale name="erp.panel.temporary.invoice.category.comments">
             <de><![CDATA[Kommentare]]></de>
             <en><![CDATA[Comments]]></en>
diff --git a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
index ef32d21..d4bef34 100644
--- a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
+++ b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php
@@ -359,6 +359,28 @@ public function getCustomer()
         return null;
     }
 
+    /**
+     * @param string|QUI\ERP\Currency\Currency $currency
+     */
+    public function setCurrency($currency)
+    {
+        if (\is_string($currency)) {
+            try {
+                $currency = QUI\ERP\Currency\Handler::getCurrency($currency);
+            } catch (QUI\Exception $Exception) {
+                QUI\System\Log::addError($Exception->getMessage());
+
+                return;
+            }
+        }
+
+        if (!($currency instanceof QUI\ERP\Currency\Currency)) {
+            return;
+        }
+
+        $this->setAttribute('currency_data', $currency->toArray());
+    }
+
     /**
      * Return the invoice currency
      *
@@ -1313,8 +1335,8 @@ public function post($PermissionUser = null): Invoice
 
             $acData = [
                 'accountingCurrency' => $AccountingCurrency->toArray(),
-                'baseCurrency'       => $this->getCurrency()->toArray(),
-                'rate'               => $AccountingCurrency->getExchangeRate($this->getCurrency())
+                'currency'           => $this->getCurrency()->toArray(),
+                'rate'               => $this->getCurrency()->getExchangeRate($AccountingCurrency)
             ];
 
             $Invoice->addCustomDataEntry('accountingCurrencyData', $acData);
-- 
GitLab