diff --git a/ajax/invoices/temporary/save.php b/ajax/invoices/temporary/save.php index a0e5edc29d80731b802981c99e853cb1951499fe..84d34834031ca2f19be05a88bc8ebdee9deb7a7a 100644 --- a/ajax/invoices/temporary/save.php +++ b/ajax/invoices/temporary/save.php @@ -24,18 +24,17 @@ function ($invoiceId, $data) { $data['invoice_address'] = ''; } - if (isset($data['addressDelivery']) && !empty($data['addressDelivery'])) { + if (!empty($data['addressDelivery'])) { $delivery = $data['addressDelivery']; + unset($data['addressDelivery']); - if ($delivery) { - unset($data['addressDelivery']); - - try { - $Invoice->setDeliveryAddress($delivery); - } catch (QUI\Exception $Exception) { - QUI\System\Log::writeDebugException($Exception); - } + try { + $Invoice->setDeliveryAddress($delivery); + } catch (QUI\Exception $Exception) { + QUI\System\Log::writeDebugException($Exception); } + } else { + $Invoice->removeDeliveryAddress(); } $Invoice->clearArticles(); diff --git a/bin/backend/controls/panels/DeliveryAddress.js b/bin/backend/controls/panels/DeliveryAddress.js deleted file mode 100644 index 2533694d3083e73ec67ae99cfdd6796e5b9b2cf9..0000000000000000000000000000000000000000 --- a/bin/backend/controls/panels/DeliveryAddress.js +++ /dev/null @@ -1,433 +0,0 @@ -/** - * @module package/quiqqer/invoice/bin/backend/controls/panels/DeliveryAddress - * @author www.pcsg.de (Henning Leutz) - * - * @event onLoaded [this] - Fires if Control finished loading - */ -define('package/quiqqer/invoice/bin/backend/controls/panels/DeliveryAddress', [ - - 'qui/QUI', - 'qui/controls/Control', - 'qui/controls/windows/Confirm', - 'package/quiqqer/countries/bin/Countries', - 'Users', - 'Locale' - -], function (QUI, QUIControl, QUIConfirm, Countries, Users, QUILocale) { - "use strict"; - - return new Class({ - - Extends: QUIControl, - Type : 'package/quiqqer/invoice/bin/backend/controls/panels/DeliveryAddress', - - Binds: [ - '$onImport', - '$checkBoxChange', - '$onSelectChange', - 'isLoaded' - ], - - initialize: function (options) { - this.parent(options); - - this.$Addresses = null; - this.$Company = null; - this.$Street = null; - this.$ZIP = null; - this.$City = null; - this.$Country = null; - - this.$Salutation = null; - this.$Firstname = null; - this.$Lastname = null; - - this.checked = false; - this.$loaded = false; - this.$userId = this.getAttribute('userId'); - - this.addEvents({ - onImport : this.$onImport, - onSetAttribute: this.$onSetAttribute - }); - }, - - /** - * event: on import - */ - $onImport: function () { - var self = this, - Elm = this.getElm(); - - // changeable - this.$Checked = Elm.getElement('[name="differentDeliveryAddress"]'); - this.$Checked.addEvent('change', this.$checkBoxChange); - - // address stuff - this.$addressId = false; - this.$Company = Elm.getElement('[name="delivery-company"]'); - this.$Street = Elm.getElement('[name="delivery-street_no"]'); - this.$ZIP = Elm.getElement('[name="delivery-zip"]'); - this.$City = Elm.getElement('[name="delivery-city"]'); - this.$Country = Elm.getElement('[name="delivery-country"]'); - - this.$Salutation = Elm.getElement('[name="delivery-salutation"]'); - this.$Firstname = Elm.getElement('[name="delivery-firstname"]'); - this.$Lastname = Elm.getElement('[name="delivery-lastname"]'); - - this.$Addresses = Elm.getElement('[name="delivery-addresses"]'); - this.$Addresses.addEvent('change', this.$onSelectChange); - - this.$Company.disabled = false; - this.$Street.disabled = false; - this.$ZIP.disabled = false; - this.$City.disabled = false; - - this.$Salutation.disabled = false; - this.$Firstname.disabled = false; - this.$Lastname.disabled = false; - - var Panel = QUI.Controls.getById( - this.getElm().getParent('.qui-panel').get('data-quiid') - ); - - this.$Customer = QUI.Controls.getById( - Panel.getElm().getElement('[name="customer"]').get('data-quiid') - ); - - if (this.$Customer) { - this.$userId = this.$Customer.getValue(); - } - - Countries.getCountries().then(function (result) { - new Element('option', { - value: '', - html : '' - }).inject(self.$Country); - - for (var code in result) { - if (!result.hasOwnProperty(code)) { - continue; - } - - new Element('option', { - value: code, - html : result[code] - }).inject(self.$Country); - } - - if (self.getAttribute('country')) { - self.$Country.value = self.getAttribute('country'); - } - - self.$Country.disabled = false; - self.$loaded = true; - - self.fireEvent('loaded', [self]); - }); - }, - - /** - * Return the current value - * - * @return {{zip, uid: *, country, firstname, city, street_no, company, id: (boolean|*), salutation, lastname}} - */ - getValue: function () { - return { - uid : this.$userId, - id : this.$addressId, - company : this.$Company.value, - salutation: this.$Salutation.value, - firstname : this.$Firstname.value, - lastname : this.$Lastname.value, - street_no : this.$Street.value, - zip : this.$ZIP.value, - city : this.$City.value, - country : this.$Country.value - }; - }, - - /** - * Clears the selection - no address are selected - */ - clear: function () { - this.$Addresses.value = ''; - this.$onSelectChange(); - }, - - /** - * Set values - * - * @param {Object} value - */ - setValue: function (value) { - if (typeOf(value) !== 'object') { - return; - } - - if ("id" in value) { - this.$addressId = value.id; - } - - if ("company" in value) { - this.$Company.value = value.company; - } - - if ("salutation" in value) { - this.$Salutation.value = value.salutation; - } - - if ("firstname" in value) { - this.$Firstname.value = value.firstname; - } - - if ("lastname" in value) { - this.$Lastname.value = value.company; - } - - if ("street_no" in value) { - this.$Street.value = value.street_no; - } - - if ("zip" in value) { - this.$ZIP.value = value.zip; - } - - if ("city" in value) { - this.$City.value = value.city; - } - - if ("country" in value) { - if (this.$loaded) { - this.$Country.value = value.country; - } else { - this.setAttribute('country', value.country); - } - } - - if ("uid" in value) { - this.$userId = value.uid; - //this.loadAddresses().catch(function () { - // this.$Addresses.disabled = true; - //}.bind(this)); - } - - this.$checkBoxChange(); - }, - - /** - * Return the selected user - * - * @return {Promise} - */ - getUser: function () { - if (!this.$userId) { - return Promise.reject('No User-ID'); - } - - var User = Users.get(this.$userId); - - if (User.isLoaded()) { - return Promise.resolve(User); - } - - return User.load(); - }, - - /** - * Refresh the address list - * - * @return {Promise} - */ - refresh: function () { - var self = this; - - this.$Addresses.set('html', ''); - - if (!this.$userId) { - this.$Company.value = ''; - this.$Street.value = ''; - this.$ZIP.value = ''; - this.$City.value = ''; - this.$Country.value = ''; - - this.$Salutation.value = ''; - this.$Firstname.value = ''; - this.$Lastname.value = ''; - - return Promise.reject(); - } - - return this.loadAddresses().then(function () { - self.$onSelectChange(); - }).catch(function () { - self.$Addresses.disabled = true; - }); - }, - - /** - * Load the addresses - */ - loadAddresses: function () { - var self = this; - - this.$Addresses.set('html', ''); - this.$Addresses.disabled = true; - - if (!this.$userId) { - return Promise.reject(); - } - - return this.getUser().then(function (User) { - return User.getAddressList(); - }).then(function (addresses) { - self.$Addresses.set('html', ''); - - new Element('option', { - value : '', - html : '', - 'data-value': '' - }).inject(self.$Addresses); - - for (var i = 0, len = addresses.length; i < len; i++) { - new Element('option', { - value : addresses[i].id, - html : addresses[i].text, - 'data-value': JSON.encode(addresses[i]) - }).inject(self.$Addresses); - } - - if (self.$addressId) { - self.$Addresses.value = self.$addressId; - } - - self.$Addresses.disabled = false; - }).catch(function (err) { - console.error(err); - }); - }, - - /** - * event : on select change - */ - $onSelectChange: function () { - var Select = this.$Addresses; - - var options = Select.getElements('option').filter(function (Option) { - return Option.value === Select.value; - }); - - if (Select.value === '' || !options.length || options[0].get('data-value') === '') { - this.$addressId = false; - - this.$Company.value = ''; - this.$Street.value = ''; - this.$ZIP.value = ''; - this.$City.value = ''; - this.$Country.value = ''; - - this.$Salutation.value = ''; - this.$Firstname.value = ''; - this.$Lastname.value = ''; - return; - } - - var data = JSON.decode(options[0].get('data-value')); - - this.$addressId = data.id; - this.$Company.value = data.company; - this.$Street.value = data.street_no; - this.$ZIP.value = data.zip; - this.$City.value = data.city; - this.$Country.value = data.country; - - this.$Salutation.value = data.salutation; - this.$Firstname.value = data.firstname; - this.$Lastname.value = data.lastname; - }, - - /** - * event: on attribute change - * - * @param {String} key - * @param {String} value - */ - $onSetAttribute: function (key, value) { - var self = this; - - if (key === 'userId') { - this.$userId = value; - - self.refresh().then(function () { - self.$Addresses.disabled = false; - }).catch(function () { - self.$Addresses.disabled = true; - }); - } - }, - - /** - * event if the check box changes - * - * @param event - */ - $checkBoxChange: function (event) { - var self = this, - Checkbox = this.getElm().getElement('[name="differentDeliveryAddress"]'), - closables = this.getElm().getElements('.closable'); - - if (event) { - event.stop(); - } - - if (!Checkbox) { - return; - } - - if (!this.$userId) { - var Panel = QUI.Controls.getById( - this.getElm().getParent('.qui-panel').get('data-quiid') - ); - - this.$Customer = QUI.Controls.getById( - Panel.getElm().getElement('[name="customer"]').get('data-quiid') - ); - - if (this.$Customer) { - this.$userId = this.$Customer.getValue(); - } - } - - if (!this.$userId) { - Checkbox.checked = false; - - QUI.getMessageHandler().then(function (MH) { - MH.addInformation( - QUILocale.get('quiqqer/invoice', 'message.select.customer'), - self.$Customer.getElm() - ); - }); - - return; - } - - if (Checkbox.checked) { - closables.setStyle('display', null); - this.loadAddresses(); - return; - } - - closables.setStyle('display', 'none'); - this.clear(); - }, - - /** - * Check if control has finished loading - * - * @return {boolean} - */ - isLoaded: function () { - return this.$loaded; - } - }); -}); diff --git a/bin/backend/controls/panels/TemporaryInvoice.Data.html b/bin/backend/controls/panels/TemporaryInvoice.Data.html index db066737e304791b6175f4a766ea00573993b1b0..59bc2b617678101df6135612250ff9891ecc07ac 100644 --- a/bin/backend/controls/panels/TemporaryInvoice.Data.html +++ b/bin/backend/controls/panels/TemporaryInvoice.Data.html @@ -100,114 +100,14 @@ </tbody> </table> - <table class="data-table data-table-flexbox invoice-delivery" - data-qui="package/quiqqer/invoice/bin/backend/controls/panels/DeliveryAddress" - > + <table class="data-table data-table-flexbox invoice-delivery"> <thead> <tr> <th>{{textInvoiceDeliveryAddress}}</th> </tr> </thead> - <tbody> - <tr> - <td> - <label class="field-container"> - <input type="checkbox" name="differentDeliveryAddress"/> - <span>{{messageDifferentDeliveryAddress}}</span> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textCustomer}}"> - {{textAddresses}} - </span> - <select name="delivery-addresses" class="field-container-field" disabled></select> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textCompany}}"> - {{textCompany}} - </span> - <input type="text" class="field-container-field" name="delivery-company" disabled/> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textSalutation}}"> - {{textSalutation}} - </span> - <input type="text" class="field-container-field" name="delivery-salutation" disabled/> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textFirstname}}"> - {{textFirstname}} - </span> - <input type="text" class="field-container-field" name="delivery-firstname" disabled/> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textLastname}}"> - {{textLastname}} - </span> - <input type="text" class="field-container-field" name="delivery-lastname" disabled/> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textStreet}}"> - {{textStreet}} - </span> - <input type="text" class="field-container-field" name="delivery-street_no" disabled/> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textZip}}"> - {{textZip}} - </span> - <input type="text" class="field-container-field" name="delivery-zip" disabled/> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textCity}}"> - {{textCity}} - </span> - <input type="text" class="field-container-field" name="delivery-city" disabled/> - </label> - </td> - </tr> - <tr class="closable" style="display: none"> - <td> - <label class="field-container"> - <span class="field-container-item" title="{{textCountry}}"> - {{textCountry}} - </span> - <select type="text" class="field-container-field" name="delivery-country" disabled></select> - </label> - </td> - </tr> + <tbody data-qui="package/quiqqer/erp/bin/backend/controls/DeliveryAddress"> </tbody> </table> diff --git a/bin/backend/controls/panels/TemporaryInvoice.js b/bin/backend/controls/panels/TemporaryInvoice.js index 19bc2e88ec5dd1af7c9078886ac4a6c6921491c4..452112e71df18cb5ae5688a9badd974ea5bcb5c4 100644 --- a/bin/backend/controls/panels/TemporaryInvoice.js +++ b/bin/backend/controls/panels/TemporaryInvoice.js @@ -319,17 +319,7 @@ 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'), - textInvoiceDeliveryAddress : QUILocale.get(lg, 'deliveryAddress'), - messageDifferentDeliveryAddress: QUILocale.get(lg, 'message.different,delivery.address'), - textAddresses : QUILocale.get(lg, 'address'), - textCompany : QUILocale.get(lg, 'company'), - textStreet : QUILocale.get(lg, 'street'), - textZip : QUILocale.get(lg, 'zip'), - textCity : QUILocale.get(lg, 'city'), - textCountry : QUILocale.get(lg, 'country'), - textSalutation : QUILocale.get(lg, 'salutation'), - textFirstname : QUILocale.get(lg, 'firstname'), - textLastname : QUILocale.get(lg, 'lastname') + textInvoiceDeliveryAddress: QUILocale.get(lg, 'deliveryAddress'), }) }); @@ -449,9 +439,8 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ // reset deliver address if (self.$AddressDelivery) { + self.$AddressDelivery.reset(); self.$AddressDelivery.setAttribute('userId', userId); - self.$AddressDelivery.refresh().catch(function () { - }); } Promise.all([ @@ -523,25 +512,24 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ // delivery address self.$AddressDelivery = QUI.Controls.getById( self.getContent().getElement( - '[data-qui="package/quiqqer/invoice/bin/backend/controls/panels/DeliveryAddress"]' + '[data-qui="package/quiqqer/erp/bin/backend/controls/DeliveryAddress"]' ).get('data-quiid') ); - if (self.getAttribute('delivery_address_id')) { - var deliveryAddress = self.getAttribute('delivery_address'); - - try { - deliveryAddress = JSON.decode(deliveryAddress); + var deliveryAddress = self.getAttribute('addressDelivery'); - if (deliveryAddress) { - self.getContent().getElement('[name="differentDeliveryAddress"]').checked = true; + if (!deliveryAddress) { + deliveryAddress = self.getAttribute('delivery_address'); - self.$AddressDelivery.setAttribute('userId', self.getAttribute('customer_id')); - self.$AddressDelivery.setValue(deliveryAddress); - } - } catch (e) { + if (deliveryAddress) { + deliveryAddress = JSON.decode(deliveryAddress); } } + + if (deliveryAddress) { + self.$AddressDelivery.setAttribute('userId', self.getAttribute('customer_id')); + self.$AddressDelivery.setValue(deliveryAddress); + } }).then(function () { var Container = self.getContent().getElement('.container'); @@ -1143,6 +1131,7 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ if (this.$AddressDelivery) { this.setAttribute('addressDelivery', this.$AddressDelivery.getValue()); + this.setAttribute('delivery_address', this.$AddressDelivery.getValue()); } if (this.$AdditionalText) { diff --git a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php index b2e886ffb30488231215231a1b6f0a136fe93ede..b28ca3a31d2138b99cc1a8ba34eac8e8bf660528 100644 --- a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php +++ b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php @@ -110,18 +110,26 @@ public function __construct($id, Handler $Handler) $this->History = new QUI\ERP\Comments(); $this->Comments = new QUI\ERP\Comments(); - $this->addressDelivery = \json_decode($data['delivery_address'], true); - - if (!empty($data['delivery_address_id'])) { - $this->addressDelivery['id'] = (int)$data['delivery_address_id']; - } elseif (empty($this->addressDelivery['company']) - && empty($this->addressDelivery['firstname']) - && empty($this->addressDelivery['lastname']) - && empty($this->addressDelivery['street_no']) - && empty($this->addressDelivery['zip']) - && empty($this->addressDelivery['city']) - && empty($this->addressDelivery['county']) - ) { + if (!empty($data['delivery_address'])) { + $deliveryAddressData = \json_decode($data['delivery_address'], true); + + if (\json_last_error() === \JSON_ERROR_NONE) { + $this->addressDelivery = $deliveryAddressData; + } else { + $this->addressDelivery = false; + } + + if (empty($this->addressDelivery['company']) + && empty($this->addressDelivery['firstname']) + && empty($this->addressDelivery['lastname']) + && empty($this->addressDelivery['street_no']) + && empty($this->addressDelivery['zip']) + && empty($this->addressDelivery['city']) + && empty($this->addressDelivery['county']) + ) { + $this->addressDelivery = false; + } + } else { $this->addressDelivery = false; } @@ -637,6 +645,8 @@ public function update($PermissionUser = null) } // address + $invoiceAddressId = false; + try { $Customer = QUI::getUsers()->get((int)$this->getAttribute('customer_id')); @@ -653,6 +663,8 @@ public function update($PermissionUser = null) $invoiceAddress = $Address->toJSON(); $this->Articles->setUser($Customer); + + $invoiceAddressId = $Address->getId(); } catch (QUI\Exception $Exception) { $invoiceAddress = $this->getAttribute('invoice_address'); $invoiceAddressCheck = false; @@ -663,6 +675,8 @@ public function update($PermissionUser = null) if (!$invoiceAddressCheck) { QUI\System\Log::addNotice($Exception->getMessage()); + } else { + $invoiceAddressId = $invoiceAddressCheck['id']; } } @@ -747,10 +761,12 @@ public function update($PermissionUser = null) // delivery address $deliveryAddress = ''; $deliveryAddressId = null; + $DeliveryAddress = $this->getDeliveryAddress(); - if ($this->getDeliveryAddress()) { - $deliveryAddress = $this->getDeliveryAddress()->toJSON(); - $deliveryAddressId = $this->getDeliveryAddress()->getId(); + // Save delivery address separately only if it differs from invoice address + if ($DeliveryAddress && ($invoiceAddressId && $DeliveryAddress->getId() !== $invoiceAddressId)) { + $deliveryAddress = $DeliveryAddress->toJSON(); + $deliveryAddressId = $DeliveryAddress->getId(); if (empty($deliveryAddressId)) { $deliveryAddressId = null; @@ -1837,10 +1853,10 @@ public function getDeliveryAddress(): ?QUI\ERP\Address ); if ($Address) { - return new QUI\ERP\Address( - $Address->getAttributes(), - $this->getCustomer() - ); + $addressData = $Address->getAttributes(); + $addressData['id'] = $Address->getId(); + + return new QUI\ERP\Address($addressData, $this->getCustomer()); } } catch (QUI\Exception $Exception) { QUI\System\Log::addDebug($Exception->getMessage()); @@ -1902,5 +1918,15 @@ protected function parseAddressData(array $address): array return $result; } + /** + * Removes any delivery address that is saved in this invoice + * + * @return void + */ + public function removeDeliveryAddress() + { + $this->addressDelivery = false; + } + // endregion } diff --git a/src/QUI/ERP/Accounting/Invoice/Output/OutputProviderInvoice.php b/src/QUI/ERP/Accounting/Invoice/Output/OutputProviderInvoice.php index 69de5ab9e86059996df965c642218fbf1bf68406..a454a77570c651e72815142de27e6afde0115177 100644 --- a/src/QUI/ERP/Accounting/Invoice/Output/OutputProviderInvoice.php +++ b/src/QUI/ERP/Accounting/Invoice/Output/OutputProviderInvoice.php @@ -159,6 +159,10 @@ public static function getTemplateData($entityId) $DeliveryAddress->clearMail(); $DeliveryAddress->clearPhone(); + + if ($DeliveryAddress->equals($Address)) { + $DeliveryAddress = false; + } } QUI::getLocale()->setTemporaryCurrent($Customer->getLang());