/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = GoodsMovementModel::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         $query->where('1=0');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'warehouse_id' => $this->warehouse_id, 'date' => $this->date, 'type' => $this->type, 'reff_type' => $this->reff_type, 'reff_id' => $this->reff_id, 'vendor_id' => $this->vendor_id, 'status' => $this->status, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]);
     $query->andFilterWhere(['like', 'number', $this->number])->andFilterWhere(['like', 'description', $this->description]);
     return $dataProvider;
 }
 /**
  * Creates a new Invoice model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Invoice();
     $dgets = Yii::$app->request->get();
     $model->load($dgets);
     $model->reff_type = $model->type == $model::TYPE_INCOMING ? $model::REFF_PURCH : null;
     $model->reff_type = $model->type == $model::TYPE_OUTGOING ? $model::REFF_SALES : $model->reff_type;
     $model->status = Invoice::STATUS_DRAFT;
     $model->date = date('Y-m-d');
     $model->due_date = date('Y-m-d', time() + 30 * 24 * 3600);
     $model->value = 0;
     if (isset($dgets['goodsMovement']['id'])) {
         $gmv = \backend\models\inventory\GoodsMovement::find()->where(['=', 'id', $dgets['goodsMovement']['id']])->with(['vendor'])->one();
         $model->reff_type = $model::REFF_GOODS_MOVEMENT;
         $model->reff_id = $gmv->id;
         $model->vendor_id = $gmv->vendor_id;
         $model->vendor_name = $gmv->vendor->name;
         $model->description = 'GR Invoice';
         $gmItems = [];
         $subtotal = 0;
         foreach ($gmv->items as $rvalue) {
             $ditem = new \backend\models\accounting\InvoiceDtl();
             $ditem->item_id = $rvalue->product_id;
             $ditem->qty = $rvalue->qty;
             $ditem->item_value = $rvalue->cogs;
             $subtotal += $rvalue->qty * $rvalue->cogs;
             $gmItems[] = $ditem;
         }
         $model->value = $subtotal;
         $model->items = $gmItems;
     }
     if ($model->load(Yii::$app->request->post())) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $model->items = Yii::$app->request->post('InvoiceDtl', []);
             if ($model->save()) {
                 $transaction->commit();
                 return $this->redirect(['view', 'id' => $model->id]);
             }
         } catch (\Exception $exc) {
             $transaction->rollBack();
             throw $exc;
         }
         $transaction->rollBack();
     }
     return $this->render('create', ['model' => $model]);
 }