From 1a8f1df5a82ded57a619a31aa3fc99574123c8c9 Mon Sep 17 00:00:00 2001 From: Henning <leutz@pcsg.de> Date: Wed, 26 Mar 2025 08:06:18 +0100 Subject: [PATCH] feat: runtime currency In this commit, we have made several changes to improve the handling of currencies within the application. - Added runtime currency support to the 'Handler' class in 'src/QUI/ERP/Currency/Handler.php'. This method provides the ability to get and set the currency during runtime. - Refactored the 'setUserCurrency' function in 'ajax/setUserCurrency.php'. Removed the use of namespace on every call to 'getAllowedCurrencies' and made it less complex. - Added a script to extend the header in 'src/QUI/ERP/Currency/EventHandler.php'. The script retrieves the default and runtime currencies. - Updated 'bin/controls/Switch.js'. Commented out the previous code and added a window location reload call after changing the currency. These changes ensure that the active user has the correct currency displayed throughout their session. --- ajax/setUserCurrency.php | 9 ++++-- bin/controls/Switch.js | 4 +++ src/QUI/ERP/Currency/EventHandler.php | 21 ++++++------- src/QUI/ERP/Currency/Handler.php | 43 +++++++++++++++++++++++---- 4 files changed, 57 insertions(+), 20 deletions(-) diff --git a/ajax/setUserCurrency.php b/ajax/setUserCurrency.php index 55f9b04..48dd7fe 100644 --- a/ajax/setUserCurrency.php +++ b/ajax/setUserCurrency.php @@ -7,11 +7,14 @@ /** * Set the user currency */ + +use QUI\ERP\Currency\Handler; + QUI::$Ajax->registerFunction( 'package_quiqqer_currency_ajax_setUserCurrency', function ($currency) { - $allowed = QUI\ERP\Currency\Handler::getAllowedCurrencies(); - $allowed = \array_map(function ($Currency) { + $allowed = Handler::getAllowedCurrencies(); + $allowed = array_map(function ($Currency) { return $Currency->getCode(); }, $allowed); @@ -24,6 +27,8 @@ function ($currency) { $User = QUI::getUserBySession(); $User->setAttribute('quiqqer.erp.currency', $currency); $User->save(); + + Handler::setRuntimeCurrency(Handler::getCurrency($currency)); }, ['currency'] ); diff --git a/bin/controls/Switch.js b/bin/controls/Switch.js index 798d006..eca3cf8 100644 --- a/bin/controls/Switch.js +++ b/bin/controls/Switch.js @@ -209,6 +209,7 @@ define('package/quiqqer/currency/bin/controls/Switch', [ }); QUIAjax.post('package_quiqqer_currency_ajax_setUserCurrency', () => { + /* window.DEFAULT_USER_CURRENCY = Curr; this.fireEvent('changeCurrency', [ @@ -220,6 +221,9 @@ define('package/quiqqer/currency/bin/controls/Switch', [ this, Curr ]); + */ + + window.location.reload(); }, { 'package': 'quiqqer/currency', currency: Curr.code diff --git a/src/QUI/ERP/Currency/EventHandler.php b/src/QUI/ERP/Currency/EventHandler.php index 6ae24e0..ea0b520 100644 --- a/src/QUI/ERP/Currency/EventHandler.php +++ b/src/QUI/ERP/Currency/EventHandler.php @@ -22,22 +22,19 @@ class EventHandler */ public static function onTemplateGetHeader(QUI\Template $TemplateManager): void { - try { - $Currency = Handler::getDefaultCurrency(); + $TemplateManager->extendHeader( + '<script> + window.DEFAULT_CURRENCY = "' . Handler::getDefaultCurrency()->getCode() . '"; + window.RUNTIME_CURRENCY = ' . Handler::getRuntimeCurrency()->getCode() . '; + </script>' + ); - $TemplateManager->extendHeader( - '<script>window.DEFAULT_CURRENCY = "' . $Currency->getCode() . '";</script>' - ); - } catch (QUI\Exception $Exception) { - QUI\System\Log::writeException($Exception, QUI\System\Log::LEVEL_WARNING); - } - - $Currency = Handler::getUserCurrency(); + $UserCurrency = Handler::getUserCurrency(); - if ($Currency) { + if ($UserCurrency) { $TemplateManager->extendHeader( '<script> - window.DEFAULT_USER_CURRENCY = ' . json_encode($Currency->toArray()) . '; + window.DEFAULT_USER_CURRENCY = ' . json_encode($UserCurrency->toArray()) . '; </script>' ); } diff --git a/src/QUI/ERP/Currency/Handler.php b/src/QUI/ERP/Currency/Handler.php index 1b71cc2..df44363 100644 --- a/src/QUI/ERP/Currency/Handler.php +++ b/src/QUI/ERP/Currency/Handler.php @@ -33,15 +33,11 @@ class Handler /** * currency temp list - * - * @var array */ protected static array $currencies = []; - /** - * @var Currency|null - */ protected static ?Currency $Default = null; + protected static ?Currency $RuntimeCurrency = null; /** * Return the real table name @@ -188,7 +184,6 @@ public static function deleteCurrency(string $currency): void * Return the default currency * * @return Currency|null - * @throws QUI\Exception */ public static function getDefaultCurrency(): ?Currency { @@ -590,4 +585,40 @@ public static function getCurrencyTypes(): array return $currencyTypes; } // endregion + + //region runtime + + public static function getRuntimeCurrency(): Currency + { + if (self::$RuntimeCurrency) { + return self::$RuntimeCurrency; + } + + if (QUI::getSession()->get('currency')) { + try { + $Currency = self::getCurrency(QUI::getSession()->get('currency')); + self::$RuntimeCurrency = $Currency; + return self::$RuntimeCurrency; + } catch (QUI\Exception) { + } + } + + if (QUI::isFrontend()) { + self::$RuntimeCurrency = self::getUserCurrency(QUI::getUserBySession()); + return self::$RuntimeCurrency; + } + + return self::getDefaultCurrency(); + } + + public static function setRuntimeCurrency(Currency $currency): void + { + self::$RuntimeCurrency = $currency; + + if (QUI::isFrontend()) { + QUI::getSession()->set('currency', $currency->getCode()); + } + } + + //endregion } -- GitLab