public function buildModelCriteria() { $customer = $this->getCustomer(); $search = CreditAccountQuery::create(); if ($customer !== null) { $search->filterByCustomerId($customer); } return $search; }
public function addAmount(CreditAccountEvent $event) { $customer = $event->getCustomer(); $creditAccount = CreditAccountQuery::create()->filterByCustomerId($customer->getId())->findOne(); if (null === $creditAccount) { $creditAccount = (new CreditAccountModel())->setCustomerId($customer->getId()); } $creditAccount->addAmount($event->getAmount(), $event->getOrderId(), $event->getWhoDidIt())->save(); $event->setCreditAccount($creditAccount); }
public function useAmount() { $this->checkAuth(); $this->checkCartNotEmpty(); $customer = $this->getSecurityContext()->getCustomerUser(); $creditAccount = CreditAccountQuery::create()->findOneByCustomerId($customer->getId()); if ($creditAccount->getAmount() > 0) { $cart = $this->getSession()->getSessionCart($this->getDispatcher()); $order = $this->getSession()->getOrder(); $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); $total = $cart->getTaxedAmount($taxCountry); $totalDiscount = $creditAccount->getAmount(); if ($totalDiscount > $total) { $totalDiscount = $total; } $order->setDiscount($totalDiscount); $cart->setDiscount($cart->getDiscount() + $totalDiscount)->save(); $this->getSession()->set('creditAccount.used', 1); $this->getSession()->set('creditAccount.amount', $totalDiscount); } return $this->generateRedirectFromRoute('order.invoice'); }
/** * Get the associated ChildCreditAccount object * * @param ConnectionInterface $con Optional Connection object. * @return ChildCreditAccount The associated ChildCreditAccount object. * @throws PropelException */ public function getCreditAccount(ConnectionInterface $con = null) { if ($this->aCreditAccount === null && $this->credit_account_id !== null) { $this->aCreditAccount = ChildCreditAccountQuery::create()->findPk($this->credit_account_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aCreditAccount->addCreditAmountHistories($this); */ } return $this->aCreditAccount; }
/** * Performs an INSERT on the database, given a CreditAccount or Criteria object. * * @param mixed $criteria Criteria or CreditAccount 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(CreditAccountTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from CreditAccount object } if ($criteria->containsKey(CreditAccountTableMap::ID) && $criteria->keyContainsValue(CreditAccountTableMap::ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . CreditAccountTableMap::ID . ')'); } // Set the correct dbName $query = CreditAccountQuery::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; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see CreditAccount::setDeleted() * @see CreditAccount::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(CreditAccountTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildCreditAccountQuery::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; } }