From 40f63f66b8ce2713962771fa4582b47ffd0ec630 Mon Sep 17 00:00:00 2001 From: Henning <leutz@pcsg.de> Date: Tue, 25 Mar 2025 14:22:47 +0100 Subject: [PATCH] feat: add currency support for product calculation This commit expands product calculations to include support for different currencies. The 'calc.php' file has been updated to now take a currency input and use it when calculating the product cost. If no currency is given, it will default to the current user's currency. The 'Product.js', 'Products.js' and 'frontend/Product.js' files have also been updated to pass the currency when making Ajax calls. Finally, if no currency is set for the product in 'ViewFrontend.php', the default currency or the current user's currency will be used. Additionally, 'UniqueProduct.php' now includes the currency code when converting to an article. This feature enhances the flexibility of product cost calculations and accommodates for international users by including their local currency in the calculation process. --- ajax/products/calc.php | 12 ++++++++++-- bin/classes/Product.js | 3 ++- bin/classes/Products.js | 1 + bin/classes/frontend/Product.js | 1 + src/QUI/ERP/Products/Product/UniqueProduct.php | 3 ++- src/QUI/ERP/Products/Product/ViewFrontend.php | 15 +++++++++++++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ajax/products/calc.php b/ajax/products/calc.php index 4927404d..23f58b9f 100644 --- a/ajax/products/calc.php +++ b/ajax/products/calc.php @@ -4,6 +4,7 @@ * This file contains package_quiqqer_products_ajax_products_calc */ +use QUI\ERP\Currency\Handler; use QUI\ERP\Products\Field\CustomCalcFieldInterface; use QUI\ERP\Products\Handler\Products; @@ -16,10 +17,17 @@ */ QUI::$Ajax->registerFunction( 'package_quiqqer_products_ajax_products_calc', - function ($productId, $fields, $quantity) { + function ($productId, $fields, $quantity, $currency) { $fields = json_decode($fields, true); $Product = Products::getProduct($productId); + if (!empty($currency)) { + try { + $Product->setCurrency(Handler::getCurrency($currency)); + } catch (QUI\Exception) { + } + } + if (!is_array($fields)) { $fields = []; } @@ -50,5 +58,5 @@ function ($productId, $fields, $quantity) { return $Unique->getView()->toArray(); }, - ['productId', 'fields', 'quantity'] + ['productId', 'fields', 'quantity', 'currency'] ); diff --git a/bin/classes/Product.js b/bin/classes/Product.js index 8da7ef7a..82fd521c 100644 --- a/bin/classes/Product.js +++ b/bin/classes/Product.js @@ -595,7 +595,8 @@ define('package/quiqqer/products/bin/classes/Product', [ 'package': 'quiqqer/products', productId: this.getId(), fields: JSON.encode(this.$data.fields), - quantity: quantity + quantity: quantity, + currency: window.DEFAULT_USER_CURRENCY.code }); }.bind(this)); }, diff --git a/bin/classes/Products.js b/bin/classes/Products.js index 3f310f7c..c52826ff 100644 --- a/bin/classes/Products.js +++ b/bin/classes/Products.js @@ -157,6 +157,7 @@ define('package/quiqqer/products/bin/classes/Products', [ 'package': 'quiqqer/products', productId: productId, fields: JSON.encode(fields), + currency: window.DEFAULT_USER_CURRENCY.code, onError: reject }); }); diff --git a/bin/classes/frontend/Product.js b/bin/classes/frontend/Product.js index 3d16ffe4..af1acd72 100644 --- a/bin/classes/frontend/Product.js +++ b/bin/classes/frontend/Product.js @@ -364,6 +364,7 @@ define('package/quiqqer/products/bin/classes/frontend/Product', [ 'package': 'quiqqer/products', productId: this.getId(), fields: JSON.encode(fieldList), + currency: window.DEFAULT_USER_CURRENCY.code, quantity: quantity }); }); diff --git a/src/QUI/ERP/Products/Product/UniqueProduct.php b/src/QUI/ERP/Products/Product/UniqueProduct.php index 8308dc56..ba44ee8b 100644 --- a/src/QUI/ERP/Products/Product/UniqueProduct.php +++ b/src/QUI/ERP/Products/Product/UniqueProduct.php @@ -1336,7 +1336,8 @@ public function toArticle( 'quantity' => $this->getQuantity(), 'customFields' => $this->getCustomFieldsData(), 'customData' => $this->getCustomData(), - 'displayPrice' => true + 'displayPrice' => true, + 'currency' => $this->getCurrency()?->getCode() ]; if (!$initialCalcStatus) { diff --git a/src/QUI/ERP/Products/Product/ViewFrontend.php b/src/QUI/ERP/Products/Product/ViewFrontend.php index 929e1194..51e96595 100644 --- a/src/QUI/ERP/Products/Product/ViewFrontend.php +++ b/src/QUI/ERP/Products/Product/ViewFrontend.php @@ -43,6 +43,21 @@ public function __construct(Model $Product) { $this->Product = $Product; + if (!$this->Product->getCurrency()) { + $currency = QUI::getUserBySession()->getAttribute('quiqqer.erp.currency'); + $Currency = QUI\ERP\Currency\Handler::getDefaultCurrency(); + + if (!empty($currency)) { + try { + $Currency = QUI\ERP\Currency\Handler::getCurrency($currency); + } catch (QUI\Exception) { + } + } + + $this->Product->setCurrency($Currency); + } + + if (!$Product->isActive()) { throw new QUI\ERP\Products\Product\Exception( [ -- GitLab