Example #1
0
 /**
  * @param    Coupon $coupon The coupon object to add.
  */
 protected function doAddCoupon($coupon)
 {
     $couponCustomerCount = new ChildCouponCustomerCount();
     $couponCustomerCount->setCoupon($coupon);
     $this->addCouponCustomerCount($couponCustomerCount);
     // set the back reference to this object directly as using provided method either results
     // in endless loop or in multiple relations
     if (!$coupon->getCustomers()->contains($this)) {
         $foreignCollection = $coupon->getCustomers();
         $foreignCollection[] = $this;
     }
 }
Example #2
0
 /**
  * Filter the query by a related \Thelia\Model\CouponCustomerCount object
  *
  * @param \Thelia\Model\CouponCustomerCount|ObjectCollection $couponCustomerCount  the related object to use as filter
  * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return ChildCouponQuery The current query, for fluid interface
  */
 public function filterByCouponCustomerCount($couponCustomerCount, $comparison = null)
 {
     if ($couponCustomerCount instanceof \Thelia\Model\CouponCustomerCount) {
         return $this->addUsingAlias(CouponTableMap::ID, $couponCustomerCount->getCouponId(), $comparison);
     } elseif ($couponCustomerCount instanceof ObjectCollection) {
         return $this->useCouponCustomerCountQuery()->filterByPrimaryKeys($couponCustomerCount->getPrimaryKeys())->endUse();
     } else {
         throw new PropelException('filterByCouponCustomerCount() only accepts arguments of type \\Thelia\\Model\\CouponCustomerCount or Collection');
     }
 }
 /**
  * Exclude object from result
  *
  * @param   ChildCouponCustomerCount $couponCustomerCount Object to remove from the list of results
  *
  * @return ChildCouponCustomerCountQuery The current query, for fluid interface
  */
 public function prune($couponCustomerCount = null)
 {
     if ($couponCustomerCount) {
         $this->addCond('pruneCond0', $this->getAliasedColName(CouponCustomerCountTableMap::COUPON_ID), $couponCustomerCount->getCouponId(), Criteria::NOT_EQUAL);
         $this->addCond('pruneCond1', $this->getAliasedColName(CouponCustomerCountTableMap::CUSTOMER_ID), $couponCustomerCount->getCustomerId(), Criteria::NOT_EQUAL);
         $this->combine(array('pruneCond0', 'pruneCond1'), Criteria::LOGICAL_OR);
     }
     return $this;
 }
Example #4
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()) {
         return true;
     } else {
         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();
                     return $usageLeft - $newCount;
                 } else {
                     $coupon->setMaxUsage(--$usageLeft);
                     $coupon->save();
                     return $usageLeft;
                 }
             }
         } catch (\Exception $ex) {
             // Just log the problem.
             Tlog::getInstance()->addError(sprintf("Failed to decrement coupon %s: %s", $coupon->getCode(), $ex->getMessage()));
         }
     }
     return false;
 }
 /**
  * Adds an object to the instance pool.
  *
  * Propel keeps cached copies of objects in an instance pool when they are retrieved
  * from the database. In some cases you may need to explicitly add objects
  * to the cache in order to ensure that the same objects are always returned by find*()
  * and findPk*() calls.
  *
  * @param \Thelia\Model\CouponCustomerCount $obj A \Thelia\Model\CouponCustomerCount object.
  * @param string $key             (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool($obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if (null === $key) {
             $key = serialize(array((string) $obj->getCouponId(), (string) $obj->getCustomerId()));
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }