Ejemplo n.º 1
0
 protected function updateCogs($params)
 {
     $cogs = Cogs::findOne(['id_product' => $params['id_product']]);
     if (!$cogs) {
         $smallest_uom = Helper::getSmallestProductUom($params['id_product']);
         $cogs = new Cogs();
         $cogs->setAttributes(['id_product' => $params['id_product'], 'id_uom' => $smallest_uom, 'cogs' => 0.0]);
     }
     $current_stock = Helper::getCurrentStockAll($params['id_product']);
     $qty_per_uom = Helper::getQtyProductUom($params['id_product'], $params['id_uom']);
     $added_stock = $params['added_stock'] * $qty_per_uom;
     $cogs->cogs = 1.0 * ($cogs->cogs * $current_stock + $params['price'] * $params['added_stock']) / ($current_stock + $added_stock);
     if ($cogs->canSetProperty('logParams')) {
         $cogs->logParams = ['app' => $params['app'], 'id_ref' => $params['id_ref']];
     }
     if (!$cogs->save()) {
         throw new UserException(implode(",\n", $cogs->firstErrors));
     }
     return true;
 }
Ejemplo n.º 2
0
 public static function updateCogs($params, $logs = [])
 {
     $cogs = Cogs::findOne(['id_product' => $params['id_product']]);
     if (!$cogs) {
         $cogs = new Cogs();
         $cogs->setAttributes(['id_product' => $params['id_product'], 'id_uom' => $params['id_uom'], 'cogs' => 0.0]);
     }
     $cogs->cogs = 1.0 * ($cogs->cogs * $params['old_stock'] + $params['price'] * $params['added_stock']) / ($params['old_stock'] + $params['added_stock']);
     if (!empty($logs) && $cogs->canSetProperty('logParams')) {
         $cogs->logParams = $logs;
     }
     if (!$cogs->save()) {
         throw new UserException(implode(",\n", $cogs->firstErrors));
     }
     return true;
 }
Ejemplo n.º 3
0
 public function actionSavePos()
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $post = Yii::$app->request->post();
     try {
         $transaction = Yii::$app->db->beginTransaction();
         $drawer = Cashdrawer::findOne(['id_cashdrawer' => $post['id_drawer']]);
         $hdr = new SalesHdr(['id_cashdrawer' => $post['id_drawer'], 'id_branch' => $drawer->id_branch, 'create_by' => $drawer->id_user]);
         $total = 0.0;
         $dtls = [];
         foreach ($post['detail'] as $detail) {
             $cogs = Cogs::findOne(['id_product' => $detail['id_product']]);
             $dtl = new SalesDtl(['id_product' => $detail['id_product'], 'id_uom' => $detail['id_uom'], 'sales_price' => $detail['price'], 'sales_qty' => $detail['qty'], 'discount' => $detail['discon'], 'cogs' => $cogs ? $cogs->cogs : 0]);
             $total += $detail['qty'] * $detail['price'] * (1 - 0.01 * $detail['discon']);
             $dtls[] = $dtl;
         }
         $transaction->commit();
     } catch (\Exception $exc) {
         $transaction->rollback();
     }
     return ['type' => 'E'];
 }