diff --git a/bin/frontend/controls/CouponCodeInput.js b/bin/frontend/controls/CouponCodeInput.js index 21c4198578143efc939b0439c42fcf6b59ab1cdd..a602010a8eef5031d2f392c9d86d811f42691c4a 100644 --- a/bin/frontend/controls/CouponCodeInput.js +++ b/bin/frontend/controls/CouponCodeInput.js @@ -114,7 +114,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ return; } - OrderProcess.refreshCurrentStep(); + OrderProcess.reload(); }); } }); diff --git a/src/QUI/ERP/Coupons/CouponCode.php b/src/QUI/ERP/Coupons/CouponCode.php index 837695e90bd70c79a84cfc50e66016c3fbc184bd..aec25c25e84917a7b15b7a79399322f82649f461 100644 --- a/src/QUI/ERP/Coupons/CouponCode.php +++ b/src/QUI/ERP/Coupons/CouponCode.php @@ -405,7 +405,7 @@ public function isValid() /** * Checks if an CouponCode has been redeemed by a user * - * @param QUI\Users\User $User + * @param QUI\Interfaces\Users\User $User * @return bool */ public function hasUserRedeemed($User) @@ -536,6 +536,7 @@ public function addToOrder(QUI\ERP\Order\OrderInProcess $Order) } $priceFactors = []; + $articles = []; foreach ($coupons as $coupon) { /* @var $Coupon CouponCode */ @@ -562,11 +563,65 @@ public function addToOrder(QUI\ERP\Order\OrderInProcess $Order) ); $priceFactors[] = $PriceFactor; + + $articles[] = new QUI\ERP\Accounting\Invoice\Articles\Text([ + 'id' => '-', + 'articleNo' => $Coupon->getCode(), + 'title' => $PriceFactor->getTitle(), + 'description' => '', + 'unitPrice' => 0, + 'control' => '', + 'quantity' => 1, + 'customData' => [ + 'package' => 'quiqqer/coupon', + 'code' => $Coupon->getCode() + ] + ]); } } - if (!empty($priceFactors)) { - $Order->addPriceFactors($priceFactors); + if (empty($priceFactors)) { + return; } + + /** + * @param QUI\ERP\Accounting\Invoice\Articles\Text $Article + * @return boolean + */ + $isInArticles = function ($Article) use ($Order) { + $articles = $Order->getArticles(); + $code = $Article->getCustomData()['code']; + + foreach ($articles as $Entry) { + if (!method_exists($Entry, 'getCustomData')) { + continue; + } + + $customData = $Entry->getCustomData(); + + if (!$customData || !is_array($customData)) { + continue; + } + + if (!isset($customData['package']) || !isset($customData['code'])) { + continue; + } + + return $customData['package'] === 'quiqqer/coupon' + && $customData['code'] === $code; + } + + return false; + }; + + foreach ($articles as $Article) { + /* @var $PriceFactor QUI\ERP\Accounting\Invoice\Articles\Text */ + if ($isInArticles($Article) === false) { + $Order->addArticle($Article); + } + } + + $Order->update(); + $Order->addPriceFactors($priceFactors); } }