/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = WorkloadEvent2::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->andFilterWhere(['ref' => $this->ref, 'event_date' => $this->event_date, 'bed_no' => $this->bed_no, 'cpr' => $this->cpr, 'doctor_ref' => $this->doctor_ref, 'last_update' => $this->last_update]);
     $query->andFilterWhere(['like', 'event_period', $this->event_period])->andFilterWhere(['like', 'hn', $this->hn])->andFilterWhere(['like', 'an', $this->an])->andFilterWhere(['like', 'ward', $this->ward])->andFilterWhere(['like', 'bed_type', $this->bed_type])->andFilterWhere(['like', 'pt_type', $this->pt_type])->andFilterWhere(['like', 'admit_type', $this->admit_type])->andFilterWhere(['like', 'disc_type', $this->disc_type])->andFilterWhere(['like', 'disability', $this->disability])->andFilterWhere(['like', 'operate', $this->operate])->andFilterWhere(['like', 'prepare', $this->prepare])->andFilterWhere(['like', 'uti', $this->uti])->andFilterWhere(['like', 'vap', $this->vap])->andFilterWhere(['like', 'phleb', $this->phleb])->andFilterWhere(['like', 'user', $this->user])->andFilterWhere(['like', 'cutdown', $this->cutdown]);
     return $dataProvider;
 }
    public function actionCreateP3()
    {
        $ward = Yii::$app->session->get('user.ward');
        $prev_data = WorkloadEvent2::find()->where(['ward' => $ward, 'event_date' => date('Y-m-d'), 'event_period' => 2])->all();
        $sql = 'select i.hn,i.an,i.title,i.name,i.surname from ipd_ipd i
where i.ward = ' . $ward . ' and (i.disc="" or i.disc is null or i.disc = "0000-00-00" or i.disc = curdate())
and not exists (select "x" from workload_ptdisc p where p.ward = ' . $ward . ' and i.an = p.an )';
        $patient = IpdIpd::findBySql($sql)->all();
        $model = new WorkloadEvent2();
        if (isset($_POST['WorkloadEvent2'])) {
            if ($model->validate()) {
                foreach ($patient as $v) {
                    $model = new WorkloadEvent2();
                    $model->attributes = $_POST['WorkloadEvent2'][$v->hn];
                    $model->save();
                    if (isset($_POST['WorkloadEvent2'][$v->hn]) && $_POST['WorkloadEvent2'][$v->hn]['disc_type'] != 0) {
                        $model_ptdisc = new WorkloadPtdisc();
                        $model_ptdisc->hn = $v->hn;
                        $model_ptdisc->an = $v->an;
                        $model_ptdisc->title = $v->title;
                        $model_ptdisc->name = $v->name;
                        $model_ptdisc->surname = $v->surname;
                        $model_ptdisc->ward = $ward;
                        $model_ptdisc->movestatus = $_POST['WorkloadEvent2'][$v->hn]['disc_type'];
                        if ($model_ptdisc->validate()) {
                            $model_ptdisc->save();
                        }
                    }
                }
            }
            return $this->redirect(['workload-event2/index']);
        } else {
            return $this->render('create-p3', ['model' => $model, 'patient' => $patient, 'prev_data' => $prev_data]);
        }
    }