From 5fc962f93c8db721d6c40917f06fa574392bdb05 Mon Sep 17 00:00:00 2001
From: Henning <leutz@pcsg.de>
Date: Mon, 24 Mar 2025 15:33:41 +0100
Subject: [PATCH] fix: consider default currency and change event to price
 field

This commit adds the `getDefaultCurrency` function call and incorporates the default currency in
the price calculation. It further enhances the price input field to be disabled and auto-filled
when the default currency is in use. Changes to the primary input field are now also propagated to
the relevant currency input field.

In addition, the composer.json has been updated to require a minimum version of 2.1 for
`quiqqer/currency`.

Note: Besides, several function calls have been added to trigger `change` event on main input
element for accurate price calculation whenever the value changes. Moreover, the `blur` event has
been added to the input element in `onSubmit` event handler to reflect the changes immediately.
---
 bin/controls/fields/types/Price.js | 27 +++++++++++++++++++++++++--
 composer.json                      |  2 +-
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/bin/controls/fields/types/Price.js b/bin/controls/fields/types/Price.js
index dbb86709..77d51ca2 100644
--- a/bin/controls/fields/types/Price.js
+++ b/bin/controls/fields/types/Price.js
@@ -108,10 +108,12 @@ define('package/quiqqer/products/bin/controls/fields/types/Price', [
             require(['package/quiqqer/currency/bin/Currency'], (Currencies) => {
                 Promise.all([
                     Currencies.getCurrencies(),
+                    Currencies.getDefaultCurrency(),
                     this.getFormatter()
                 ]).then((r) => {
                     const currencies = r[0];
-                    const Formatter = r[1];
+                    const defaultCurrency = r[1];
+                    const Formatter = r[2];
 
                     if (currencies.length <= 1) {
                         return;
@@ -164,6 +166,21 @@ define('package/quiqqer/products/bin/controls/fields/types/Price', [
                         const displayNode = container.querySelector('.displayNode');
                         const input = container.querySelector('input');
 
+                        if (defaultCurrency === currency.code) {
+                            input.disabled = true;
+                            input.value = this.$Input.value;
+
+                            this.$Input.addEvent('change', () => {
+                                input.value = this.$Input.value;
+
+                                this.$calcBruttoPriceForCurrency(
+                                    input.value,
+                                    currency.code,
+                                    displayNode
+                                );
+                            });
+                        }
+
                         container.querySelector('button')
                             .addEventListener('click', this.openBruttoInputForCurrencies);
 
@@ -277,6 +294,7 @@ define('package/quiqqer/products/bin/controls/fields/types/Price', [
             ) {
                 this.getElm().value = '';
                 this.$Input.value = '';
+                this.$Input.fireEvent('change');
                 this.$calcBruttoPrice();
                 return;
             }
@@ -290,12 +308,14 @@ define('package/quiqqer/products/bin/controls/fields/types/Price', [
             if ((foundGroupSeparator || foundDecimalSeparator) && !(foundGroupSeparator && !foundDecimalSeparator)) {
                 this.getElm().value = value;
                 this.$Input.value = value;
+                this.$Input.fireEvent('change');
                 return;
             }
 
             this.getFormatter().then((Formatter) => {
                 this.getElm().value = Formatter.format(parseFloat(value));
                 this.$Input.value = Formatter.format(parseFloat(value));
+                this.$Input.fireEvent('change');
             });
 
             this.$calcBruttoPrice();
@@ -408,7 +428,10 @@ define('package/quiqqer/products/bin/controls/fields/types/Price', [
                 productId: this.$productId,
                 events: {
                     onSubmit: (Win, value) => {
-                        e.target.getParent('label').querySelector('input').value = value;
+                        const input = e.target.getParent('label').querySelector('input');
+
+                        input.value = value;
+                        input.dispatchEvent(new Event('blur', {bubbles: false}));
                     }
                 }
             }).open();
diff --git a/composer.json b/composer.json
index b8047731..70443086 100644
--- a/composer.json
+++ b/composer.json
@@ -21,7 +21,7 @@
         "quiqqer/erp": "^3.2",
         "quiqqer/areas": "^2",
         "quiqqer/bricks": "^2",
-        "quiqqer/currency": "^2",
+        "quiqqer/currency": "^2.1",
         "quiqqer/discount": "^2",
         "quiqqer/pace": "^2",
         "quiqqer/product-bricks": "^2",
-- 
GitLab