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