/** * * @param array $params Required field warehouse_id, product_id, qty * Optional field app, reff_id, uom_id, item_value * @return boolean * @throws UserException */ public function updateStock($params) { $stock = MProductStock::findOne(['warehouse_id' => $params['warehouse_id'], 'product_id' => $params['product_id']]); if (isset($params['uom_id'])) { $qty_per_uom = ProductUom::find()->select('isi')->where(['product_id' => $params['product_id'], 'uom_id' => $params['uom_id']])->scalar(); if ($qty_per_uom === false) { throw new NotFoundException("Uom '{$params['uom_id']}' not found for product '{$params['product_id']}'"); } } else { $qty_per_uom = 1; } if (!$stock) { $stock = new MProductStock(['warehouse_id' => $params['warehouse_id'], 'product_id' => $params['product_id'], 'qty' => 0]); } // update cogs if (isset($params['price']) && $params['price'] !== '') { $params['qty_per_uom'] = $qty_per_uom; $this->updateCogs($params); } $stock->qty = $stock->qty + $params['qty'] * $qty_per_uom; if ($stock->canSetProperty('logParams')) { $logParams = ['mv_qty' => $params['qty'] * $qty_per_uom]; foreach (['app', 'reff_id'] 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; }
/** * @return \yii\db\ActiveQuery */ public function getProductUoms() { return $this->hasMany(ProductUom::className(), ['product_id' => 'id']); }
/** * * @param StockOpname $opname * @param MStockAdjustment $model * @return mixed * @throws \Exception */ public function createFromOpname($opname, $model = null) { // info product $currentStocks = ProductStock::find()->select(['product_id', 'qty_stock'])->where(['warehouse_id' => $opname->warehouse_id])->indexBy('product_id')->asArray()->all(); $isiProductUoms = []; foreach (ProductUom::find()->asArray()->all() as $row) { $isiProductUoms[$row['product_id']][$row['uom_id']] = $row['isi']; } // *** $data = ['warehouse_id' => $opname->warehouse_id, 'adjustment_date' => date('Y-m-d'), 'reff_id' => $opname->id, 'description' => "Stock adjustment from stock opname no \"{$opname->opname_num}\"."]; $details = []; foreach ($opname->stockOpnameDtls as $detail) { $cQty = $currentStocks[$detail->product_id] / $isiProductUoms[$detail->product_id][$detail->uom_id]; $details[] = ['product_id' => $detail->product_id, 'uom_id' => $detail->uom_id, 'qty' => $detail->qty - $cQty]; } $data['details'] = $details; return $this->create($data, $model); }