Beispiel #1
0
 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;
 }