/** * Filter the query by a related \ORM\Purchase object * * @param \ORM\Purchase|ObjectCollection $purchase The related object(s) to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return ChildDebitQuery The current query, for fluid interface */ public function filterByPurchase($purchase, $comparison = null) { if ($purchase instanceof \ORM\Purchase) { return $this->addUsingAlias(DebitTableMap::COL_PURCHASE_ID, $purchase->getId(), $comparison); } elseif ($purchase instanceof ObjectCollection) { if (null === $comparison) { $comparison = Criteria::IN; } return $this->addUsingAlias(DebitTableMap::COL_PURCHASE_ID, $purchase->toKeyValue('PrimaryKey', 'Id'), $comparison); } else { throw new PropelException('filterByPurchase() only accepts arguments of type \\ORM\\Purchase or Collection'); } }
/** * Declares an association between this object and a ChildPurchase object. * * @param ChildPurchase $v * @return $this|\ORM\Debit The current object (for fluent API support) * @throws PropelException */ public function setPurchase(ChildPurchase $v = null) { if ($v === null) { $this->setPurchaseId(NULL); } else { $this->setPurchaseId($v->getId()); } $this->aPurchase = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the ChildPurchase object, it will not be re-added. if ($v !== null) { $v->addDebit($this); } return $this; }
/** * Exclude object from result * * @param ChildPurchase $purchase Object to remove from the list of results * * @return $this|ChildPurchaseQuery The current query, for fluid interface */ public function prune($purchase = null) { if ($purchase) { $this->addUsingAlias(PurchaseTableMap::COL_ID, $purchase->getId(), Criteria::NOT_EQUAL); } return $this; }
public static function create($params, $currentUser, $con) { // check role's permission $permission = RolePermissionQuery::create()->select('create_purchase')->findOneById($currentUser->role_id, $con); if (!$permission || $permission != 1) { throw new \Exception('Akses ditolak. Anda tidak mempunyai izin untuk melakukan operasi ini.'); } // create new purchase $purchase = new Purchase(); $purchase->setDate($params->date)->setSecondPartyId($params->second_party_id)->setTotalPrice($params->total_price)->setPaid($params->paid)->setNote($params->note)->setStatus('Active')->save($con); // check wether this transaction is debit or not, then if yes, create new debit record $balance = $params->paid - $params->total_price; if ($balance < 0) { $debit = new Debit(); $debit->setPurchaseId($purchase->getId())->setTotal(abs($balance))->setStatus('Active')->save($con); } $products = json_decode($params->products); foreach ($products as $product) { // create new record representing product stock which is being purchased $purchaseDetail = new PurchaseDetail(); $purchaseDetail->setPurchaseId($purchase->getId())->setStockId($product->stock_id)->setAmount($product->amount)->setTotalPrice($product->total_price)->setStatus('Active')->save($con); // add stock amount $stock = StockQuery::create()->findOneById($product->stock_id, $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() + $product->amount)->save($con); } $notification = Purchases::newPriceNotification($stock, $purchaseDetail, $con); $purchaseDetail->setNotificationId($notification)->save($con); } $logData['params'] = $params; // log history $purchaseHistory = new PurchaseHistory(); $purchaseHistory->setUserId($currentUser->id)->setPurchaseId($purchase->getId())->setTime(time())->setOperation('create')->setData(json_encode($logData))->save($con); $results['success'] = true; $results['id'] = $purchase->getId(); return $results; }