Example #1
0
 /**
  * @param  string                             $id
  * @param  array                              $data
  * @param  \biz\core\inventory\models\StockOpname $model
  * @return \biz\core\inventory\models\StockOpname
  */
 public function append($id, $data, $model = null)
 {
     /* @var $model MStockOpname */
     $model = $model ?: $this->findModel($id);
     $success = true;
     $model->scenario = MStockOpname::SCENARIO_DEFAULT;
     $model->load($data, '');
     $this->fire('_append', [$model]);
     $success = $model->save();
     $stockOpnameDtls = ArrayHelper::index($model->stockOpnameDtls, 'product_id');
     foreach ($data['details'] as $dataDetail) {
         $index = $dataDetail['product_id'];
         // product_id
         if (isset($stockOpnameDtls[$index])) {
             $detail = $stockOpnameDtls[$index];
         } else {
             $detail = new StockOpnameDtl(['id_opname' => $model->id_opname, 'product_id' => $dataDetail['product_id'], 'uom_id' => $dataDetail['uom_id'], 'qty' => 0]);
         }
         $detail->qty += $dataDetail['qty'];
         $success = $success && $detail->save();
         $stockOpnameDtls[$index] = $detail;
         $this->fire('_append_body', [$model, $detail]);
     }
     $model->populateRelation('stockOpnameDtls', array_values($stockOpnameDtls));
     if ($success) {
         $this->fire('_appended', [$model]);
     }
     return $this->processOutput($success, $model);
 }
Example #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getStockOpnameDtls()
 {
     return $this->hasMany(StockOpnameDtl::className(), ['opname_id' => 'id']);
 }