From be5214419fa53cef7a2df5e8a6f2126aa9cbe081 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Wed, 3 Mar 2021 17:22:12 +0100
Subject: [PATCH] fix: quiqqer/discount#10

---
 src/QUI/ERP/Products/Utils/Calc.php | 95 ++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 37 deletions(-)

diff --git a/src/QUI/ERP/Products/Utils/Calc.php b/src/QUI/ERP/Products/Utils/Calc.php
index cba0be86..6cc897ac 100644
--- a/src/QUI/ERP/Products/Utils/Calc.php
+++ b/src/QUI/ERP/Products/Utils/Calc.php
@@ -300,8 +300,8 @@ public function calcProductList(ProductList $List, $callback = false): ProductLi
             $vatArray[$vat]['sum'] = $vatArray[$vat]['sum'] + $productVatArray['sum'];
         }
 
-        $subSum   = \round($subSum, $Currency->getPrecision());
-        $nettoSum = \round($nettoSum, $Currency->getPrecision());
+//        $subSum   = \round($subSum, $Currency->getPrecision());
+//        $nettoSum = \round($nettoSum, $Currency->getPrecision());
 
         QUI\ERP\Debug::getInstance()->log('Berechnetet Produktliste MwSt', 'quiqqer/product');
         QUI\ERP\Debug::getInstance()->log($vatArray, 'quiqqer/product');
@@ -375,6 +375,14 @@ public function calcProductList(ProductList $List, $callback = false): ProductLi
                     $priceFactorValue = \round($priceFactorValue, $Currency->getPrecision());
 
                     $PriceFactor->setNettoSum($priceFactorValue);
+
+                    if (!$isNetto) {
+                        $vCalc         = $vat / 100 + 1;
+                        $bruttoDisplay = \round($priceFactorValue * $vCalc, $Currency->getPrecision());
+                        $bruttoDisplay = $Currency->format($bruttoDisplay);
+
+                        $PriceFactor->setValueText($bruttoDisplay);
+                    }
                     break;
 
                 // Prozent Angabe
@@ -422,17 +430,19 @@ public function calcProductList(ProductList $List, $callback = false): ProductLi
                     continue 2;
             }
 
-            $vatSum = \round(
-                $PriceFactor->getNettoSum() * ($vatValue / 100),
-                $Currency->getPrecision()
-            );
+            $vatSum        = $PriceFactor->getNettoSum() * ($vatValue / 100);
+            $vatSumRounded = \round($vatSum, $Currency->getPrecision());
 
             $PriceFactor->setVat($vatValue);
 
             if ($isNetto) {
-                $PriceFactor->setSum($PriceFactor->getNettoSum());
+                $PriceFactor->setSum(
+                    \round($PriceFactor->getNettoSum(), $Currency->getPrecision())
+                );
             } else {
-                $PriceFactor->setSum($vatSum + $PriceFactor->getNettoSum());
+                $vatBruttoSum = $vatSumRounded + $PriceFactor->getNettoSum();
+
+                $PriceFactor->setSum($vatBruttoSum);
             }
 
             if ($Vat && !$Vat->isVisible()) {
@@ -464,9 +474,11 @@ public function calcProductList(ProductList $List, $callback = false): ProductLi
         foreach ($vatArray as $vatEntry) {
             $vatLists[$vatEntry['vat']] = true; // liste für MWST texte
 
-            $bruttoSum = $bruttoSum + \round($vatEntry['sum'], $precision);
+            $bruttoSum = $bruttoSum + $vatEntry['sum'];
         }
 
+        $bruttoSum = \round($bruttoSum, $Currency->getPrecision());
+
         foreach ($vatLists as $vat => $bool) {
             $vatText[$vat] = ErpCalc::getVatText($vat, $this->getUser(), $Locale);
         }
@@ -491,31 +503,33 @@ public function calcProductList(ProductList $List, $callback = false): ProductLi
 
             foreach ($priceFactors as $Factor) {
                 /* @var $Factor QUI\ERP\Products\Utils\PriceFactor */
-                $priceFactorBruttoSums = $priceFactorBruttoSums + \round($Factor->getSum(), $precision);
+                $priceFactorBruttoSums = $priceFactorBruttoSums + $Factor->getSum();
             }
 
             $priceFactorBruttoSum = $subSum + $priceFactorBruttoSums;
-
-            if ($priceFactorBruttoSum !== \round($bruttoSum, $precision)) {
-                $diff = $priceFactorBruttoSum - \round($bruttoSum, $precision);
-                $diff = \round($diff, $precision);
-
-                // if we have a diff, we change the first vat price factor
-                $added = false;
-
-                foreach ($priceFactors as $Factor) {
-                    if ($Factor instanceof QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface) {
-                        $Factor->setSum(\round($Factor->getSum() - $diff, $precision));
-                        $bruttoSum = \round($bruttoSum, $precision);
-                        $added     = true;
-                        break;
-                    }
-                }
-
-                if ($added === false) {
-                    $bruttoSum = $bruttoSum + $diff;
-                }
-            }
+            $bruttoSum            = $priceFactorBruttoSum;
+//            $bruttoSumRounded     = \round($bruttoSum, $precision);
+//
+//            if ($priceFactorBruttoSum !== $bruttoSumRounded) {
+//                $diff = $priceFactorBruttoSum - \round($bruttoSum, $precision);
+//                $diff = \round($diff, $precision);
+//
+//                // if we have a diff, we change the first vat price factor
+//                $added = false;
+//
+//                foreach ($priceFactors as $Factor) {
+//                    if ($Factor instanceof QUI\ERP\Products\Interfaces\PriceFactorWithVatInterface) {
+//                        $Factor->setSum(\round($Factor->getSum() - $diff, $precision));
+//                        $bruttoSum = \round($bruttoSum, $precision);
+//                        $added     = true;
+//                        break;
+//                    }
+//                }
+//
+//                if ($added === false) {
+//                    $bruttoSum = $bruttoSum + $diff;
+//                }
+//            }
         }
 
         if ($bruttoSum <= 0 || $nettoSum <= 0) {
@@ -730,8 +744,10 @@ public function getProductPrice(
             $bruttoPrice = $this->round($nettoPrice + $vatSum);
 
             // sum
-            $nettoSum = $this->round($nettoPrice * $Product->getQuantity());
-            $vatSum   = \round($nettoSum * ($Vat->getValue() / 100), $Currency->getPrecision());
+            $nettoSum           = $this->round($nettoPrice * $Product->getQuantity());
+            $nettoSumNotRounded = $nettoPriceNotRounded * $Product->getQuantity();
+
+            $vatSum = \round($nettoSumNotRounded * ($Vat->getValue() / 100), $Currency->getPrecision());
 
             // korrektur rechnung / 1 cent problem
             if ($checkVatBrutto !== $bruttoPrice) {
@@ -746,12 +762,14 @@ public function getProductPrice(
             $bruttoSum = $bruttoPrice * $Product->getQuantity();
         } else {
             // sum
-            $nettoSum = $this->round($nettoPrice * $Product->getQuantity());
-            $vatSum   = \round($nettoSum * ($Vat->getValue() / 100), $Currency->getPrecision());
-
+            $nettoSum  = $this->round($nettoPrice * $Product->getQuantity());
+            $vatSum    = \round($nettoSum * ($Vat->getValue() / 100), $Currency->getPrecision());
             $bruttoSum = $this->round($nettoSum + $vatSum);
+
+            $nettoSumNotRounded = $nettoPriceNotRounded * $Product->getQuantity();
         }
 
+
         $price = $isNetto ? $nettoPrice : $bruttoPrice;
         $sum   = $isNetto ? $nettoSum : $bruttoSum;
 
@@ -779,13 +797,15 @@ public function getProductPrice(
         );
 
         QUI\ERP\Debug::getInstance()->log([
-            'basisPriceNotRounded' => $nettoPriceNotRounded,
+            'nettoPriceNotRounded' => $nettoPriceNotRounded,
             'basisPrice'           => $basisPrice,
             'price'                => $price,
             'sum'                  => $sum,
             'nettoSum'             => $nettoSum,
+            'nettoSumNotRounded'   => $nettoSumNotRounded,
             'nettoPrice'           => $nettoPrice,
             'vatArray'             => $vatArray,
+            'vatText'              => !empty($vatArray) ? $vatArray['text'] : '',
             'isEuVat'              => $isEuVatUser,
             'isNetto'              => $isNetto,
             'currencyData'         => $this->getCurrency()->toArray(),
@@ -799,6 +819,7 @@ public function getProductPrice(
             'price'                => $price,
             'sum'                  => $sum,
             'nettoSum'             => $nettoSum,
+            'nettoSumNotRounded'   => $nettoSumNotRounded,
             'nettoPrice'           => $nettoPrice,
             'vatArray'             => $vatArray,
             'vatText'              => !empty($vatArray) ? $vatArray['text'] : '',
-- 
GitLab