Beispiel #1
0
 private static function getPurchasedProduct($date, $con)
 {
     $purchasedProducts = PurchaseDetailQuery::create()->filterByStatus('Active')->useStockQuery()->leftJoin('Product')->leftJoin('Unit')->withColumn('Product.Name', 'product_name')->withColumn('Unit.Name', 'unit_name')->endUse()->usePurchaseQuery()->filterByStatus('Active')->filterByDate(array('min' => $date->start, 'max' => $date->until))->endUse()->withColumn('SUM(PurchaseDetail.Amount)', 'purchased_amount')->withColumn('SUM(PurchaseDetail.TotalPrice)', 'purchased_total')->select(array('stock_id', 'product_name', 'unit_name', 'purchased_amount', 'purchased_total'))->groupBy('PurchaseDetail.StockId')->orderBy('purchased_amount', 'DESC')->find($con);
     $data = [];
     foreach ($purchasedProducts as $purchasedProduct) {
         $data[] = $purchasedProduct;
     }
     $results['success'] = true;
     $results['data'] = $data;
     return $results;
 }
Beispiel #2
0
 /**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this Stock is new, it will return
  * an empty collection; or if this Stock has previously
  * been saved, it will retrieve related Purchases 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 Stock.
  *
  * @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|ChildPurchaseDetail[] List of ChildPurchaseDetail objects
  */
 public function getPurchasesJoinNotification(Criteria $criteria = null, ConnectionInterface $con = null, $joinBehavior = Criteria::LEFT_JOIN)
 {
     $query = ChildPurchaseDetailQuery::create(null, $criteria);
     $query->joinWith('Notification', $joinBehavior);
     return $this->getPurchases($query, $con);
 }
Beispiel #3
0
 /**
  * Returns a new ChildPurchaseDetailQuery object.
  *
  * @param     string $modelAlias The alias of a model in the query
  * @param     Criteria $criteria Optional Criteria to build the query from
  *
  * @return ChildPurchaseDetailQuery
  */
 public static function create($modelAlias = null, Criteria $criteria = null)
 {
     if ($criteria instanceof ChildPurchaseDetailQuery) {
         return $criteria;
     }
     $query = new ChildPurchaseDetailQuery();
     if (null !== $modelAlias) {
         $query->setModelAlias($modelAlias);
     }
     if ($criteria instanceof Criteria) {
         $query->mergeWith($criteria);
     }
     return $query;
 }
Beispiel #4
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      ConnectionInterface $con
  * @return void
  * @throws PropelException
  * @see PurchaseDetail::setDeleted()
  * @see PurchaseDetail::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(PurchaseDetailTableMap::DATABASE_NAME);
     }
     $con->transaction(function () use($con) {
         $deleteQuery = ChildPurchaseDetailQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             $this->setDeleted(true);
         }
     });
 }
 /**
  * Performs an INSERT on the database, given a PurchaseDetail or Criteria object.
  *
  * @param mixed               $criteria Criteria or PurchaseDetail 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(PurchaseDetailTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from PurchaseDetail object
     }
     if ($criteria->containsKey(PurchaseDetailTableMap::COL_ID) && $criteria->keyContainsValue(PurchaseDetailTableMap::COL_ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . PurchaseDetailTableMap::COL_ID . ')');
     }
     // Set the correct dbName
     $query = PurchaseDetailQuery::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);
     });
 }
Beispiel #6
0
 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;
 }