public static function create($params, $currentUser, $con) { // check role's permission $permission = RolePermissionQuery::create()->select('create_stock')->findOneById($currentUser->role_id, $con); if (!$permission || $permission != 1) { throw new \Exception('Akses ditolak. Anda tidak mempunyai izin untuk melakukan operasi ini.'); } // check whether chosen product is still Active $product = ProductQuery::create()->select('status')->findOneById($params->product_id, $con); if (!$product || $product != 'Active') { throw new \Exception('Produk tidak ditemukan. Mungkin Produk itu sudah dihapus.'); } // make sure there are no duplicate (same product and unit) variant in stock $stock = StockQuery::create()->filterByStatus('Active')->leftJoin('Product')->leftJoin('Unit')->filterByProductId($params->product_id)->filterByUnitId($params->unit_id)->withColumn('Product.Name', 'product_name')->withColumn('Unit.Name', 'unit_name')->select(array('product_name', 'unit_name'))->findOne($con); if ($stock) { throw new \Exception('Gagal menyimpan karena variant ' . $stock['product_name'] . ' <strong>' . $stock['unit_name'] . '</strong> sudah ada.'); } // create new stock $stock = new Stock(); $stock->setProductId($params->product_id)->setUnitId($params->unit_id)->setAmount(isset($params->amount) ? $params->amount : 0)->setBuy($params->buy)->setSellPublic($params->sell_public)->setSellDistributor($params->sell_distributor == 0 ? $params->sell_public : $params->sell_distributor)->setSellMisc($params->sell_misc == 0 ? $params->sell_public : $params->sell_misc)->setDiscount($params->discount)->setUnlimited(isset($params->unlimited) ? $params->unlimited : 0)->setStatus('Active')->save($con); // log history $rowHistory = new RowHistory(); $rowHistory->setRowId($stock->getId())->setData('stock')->setTime(time())->setOperation('create')->setUserId($currentUser->id)->save($con); $params->id = $stock->getId(); $stock = Stocks::seeker($params, $currentUser, $con); $results['success'] = true; $results['data'] = $stock['data']; return $results; }
/** * Filter the query by a related \ORM\Stock object * * @param \ORM\Stock|ObjectCollection $stock The related object(s) to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return ChildSalesDetailQuery The current query, for fluid interface */ public function filterByStock($stock, $comparison = null) { if ($stock instanceof \ORM\Stock) { return $this->addUsingAlias(SalesDetailTableMap::COL_STOCK_ID, $stock->getId(), $comparison); } elseif ($stock instanceof ObjectCollection) { if (null === $comparison) { $comparison = Criteria::IN; } return $this->addUsingAlias(SalesDetailTableMap::COL_STOCK_ID, $stock->toKeyValue('PrimaryKey', 'Id'), $comparison); } else { throw new PropelException('filterByStock() only accepts arguments of type \\ORM\\Stock or Collection'); } }
/** * Exclude object from result * * @param ChildStock $stock Object to remove from the list of results * * @return $this|ChildStockQuery The current query, for fluid interface */ public function prune($stock = null) { if ($stock) { $this->addUsingAlias(StockTableMap::COL_ID, $stock->getId(), Criteria::NOT_EQUAL); } return $this; }
/** * Declares an association between this object and a ChildStock object. * * @param ChildStock $v * @return $this|\ORM\SalesDetail The current object (for fluent API support) * @throws PropelException */ public function setStock(ChildStock $v = null) { if ($v === null) { $this->setStockId(NULL); } else { $this->setStockId($v->getId()); } $this->aStock = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the ChildStock object, it will not be re-added. if ($v !== null) { $v->addSales($this); } return $this; }