diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php index c32a23230adcef6c6fecd634249d4ca52644f555..2648137f26135447a71acfd96880842af513b5c3 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 1cb6dbacfbd6cada8640ff0d110e8b445cb433f1..83acd3f232d51aa310e30c0747f650406cc85cc2 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 28b3893a3fc7fa6dc17b19db852ec9bea71ac671..1d13076e46bce8ba32a964906480d3aded987084 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; }