/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Delivery::find();
     $subquery = DeliveryDetail::find()->select('delivery_id, SUM(count) as detail_count')->groupBy('delivery_id');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['time' => SORT_DESC]]]);
     /*
      *set the count search
      */
     $dataProvider->sort->attributes['detailCount'] = ['asc' => ['detailSum.detail_count' => SORT_ASC], 'desc' => ['detailSum.detail_count' => SORT_DESC], 'label' => Yii::t('app', 'Count')];
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     if (isset($this->time) && $this->time != '') {
         $date_explode = explode(" to ", $this->time);
         $date1 = trim($date_explode[0]);
         $date2 = trim($date_explode[1]);
         $date2 = date("Y-m-d", strtotime("+1 day", strtotime($date2)));
         $query->andFilterWhere(['between', 'delivery.time', $date1, $date2]);
     }
     $query->joinwith('customer');
     $query->andFilterWhere(['id' => $this->id, 'detailSum.detail_count' => $this->detailCount]);
     $query->andFilterWhere(['like', 'state', $this->state]);
     $query->andFilterWhere(['like', 'money', $this->money]);
     $query->andFilterWhere(['like', 'profit', $this->profit]);
     $query->andFilterWhere(['like', 'customer.name', $this->customer_id]);
     return $dataProvider;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = DeliveryDetail::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->joinWith('product');
     $query->andFilterWhere(['id' => $this->id, 'delivery_id' => $this->delivery_id, 'count' => $this->count, 'price' => $this->price]);
     $query->andFilterWhere(['like', 'product.name', $this->product_id]);
     return $dataProvider;
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDeliveryDetails()
 {
     return $this->hasMany(DeliveryDetail::className(), ['product_id' => 'id']);
 }
 /**
  * Deletes an existing Delivery model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     DeliveryDetail::deleteAll(['delivery_Id' => $id]);
     $model = $this->findModel($id);
     //delete the money to customer
     $customer = $model->customer;
     $customer->unpay -= $model->money;
     $customer->sum -= $model->money;
     $customer->save();
     $model->delete();
     return $this->redirect(['index']);
 }