Пример #1
0
 protected function updateCogs($params)
 {
     $cogs = Cogs::findOne(['product_id' => $params['product_id']]);
     if (!$cogs) {
         $cogs = new Cogs(['product_id' => $params['product_id'], 'cogs' => 0.0]);
     }
     $current_stock = MProductStock::find()->where(['product_id' => $params['product_id']])->sum('qty');
     $qty_per_uom = $params['qty_per_uom'];
     $added_stock = $params['qty'] * $qty_per_uom;
     if ($current_stock + $added_stock != 0) {
         $cogs->cogs = 1.0 * ($cogs->cogs * $current_stock + $params['price'] * $params['qty']) / ($current_stock + $added_stock);
     } else {
         $cogs->cogs = 0;
     }
     if ($cogs->canSetProperty('logParams')) {
         $cogs->logParams = ['app' => $params['app'], 'reff_id' => $params['reff_id']];
     }
     if (!$cogs->save()) {
         throw new UserException(implode(",\n", $cogs->firstErrors));
     }
     return true;
 }
Пример #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProductStocks()
 {
     return $this->hasMany(ProductStock::className(), ['product_id' => 'id']);
 }
Пример #3
0
 /**
  *
  * @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);
 }