diff --git a/ajax/invoices/temporary/save.php b/ajax/invoices/temporary/save.php index c70cd984ee3440031e2dd1cc297174844fc36c9f..3718a0457a8c7a8cc8bcad51d83fad15cec56716 100644 --- a/ajax/invoices/temporary/save.php +++ b/ajax/invoices/temporary/save.php @@ -4,6 +4,9 @@ * This file contains package_quiqqer_invoice_ajax_invoices_temporary_save */ +use QUI\ERP\Shipping\Shipping; +use QUI\ERP\Accounting\Invoice\Handler as InvoiceHandler; + /** * Saves the temporary invoice * @@ -13,7 +16,7 @@ QUI::$Ajax->registerFunction( 'package_quiqqer_invoice_ajax_invoices_temporary_save', function ($invoiceId, $data) { - $Invoices = QUI\ERP\Accounting\Invoice\Handler::getInstance(); + $Invoices = InvoiceHandler::getInstance(); $Invoice = $Invoices->getTemporaryInvoice($invoiceId); $data = \json_decode($data, true); @@ -22,6 +25,20 @@ function ($invoiceId, $data) { $data['invoice_address'] = ''; } + if (isset($data['addressDelivery']) && !empty($data['addressDelivery'])) { + $delivery = $data['addressDelivery']; + + if ($delivery) { + unset($data['addressDelivery']); + + try { + $Invoice->setDeliveryAddress($delivery); + } catch (QUI\Exception $Exception) { + QUI\System\Log::writeDebugException($Exception); + } + } + } + $Invoice->clearArticles(); if (isset($data['articles'])) { diff --git a/bin/backend/controls/panels/DeliveryAddress.js b/bin/backend/controls/panels/DeliveryAddress.js index 8bb3d4af141a8efa8e7e93c49993a5c5e6f58459..563eb64b0dfd639abe4e835ba792d9a964c246a0 100644 --- a/bin/backend/controls/panels/DeliveryAddress.js +++ b/bin/backend/controls/panels/DeliveryAddress.js @@ -225,6 +225,8 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/DeliveryAddress', [ return this.getUser().then(function (User) { return User.getAddressList(); }).then(function (addresses) { + self.$Addresses.set('html', ''); + new Element('option', { value : '', html : '', diff --git a/bin/backend/controls/panels/Invoice.Data.html b/bin/backend/controls/panels/Invoice.Data.html index fd9d5e68e7ad371fcfa301b8a5c0716fdaf62290..202d83a0344205ae921e1b5798732f83d09a1893 100644 --- a/bin/backend/controls/panels/Invoice.Data.html +++ b/bin/backend/controls/panels/Invoice.Data.html @@ -60,6 +60,68 @@ </tbody> </table> + <table class="data-table data-table-flexbox invoice-delivery-data" style="display: none"> + <thead> + <tr> + <th>{{textInvoiceDelivery}}</th> + </tr> + </thead> + + <tbody> + <tr> + <td> + <label class="field-container"> + <span class="field-container-item" title="{{textCustomer}}"> + {{textCustomer}} + </span> + <input type="text" class="field-container-field" name="delivery-customer" disabled/> + </label> + </td> + </tr> + <tr> + <td> + <label class="field-container"> + <span class="field-container-item" title="{{textCompany}}"> + {{textDeliveryCompany}} + </span> + <input type="text" class="field-container-field" name="delivery-company" disabled/> + </label> + </td> + </tr> + <tr> + <td> + <label class="field-container"> + <span class="field-container-item" title="{{textStreet}}"> + {{textDeliveryStreet}} + </span> + <input type="text" class="field-container-field" name="delivery-street_no" disabled/> + </label> + </td> + </tr> + <tr> + <td> + <label class="field-container"> + <span class="field-container-item" title="{{textZip}}"> + {{textDeliveryZip}} + </span> + <input type="text" class="field-container-field" name="delivery-zip" disabled/> + </label> + </td> + </tr> + <tr> + <td> + <label class="field-container"> + <span class="field-container-item" title="{{textCity}}"> + {{textDeliveryCity}} + </span> + <input type="text" class="field-container-field" name="delivery-city" disabled/> + </label> + </td> + </tr> + </tbody> + </table> + + <table class="data-table data-table-flexbox invoice-data"> <thead> <tr> @@ -187,4 +249,4 @@ </tr> </tbody> </table> -</form> \ No newline at end of file +</form> diff --git a/bin/backend/controls/panels/Invoice.js b/bin/backend/controls/panels/Invoice.js index 7f477ac77f107c32d81ce3535c946e9770c36bc6..0a469b1acda1ac5e6b6d121aa309fc1c12cc14ee 100644 --- a/bin/backend/controls/panels/Invoice.js +++ b/bin/backend/controls/panels/Invoice.js @@ -504,6 +504,13 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/Invoice', [ data.textZip = QUILocale.get(lg, 'zip'); data.textCity = QUILocale.get(lg, 'city'); + data.textInvoiceDelivery = QUILocale.get(lg, 'deliveryAddress'); + data.textDeliveryCompany = QUILocale.get(lg, 'company'); + data.textDeliveryStreet = QUILocale.get(lg, 'street'); + data.textDeliveryZip = QUILocale.get(lg, 'zip'); + data.textDeliveryCity = QUILocale.get(lg, 'city'); + data.textDeliveryCountry = QUILocale.get(lg, 'country'); + data.textInvoiceData = QUILocale.get(lg, 'erp.panel.invoice.data.title'); data.textInvoiceDate = QUILocale.get(lg, 'erp.panel.invoice.data.date'); data.textProjectName = QUILocale.get(lg, 'erp.panel.invoice.data.projectName'); @@ -520,9 +527,11 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/Invoice', [ html: Mustache.render(template, data) }); + var Form = Container.getElement('form'); + var address = {}; + try { - var Form = Container.getElement('form'), - address = JSON.decode(data.invoice_address); + address = JSON.decode(data.invoice_address); Form.elements.customer.value = address.salutation + ' ' + address.firstname + ' ' + address.lastname; Form.elements.company.value = address.company; @@ -533,6 +542,40 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/Invoice', [ console.error(e); } + if (data.delivery_address !== '') { + Container.getElement('.invoice-delivery-data').setStyle('display', null); + + try { + var deliveryAddress = JSON.decode(data.delivery_address); + + if (typeof deliveryAddress.salutation === 'undefined') { + deliveryAddress.salutation = ''; + } + + if (typeof deliveryAddress.firstname === 'undefined') { + deliveryAddress.firstname = address.firstname; + } + + if (typeof deliveryAddress.lastname === 'undefined') { + deliveryAddress.lastname = address.lastname; + } + + Form.elements['delivery-customer'].value = deliveryAddress.salutation + ' ' + + deliveryAddress.firstname + ' ' + + deliveryAddress.lastname; + + Form.elements['delivery-company'].value = deliveryAddress.company; + Form.elements['delivery-street_no'].value = deliveryAddress.street_no; + Form.elements['delivery-zip'].value = deliveryAddress.zip; + Form.elements['delivery-city'].value = deliveryAddress.city; + } catch (e) { + console.error(e); + } + } + + console.log(data); + + resolve(); }); }); diff --git a/bin/backend/controls/panels/TemporaryInvoice.js b/bin/backend/controls/panels/TemporaryInvoice.js index 315bf96eb2728f262f3cef4eee864e90a502f496..57b5bfa4f15e605af81897e897de9b557c56cc0b 100644 --- a/bin/backend/controls/panels/TemporaryInvoice.js +++ b/bin/backend/controls/panels/TemporaryInvoice.js @@ -88,6 +88,7 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ this.$ArticleListSummary = null; this.$AddProduct = null; this.$ArticleSort = null; + this.$AddressDelivery = null; this.$AddSeparator = null; this.$SortSeparator = null; @@ -254,7 +255,8 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ ordered_by : this.getAttribute('ordered_by'), time_for_payment : this.getAttribute('time_for_payment'), payment_method : this.getAttribute('payment_method'), - additional_invoice_text: this.getAttribute('additional_invoice_text') + additional_invoice_text: this.getAttribute('additional_invoice_text'), + addressDelivery : this.getAttribute('addressDelivery') }; }, @@ -446,10 +448,20 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ ).get('data-quiid') ); - console.log(self.getAttribute('addressDelivery')); + if (self.getAttribute('delivery_address')) { + var deliveryAddress = self.getAttribute('delivery_address'); - if (self.getAttribute('addressDelivery') && self.getAttribute('hasDeliveryAddress')) { - self.$AddressDelivery.setValue(self.getAttribute('addressDelivery')); + try { + deliveryAddress = JSON.decode(deliveryAddress); + + if (deliveryAddress) { + self.getContent().getElement('[name="differentDeliveryAddress"]').checked = true; + + self.$AddressDelivery.setAttribute('userId', self.getAttribute('customer_id')); + self.$AddressDelivery.setValue(deliveryAddress); + } + } catch (e) { + } } }).then(function () { var Container = self.getContent().getElement('.container'); @@ -464,7 +476,9 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ }, events : { onClick: function () { - self.openArticles(); + self.openArticles().catch(function (e) { + console.error(e); + }); } } }).inject(Container); @@ -935,6 +949,11 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ callback: function () { self.$unloadCategory(); + if (self.$AddressDelivery) { + self.$AddressDelivery.destroy(); + self.$AddressDelivery = null; + } + Container.set('html', ''); Container.setStyle('padding', 20); @@ -994,6 +1013,10 @@ define('package/quiqqer/invoice/bin/backend/controls/panels/TemporaryInvoice', [ } } + if (this.$AddressDelivery) { + this.setAttribute('addressDelivery', this.$AddressDelivery.getValue()); + } + if (this.$AdditionalText) { this.setAttribute( 'additional_invoice_text', diff --git a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php index 235f6b99425e7089ed964522d5f910bcabb3c704..4bc791924ae15afcef4f353145a25365c8d6c450 100644 --- a/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php +++ b/src/QUI/ERP/Accounting/Invoice/InvoiceTemporary.php @@ -83,6 +83,11 @@ class InvoiceTemporary extends QUI\QDOM */ protected $shippingId = null; + /** + * @var array + */ + protected $addressDelivery = []; + /** * Invoice constructor. * @@ -104,6 +109,12 @@ 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']; + } + if (isset($data['articles'])) { $articles = \json_decode($data['articles'], true); @@ -173,7 +184,6 @@ public function __construct($id, Handler $Handler) $this->setAttributes($data); - if (!$this->getCustomer()) { $this->setAttribute('invoice_address', false); $this->setAttribute('customer_id', false); @@ -703,6 +713,20 @@ public function update($PermissionUser = null) $shippingData = $this->getShipping()->toJSON(); } + // delivery address + $deliveryAddress = ''; + $deliveryAddressId = null; + + if ($this->getDeliveryAddress()) { + $deliveryAddress = $this->getDeliveryAddress()->toJSON(); + $deliveryAddressId = $this->getDeliveryAddress()->getId(); + + if (empty($deliveryAddressId)) { + $deliveryAddressId = null; + } + } + + QUI::getEvents()->fireEvent( 'quiqqerInvoiceTemporaryInvoiceSave', [$this] @@ -729,8 +753,8 @@ public function update($PermissionUser = null) // address 'invoice_address_id' => (int)$this->getAttribute('invoice_address_id'), 'invoice_address' => $invoiceAddress, - 'delivery_address_id' => null, - 'delivery_address' => null, + 'delivery_address_id' => $deliveryAddressId, + 'delivery_address' => $deliveryAddress, // processing 'time_for_payment' => $timeForPayment, @@ -1029,6 +1053,17 @@ public function post($PermissionUser = null) $shippingData = $this->getShipping()->toJSON(); } + // delivery address + $deliveryAddress = $this->getAttribute('delivery_address'); + + if (empty($deliveryAddress)) { + $DeliveryAddress = $this->getDeliveryAddress(); + + if ($DeliveryAddress) { + $deliveryAddress = $DeliveryAddress->toJSON(); + } + } + // create invoice QUI::getDataBase()->insert( $Handler->invoiceTable(), @@ -1050,7 +1085,7 @@ public function post($PermissionUser = null) // addresses 'invoice_address' => $invoiceAddress, - 'delivery_address' => $this->getAttribute('delivery_address'), + 'delivery_address' => $deliveryAddress, // payments 'payment_method' => $this->getAttribute('payment_method'), @@ -1665,4 +1700,94 @@ public function setShipping(QUI\ERP\Shipping\Api\ShippingInterface $Shipping) } //endregion + + //region delivery address + + /** + * Return delivery address + * + * @return QUI\ERP\Address|null + */ + public function getDeliveryAddress() + { + $delivery = $this->addressDelivery; + + if (isset($delivery['id'])) { + unset($delivery['id']); + } + + if (empty($delivery)) { + try { + $Customer = QUI::getUsers()->get((int)$this->getAttribute('customer_id')); + + $Address = $Customer->getAddress( + (int)$this->getAttribute('invoice_address_id') + ); + + if ($Address) { + return new QUI\ERP\Address( + $Address->getAttributes(), + $this->getCustomer() + ); + } + } catch (QUI\Exception $Exception) { + QUI\System\Log::addDebug($Exception->getMessage()); + } + } + + if (empty($this->addressDelivery)) { + return null; + } + + return new QUI\ERP\Address($this->addressDelivery, $this->getCustomer()); + } + + /** + * Set the delivery address + * + * @param array|QUI\ERP\Address $address + */ + public function setDeliveryAddress($address) + { + if ($address instanceof QUI\ERP\Address) { + $this->addressDelivery = $address->getAttributes(); + + return; + } + + if (\is_array($address)) { + $this->addressDelivery = $this->parseAddressData($address); + } + } + + /** + * @param array $address + * @return array + */ + protected function parseAddressData(array $address) + { + $fields = \array_flip([ + 'id', + 'salutation', + 'firstname', + 'lastname', + 'street_no', + 'zip', + 'city', + 'country', + 'company' + ]); + + $result = []; + + foreach ($address as $entry => $value) { + if (isset($fields[$entry])) { + $result[$entry] = $value; + } + } + + return $result; + } + + // endregion }