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
No related branches found
No related tags found
2 Merge Requests!56Update 'next-3.x' with latest changes from 'main',!55feat: runtime currency
Pipeline #16589 fehlgeschlagen mit Phase
in 3 Minuten
......@@ -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']
);
......@@ -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
......
......@@ -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>'
);
}
......
......@@ -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
}
0% oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren