/** * * @param array $params * Required field id_warehouse, id_product, qty * Optional field app, id_ref, id_uom, item_value * @return boolean * @throws UserException */ public function updateStock($params) { $stock = ProductStock::findOne(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product']]); if (isset($params['id_uom'])) { $qty_per_uom = MasterHelper::getQtyProductUom($params['id_product'], $params['id_uom']); } else { $qty_per_uom = 1; } if (!$stock) { $stock = new ProductStock(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product'], 'qty_stock' => 0]); } // update cogs if (isset($params['price'])) { $this->updateCogs($params); } $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; }
public static function updateStock($params, $logs = []) { $stock = ProductStock::findOne(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product']]); if (!$stock) { $stock = new ProductStock(); $stock->setAttributes(['id_warehouse' => $params['id_warehouse'], 'id_product' => $params['id_product'], 'id_uom' => $params['id_uom'], 'qty_stock' => 0]); } $stock->qty_stock = $stock->qty_stock + $params['qty']; if (!empty($logs) && $stock->canSetProperty('logParams')) { $stock->logParams = $logs; } if (!$stock->save()) { throw new UserException(implode(",\n", $stock->firstErrors)); } return true; }
public function search($params) { $query = ProductStockModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id_warehouse' => $this->id_warehouse, 'id_product' => $this->id_product, 'id_uom' => $this->id_uom, 'create_by' => $this->create_by, 'update_by' => $this->update_by]); $query->andFilterWhere(['like', 'qty_stock', $this->qty_stock])->andFilterWhere(['like', 'create_at', $this->create_at])->andFilterWhere(['like', 'update_at', $this->update_at]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getProductStocks() { return $this->hasMany(ProductStock::className(), ['id_uom' => 'id_uom']); }
/** * Finds the ProductStock model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ProductStock the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ProductStock::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * * @param StockOpname $opname * @param MStockAdjustment $model * @return mixed * @throws \Exception */ public static function createFromOpname($opname, $model = null) { // info product $currentStocks = ProductStock::find()->select(['id_product', 'qty_stock'])->where(['id_warehouse' => $opname->id_warehouse])->indexBy('id_product')->asArray()->all(); $isiProductUoms = []; foreach (ProductUom::find()->asArray()->all() as $row) { $isiProductUoms[$row['id_product']][$row['id_uom']] = $row['isi']; } // *** $data = ['id_warehouse' => $opname->id_warehouse, 'adjustment_date' => date('Y-m-d'), 'id_reff' => $opname->id_opname, 'description' => "Stock adjustment from stock opname no \"{$opname->opname_num}\"."]; $details = []; foreach ($opname->stockOpnameDtls as $detail) { $cQty = $currentStocks[$detail->id_product] / $isiProductUoms[$detail->id_product][$detail->id_uom]; $details[] = ['id_product' => $detail->id_product, 'id_uom' => $detail->id_uom, 'qty' => $detail->qty - $cQty]; } $data['details'] = $details; return static::create($data, $model); }