/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Customer is new, it will return * an empty collection; or if this Customer has previously * been saved, it will retrieve related CouponCustomerCounts from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Customer. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return Collection|ChildCouponCustomerCount[] List of ChildCouponCustomerCount objects */ public function getCouponCustomerCountsJoinCoupon($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildCouponCustomerCountQuery::create(null, $criteria); $query->joinWith('Coupon', $joinBehavior); return $this->getCouponCustomerCounts($query, $con); }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see CouponCustomerCount::setDeleted() * @see CouponCustomerCount::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(CouponCustomerCountTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildCouponCustomerCountQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
/** * 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; }
/** * 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; }
/** * Performs an INSERT on the database, given a CouponCustomerCount or Criteria object. * * @param mixed $criteria Criteria or CouponCustomerCount object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(CouponCustomerCountTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from CouponCustomerCount object } // Set the correct dbName $query = CouponCustomerCountQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }