Exemplo n.º 1
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;
 }
Exemplo n.º 2
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');
     }
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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;
 }