From 69174d59f52028b177ca1813109198efe70a4ac6 Mon Sep 17 00:00:00 2001
From: Henning <leutz@pcsg.de>
Date: Wed, 26 Mar 2025 09:27:21 +0100
Subject: [PATCH] feat: implement dynamic currency handling

The updates include:

- Added `quiqqer/currency` package to `composer.json`.
- Refactored code in `Basket.php`, `OrderProcess.php` and `Utils.php` to use the new
`getRuntimeCurrency` function for currency handling.
- Removed hardcoded currency setting in `Basket.php`.
- Updated code in `OrderProcess/Basket.php` to correctly reference new Basket class.
- Added logic to set currency from product data in `Utils.php`.

These changes allow for dynamic handling of currencies across the application, reducing the need
for hardcoded values and enhancing flexibility and maintainability.
---
 composer.json                                      |  1 +
 src/QUI/ERP/Order/Basket/Basket.php                | 10 ++++++++--
 src/QUI/ERP/Order/Controls/OrderProcess/Basket.php |  2 +-
 src/QUI/ERP/Order/OrderProcess.php                 |  7 ++++---
 src/QUI/ERP/Order/Utils/Utils.php                  |  3 +++
 5 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/composer.json b/composer.json
index 582afb9c..287dd392 100644
--- a/composer.json
+++ b/composer.json
@@ -23,6 +23,7 @@
     "php81_bc/strftime": "^0.7",
     "ramsey/uuid": "^3|^4",
     "quiqqer/core": "^2",
+    "quiqqer/currency": "^2.2",
     "quiqqer/qui": "^1.6.7",
     "quiqqer/erp": "^3.7.1",
     "quiqqer/countries": "^2",
diff --git a/src/QUI/ERP/Order/Basket/Basket.php b/src/QUI/ERP/Order/Basket/Basket.php
index 94a4eda1..c868498b 100644
--- a/src/QUI/ERP/Order/Basket/Basket.php
+++ b/src/QUI/ERP/Order/Basket/Basket.php
@@ -76,6 +76,7 @@ public function __construct(bool | int $basketId, null | QUI\Interfaces\Users\Us
         }
 
         $this->List->setUser($User);
+        $this->List->setCurrency(QUI\ERP\Currency\Handler::getRuntimeCurrency());
         $this->FrontendMessages = new QUI\ERP\Comments();
 
         if (is_bool($basketId)) {
@@ -101,8 +102,6 @@ public function __construct(bool | int $basketId, null | QUI\Interfaces\Users\Us
         if (!empty($data['products'])) {
             $this->import(json_decode($data['products'], true));
         }
-
-        $this->List->setCurrency(QUI\ERP\Defaults::getUserCurrency());
     }
 
     /**
@@ -461,11 +460,17 @@ public function toOrder(QUI\ERP\Order\AbstractOrder $Order): void
 
         // update the data
         $products = $Products->getProducts();
+        $Currency = $Products->getCurrency();
+
+        if (!$Currency) {
+            $Currency = QUI\ERP\Currency\Handler::getRuntimeCurrency();
+        }
 
         $InvoiceAddress = $Order->getInvoiceAddress();
         $DeliveryAddress = $Order->getDeliveryAddress();
 
         $Order->clear();
+        $Order->setCurrency($Currency);
 
         foreach ($products as $Product) {
             if (!method_exists($Product, 'toArticle')) {
@@ -479,6 +484,7 @@ public function toOrder(QUI\ERP\Order\AbstractOrder $Order): void
             }
         }
 
+
         $Order->setInvoiceAddress($InvoiceAddress);
         $Order->setDeliveryAddress($DeliveryAddress);
         $Order->update();
diff --git a/src/QUI/ERP/Order/Controls/OrderProcess/Basket.php b/src/QUI/ERP/Order/Controls/OrderProcess/Basket.php
index 7c97c2cb..29328ad2 100644
--- a/src/QUI/ERP/Order/Controls/OrderProcess/Basket.php
+++ b/src/QUI/ERP/Order/Controls/OrderProcess/Basket.php
@@ -113,7 +113,7 @@ public function validate(): void
                 $Current = $OrderProcess->getCurrentStep();
 
                 // if current step is basket, we need no cleanup ... only later
-                if ($Current instanceof QUI\ERP\Order\Controls\Basket\Basket) {
+                if ($Current instanceof QUI\ERP\Order\Controls\OrderProcess\Basket) {
                     return;
                 }
             }
diff --git a/src/QUI/ERP/Order/OrderProcess.php b/src/QUI/ERP/Order/OrderProcess.php
index d3e413cb..39e25168 100644
--- a/src/QUI/ERP/Order/OrderProcess.php
+++ b/src/QUI/ERP/Order/OrderProcess.php
@@ -153,15 +153,15 @@ public function __construct(array $attributes = [])
         $this->setAttribute('orderHash', $Order->getUUID());
 
         // set order currency
-        $UserCurrency = QUI\ERP\Defaults::getUserCurrency();
+        $UserCurrency = QUI\ERP\Currency\Handler::getRuntimeCurrency();
 
-        if ($UserCurrency && $UserCurrency->getCode() !== $Order->getCurrency()->getCode()) {
+        if ($UserCurrency->getCode() !== $Order->getCurrency()->getCode()) {
             $Order->setCurrency($UserCurrency);
             $Order->update();
         }
 
         // order is successful, so no other step must be shown
-        if ($Order && $Order->isSuccessful()) {
+        if ($Order->isSuccessful()) {
             $LastStep = end($steps);
 
             $this->setAttribute('step', $LastStep->getName());
@@ -1426,6 +1426,7 @@ protected function getProcessingStep(): mixed
         } catch (QUI\Exception) {
         }
 
+        // @todo process step sich merken, sonst 1000 neue objekte
         return new Controls\OrderProcess\Processing([
             'Order' => $this->getOrder(),
             'priority' => 40
diff --git a/src/QUI/ERP/Order/Utils/Utils.php b/src/QUI/ERP/Order/Utils/Utils.php
index ee5cfce6..e9461234 100644
--- a/src/QUI/ERP/Order/Utils/Utils.php
+++ b/src/QUI/ERP/Order/Utils/Utils.php
@@ -298,6 +298,9 @@ public static function importProductsToBasketList(
                 $productClass = $productData['class'];
             }
 
+            if (!isset($productData['price_currency'])) {
+                $productData['price_currency'] = $List->getCurrency()?->getCode();
+            }
 
             // bridge for text articles
             if (
-- 
GitLab