Example #1
0
 /**
  * Build a CouponInterface from its Model data contained in the DataBase
  *
  * @param Coupon $model Database data
  *
  * @return CouponInterface ready to use CouponInterface object instance
  */
 public function buildCouponFromModel(Coupon $model)
 {
     $isCumulative = $model->getIsCumulative() == 1 ? true : false;
     $isRemovingPostage = $model->getIsRemovingPostage() == 1 ? true : false;
     if (!$this->container->has($model->getType())) {
         return false;
     }
     /** @var CouponInterface $couponManager*/
     $couponManager = $this->container->get($model->getType());
     $couponManager->set($this->facade, $model->getCode(), $model->getTitle(), $model->getShortDescription(), $model->getDescription(), $model->getEffects(), $isCumulative, $isRemovingPostage, $model->getIsAvailableOnSpecialOffers(), $model->getIsEnabled(), $model->getMaxUsage(), $model->getExpirationDate(), $model->getFreeShippingForCountries(), $model->getFreeShippingForModules(), $model->getPerCustomerUsageCount());
     /** @var ConditionFactory $conditionFactory */
     $conditionFactory = $this->container->get('thelia.condition.factory');
     $conditions = $conditionFactory->unserializeConditionCollection($model->getSerializedConditions());
     $couponManager->setConditions($conditions);
     return clone $couponManager;
 }
Example #2
0
 /**
  * Add a coupon usage, for the case the related order is canceled.
  *
  * @param Coupon $coupon
  * @param int $customerId
  */
 public function incrementQuantity(Coupon $coupon, $customerId = null)
 {
     if ($coupon->isUsageUnlimited()) {
         return true;
     } else {
         try {
             $usageLeft = $coupon->getUsagesLeft($customerId);
             // If the coupon usage is per user, remove an entry from coupon customer usage count table
             if ($coupon->getPerCustomerUsageCount()) {
                 if (null == $customerId) {
                     throw new \LogicException("Customer should not be null at this time.");
                 }
                 $ccc = CouponCustomerCountQuery::create()->filterByCouponId($coupon->getId())->filterByCustomerId($customerId)->findOne();
                 if ($ccc !== null && $ccc->getCount() > 0) {
                     $newCount = $ccc->getCount() - 1;
                     $ccc->setCount($newCount)->save();
                     return $usageLeft - $newCount;
                 }
             } else {
                 // Ad one usage to coupon
                 $coupon->setMaxUsage(++$usageLeft);
                 $coupon->save();
                 return $usageLeft;
             }
         } catch (\Exception $ex) {
             // Just log the problem.
             Tlog::getInstance()->addError(sprintf("Failed to increment coupon %s: %s", $coupon->getCode(), $ex->getMessage()));
         }
     }
     return false;
 }
Example #3
0
 /**
  * Decrement this coupon quantity
  *
  * To call when a coupon is consumed
  *
  * @param \Thelia\Model\Coupon $coupon     Coupon consumed
  * @param int|null             $customerId the ID of the ordering customer
  *
  * @return int Usage left after decremental
  */
 public function decrementQuantity(Coupon $coupon, $customerId = null)
 {
     if ($coupon->isUsageUnlimited()) {
         $ret = true;
     } else {
         $ret = false;
         try {
             $usageLeft = $coupon->getUsagesLeft($customerId);
             if ($usageLeft > 0) {
                 // 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.");
                     }
                     $ccc = CouponCustomerCountQuery::create()->filterByCouponId($coupon->getId())->filterByCustomerId($customerId)->findOne();
                     if ($ccc === null) {
                         $ccc = new CouponCustomerCount();
                         $ccc->setCustomerId($customerId)->setCouponId($coupon->getId())->setCount(0);
                     }
                     $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()));
         }
     }
     return $ret;
 }
Example #4
0
 /**
  * Manage how a Condition is updated
  *
  * @param Coupon              $coupon     Coupon Model
  * @param ConditionCollection $conditions Condition collection
  */
 protected function manageConditionUpdate(Coupon $coupon, ConditionCollection $conditions)
 {
     $couponEvent = new CouponCreateOrUpdateEvent($coupon->getCode(), $coupon->getType(), $coupon->getTitle(), $coupon->getEffects(), $coupon->getShortDescription(), $coupon->getDescription(), $coupon->getIsEnabled(), $coupon->getExpirationDate(), $coupon->getIsAvailableOnSpecialOffers(), $coupon->getIsCumulative(), $coupon->getIsRemovingPostage(), $coupon->getMaxUsage(), $coupon->getLocale(), $coupon->getFreeShippingForCountries(), $coupon->getFreeShippingForModules(), $coupon->getPerCustomerUsageCount());
     $couponEvent->setCouponModel($coupon);
     $couponEvent->setConditions($conditions);
     $eventToDispatch = TheliaEvents::COUPON_CONDITION_UPDATE;
     // Dispatch Event to the Action
     $this->dispatch($eventToDispatch, $couponEvent);
     $this->adminLogAppend(AdminResources::COUPON, AccessManager::UPDATE, sprintf('Coupon %s (ID %s) conditions updated', $couponEvent->getCouponModel()->getTitle(), $couponEvent->getCouponModel()->getType()), $couponEvent->getCouponModel()->getId());
 }
 /**
  * @param string $action
  * @param Coupon|null $coupon
  * @return \Thelia\Form\BaseForm
  */
 protected function getForm($action, $coupon)
 {
     $data = array();
     if (null !== $coupon) {
         $data["code"] = $coupon->getCode();
     }
     return $this->createForm(AdminForm::COUPON_CREATION, "form", $data, ['validation_groups' => ["Default", $action]]);
 }