public static function read($params, $currentUser, $con) { $page = isset($params->page) ? $params->page : 0; $limit = isset($params->limit) ? $params->limit : 100; $notifications = NotificationOnUserQuery::create()->filterByUserId($currentUser->id)->filterByStatus('Unread')->leftJoin('Notification')->withColumn('Notification.Time', 'time')->withColumn('Notification.Type', 'type')->withColumn('Notification.Data', 'data')->where('Notification.Status = ?', 'Active')->select(array('id'))->orderBy('time', 'DESC'); $notifications = $notifications->paginate($page, $limit); $total = $notifications->getNbResults(); $data = []; foreach ($notifications as $notification) { $notification = (object) $notification; switch ($notification->type) { case 'price': $notification->data = json_decode($notification->data); $stock = StockQuery::create()->findOneById($notification->data->stock_id, $con); if ($stock) { $notification->data->product_id = $stock->getProduct()->getId(); $notification->data->product_name = $stock->getProduct()->getName(); $notification->data->unit_name = $stock->getUnit()->getName(); } else { $notification->delete($con); } } $data[] = $notification; } $results['success'] = true; $results['data'] = $data; $results['total'] = $total; return $results; }
public static function stock($params, $currentUser, $con) { $stocks = StockQuery::create()->filterByStatus('Active')->leftJoin('Product')->leftJoin('Unit'); if (isset($params->product_id)) { $stocks->filterByProductId($params->product_id); } // if limit is not specified then make it 0 $limit = isset($params->limit) ? $params->limit : 0; $stocks = $stocks->select(array('unit_id', 'amount', 'buy', 'sell_public', 'sell_distributor', 'sell_misc', 'discount', 'unlimited'))->withColumn('Stock.Id', 'stock_id')->withColumn('Product.Code', 'product_code')->withColumn('Product.Name', 'product_name')->withColumn('Unit.Name', 'unit_name')->orderBy('product_name', 'ASC')->limit($limit)->find($con); $data = []; foreach ($stocks as $stock) { $data[] = $stock; } $results['success'] = true; $results['data'] = $data; return $results; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Unit is new, it will return * an empty collection; or if this Unit has previously * been saved, it will retrieve related Stocks 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 Unit. * * @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 ObjectCollection|ChildStock[] List of ChildStock objects */ public function getStocksJoinProduct(Criteria $criteria = null, ConnectionInterface $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildStockQuery::create(null, $criteria); $query->joinWith('Product', $joinBehavior); return $this->getStocks($query, $con); }
public static function stock($params, $currentUser, $con) { $stocks = StockQuery::create()->filterByStatus('Active')->useProductQuery()->filterByStatus('Active')->endUse()->leftJoin('Unit'); if (isset($params->query)) { $stocks->condition('cond1', 'Product.Name like ?', "%{$params->query}%"); $stocks->condition('cond2', 'Product.Code like ?', "%{$params->query}%"); $stocks->where(array('cond1', 'cond2'), 'or'); } $stocks = $stocks->select(array('unit_id', 'amount', 'buy', 'sell_public', 'sell_distributor', 'sell_misc', 'discount'))->withColumn('Stock.Id', 'stock_id')->withColumn('Product.Code', 'product_code')->withColumn('Product.Name', 'product_name')->withColumn('Unit.Name', 'unit_name')->orderBy('product_name', 'ASC')->limit(20)->find($con); $data = []; foreach ($stocks as $stock) { $data[] = $stock; } $results['success'] = true; $results['data'] = $data; return $results; }
public static function update($params, $currentUser, $con) { // check role's permission $permission = RolePermissionQuery::create()->select('update_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.'); } $stock = StockQuery::create()->findOneById($params->id, $con); if (!$stock) { throw new \Exception('Data tidak ditemukan'); } $stock->setProductId($params->product_id)->setAmount($params->amount)->setUnitId($params->unit_id)->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)->save($con); $rowHistory = new RowHistory(); $rowHistory->setRowId($params->id)->setData('stock')->setTime(time())->setOperation('update')->setUserId($currentUser->id)->save($con); $results['success'] = true; $results['id'] = $params->id; return $results; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see Stock::setDeleted() * @see Stock::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(StockTableMap::DATABASE_NAME); } $con->transaction(function () use($con) { $deleteQuery = ChildStockQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $this->setDeleted(true); } }); }
public static function update($params, $currentUser, $con) { // check role's permission $permission = RolePermissionQuery::create()->select('update_sales')->findOneById($currentUser->role_id, $con); if (!$permission || $permission != 1) { throw new \Exception('Akses ditolak. Anda tidak mempunyai izin untuk melakukan operasi ini.'); } $sales = SalesQuery::create()->findOneById($params->id, $con); if (!$sales) { throw new \Exception('Data tidak ditemukan'); } $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 = CreditQuery::create()->filterBySalesId($sales->getId())->findOne($con); if (!$credit) { $credit = new Credit(); } $credit->setSalesId($sales->getId())->setTotal(abs($balance))->setStatus('Active')->save($con); } else { $credit = CreditQuery::create()->filterBySalesId($sales->getId())->findOne($con); if ($credit) { $credit->setSalesId($sales->getId())->setTotal(0)->setStatus('Canceled')->save($con); } } $products = json_decode($params->products); // iterate through every product on this sales operation foreach ($products as $product) { $newDetail = SalesDetailQuery::create()->findOneById($product->id); // check whether current detail iteration is brand new or just updating the old one if (!$newDetail) { $isNew = true; $newDetail = new SalesDetail(); } else { $isNew = false; $oldDetail = $newDetail->copy(); } $newDetail->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); // make stock dance ^_^ if ($isNew) { $stock = StockQuery::create()->findOneById($newDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() - $newDetail->getAmount())->save($con); } } else { // check whether updated detail stock is the same old one or not if ($newDetail->getStockId() == $oldDetail->getStockId()) { // and if actually the same, then set stock amount like this // amount = currentAmount + oldTransAmount - newTransAmount $stock = StockQuery::create()->findOneById($newDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() + $oldDetail->getAmount() - $newDetail->getAmount())->save($con); } } else { // but if two stocks is not the same, // then give back oldTransAmount to old-stock, and take newTransAmount from new-stock $stock = StockQuery::create()->findOneById($oldDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() + $oldDetail->getAmount())->save($con); } $stock = StockQuery::create()->findOneById($newDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() - $newDetail->getAmount())->save($con); } } } } // if there are any sales detail removed then make sure the stocks get what it deserve... 'gimme amount' $removeds = SalesDetailQuery::create()->filterById($params->removed_id)->find($con); foreach ($removeds as $removed) { $stock = StockQuery::create()->findOneById($removed->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() + $removed->getAmount())->save($con); } $removed->setStatus('Deleted')->save($con); } $logData['params'] = $params; // log history $salesHistory = new SalesHistory(); $salesHistory->setUserId($currentUser->id)->setSalesId($params->id)->setTime(time())->setOperation('update')->setData(json_encode($logData))->save($con); $results['success'] = true; $results['id'] = $params->id; return $results; }
/** * Returns a new ChildStockQuery object. * * @param string $modelAlias The alias of a model in the query * @param Criteria $criteria Optional Criteria to build the query from * * @return ChildStockQuery */ public static function create($modelAlias = null, Criteria $criteria = null) { if ($criteria instanceof ChildStockQuery) { return $criteria; } $query = new ChildStockQuery(); if (null !== $modelAlias) { $query->setModelAlias($modelAlias); } if ($criteria instanceof Criteria) { $query->mergeWith($criteria); } return $query; }
/** * Get the associated ChildStock object * * @param ConnectionInterface $con Optional Connection object. * @return ChildStock The associated ChildStock object. * @throws PropelException */ public function getStock(ConnectionInterface $con = null) { if ($this->aStock === null && ($this->stock_id !== "" && $this->stock_id !== null)) { $this->aStock = ChildStockQuery::create()->findPk($this->stock_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->aStock->addSaless($this); */ } return $this->aStock; }
/** * Performs an INSERT on the database, given a Stock or Criteria object. * * @param mixed $criteria Criteria or Stock 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(StockTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from Stock object } if ($criteria->containsKey(StockTableMap::COL_ID) && $criteria->keyContainsValue(StockTableMap::COL_ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . StockTableMap::COL_ID . ')'); } // Set the correct dbName $query = StockQuery::create()->mergeWith($criteria); // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) return $con->transaction(function () use($con, $query) { return $query->doInsert($con); }); }
public static function update($params, $currentUser, $con) { // check role's permission $permission = RolePermissionQuery::create()->select('update_purchase')->findOneById($currentUser->role_id, $con); if (!$permission || $permission != 1) { throw new \Exception('Akses ditolak. Anda tidak mempunyai izin untuk melakukan operasi ini.'); } $purchase = PurchaseQuery::create()->findOneById($params->id, $con); if (!$purchase) { throw new \Exception('Data tidak ditemukan'); } $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 = DebitQuery::create()->filterByPurchaseId($purchase->getId())->findOne($con); if (!$debit) { $debit = new Debit(); } $debit->setPurchaseId($purchase->getId())->setTotal(abs($balance))->setStatus('Active')->save($con); } else { $debit = DebitQuery::create()->filterByPurchaseId($purchase->getId())->findOne($con); if ($debit) { $debit->setPurchaseId($purchase->getId())->setTotal(0)->setStatus('Canceled')->save($con); } } $products = json_decode($params->products); foreach ($products as $product) { // check whether current detail iteration is brand new or just updating the old one $newDetail = PurchaseDetailQuery::create()->findOneById($product->id); if (!$newDetail) { $isNew = true; $newDetail = new PurchaseDetail(); } else { $isNew = false; $oldDetail = $newDetail->copy(); } $newDetail->setPurchaseId($purchase->getId())->setStockId($product->stock_id)->setAmount($product->amount)->setTotalPrice($product->total_price)->setStatus('Active')->save($con); // make stock dance ^_^ if ($isNew) { $stock = StockQuery::create()->findOneById($newDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() + $product->amount)->save($con); } } else { // check whether updated detail stock is the same old one or not if ($newDetail->getStockId() == $oldDetail->getStockId()) { // and if actually the same, then set stock amount like this // amount = currentAmount - oldTransAmount + newTransAmount $stock = StockQuery::create()->findOneById($newDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() - $oldDetail->getAmount() + $newDetail->getAmount())->save($con); } } else { // but if two stocks is not the same, // then take back oldTransAmount from old-stock, and give newTransAmount to new-stock $stock = StockQuery::create()->findOneById($oldDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() - $oldDetail->getAmount())->save($con); } $stock = StockQuery::create()->findOneById($newDetail->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() + $newDetail->getAmount())->save($con); } } } $notificationId = Purchases::newPriceNotification($stock, $newDetail, $con); if ($isNew || !($notificationId == 0)) { $newDetail->setNotificationId($notificationId)->save($con); } } // if there are any sales detail removed then make sure the stocks give back something they own... 'give back amount' $removeds = PurchaseDetailQuery::create()->filterById($params->removed_id)->find($con); foreach ($removeds as $removed) { $stock = StockQuery::create()->findOneById($removed->getStockId(), $con); if ($stock->getUnlimited() == false) { $stock->setAmount($stock->getAmount() - $removed->getAmount())->save($con); } $removed->setStatus('Deleted')->save($con); $notification = $removed->getNotification(); if ($notification) { $notification->delete($con); } } $logData['params'] = $params; // log history $purchaseHistory = new PurchaseHistory(); $purchaseHistory->setUserId($currentUser->id)->setPurchaseId($params->id)->setTime(time())->setOperation('update')->setData(json_encode($logData))->save($con); $results['success'] = true; $results['id'] = $params->id; return $results; }