From a6e88269e7c61bdf80766d6ffe872a5f9ce68956 Mon Sep 17 00:00:00 2001 From: Henning Leutz <leutz@pcsg.de> Date: Fri, 26 Jun 2020 11:00:03 +0200 Subject: [PATCH] fix: consider null getActiveUserDiscounts --- src/QUI/ERP/Discount/Discount.php | 2 ++ src/QUI/ERP/Discount/EventHandling.php | 43 ++++++++++++++++++-------- src/QUI/ERP/Discount/Utils.php | 24 ++++++++++++++ 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php index c32a232..2648137 100644 --- a/src/QUI/ERP/Discount/Discount.php +++ b/src/QUI/ERP/Discount/Discount.php @@ -503,6 +503,7 @@ public function toPriceFactor($Locale = null, $Customer = null) if ($this->getAttribute('scope') === Handler::DISCOUNT_SCOPE_TOTAL) { return new PriceFactor([ + 'identifier' => 'discount-'.$this->getId(), 'title' => $this->getTitle($Locale), 'description' => '', 'priority' => (int)$this->getAttribute('priority'), @@ -515,6 +516,7 @@ public function toPriceFactor($Locale = null, $Customer = null) } return new QUI\ERP\Products\Utils\PriceFactor([ + 'identifier' => 'discount-'.$this->getId(), 'title' => $this->getTitle($Locale), 'description' => '', 'priority' => (int)$this->getAttribute('priority'), diff --git a/src/QUI/ERP/Discount/EventHandling.php b/src/QUI/ERP/Discount/EventHandling.php index 1cb6dba..83acd3f 100644 --- a/src/QUI/ERP/Discount/EventHandling.php +++ b/src/QUI/ERP/Discount/EventHandling.php @@ -153,7 +153,7 @@ public static function onQuiqqerProductsCalcListProduct( return false; } - return $Discount->getAttribute('scope') == Handler::DISCOUNT_SCOPE_EVERY_PRODUCT; + return (int)$Discount->getAttribute('scope') === Handler::DISCOUNT_SCOPE_EVERY_PRODUCT; }); if (!\is_array($userDiscounts) || empty($userDiscounts)) { @@ -168,6 +168,7 @@ public static function onQuiqqerProductsCalcListProduct( return; } + $PriceFactors = $Product->getPriceFactors(); $productQuantity = $Product->getQuantity(); $productNettoSum = $attributes['calculated_nettoSum']; @@ -181,13 +182,21 @@ public static function onQuiqqerProductsCalcListProduct( continue; } - $Product->getPriceFactors()->addToEnd( - $Discount->toPriceFactor( - $Calc->getUser()->getLocale(), - $Calc->getUser() - ) + // check if Pricefactor is already in + $factors = $PriceFactors->toArray(); + $Factor = $Discount->toPriceFactor( + $Calc->getUser()->getLocale(), + $Calc->getUser() ); + foreach ($factors['end'] as $factor) { + if ($factor['identifier'] === $Factor->getIdentifier()) { + continue 2; + } + } + + $PriceFactors->addToEnd($Factor); + if ($Discount->getAttribute('lastProductDiscount')) { return; } @@ -220,7 +229,7 @@ public static function onQuiqqerProductsCalcList( return false; } - return $Discount->getAttribute('scope') == Handler::DISCOUNT_SCOPE_TOTAL; + return (int)$Discount->getAttribute('scope') == Handler::DISCOUNT_SCOPE_TOTAL; }); if (!\is_array($userDiscounts)) { @@ -229,6 +238,7 @@ public static function onQuiqqerProductsCalcList( $listQuantity = $List->getQuantity(); $products = $List->getProducts(); + $PriceFactors = $List->getPriceFactors(); /* @var $Discount Discount */ foreach ($userDiscounts as $Discount) { @@ -295,14 +305,21 @@ public static function onQuiqqerProductsCalcList( } } - - $List->getPriceFactors()->addToEnd( - $Discount->toPriceFactor( - $Calc->getUser()->getLocale(), - $Calc->getUser() - ) + // check if Pricefactor is already in + $factors = $PriceFactors->toArray(); + $Factor = $Discount->toPriceFactor( + $Calc->getUser()->getLocale(), + $Calc->getUser() ); + foreach ($factors['end'] as $factor) { + if ($factor['identifier'] === $Factor->getIdentifier()) { + continue 2; + } + } + + $PriceFactors->addToEnd($Factor); + if ($Discount->getAttribute('lastSumDiscount')) { return; } diff --git a/src/QUI/ERP/Discount/Utils.php b/src/QUI/ERP/Discount/Utils.php index 28b3893..1d13076 100644 --- a/src/QUI/ERP/Discount/Utils.php +++ b/src/QUI/ERP/Discount/Utils.php @@ -127,6 +127,14 @@ public static function getActiveUserDiscounts(UserInterface $User) ] ]); + $discountsNULL = $Discounts->getChildren([ + 'where' => [ + 'active' => 1, + 'user_groups' => null + ] + ]); + + $discounts = \array_merge($discounts, $discountsNULL); if (!empty($personalDiscounts)) { $result = \array_merge($personalDiscounts, $result); @@ -136,6 +144,22 @@ public static function getActiveUserDiscounts(UserInterface $User) $result = \array_merge($discounts, $result); } + $alreadyAttached = []; + + $result = \array_filter($result, function ($Discount) use (&$alreadyAttached) { + /* @var $Discount Discount */ + $id = $Discount->getId(); + + if (isset($alreadyAttached[$id])) { + return false; + } + + $alreadyAttached[$id] = true; + + return true; + }); + + return $result; } -- GitLab