From 39746eb2363915881decfd547da0ea3eee8484c9 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Thu, 18 Apr 2024 07:29:10 +0200
Subject: [PATCH] feat: compatibility quiqqer v2

---
 ajax/getAllowedCurrencies.php             |  1 -
 ajax/setUserCurrency.php                  |  3 +-
 ajax/update.php                           |  5 ++-
 composer.json                             |  5 +--
 src/QUI/ERP/Currency/AbstractCurrency.php | 33 ++++++++++----------
 src/QUI/ERP/Currency/Calc.php             | 38 +++++++++++++----------
 src/QUI/ERP/Currency/Conf.php             | 10 +++---
 src/QUI/ERP/Currency/Console.php          |  3 +-
 src/QUI/ERP/Currency/Cron.php             |  3 +-
 src/QUI/ERP/Currency/EventHandler.php     |  4 +--
 src/QUI/ERP/Currency/Handler.php          |  4 +--
 src/QUI/ERP/Currency/Import.php           |  6 ++--
 12 files changed, 61 insertions(+), 54 deletions(-)

diff --git a/ajax/getAllowedCurrencies.php b/ajax/getAllowedCurrencies.php
index 1b35b6a..542f52c 100644
--- a/ajax/getAllowedCurrencies.php
+++ b/ajax/getAllowedCurrencies.php
@@ -14,7 +14,6 @@
     $allowed = QUI\ERP\Currency\Handler::getAllowedCurrencies();
     $result = [];
 
-    /* @var $Currency \QUI\ERP\Currency\Currency */
     foreach ($allowed as $Currency) {
         $result[] = $Currency->toArray();
     }
diff --git a/ajax/setUserCurrency.php b/ajax/setUserCurrency.php
index 4e4af74..55f9b04 100644
--- a/ajax/setUserCurrency.php
+++ b/ajax/setUserCurrency.php
@@ -12,11 +12,10 @@
     function ($currency) {
         $allowed = QUI\ERP\Currency\Handler::getAllowedCurrencies();
         $allowed = \array_map(function ($Currency) {
-            /* @var $Currency \QUI\ERP\Currency\Currency */
             return $Currency->getCode();
         }, $allowed);
 
-        $allowed = \array_flip($allowed);
+        $allowed = array_flip($allowed);
 
         if (!isset($allowed[$currency])) {
             return;
diff --git a/ajax/update.php b/ajax/update.php
index ed59b60..f770dc7 100644
--- a/ajax/update.php
+++ b/ajax/update.php
@@ -9,6 +9,9 @@
  *
  * @return array
  */
+
+use QUI\ERP\Currency\Handler;
+
 QUI::$Ajax->registerFunction(
     'package_quiqqer_currency_ajax_update',
     function ($currency, $code, $rate, $precision, $type, $customData) {
@@ -17,7 +20,7 @@ function ($currency, $code, $rate, $precision, $type, $customData) {
         QUI\ERP\Currency\Handler::updateCurrency($currency, [
             'rate' => $rate,
             'code' => $code,
-            'type' => !empty($type) ? $type : \QUI\ERP\Currency\Handler::CURRENCY_TYPE_DEFAULT,
+            'type' => !empty($type) ? $type : Handler::CURRENCY_TYPE_DEFAULT,
             'customData' => $customData
         ]);
 
diff --git a/composer.json b/composer.json
index 55ba4e1..ea7c1cd 100644
--- a/composer.json
+++ b/composer.json
@@ -15,9 +15,10 @@
     "email": "support@pcsg.de"
   },
   "require": {
+    "php": "^8.1",
     "ext-intl": "*",
-    "quiqqer\/quiqqer": "*",
-    "quiqqer\/qui": ">=1|*@dev"
+    "quiqqer\/quiqqer": "^2",
+    "quiqqer\/qui": "^1|^2"
   },
   "autoload": {
     "psr-4": {
diff --git a/src/QUI/ERP/Currency/AbstractCurrency.php b/src/QUI/ERP/Currency/AbstractCurrency.php
index febd599..ef7cae3 100644
--- a/src/QUI/ERP/Currency/AbstractCurrency.php
+++ b/src/QUI/ERP/Currency/AbstractCurrency.php
@@ -9,6 +9,8 @@
 use NumberFormatter;
 use QUI;
 
+use QUI\Exception;
+
 use function array_key_exists;
 use function floatval;
 use function is_array;
@@ -35,27 +37,27 @@ abstract class AbstractCurrency implements CurrencyInterface
     /**
      * @var string
      */
-    protected $code;
+    protected string $code;
 
     /**
      * @var integer
      */
-    protected $precision = 2;
+    protected int $precision = 2;
 
     /**
      * @var float|bool
      */
-    protected $exchangeRate = false;
+    protected bool|float $exchangeRate = false;
 
     /**
      * @var int
      */
-    protected $autoupdate = 1;
+    protected mixed $autoupdate = 1;
 
     /**
-     * @var QUI\Locale
+     * @var ?QUI\Locale
      */
-    protected $Locale;
+    protected ?QUI\Locale $Locale;
 
     protected array $customData = [];
 
@@ -104,7 +106,7 @@ public function __construct(array $data, $Locale = false)
      *
      * @param QUI\Locale $Locale
      */
-    public function setLocale(QUI\Locale $Locale)
+    public function setLocale(QUI\Locale $Locale): void
     {
         $this->Locale = $Locale;
     }
@@ -203,9 +205,8 @@ public function amount($amount, QUI\Locale $Locale = null): float
         }
 
         $amount = str_replace(',', '.', $amount);
-        $amount = floatval($amount);
 
-        return $amount;
+        return floatval($amount);
     }
 
     /**
@@ -275,11 +276,11 @@ public function autoupdate(): bool
      *
      * @param float|string $amount
      * @param string|CurrencyInterface $Currency
-     * @return float
+     * @return float|int|string
      *
-     * @throws QUI\Exception
+     * @throws Exception
      */
-    public function convert($amount, $Currency)
+    public function convert($amount, $Currency): float|int|string
     {
         if (!is_numeric($amount)) {
             QUI\System\Log::addError('Only numeric are allowed Currency->convert()', [
@@ -347,7 +348,7 @@ public function convertFormat($amount, $Currency): string
      * @param boolean|string|Currency $Currency - optional, default = false -> return own exchange rate
      * @return float|boolean
      */
-    public function getExchangeRate($Currency = false)
+    public function getExchangeRate($Currency = false): float|bool
     {
         if ($Currency === false) {
             return $this->exchangeRate;
@@ -376,7 +377,7 @@ public function getExchangeRate($Currency = false)
      *
      * @param float|integer $rate
      */
-    public function setExchangeRate($rate)
+    public function setExchangeRate($rate): void
     {
         $this->exchangeRate = (float)$rate;
     }
@@ -408,7 +409,7 @@ public function getCustomData(): array
      * @param $value
      * @return void
      */
-    public function setCustomDataEntry(string $key, $value)
+    public function setCustomDataEntry(string $key, $value): void
     {
         $this->customData[$key] = $value;
     }
@@ -419,7 +420,7 @@ public function setCustomDataEntry(string $key, $value)
      * @param string $key
      * @return mixed
      */
-    public function getCustomDataEntry(string $key)
+    public function getCustomDataEntry(string $key): mixed
     {
         if (array_key_exists($key, $this->customData)) {
             return $this->customData[$key];
diff --git a/src/QUI/ERP/Currency/Calc.php b/src/QUI/ERP/Currency/Calc.php
index 2ab0d70..203012f 100644
--- a/src/QUI/ERP/Currency/Calc.php
+++ b/src/QUI/ERP/Currency/Calc.php
@@ -18,15 +18,18 @@ class Calc
     /**
      * Convert the amount from one to another currency
      *
-     * @param float|string $amount
-     * @param string|Currency $currencyFrom - based currency
-     * @param string|Currency $currencyTo - optional, wanted currency, default = EUR
+     * @param float|int|string $amount
+     * @param string|array|Currency $currencyFrom - based currency
+     * @param array|string|Currency $currencyTo - optional, wanted currency, default = EUR
      * @return float
      *
      * @throws QUI\Exception
      */
-    public static function convert($amount, $currencyFrom, $currencyTo = 'EUR'): float
-    {
+    public static function convert(
+        float|int|string $amount,
+        Currency|array|string $currencyFrom,
+        Currency|array|string $currencyTo = 'EUR'
+    ): float {
         $From = Handler::getCurrency($currencyFrom);
         $To = Handler::getCurrency($currencyTo);
 
@@ -36,16 +39,18 @@ public static function convert($amount, $currencyFrom, $currencyTo = 'EUR'): flo
     /**
      * Convert with currency sign
      *
-     * @param float|string $amount
-     * @param string|Currency $currencyFrom - based currency
-     * @param string|Currency $currencyTo - optional, wanted currency, default = EUR
-     *
+     * @param float|int|string $amount
+     * @param array|string|Currency $currencyFrom - based currency
+     * @param array|string|Currency $currencyTo - optional, wanted currency, default = EUR
      * @return string
      *
      * @throws QUI\Exception
      */
-    public static function convertWithSign($amount, $currencyFrom, $currencyTo = 'EUR'): string
-    {
+    public static function convertWithSign(
+        float|int|string $amount,
+        Currency|array|string $currencyFrom,
+        Currency|array|string $currencyTo = 'EUR'
+    ): string {
         $From = Handler::getCurrency($currencyFrom);
         $To = Handler::getCurrency($currencyTo);
 
@@ -55,15 +60,16 @@ public static function convertWithSign($amount, $currencyFrom, $currencyTo = 'EU
     /**
      * Return the exchange rate between two currencies
      *
-     * @param string|Currency $currencyFrom
-     * @param string|Currency $currencyTo
-     *
+     * @param array|string|Currency $currencyFrom
+     * @param array|string|Currency $currencyTo
      * @return float|boolean
      *
      * @throws QUI\Exception
      */
-    public static function getExchangeRateBetween($currencyFrom, $currencyTo)
-    {
+    public static function getExchangeRateBetween(
+        Currency|array|string $currencyFrom,
+        Currency|array|string $currencyTo
+    ): float|bool {
         $From = Handler::getCurrency($currencyFrom);
         $To = Handler::getCurrency($currencyTo);
 
diff --git a/src/QUI/ERP/Currency/Conf.php b/src/QUI/ERP/Currency/Conf.php
index abee516..1f15810 100644
--- a/src/QUI/ERP/Currency/Conf.php
+++ b/src/QUI/ERP/Currency/Conf.php
@@ -39,7 +39,7 @@ public static function getAccountingCurrency(): ?Currency
             return Handler::getCurrency(
                 self::conf('currency', 'accountingCurrency')
             );
-        } catch (QUI\Exception $Exception) {
+        } catch (QUI\Exception) {
             return Handler::getDefaultCurrency();
         }
     }
@@ -47,19 +47,19 @@ public static function getAccountingCurrency(): ?Currency
     /**
      * Return currency conf
      *
-     * @param $section
-     * @param $key
+     * @param string $section
+     * @param string|null $key
      *
      * @return array|bool|string
      */
-    public static function conf($section, $key)
+    public static function conf(string $section, ?string $key): bool|array|string
     {
         try {
             $Package = QUI::getPackage('quiqqer/currency');
             $Config = $Package->getConfig();
 
             return $Config->get($section, $key);
-        } catch (QUI\Exception $Exception) {
+        } catch (QUI\Exception) {
         }
 
         return false;
diff --git a/src/QUI/ERP/Currency/Console.php b/src/QUI/ERP/Currency/Console.php
index 5f98dbf..6318202 100644
--- a/src/QUI/ERP/Currency/Console.php
+++ b/src/QUI/ERP/Currency/Console.php
@@ -35,10 +35,9 @@ public function __construct()
      * @throws Exception
      * @see \QUI\System\Console\Tool::execute()
      */
-    public function execute()
+    public function execute(): void
     {
         Import::import();
-
         QUI::getEvents()->fireEvent('quiqqerCurrencyImport');
     }
 }
diff --git a/src/QUI/ERP/Currency/Cron.php b/src/QUI/ERP/Currency/Cron.php
index f55cd90..94a2115 100644
--- a/src/QUI/ERP/Currency/Cron.php
+++ b/src/QUI/ERP/Currency/Cron.php
@@ -18,10 +18,9 @@ class Cron
      * Start import
      * @throws QUI\Exception
      */
-    public static function import()
+    public static function import(): void
     {
         Import::import();
-
         QUI::getEvents()->fireEvent('quiqqerCurrencyImport');
     }
 }
diff --git a/src/QUI/ERP/Currency/EventHandler.php b/src/QUI/ERP/Currency/EventHandler.php
index 0c02f0a..6ae24e0 100644
--- a/src/QUI/ERP/Currency/EventHandler.php
+++ b/src/QUI/ERP/Currency/EventHandler.php
@@ -20,7 +20,7 @@ class EventHandler
     /**
      * @param QUI\Template $TemplateManager
      */
-    public static function onTemplateGetHeader(QUI\Template $TemplateManager)
+    public static function onTemplateGetHeader(QUI\Template $TemplateManager): void
     {
         try {
             $Currency = Handler::getDefaultCurrency();
@@ -50,7 +50,7 @@ public static function onTemplateGetHeader(QUI\Template $TemplateManager)
      * @param array $params Additional parameters passed to the method.
      * @return void
      */
-    public static function onPackageConfigSave(Package $Package, array $params)
+    public static function onPackageConfigSave(Package $Package, array $params): void
     {
         if ($Package->getName() !== 'quiqqer/currency') {
             return;
diff --git a/src/QUI/ERP/Currency/Handler.php b/src/QUI/ERP/Currency/Handler.php
index 5108e30..6c785e7 100644
--- a/src/QUI/ERP/Currency/Handler.php
+++ b/src/QUI/ERP/Currency/Handler.php
@@ -362,11 +362,11 @@ public static function getData(): array
     /**
      * Return a currency
      *
-     * @param string|Currency $currency
+     * @param Currency|string|array $currency
      * @return Currency
      * @throws QUI\Exception
      */
-    public static function getCurrency($currency): Currency
+    public static function getCurrency(Currency|string|array $currency): Currency
     {
         if ($currency instanceof Currency) {
             return $currency;
diff --git a/src/QUI/ERP/Currency/Import.php b/src/QUI/ERP/Currency/Import.php
index 16e8892..821540d 100644
--- a/src/QUI/ERP/Currency/Import.php
+++ b/src/QUI/ERP/Currency/Import.php
@@ -26,14 +26,14 @@ class Import
      *
      * @throws QUI\Exception
      */
-    public static function importCurrenciesFromECB()
+    public static function importCurrenciesFromECB(): void
     {
         $values = self::getECBData();
 
         foreach ($values as $currency => $rate) {
             try {
                 Handler::getCurrency($currency);
-            } catch (QUI\Exception $Exception) {
+            } catch (QUI\Exception) {
                 // currency not exists, we must create it
                 Handler::createCurrency($currency, $rate);
             }
@@ -50,7 +50,7 @@ public static function importCurrenciesFromECB()
      *
      * @throws QUI\Exception
      */
-    public static function import()
+    public static function import(): void
     {
         $values = self::getECBData();
 
-- 
GitLab