Пример #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;
 }
Пример #2
0
    /**
     * Generate a valid Coupon model
     */
    public function generateCouponModel($facade, ConditionFactory $conditionFactory)
    {
        // Coupons
        $coupon1 = new Coupon();
        $coupon1->setCode('XMAS');
        $coupon1->setType('thelia.coupon.type.remove_x_amount');
        $coupon1->setTitle('Christmas coupon');
        $coupon1->setShortDescription('Coupon for Christmas removing 10€ if your total checkout is more than 40€');
        $coupon1->setDescription('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras at luctus tellus. Integer turpis mauris, aliquet vitae risus tristique, pellentesque vestibulum urna. Vestibulum sodales laoreet lectus dictum suscipit. Praesent vulputate, sem id varius condimentum, quam magna tempor elit, quis venenatis ligula nulla eget libero. Cras egestas euismod tellus, id pharetra leo suscipit quis. Donec lacinia ac lacus et ultricies. Nunc in porttitor neque. Proin at quam congue, consectetur orci sed, congue nulla. Nulla eleifend nunc ligula, nec pharetra elit tempus quis. Vivamus vel mauris sed est dictum blandit. Maecenas blandit dapibus velit ut sollicitudin. In in euismod mauris, consequat viverra magna. Cras velit velit, sollicitudin commodo tortor gravida, tempus varius nulla.

Donec rhoncus leo mauris, id porttitor ante luctus tempus. Curabitur quis augue feugiat, ullamcorper mauris ac, interdum mi. Quisque aliquam lorem vitae felis lobortis, id interdum turpis mattis. Vestibulum diam massa, ornare congue blandit quis, facilisis at nisl. In tortor metus, venenatis non arcu nec, sollicitudin ornare nisl. Nunc erat risus, varius nec urna at, iaculis lacinia elit. Aenean ut felis tempus, tincidunt odio non, sagittis nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vitae hendrerit elit. Nunc sit amet gravida risus, euismod lobortis massa. Nam a erat mauris. Nam a malesuada lorem. Nulla id accumsan dolor, sed rhoncus tellus. Quisque dictum felis sed leo auctor, at volutpat lectus viverra. Morbi rutrum, est ac aliquam imperdiet, nibh sem sagittis justo, ac mattis magna lacus eu nulla.

Duis interdum lectus nulla, nec pellentesque sapien condimentum at. Suspendisse potenti. Sed eu purus tellus. Nunc quis rhoncus metus. Fusce vitae tellus enim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor porttitor erat vitae iaculis. Sed est elit, consequat non ornare vitae, vehicula eget lectus. Etiam consequat sapien mauris, eget consectetur magna imperdiet eget. Nunc sollicitudin luctus velit, in commodo nulla adipiscing fermentum. Fusce nisi sapien, posuere vitae metus sit amet, facilisis sollicitudin dui. Fusce ultricies auctor enim sit amet iaculis. Morbi at vestibulum enim, eget adipiscing eros.

Praesent ligula lorem, faucibus ut metus quis, fermentum iaculis erat. Pellentesque elit erat, lacinia sed semper ac, sagittis vel elit. Nam eu convallis est. Curabitur rhoncus odio vitae consectetur pellentesque. Nam vitae arcu nec ante scelerisque dignissim vel nec neque. Suspendisse augue nulla, mollis eget dui et, tempor facilisis erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac diam ipsum. Donec convallis dui ultricies velit auctor, non lobortis nulla ultrices. Morbi vitae dignissim ante, sit amet lobortis tortor. Nunc dapibus condimentum augue, in molestie neque congue non.

Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesuada tortor vel erat volutpat tincidunt. In vehicula diam est, a convallis eros scelerisque ut. Donec aliquet venenatis iaculis. Ut a arcu gravida, placerat dui eu, iaculis nisl. Quisque adipiscing orci sit amet dui dignissim lacinia. Sed vulputate lorem non dolor adipiscing ornare. Morbi ornare id nisl id aliquam. Ut fringilla elit ante, nec lacinia enim fermentum sit amet. Aenean rutrum lorem eu convallis pharetra. Cras malesuada varius metus, vitae gravida velit. Nam a varius ipsum, ac commodo dolor. Phasellus nec elementum elit. Etiam vel adipiscing leo.');
        $coupon1->setAmount(10.0);
        $coupon1->setIsUsed(true);
        $coupon1->setIsEnabled(true);
        $date = new \DateTime();
        $coupon1->setExpirationDate($date->setTimestamp(strtotime("today + 3 months")));
        $condition1 = new MatchForTotalAmount($facade);
        $operators = array(MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL);
        $values = array(MatchForTotalAmount::CART_TOTAL => 40.0, MatchForTotalAmount::CART_CURRENCY => 'EUR');
        $condition1->setValidatorsFromForm($operators, $values);
        $condition2 = new MatchForTotalAmount($facade);
        $operators = array(MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL);
        $values = array(MatchForTotalAmount::CART_TOTAL => 400.0, MatchForTotalAmount::CART_CURRENCY => 'EUR');
        $condition2->setValidatorsFromForm($operators, $values);
        $conditions = new ConditionCollection();
        $conditions[] = $condition1;
        $conditions[] = $condition2;
        $serializedConditions = $conditionFactory->serializeConditionCollection($conditions);
        $coupon1->setSerializedConditions($serializedConditions);
        $coupon1->setMaxUsage(40);
        $coupon1->setIsCumulative(true);
        $coupon1->setIsRemovingPostage(false);
        $coupon1->setIsAvailableOnSpecialOffers(true);
        return $coupon1;
    }
Пример #3
0
 /**
  * Declares an association between this object and a ChildCoupon object.
  *
  * @param                  ChildCoupon $v
  * @return                 \Thelia\Model\CouponVersion The current object (for fluent API support)
  * @throws PropelException
  */
 public function setCoupon(ChildCoupon $v = null)
 {
     if ($v === null) {
         $this->setId(NULL);
     } else {
         $this->setId($v->getId());
     }
     $this->aCoupon = $v;
     // Add binding for other direction of this n:n relationship.
     // If this object has already been added to the ChildCoupon object, it will not be re-added.
     if ($v !== null) {
         $v->addCouponVersion($this);
     }
     return $this;
 }
Пример #4
0
 /**
  * Filter the query by a related \Thelia\Model\Coupon object
  *
  * @param \Thelia\Model\Coupon|ObjectCollection $coupon The related object(s) to use as filter
  * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return ChildCouponCountryQuery The current query, for fluid interface
  */
 public function filterByCoupon($coupon, $comparison = null)
 {
     if ($coupon instanceof \Thelia\Model\Coupon) {
         return $this->addUsingAlias(CouponCountryTableMap::COUPON_ID, $coupon->getId(), $comparison);
     } elseif ($coupon instanceof ObjectCollection) {
         if (null === $comparison) {
             $comparison = Criteria::IN;
         }
         return $this->addUsingAlias(CouponCountryTableMap::COUPON_ID, $coupon->toKeyValue('PrimaryKey', 'Id'), $comparison);
     } else {
         throw new PropelException('filterByCoupon() only accepts arguments of type \\Thelia\\Model\\Coupon or Collection');
     }
 }
Пример #5
0
 /**
  * Exclude object from result
  *
  * @param   ChildCoupon $coupon Object to remove from the list of results
  *
  * @return ChildCouponQuery The current query, for fluid interface
  */
 public function prune($coupon = null)
 {
     if ($coupon) {
         $this->addUsingAlias(CouponTableMap::ID, $coupon->getId(), Criteria::NOT_EQUAL);
     }
     return $this;
 }
Пример #6
0
 /**
  * Call the Model and delegate the create or delete action
  * Feed the Event with the updated model
  *
  * @param CouponModel               $coupon Model to save
  * @param CouponCreateOrUpdateEvent $event  Event containing data
  */
 protected function createOrUpdateCondition(CouponModel $coupon, CouponCreateOrUpdateEvent $event)
 {
     $coupon->setDispatcher($event->getDispatcher());
     /** @var ConditionFactory $conditionFactory */
     $conditionFactory = $this->conditionFactory;
     $coupon->createOrUpdateConditions($conditionFactory->serializeConditionCollection($event->getConditions()), $event->getLocale());
     $event->setCouponModel($coupon);
 }
Пример #7
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;
 }
Пример #8
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());
 }
Пример #9
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;
 }
Пример #10
0
 /**
  * @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]]);
 }