diff --git a/ajax/setUserCurrency.php b/ajax/setUserCurrency.php index 55f9b043601978c934aafac12d9c68fe540cfcd6..48dd7fe6736f355e7b8e23a90a3d1634baaf74be 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 798d006bc02298dce0aada524ea9c6117baea36f..eca3cf840e3e0b052dd65fdc822768eb46f970a0 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 6ae24e0ff3579461ee7a21eb9d3216236a7f538d..ea0b52096cf4bb8b25187bd651143ce90cdafdce 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 1b71cc239772bf26ab6f6dda76b6ffb121fb1274..df443632266eb4ffad9df819fa1e86cc47c76637 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 }