From b95759398cf127e790e7e4e951b144f0f750c96c Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Thu, 7 Jan 2021 12:59:34 +0100
Subject: [PATCH] refactor: invoice -> contact person; fix: payment time for
 user needs to be an int

---
 ajax/invoices/temporary/getTimeForPayment.php |  2 +-
 .../controls/panels/TemporaryInvoice.js       | 54 +++++++++++++++----
 locale.xml                                    |  2 +-
 3 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/ajax/invoices/temporary/getTimeForPayment.php b/ajax/invoices/temporary/getTimeForPayment.php
index fe372b8..4e193cc 100644
--- a/ajax/invoices/temporary/getTimeForPayment.php
+++ b/ajax/invoices/temporary/getTimeForPayment.php
@@ -10,7 +10,7 @@
 QUI::$Ajax->registerFunction(
     'package_quiqqer_invoice_ajax_invoices_temporary_getTimeForPayment',
     function ($uid) {
-        return QUI\ERP\Customer\Utils::getInstance()->getPaymentTimeForUser($uid);
+        return QUI\ERP\Customer\Utils::getInstance()->getPaymentTimeForUser((int)$uid);
     },
     ['uid'],
     'Permission::checkAdminUser'
diff --git a/bin/backend/controls/panels/TemporaryInvoice.js b/bin/backend/controls/panels/TemporaryInvoice.js
index a71d69f..188062b 100644
--- a/bin/backend/controls/panels/TemporaryInvoice.js
+++ b/bin/backend/controls/panels/TemporaryInvoice.js
@@ -371,11 +371,7 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                         events   : {
                             onSubmit: function (Win, addressId, address) {
                                 Win.close();
-
-                                Container.getElement('[name="contact_person"]').set(
-                                    'value',
-                                    (address.salutation + ' ' + address.firstname + ' ' + address.lastname).trim()
-                                );
+                                self.$setContactPersonByAddress(address);
                             }
                         }
                     }).open();
@@ -415,10 +411,33 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
 
                     var userId = Data.getValue().userId;
 
-                    self.setAttribute('customer_id', userId);
+                    self.setAttribute('customer_id', parseInt(userId));
                     self.setAttribute('invoice_address_id', Data.getValue().addressId);
 
-                    Content.getElements('[name="select-contact-id-address"]').set('disabled', false);
+                    if (!userId) {
+                        Content.getElements('[name="select-contact-id-address"]').set('disabled', true);
+                        Content.getElements('[name="contact_person"]').set('value', '');
+                    } else {
+                        Content.getElements('[name="select-contact-id-address"]').set('disabled', false);
+
+                        Users.get(userId).loadIfNotLoaded().then(function (User) {
+                            var addressId = User.getAttribute('quiqqer.erp.customer.contact.person');
+
+                            if (!addressId) {
+                                return;
+                            }
+
+                            addressId = parseInt(addressId);
+
+                            User.getAddressList().then(function (addressList) {
+                                for (var i = 0, len = addressList.length; i < len; i++) {
+                                    if (addressList[i].id === addressId) {
+                                        self.$setContactPersonByAddress(addressList[i]);
+                                    }
+                                }
+                            });
+                        });
+                    }
 
                     // reset deliver address
                     if (self.$AddressDelivery) {
@@ -442,8 +461,6 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
                     });
                 });
 
-                //Data.setAddress();
-
                 // editor
                 EditorId.addEvent('onChange', function () {
                     self.setAttribute('editor_id', EditorId.getValue());
@@ -1640,8 +1657,25 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [
          */
         addComment: function (message) {
             return Invoices.addComment(this.getAttribute('invoiceId'), message);
-        }
+        },
 
         //endregion
+
+        /**
+         * set the contact person by an address data object to the contact person input field
+         *
+         * @param address
+         */
+        $setContactPersonByAddress: function (address) {
+            var Content     = this.getContent(),
+                PersonInput = Content.getElement('[name="contact_person"]');
+
+            if (!PersonInput) {
+                return;
+            }
+
+            var value = (address.salutation + ' ' + address.firstname + ' ' + address.lastname).trim();
+            PersonInput.set('value', value);
+        }
     });
 });
diff --git a/locale.xml b/locale.xml
index 16163c4..6b15a7b 100644
--- a/locale.xml
+++ b/locale.xml
@@ -421,7 +421,7 @@
         </locale>
 
         <locale name="message.different,delivery.address">
-            <de><![CDATA[Der Lieferadresse weicht von der Rechnungsadresse ab]]></de>
+            <de><![CDATA[Die Lieferadresse weicht von der Rechnungsadresse ab]]></de>
             <en><![CDATA[The delivery address differs from the billing address.]]></en>
         </locale>
 
-- 
GitLab