/** * * @param \biz\app\base\Event $event */ public function purchaseReceiveBody($event) { /* @var $detail \biz\master\models\PurchaseDtl */ $detail = $event->params[1]; $smallest_uom = Helper::getSmallestProductUom($detail->id_product); $this->updatePrice(['id_product' => $detail->id_product, 'id_uom' => $smallest_uom, 'price' => $detail->sales_price, 'app' => 'purchase', 'id_ref' => $detail->id_purchase_dtl]); }
protected function updateCogs($params) { $cogs = Cogs::findOne(['id_product' => $params['id_product']]); if (!$cogs) { $smallest_uom = MasterHelper::getSmallestProductUom($params['id_product']); $cogs = new Cogs(['id_product' => $params['id_product'], 'id_uom' => $smallest_uom, 'cogs' => 0.0]); } $current_stock = MasterHelper::getCurrentStockAll($params['id_product']); $qty_per_uom = MasterHelper::getQtyProductUom($params['id_product'], $params['id_uom']); $added_stock = $params['added_stock'] * $qty_per_uom; if ($current_stock + $added_stock != 0) { $cogs->cogs = 1.0 * ($cogs->cogs * $current_stock + $params['price'] * $params['added_stock']) / ($current_stock + $added_stock); } else { $cogs->cogs = 0; } 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; }
/** * * @param array $params * Required field id_warehouse, id_product, id_uom, qty * Optional field app, id_ref * @return boolean * @throws UserException */ public function updateStock($params) { $stock = ProductStock::findOne(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product']]); $qty_per_uom = Helper::getQtyProductUom($params['id_product'], $params['id_uom']); if (!$stock) { $smallest_uom = Helper::getSmallestProductUom($params['id_product']); $stock = new ProductStock(); $stock->setAttributes(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product'], 'id_uom' => $smallest_uom, 'qty_stock' => 0]); } $stock->qty_stock = $stock->qty_stock + $params['qty'] * $qty_per_uom; if ($stock->canSetProperty('logParams')) { $logParams = ['mv_qty' => $params['qty'] * $qty_per_uom]; foreach (['app', 'id_ref'] as $key) { if (isset($params[$key]) || array_key_exists($key, $params)) { $logParams[$key] = $params[$key]; } } $stock->logParams = $logParams; } if (!$stock->save()) { throw new UserException(implode(",\n", $stock->firstErrors)); } return true; }