From 905c5471b3d927e0dfa94de9cd2437d844f046f6 Mon Sep 17 00:00:00 2001 From: Henning <leutz@pcsg.de> Date: Mon, 3 Feb 2025 14:03:43 +0100 Subject: [PATCH] feat: add attributes to createCustomer function This commit includes enhancements in customer creation feature. Specifically, it modifies the `createCustomer` function by adding a new parameter `$attributes` to handle attribute data during customer creation process. Moreover, it introduces UI components to collect these attribute details. Updates have been made to the associated form elements, button actions and display methods to accommodate these changes. In addition, it makes considerable changes in the backend JavaScript files to facilitate frontend changes and to meet compatibility. There's also a change in error handling part where it replaces the `try-catch` block, which attempt to get the standard address, with direct command. This assures the retrieval of the standard address, if exists, else a new address will be created implicitly. Finally, few general code improvements are done like changing usage of `var` to `const` or `let` where necessary for better maintainability. A minor adjustment on window height property is also made in `CustomerWindow.js` file. Related: quiqqer/customer#68 --- ajax/backend/create/createCustomer.php | 10 +- ajax/backend/getBusinessType.php | 18 +++ bin/backend/controls/create/Customer.html | 14 +++ bin/backend/controls/create/Customer.js | 112 +++++++++++------- bin/backend/controls/create/CustomerWindow.js | 3 +- src/QUI/ERP/Customer/Customers.php | 6 +- 6 files changed, 113 insertions(+), 50 deletions(-) create mode 100644 ajax/backend/getBusinessType.php diff --git a/ajax/backend/create/createCustomer.php b/ajax/backend/create/createCustomer.php index 91f0201..d807f35 100644 --- a/ajax/backend/create/createCustomer.php +++ b/ajax/backend/create/createCustomer.php @@ -12,9 +12,10 @@ QUI::$Ajax->registerFunction( 'package_quiqqer_customer_ajax_backend_create_createCustomer', - function ($customerId, $address, $groups) { + function ($customerId, $address, $groups, $attributes) { $address = json_decode($address, true); $groups = json_decode($groups, true); + $attributes = json_decode($attributes, true); $User = QUI\ERP\Customer\Customers::getInstance()->createCustomer( $customerId, @@ -22,8 +23,13 @@ function ($customerId, $address, $groups) { $groups ); + if (is_array($attributes)) { + $User->setAttributes($attributes); + $User->save(); + } + return $User->getUUID(); }, - ['customerId', 'address', 'groups'], + ['customerId', 'address', 'groups', 'attributes'], 'Permission::checkAdminUser' ); diff --git a/ajax/backend/getBusinessType.php b/ajax/backend/getBusinessType.php new file mode 100644 index 0000000..eb2a3ff --- /dev/null +++ b/ajax/backend/getBusinessType.php @@ -0,0 +1,18 @@ +<?php + +/** + * This file contains package_quiqqer_customer_ajax_backend_getBusinessType + */ + +/** + * Return the shop business type + */ + +QUI::$Ajax->registerFunction( + 'package_quiqqer_customer_ajax_backend_getBusinessType', + function () { + return QUI\ERP\Utils\Shop::getBusinessType(); + }, + false, + 'Permission::checkAdminUser' +); diff --git a/bin/backend/controls/create/Customer.html b/bin/backend/controls/create/Customer.html index 91fd442..388ff10 100644 --- a/bin/backend/controls/create/Customer.html +++ b/bin/backend/controls/create/Customer.html @@ -34,6 +34,20 @@ </label> </td> </tr> + <tr> + <td> + <label class="field-container"> + <span class="field-container-item"> + {{textIsNettoBruttoUser}} + </span> + <select name="quiqqer.erp.isNettoUser" class="field-container-field"> + <option value=""></option> + <option value="2">{{textBrutto}}</option> + <option value="1">{{textNetto}}</option> + </select> + </label> + </td> + </tr> <tr> <td> <label class="field-container"> diff --git a/bin/backend/controls/create/Customer.js b/bin/backend/controls/create/Customer.js index 101a268..8233f75 100644 --- a/bin/backend/controls/create/Customer.js +++ b/bin/backend/controls/create/Customer.js @@ -21,7 +21,7 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ ], function(QUI, QUIControl, Countries, Handler, QUILocale, QUIAjax, Mustache, template) { 'use strict'; - var lg = 'quiqqer/customer'; + const lg = 'quiqqer/customer'; return new Class({ @@ -66,6 +66,9 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ customerGroupsHeader: QUILocale.get(lg, 'window.customer.creation.groups.title'), customerGroupsText: QUILocale.get(lg, 'window.customer.creation.groups.text'), labelPrefix: QUILocale.get(lg, 'window.customer.creation.customerNo.labelPrefix'), + textIsNettoBruttoUser: QUILocale.get(lg, 'customer.user.information.textBruttoNetto'), + textNetto: QUILocale.get('quiqqer/erp', 'user.settings.userNettoStatus.netto'), + textBrutto: QUILocale.get('quiqqer/erp', 'user.settings.userNettoStatus.brutto'), textAddressCompany: QUILocale.get('quiqqer/core', 'company'), textAddressSalutation: QUILocale.get('quiqqer/core', 'salutation'), @@ -86,10 +89,10 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ this.$Form = this.$Elm.getElement('form'); // key events - var self = this; - var CustomerId = this.$Elm.getElement('[name="customerId"]'); - var Company = this.$Elm.getElement('[name="address-company"]'); - var Country = this.$Elm.getElement('[name="address-country"]'); + const self = this; + const CustomerId = this.$Elm.getElement('[name="customerId"]'); + const Company = this.$Elm.getElement('[name="address-company"]'); + const Country = this.$Elm.getElement('[name="address-country"]'); CustomerId.addEvent('keydown', function(event) { if (event.key === 'tab') { @@ -140,13 +143,13 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ * event: on inject */ $onInject: function() { - var self = this; - var Group = this.$Elm.getElement('[name="group"]'); + const self = this; + const Group = this.$Elm.getElement('[name="group"]'); Countries.getCountries().then(function(countries) { - var CountrySelect = self.$Elm.getElement('[name="address-country"]'); + const CountrySelect = self.$Elm.getElement('[name="address-country"]'); - for (var code in countries) { + for (let code in countries) { if (!countries.hasOwnProperty(code)) { continue; } @@ -160,10 +163,32 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ if (QUIQQER_CONFIG.globals.country) { CountrySelect.value = QUIQQER_CONFIG.globals.country; } + }).then(() => { + const Select = this.$Elm.getElement('[name="quiqqer.erp.isNettoUser"]') + + return new Promise((resolve) => { + QUIAjax.get('package_quiqqer_customer_ajax_backend_getBusinessType', (businessType) => { + switch (businessType.toLowerCase()) { + case 'b2b': + case 'b2b-b2c': + Select.value = '1'; + break; + + case 'b2c': + case 'b2c-b2b': + Select.value = '2'; + break; + } + + resolve(); + }, { + 'package': 'quiqqer/customer', + }); + }); }).then(function() { return QUI.parse(self.$Elm); }).then(function() { - var GroupControl = QUI.Controls.getById(Group.get('data-quiid')); + const GroupControl = QUI.Controls.getById(Group.get('data-quiid')); GroupControl.disable(); self.showCustomerNumber(); @@ -174,12 +199,12 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ * Create the customer */ createCustomer: function() { - var self = this; - var elements = this.$Form.elements; - var customerId = elements.customerId.value; - var groups = elements.groups.value.split(','); + const self = this; + const elements = this.$Form.elements; + const customerId = elements.customerId.value; + const groups = elements.groups.value.split(','); - var address = { + const address = { 'salutation': elements['address-salutation'].value, 'firstname': elements['address-firstname'].value, 'lastname': elements['address-lastname'].value, @@ -200,7 +225,10 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ 'package': 'quiqqer/customer', customerId: customerId, address: JSON.encode(address), - groups: JSON.encode(groups) + groups: JSON.encode(groups), + attributes: JSON.encode({ + 'quiqqer.erp.isNettoUser': this.$Elm.getElement('[name="quiqqer.erp.isNettoUser"]').value + }) }); }, @@ -212,16 +240,16 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ return this.createCustomer(); } - var self = this; - var steps = this.$List.getElements('li'); - var pos = this.$List.getPosition(this.$Container); - var top = pos.y; + const self = this; + const steps = this.$List.getElements('li'); + const pos = this.$List.getPosition(this.$Container); + const top = pos.y; - var height = this.$Container.getSize().y; - var scrollHeight = this.$Container.getScrollSize().y; - var newTop = this.$roundToStepPos(top - height); + const height = this.$Container.getSize().y; + const scrollHeight = this.$Container.getScrollSize().y; + const newTop = this.$roundToStepPos(top - height); - var step = 1; + let step = 1; if ((top * -1) / height) { step = Math.round(((top * -1) / height) + 1); @@ -239,11 +267,11 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ } return new Promise(function(resolve) { - var checkPromises = []; + const checkPromises = []; if (step === 1) { - var elements = self.$Form.elements; - var customerId = elements.customerId.value; + const elements = self.$Form.elements; + const customerId = elements.customerId.value; checkPromises.push(Handler.validateCustomerNo(customerId)); } @@ -267,12 +295,12 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ * Previous next step */ previous: function() { - var self = this; - var pos = this.$List.getPosition(this.$Container); - var top = pos.y; + const self = this; + const pos = this.$List.getPosition(this.$Container); + const top = pos.y; - var height = this.$Container.getSize().y; - var newTop = this.$roundToStepPos(top + height); + const height = this.$Container.getSize().y; + let newTop = this.$roundToStepPos(top + height); this.$Next.set('html', QUILocale.get(lg, 'window.customer.creation.next')); this.$Next.set('data-last', null); @@ -297,11 +325,11 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ * refresh the step display */ refreshStepDisplay: function() { - var step = 1; - var steps = this.$List.getElements('li'); - var pos = this.$List.getPosition(this.$Container); - var top = pos.y; - var height = this.$Container.getSize().y; + let step = 1; + const steps = this.$List.getElements('li'); + const pos = this.$List.getPosition(this.$Container); + const top = pos.y; + const height = this.$Container.getSize().y; if ((top * -1) / height) { step = Math.round(((top * -1) / height) + 1); @@ -329,8 +357,8 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ * @return {number} */ $roundToStepPos: function(currentPos) { - var height = this.$Container.getSize().y; - var pos = Math.round(currentPos / height) * -1; + const height = this.$Container.getSize().y; + const pos = Math.round(currentPos / height) * -1; return pos * height * -1; }, @@ -339,7 +367,7 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ * Show the customer number step */ showCustomerNumber: function() { - var self = this; + const self = this; this.$Next.disabled = true; @@ -347,8 +375,8 @@ define('package/quiqqer/customer/bin/backend/controls/create/Customer', [ Handler.getNewCustomerNo(), Handler.getCustomerIdPrefix() ]).then(function(result) { - var Input = self.$Elm.getElement('input[name="customerId"]'); - var InputPrefix = self.$Elm.getElement('input[name="prefix"]'); + const Input = self.$Elm.getElement('input[name="customerId"]'); + const InputPrefix = self.$Elm.getElement('input[name="prefix"]'); if (result[1]) { InputPrefix.value = result[1]; diff --git a/bin/backend/controls/create/CustomerWindow.js b/bin/backend/controls/create/CustomerWindow.js index c05f30e..e488371 100644 --- a/bin/backend/controls/create/CustomerWindow.js +++ b/bin/backend/controls/create/CustomerWindow.js @@ -24,7 +24,7 @@ define('package/quiqqer/customer/bin/backend/controls/create/CustomerWindow', [ ], options: { - maxHeight: 700, + maxHeight: 750, maxWidth: 600, buttons: false }, @@ -50,6 +50,7 @@ define('package/quiqqer/customer/bin/backend/controls/create/CustomerWindow', [ this.getContent().set('html', ''); this.getContent().setStyle('padding', 0); + this.Loader.show(); new CreateCustomer({ events: { diff --git a/src/QUI/ERP/Customer/Customers.php b/src/QUI/ERP/Customer/Customers.php index e80b53a..04398f7 100644 --- a/src/QUI/ERP/Customer/Customers.php +++ b/src/QUI/ERP/Customer/Customers.php @@ -67,11 +67,7 @@ public function createCustomer($customerId, array $address = [], array $groupIds $User->save(); if (!empty($address)) { - try { - $Address = $User->getStandardAddress(); - } catch (QUI\Exception) { - $Address = $User->addAddress(); - } + $Address = $User->getStandardAddress(); $needles = [ 'salutation', -- GitLab