public static function create($params, $currentUser, $con) { // check role's permission $permission = RolePermissionQuery::create()->select('create_sales')->findOneById($currentUser->role_id, $con); if (!$permission || $permission != 1) { throw new \Exception('Akses ditolak. Anda tidak mempunyai izin untuk melakukan operasi ini.'); } // create new sales $sales = new Sales(); $sales->setDate($params->date)->setSecondPartyId($params->second_party_id)->setBuyPrice($params->buy_price)->setTotalPrice($params->total_price)->setPaid($params->paid)->setCashierId($params->cashier_id)->setNote($params->note)->setStatus('Active')->save($con); // check wether this transaction is credit or not $balance = $params->paid - $params->total_price; if ($balance < 0) { $credit = new Credit(); $credit->setSalesId($sales->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 saled $salesDetail = new SalesDetail(); $salesDetail->setSalesId($sales->getId())->setType($product->type)->setStockId($product->stock_id)->setAmount($product->amount)->setUnitPrice($product->unit_price)->setDiscount($product->discount)->setTotalPrice($product->total_price)->setBuy($product->buy)->setSellPublic($product->sell_public)->setSellDistributor($product->sell_distributor)->setSellMisc($product->sell_misc)->setStatus('Active')->save($con); // substract stock $stock = StockQuery::create()->findOneById($product->stock_id, $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() - $product->amount)->save($con); } } $logData['params'] = $params; // log history $salesHistory = new SalesHistory(); $salesHistory->setUserId($currentUser->id)->setSalesId($sales->getId())->setTime(time())->setOperation('create')->setData(json_encode($logData))->save($con); $results['success'] = true; $results['id'] = $sales->getId(); return $results; }
/** * Filter the query by a related \ORM\Sales object * * @param \ORM\Sales|ObjectCollection $sales The related object(s) to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return ChildCreditQuery The current query, for fluid interface */ public function filterBySales($sales, $comparison = null) { if ($sales instanceof \ORM\Sales) { return $this->addUsingAlias(CreditTableMap::COL_SALES_ID, $sales->getId(), $comparison); } elseif ($sales instanceof ObjectCollection) { if (null === $comparison) { $comparison = Criteria::IN; } return $this->addUsingAlias(CreditTableMap::COL_SALES_ID, $sales->toKeyValue('PrimaryKey', 'Id'), $comparison); } else { throw new PropelException('filterBySales() only accepts arguments of type \\ORM\\Sales or Collection'); } }
/** * Declares an association between this object and a ChildSales object. * * @param ChildSales $v * @return $this|\ORM\SalesDetail The current object (for fluent API support) * @throws PropelException */ public function setSales(ChildSales $v = null) { if ($v === null) { $this->setSalesId(NULL); } else { $this->setSalesId($v->getId()); } $this->aSales = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the ChildSales object, it will not be re-added. if ($v !== null) { $v->addDetail($this); } return $this; }
/** * Exclude object from result * * @param ChildSales $sales Object to remove from the list of results * * @return $this|ChildSalesQuery The current query, for fluid interface */ public function prune($sales = null) { if ($sales) { $this->addUsingAlias(SalesTableMap::COL_ID, $sales->getId(), Criteria::NOT_EQUAL); } return $this; }