From 1412de0c9982660835e6344504f4afa246581ba9 Mon Sep 17 00:00:00 2001 From: Henning Leutz <leutz@pcsg.de> Date: Thu, 20 Sep 2018 17:26:07 +0200 Subject: [PATCH] fix: refactor: coupon add to order --- ajax/frontend/redeem.php | 5 +++ bin/frontend/classes/CouponCodes.js | 2 + bin/frontend/controls/CouponCodeInput.js | 12 ++++- src/QUI/ERP/Coupons/CouponCode.php | 56 +++++++++++++++++++++++- src/QUI/ERP/Coupons/Events.php | 3 -- 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/ajax/frontend/redeem.php b/ajax/frontend/redeem.php index f11c418..b78d2ea 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 c3774c4..1ba5286 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 9da8d97..21c4198 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 8852f1f..9a1b40c 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 e6b3207..8e2cdbc 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); } } -- GitLab