Skip to content
Code-Schnipsel Gruppen Projekte
Commit 1a8f1df5 erstellt von Henning Leutz's avatar Henning Leutz :martial_arts_uniform:
Dateien durchsuchen

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.
Übergeordneter cf368687
Keine zugehörigen Branchen gefunden
Keine zugehörigen Tags gefunden
2 Merge Requests!56Update 'next-3.x' with latest changes from 'main',!55feat: runtime currency
Pipeline-Nr. 16589 fehlgeschlagen
...@@ -7,11 +7,14 @@ ...@@ -7,11 +7,14 @@
/** /**
* Set the user currency * Set the user currency
*/ */
use QUI\ERP\Currency\Handler;
QUI::$Ajax->registerFunction( QUI::$Ajax->registerFunction(
'package_quiqqer_currency_ajax_setUserCurrency', 'package_quiqqer_currency_ajax_setUserCurrency',
function ($currency) { function ($currency) {
$allowed = QUI\ERP\Currency\Handler::getAllowedCurrencies(); $allowed = Handler::getAllowedCurrencies();
$allowed = \array_map(function ($Currency) { $allowed = array_map(function ($Currency) {
return $Currency->getCode(); return $Currency->getCode();
}, $allowed); }, $allowed);
...@@ -24,6 +27,8 @@ function ($currency) { ...@@ -24,6 +27,8 @@ function ($currency) {
$User = QUI::getUserBySession(); $User = QUI::getUserBySession();
$User->setAttribute('quiqqer.erp.currency', $currency); $User->setAttribute('quiqqer.erp.currency', $currency);
$User->save(); $User->save();
Handler::setRuntimeCurrency(Handler::getCurrency($currency));
}, },
['currency'] ['currency']
); );
...@@ -209,6 +209,7 @@ define('package/quiqqer/currency/bin/controls/Switch', [ ...@@ -209,6 +209,7 @@ define('package/quiqqer/currency/bin/controls/Switch', [
}); });
QUIAjax.post('package_quiqqer_currency_ajax_setUserCurrency', () => { QUIAjax.post('package_quiqqer_currency_ajax_setUserCurrency', () => {
/*
window.DEFAULT_USER_CURRENCY = Curr; window.DEFAULT_USER_CURRENCY = Curr;
this.fireEvent('changeCurrency', [ this.fireEvent('changeCurrency', [
...@@ -220,6 +221,9 @@ define('package/quiqqer/currency/bin/controls/Switch', [ ...@@ -220,6 +221,9 @@ define('package/quiqqer/currency/bin/controls/Switch', [
this, this,
Curr Curr
]); ]);
*/
window.location.reload();
}, { }, {
'package': 'quiqqer/currency', 'package': 'quiqqer/currency',
currency: Curr.code currency: Curr.code
......
...@@ -22,22 +22,19 @@ class EventHandler ...@@ -22,22 +22,19 @@ class EventHandler
*/ */
public static function onTemplateGetHeader(QUI\Template $TemplateManager): void public static function onTemplateGetHeader(QUI\Template $TemplateManager): void
{ {
try { $TemplateManager->extendHeader(
$Currency = Handler::getDefaultCurrency(); '<script>
window.DEFAULT_CURRENCY = "' . Handler::getDefaultCurrency()->getCode() . '";
window.RUNTIME_CURRENCY = ' . Handler::getRuntimeCurrency()->getCode() . ';
</script>'
);
$TemplateManager->extendHeader( $UserCurrency = Handler::getUserCurrency();
'<script>window.DEFAULT_CURRENCY = "' . $Currency->getCode() . '";</script>'
);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception, QUI\System\Log::LEVEL_WARNING);
}
$Currency = Handler::getUserCurrency();
if ($Currency) { if ($UserCurrency) {
$TemplateManager->extendHeader( $TemplateManager->extendHeader(
'<script> '<script>
window.DEFAULT_USER_CURRENCY = ' . json_encode($Currency->toArray()) . '; window.DEFAULT_USER_CURRENCY = ' . json_encode($UserCurrency->toArray()) . ';
</script>' </script>'
); );
} }
......
...@@ -33,15 +33,11 @@ class Handler ...@@ -33,15 +33,11 @@ class Handler
/** /**
* currency temp list * currency temp list
*
* @var array
*/ */
protected static array $currencies = []; protected static array $currencies = [];
/**
* @var Currency|null
*/
protected static ?Currency $Default = null; protected static ?Currency $Default = null;
protected static ?Currency $RuntimeCurrency = null;
/** /**
* Return the real table name * Return the real table name
...@@ -188,7 +184,6 @@ public static function deleteCurrency(string $currency): void ...@@ -188,7 +184,6 @@ public static function deleteCurrency(string $currency): void
* Return the default currency * Return the default currency
* *
* @return Currency|null * @return Currency|null
* @throws QUI\Exception
*/ */
public static function getDefaultCurrency(): ?Currency public static function getDefaultCurrency(): ?Currency
{ {
...@@ -590,4 +585,40 @@ public static function getCurrencyTypes(): array ...@@ -590,4 +585,40 @@ public static function getCurrencyTypes(): array
return $currencyTypes; return $currencyTypes;
} }
// endregion // 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
} }
0% Lade oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren