/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = OrderItems::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'order_id' => $this->order_id, 'goods_id' => $this->goods_id, 'multiplicity' => $this->multiplicity, 'firm_id' => $this->firm_id]);
     return $dataProvider;
 }
 /**
  * Deletes an existing Orders model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     $orders_max_id = \backend\models\Orders::find()->max('id');
     $state_max_id = \backend\models\State::find()->max('id');
     $state_model_with_max_id = \backend\models\State::findOne($state_max_id);
     /*
      * if $state_model_with_max_id->output == 0 , 
      * then the last model in the state table is an order
      * otherwise it would have been a report
      */
     if ($id == $orders_max_id && $state_model_with_max_id->output == 0) {
         /*TRANSACTION*/
         $ordersItems = \backend\models\OrderItems::find()->where(['order_id' => $id])->all();
         //delete order items, then delete the order itself and then delete the last state model
         foreach ($ordersItems as $ordersItem) {
             $ordersItem->delete();
         }
         $this->findModel($id)->delete();
         $state_model_with_max_id->delete();
         /* TRANSACTION*/
         return $this->redirect(['index']);
     } else {
         throw new \yii\web\ForbiddenHttpException('You can NOT delete this order! ' . 'Please, contact the Admin for more information.');
     }
 }