From b34f1c35c71bf88e8dba49ac2ccc8ec43d4e3ccf Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 12 Jun 2014 16:42:00 +0200 Subject: [PATCH] Fixed the checking of maximum coupon usage count --- core/lib/Thelia/Coupon/CouponFactory.php | 2 +- core/lib/Thelia/Coupon/CouponManager.php | 83 +++++++++++++----------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/core/lib/Thelia/Coupon/CouponFactory.php b/core/lib/Thelia/Coupon/CouponFactory.php index 40a4f0483..f1b87ae19 100644 --- a/core/lib/Thelia/Coupon/CouponFactory.php +++ b/core/lib/Thelia/Coupon/CouponFactory.php @@ -69,7 +69,7 @@ class CouponFactory } // Check coupon usage count - if ($couponModel->getUsagesLeft($this->facade->getCustomer()->getId()) <= 0) { + if (! $couponModel->isUsageUnlimited() && $couponModel->getUsagesLeft($this->facade->getCustomer()->getId()) <= 0) { throw new CouponNoUsageLeftException($couponCode); } diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index ecb491673..b4280f4a3 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -278,57 +278,62 @@ class CouponManager */ public function decrementQuantity(Coupon $coupon, $customerId = null) { - $ret = false; + if ($coupon->isUsageUnlimited()) { + $ret = true; + } + else { + $ret = false; - try { + try { - $usageLeft = $coupon->getUsagesLeft($customerId); + $usageLeft = $coupon->getUsagesLeft($customerId); - if ($usageLeft > 0) { + if ($usageLeft > 0) { - // If the coupon usage is per user, add an entry to coupon customer usage count table - if ($coupon->getPerCustomerUsageCount()) { + // If the coupon usage is per user, add an entry to coupon customer usage count table + if ($coupon->getPerCustomerUsageCount()) { - if (null == $customerId) { - throw new \LogicException("Customer should not be null at this time."); - } + if (null == $customerId) { + throw new \LogicException("Customer should not be null at this time."); + } - $ccc = CouponCustomerCountQuery::create() - ->filterByCouponId($coupon->getId()) - ->filterByCustomerId($customerId) - ->findOne() - ; + $ccc = CouponCustomerCountQuery::create() + ->filterByCouponId($coupon->getId()) + ->filterByCustomerId($customerId) + ->findOne() + ; - if ($ccc === null) { - $ccc = new CouponCustomerCount(); + if ($ccc === null) { + $ccc = new CouponCustomerCount(); + + $ccc + ->setCustomerId($customerId) + ->setCouponId($coupon->getId()) + ->setCount(0); + } + + $newCount = 1 + $ccc->getCount(); $ccc - ->setCustomerId($customerId) - ->setCouponId($coupon->getId()) - ->setCount(0); + ->setCount($newCount) + ->save() + ; + + $ret = $usageLeft - $newCount; + } else { + $usageLeft--; + + $coupon->setMaxUsage($usageLeft); + + $coupon->save(); + + $ret = $usageLeft; } - - $newCount = 1 + $ccc->getCount(); - - $ccc - ->setCount($newCount) - ->save() - ; - - $ret = $usageLeft - $newCount; - } else { - $usageLeft--; - - $coupon->setMaxUsage($usageLeft); - - $coupon->save(); - - $ret = $usageLeft; } + } catch (\Exception $ex) { + // Just log the problem. + Tlog::getInstance()->addError(sprintf("Failed to decrement coupon %s: %s", $coupon->getCode(), $ex->getMessage())); } - } catch (\Exception $ex) { - // Just log the problem. - Tlog::getInstance()->addError(sprintf("Failed to decrement coupon %s: %s", $coupon->getCode(), $ex->getMessage())); } return $ret;