/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Scheduleheadertemp::find()->joinWith('scdlgroup', true, 'JOIN')->joinWith('user', true, 'JOIN');
     $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(['ID' => $this->ID, 'TGL1' => $this->TGL1, 'SCDL_GROUP' => $this->SCDL_GROUP, 'STATUS' => $this->STATUS, 'CREATE_AT' => $this->CREATE_AT, 'UPDATE_AT' => $this->UPDATE_AT]);
     $query->andFilterWhere(['like', 'USER_ID', $this->USER_ID])->andFilterWhere(['like', 'NOTE', $this->NOTE])->andFilterWhere(['like', 'CREATE_BY', $this->CREATE_BY])->andFilterWhere(['like', 'UPDATE_BY', $this->UPDATE_BY])->andFilterWhere(['like', 'username', $this->getAttribute('user.username')])->andFilterWhere(['like', 'SCDL_GROUP_NM', $this->getAttribute('scdlgroup.SCDL_GROUP_NM')]);
     return $dataProvider;
 }
 public function actionCreateGroup($tgl1, $tgl2)
 {
     $ary = [['id' => 0, 'UBAH_NM' => 'permanen'], ['id' => 1, 'UBAH_NM' => 'Update']];
     $val = ArrayHelper::map($ary, 'id', 'UBAH_NM');
     $post = Yii::$app->request->post();
     $post['Scheduleheadertemp']['STT_UBAH'];
     // if equal 1 then update else save
     if ($post['Scheduleheadertemp']['STT_UBAH'] == 1) {
         $model = Scheduleheadertemp::find()->where(['TGL1' => $tgl1])->one();
         if ($model->load(Yii::$app->request->post())) {
             $model->TGL1 = $tgl1;
             $model->TGL2 = $tgl2;
             // cari group nama
             $cari_groupName = Schedulegroup::find()->where(['ID' => $model->SCDL_GROUP])->asArray()->one();
             /* split array author : wawan*/
             $temp = explode(" ", $cari_groupName['SCDL_GROUP_NM']);
             $result = '';
             foreach ($temp as $t) {
                 $result .= $t[0];
             }
             //$model->NOTE = $result;
             $model->NOTE = $cari_groupName['SCDL_GROUP_NM'];
             /*Update by ptr.nov*/
             $model->save();
             return $this->redirect(['index']);
         } else {
             return $this->renderAjax('_formschedule', ['model' => $model, 'datagroup' => $datagroup, 'datauser' => $datauser, 'val' => $val, 'tgl1' => $tgl1]);
         }
     } else {
         $model = new Scheduleheadertemp();
         // data select2 for USER_ID where CRM and STATUS 10(active)
         $connection = \Yii::$app->db_esm;
         $querycariuser = '******' . $tgl1 . '") AND POSITION_SITE = "CRM" AND STATUS = 10';
         $user = $connection->createCommand($querycariuser)->queryAll();
         $datauser = ArrayHelper::map($user, 'id', 'username');
         // data select2 for SCDL_GROUP
         $connection = \Yii::$app->db_esm;
         $model1 = $connection->createCommand('SELECT * from c0007 where ID NOT IN (SELECT DISTINCT(SCDL_GROUP) FROM c0002scdl_detail_temp  WHERE TGL="' . $tgl1 . '")');
         $query = $model1->queryAll();
         $datagroup = ArrayHelper::map($query, 'ID', 'SCDL_GROUP_NM');
         //componen user option
         $profile = Yii::$app->getUserOpt->Profile_user();
         $usercreate = $profile->username;
         //proses save
         if ($model->load(Yii::$app->request->post())) {
             $model->TGL1 = $tgl1;
             $model->TGL2 = $tgl2;
             // cari group nama
             $cari_groupName = Schedulegroup::find()->where(['ID' => $model->SCDL_GROUP])->asArray()->one();
             /* split array author : wawan*/
             $temp = explode(" ", $cari_groupName['SCDL_GROUP_NM']);
             $result = '';
             foreach ($temp as $t) {
                 $result .= $t[0];
             }
             //$model->NOTE = $result;
             $model->NOTE = $cari_groupName['SCDL_GROUP_NM'];
             /*Update by ptr.nov*/
             $model->CREATE_BY = $usercreate;
             $model->CREATE_AT = date("Y-m-d H:i:s");
             if ($model->save()) {
                 // foreach date :author wawan
                 for ($date = strtotime($tgl1); $date <= strtotime($tgl2); $date = strtotime("+1 day", $date)) {
                     $tgl = date("Y-m-d", $date);
                     //batch insert customers author :wawan
                     $Customers = Customers::find()->where(['SCDL_GROUP' => $model->SCDL_GROUP])->asArray()->all();
                     foreach ($Customers as $key => $value) {
                         # code...
                         $connection = Yii::$app->db_esm;
                         $connection->createCommand()->batchInsert('c0002scdl_detail_temp', ['TGL', 'CUST_ID', 'SCDL_GROUP', 'USER_ID'], [[$tgl, $value['CUST_KD'], $model->SCDL_GROUP, $model->USER_ID]])->execute();
                     }
                 }
             }
             return $this->redirect(['index']);
         } else {
             return $this->renderAjax('_formschedule', ['model' => $model, 'datagroup' => $datagroup, 'datauser' => $datauser, 'val' => $val, 'tgl1' => $tgl1]);
         }
     }
 }