/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = StockOpnameDtlModel::find()->with('products'); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { $query->where('1=0'); return $dataProvider; } $query->andFilterWhere(['id_opname' => $this->id_opname, 'id_product' => $this->id_product, 'id_uom' => $this->id_uom, 'qty' => $this->qty]); return $dataProvider; }
public static function append($id, $data, $model = null) { /* @var $model MStockOpname */ $model = $model ?: static::findModel($id); $e_name = static::prefixEventName(); $success = true; $model->scenario = MStockOpname::SCENARIO_DEFAULT; $model->load($data, ''); try { $transaction = Yii::$app->db->beginTransaction(); Yii::$app->trigger($e_name . '_append', new Event([$model])); $stockOpnameDtls = ArrayHelper::index($model->stockOpnameDtls, 'id_product'); foreach ($data['details'] as $dataDetail) { $index = $dataDetail['id_product']; // id_product if (isset($stockOpnameDtls[$index])) { $detail = $stockOpnameDtls[$index]; } else { $detail = new StockOpnameDtl(['id_opname' => $model->id_opname, 'id_product' => $dataDetail['id_product'], 'id_uom' => $dataDetail['id_uom'], 'qty' => 0]); } $detail->qty += $dataDetail['qty']; $success = $success && $detail->save(); $stockOpnameDtls[$index] = $detail; Yii::$app->trigger($e_name . '_append_body', new Event([$model, $detail])); } if ($success) { Yii::$app->trigger($e_name . '_appended', new Event([$model])); $transaction->commit(); } else { $transaction->rollBack(); } } catch (\Exception $exc) { $transaction->rollBack(); throw $exc; } return [$success, $model]; }
/** * @return \yii\db\ActiveQuery */ public function getStockOpnameDtls() { return $this->hasMany(StockOpnameDtl::className(), ['id_opname' => 'id_opname']); }