From d9d5a39bf5916ddeaa4af316da02b88222529ac5 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Mon, 19 Sep 2022 16:36:11 +0200
Subject: [PATCH] refactor: addPriceFactor method -> quiqqer/erp#78

---
 src/QUI/ERP/Accounting/ArticleList.php        | 38 +++++++++++++++++++
 .../Accounting/PriceFactors/FactorList.php    |  9 +++++
 2 files changed, 47 insertions(+)

diff --git a/src/QUI/ERP/Accounting/ArticleList.php b/src/QUI/ERP/Accounting/ArticleList.php
index 28d8e6d..2ede962 100644
--- a/src/QUI/ERP/Accounting/ArticleList.php
+++ b/src/QUI/ERP/Accounting/ArticleList.php
@@ -11,6 +11,8 @@
 
 use function count;
 use function is_null;
+use function key;
+use function round;
 
 /**
  * Class ArticleList
@@ -477,6 +479,42 @@ public function importPriceFactors(QUI\ERP\Accounting\PriceFactors\FactorList $P
         $this->PriceFactors = $PriceFactors;
     }
 
+    /**
+     * @param \QUI\ERP\Accounting\PriceFactors\Factor|QUI\ERP\Products\Utils\PriceFactor $PriceFactor
+     * @return void
+     *
+     * @throws \QUI\ERP\Exception
+     */
+    public function addPriceFactor($PriceFactor)
+    {
+        if ($PriceFactor instanceof QUI\ERP\Products\Utils\PriceFactor) {
+            $isNetto   = $this->isNetto;
+            $vat       = 0;
+            $netto     = $PriceFactor->getNettoSum();
+            $precision = $this->getCurrency()->getPrecision();
+
+            if (count($this->vatArray)) {
+                $vat = key($this->vatArray);
+            }
+
+            $PriceFactor->setVat($vat);
+
+            $brutto = $netto * ((100 + $vat) / 100);
+            $brutto = round($brutto, $precision);
+
+            if ($isNetto) {
+                $PriceFactor->setSum($netto);
+            } else {
+                $PriceFactor->setSum($brutto);
+            }
+
+            $PriceFactor = new QUI\ERP\Accounting\PriceFactors\Factor($PriceFactor->toArray());
+        }
+
+        $this->PriceFactors->addFactor($PriceFactor);
+        $this->recalculate();
+    }
+
     //endregion
 
     //region order
diff --git a/src/QUI/ERP/Accounting/PriceFactors/FactorList.php b/src/QUI/ERP/Accounting/PriceFactors/FactorList.php
index ac53aea..221cdd6 100644
--- a/src/QUI/ERP/Accounting/PriceFactors/FactorList.php
+++ b/src/QUI/ERP/Accounting/PriceFactors/FactorList.php
@@ -90,6 +90,15 @@ public function toJSON(): string
         return json_encode($this->toArray());
     }
 
+    /**
+     * @param \QUI\ERP\Accounting\PriceFactors\Factor $Factor
+     * @return void
+     */
+    public function addFactor(Factor $Factor)
+    {
+        $this->list[] = $Factor;
+    }
+
     //region iterator
 
     /**
-- 
GitLab