diff --git a/ajax/frontend/redeem.php b/ajax/frontend/redeem.php index f11c4182eb2b2ed2308384b72aa20370dbd32847..b78d2ea63a38c222f924be84fe879be09cdad08f 100644 --- a/ajax/frontend/redeem.php +++ b/ajax/frontend/redeem.php @@ -46,6 +46,11 @@ function ($code, $orderHash) { $Order->setData('quiqqer-coupons', $coupons); $Order->update(); + // add + if ($Order instanceof QUI\ERP\Order\OrderInProcess) { + $CouponCode->addToOrder($Order); + } + return true; }, ['code', 'orderHash'] diff --git a/bin/frontend/classes/CouponCodes.js b/bin/frontend/classes/CouponCodes.js index c3774c452855a8edea64dd1503ee243452523ade..1ba5286e206c513b41cb57421e3abcb87e60f1c2 100644 --- a/bin/frontend/classes/CouponCodes.js +++ b/bin/frontend/classes/CouponCodes.js @@ -27,6 +27,8 @@ define('package/quiqqer/coupons/bin/frontend/classes/CouponCodes', [ * @return {Promise} */ addCouponCodeToBasket: function (code, orderHash) { + console.log('1334'); + return new Promise(function (resolve, reject) { QUIAjax.post('package_quiqqer_coupons_ajax_frontend_redeem', resolve, { 'package': pkg, diff --git a/bin/frontend/controls/CouponCodeInput.js b/bin/frontend/controls/CouponCodeInput.js index 9da8d9782a9d6401f51f10cc98d464b6931999f4..21c4198578143efc939b0439c42fcf6b59ab1cdd 100644 --- a/bin/frontend/controls/CouponCodeInput.js +++ b/bin/frontend/controls/CouponCodeInput.js @@ -34,8 +34,9 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ initialize: function (options) { this.parent(options); - this.$Input = null; - this.Loader = new QUILoader(); + this.$Input = null; + this.Loader = new QUILoader(); + this.$running = false; this.addEvents({ onInject: this.$onInject @@ -78,6 +79,10 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ * Submit a CouponCode */ $submit: function () { + if (this.$running) { + return; + } + var self = this; var code = this.$Input.value.trim(); @@ -86,6 +91,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ return; } + this.$running = true; this.Loader.show(); // get order process @@ -100,6 +106,8 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ OrderProcess.getOrder().then(function (orderHash) { return CouponCodes.addCouponCodeToBasket(code, orderHash); }).then(function (redeemed) { + self.$running = false; + if (redeemed === false) { OrderProcess.Loader.hide(); self.Loader.hide(); diff --git a/src/QUI/ERP/Coupons/CouponCode.php b/src/QUI/ERP/Coupons/CouponCode.php index 8852f1f7b67f8602f5720a8f61845cd167abde14..9a1b40c235bb8fbfe772787f1751c98537509fc7 100644 --- a/src/QUI/ERP/Coupons/CouponCode.php +++ b/src/QUI/ERP/Coupons/CouponCode.php @@ -275,7 +275,7 @@ public function redeem($User = null) /** * Check if the given User can redeem this CouponCode * - * @param QUI\Users\User $User - If omitted, use session user + * @param QUI\Interfaces\Users\User $User - If omitted, use session user * @return void * @throws CouponCodeException - Thrown if not redeemable by the given User */ @@ -473,6 +473,7 @@ protected function checkValidity() if ($Now > $this->ValidUntilDate) { $this->valid = false; + return; } } @@ -499,11 +500,64 @@ protected function checkValidity() if ($usedByAllUsers) { $this->valid = false; + return; } } elseif (!empty($this->usages)) { $this->valid = false; + + return; + } + } + + /** + * @param QUI\ERP\Order\OrderInProcess $Order + * @throws QUI\Exception + */ + public function addToOrder(QUI\ERP\Order\OrderInProcess $Order) + { + $coupons = $Order->getDataEntry('quiqqer-coupons'); + + if (!$coupons) { + return; + } + + if (!is_array($coupons)) { return; } + + $priceFactors = []; + + foreach ($coupons as $coupon) { + /* @var $Coupon CouponCode */ + try { + $Coupon = Handler::getCouponCodeByCode($coupon); + } catch (\Exception $Exception) { + continue; + } + + // coupon check + if (!$Coupon->isRedeemable($Order->getCustomer())) { + continue; + } + + /* @var $Discount QUI\ERP\Discount\Discount */ + $discounts = $Coupon->getDiscounts(); + + foreach ($discounts as $Discount) { + $PriceFactor = $Discount->toPriceFactor(); + $PriceFactor->setTitle( + QUI::getLocale()->get('quiqqer/coupons', 'coupon.discount.title', [ + 'code' => $Coupon->getCode() + ]) + ); + + $priceFactors[] = $PriceFactor; + } + } + + if (!empty($priceFactors)) { + $Order->addPriceFactors($priceFactors); + } } } diff --git a/src/QUI/ERP/Coupons/Events.php b/src/QUI/ERP/Coupons/Events.php index e6b32076530bed120264d622f0036714904c96b6..8e2cdbcab076e2d64ae7c219fefcb002b806d956 100644 --- a/src/QUI/ERP/Coupons/Events.php +++ b/src/QUI/ERP/Coupons/Events.php @@ -74,8 +74,6 @@ public static function onQuiqqerOrderBasketToOrder( $discounts = $Coupon->getDiscounts(); foreach ($discounts as $Discount) { - // @todo discount check - $PriceFactor = $Discount->toPriceFactor(); $PriceFactor->setTitle( QUI::getLocale()->get('quiqqer/coupons', 'coupon.discount.title', [ @@ -83,7 +81,6 @@ public static function onQuiqqerOrderBasketToOrder( ]) ); - // @todo pos prüfen $PriceFactors->addToEnd($PriceFactor); } }