/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Timetable::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, 'start' => $this->start, 'end' => $this->end, 'week_day' => $this->week_day, 'group_id' => $this->group_id]);
     return $dataProvider;
 }
 public function actionTimetableList()
 {
     $parents = Yii::$app->request->post('depdrop_parents');
     if ($parents !== null) {
         $out = [];
         $date = Carbon::createFromFormat('Y-m-d', $parents[0]);
         $condition = 'week_day = :week_day';
         $params = [':week_day' => $date->dayOfWeek];
         $groups = Timetable::find()->with('group')->where($condition, $params)->orderBy('start, end')->all();
         foreach ($groups as $row) {
             $out[] = ['id' => $row->id, 'name' => $row->group->name . ' ' . $row->start . ' - ' . $row->end];
         }
         echo Json::encode(['output' => $out, 'selected' => '']);
     }
     return;
 }
 private function addPaidEmployment()
 {
     if (!$this->ticket_id) {
         return true;
     }
     $this->_ticketModel = SeasonTicket::findOne(['id' => $this->ticket_id]);
     if (!$this->_ticketModel) {
         return true;
     }
     $this->_currentDate = Carbon::createFromFormat('d.m.Y', $this->startdate);
     $this->_endDate = self::getEmploymentEndDate($this->_currentDate, $this->_ticketModel->limit_format, $this->_ticketModel->limit_value);
     $timeTableGroups = Timetable::find()->asArray()->where(['group_id' => $this->groups])->all();
     $cond = 'date >= :startdate AND pay.user_id = :user_id';
     $params = [':user_id' => $this->user_id, ':startdate' => $this->_currentDate->toDateString()];
     $existEmployments = PaidEmployment::find()->select(['date', 'timetable_id', 'group_id' => 'timetable.group_id'])->asArray()->joinWith(['pay', 'timetable'])->where($cond, $params)->andWhere(['timetable.group_id' => $this->groups])->all();
     $existLastEmployment = PaidEmployment::find()->select(['date', 'timetable_id', 'group_id' => 'timetable.group_id'])->asArray()->joinWith(['pay', 'timetable'])->where($cond, $params)->andWhere(['timetable.group_id' => $this->groups])->orderBy('date DESC')->one();
     $this->addInsertArrayTimeLimit($timeTableGroups, $existEmployments, $this->_currentDate, $this->_endDate);
     if ($existLastEmployment) {
         $lastEmploymentDate = Carbon::createFromFormat('Y-m-d', $existLastEmployment['date'])->addDay();
         $this->_endDate = self::getEmploymentEndDate($lastEmploymentDate, $this->_ticketModel->limit_format, $this->_ticketModel->limit_value);
     }
     if ($this->_ticketModel->amount > 0) {
         $this->addInsertArrayAmountLimit($timeTableGroups, $existEmployments, $this->_currentDate, $this->_endDate);
     }
     if (!empty($this->_insertArray)) {
         $command = Yii::$app->db->createCommand()->batchInsert('{{%paid_employment}}', ['date', 'pay_id', 'timetable_id'], $this->_insertArray);
         return is_int($command->execute());
     } else {
         return true;
     }
 }
 /**
  * Cancel lessons on a date
  * @return string|void
  * @throws \yii\db\Exception
  */
 public function actionCancelLessons()
 {
     $model = new TimetableCancel();
     $parents = Yii::$app->request->post('depdrop_parents');
     if (is_array(Yii::$app->request->post('TimetableCancel')['ids']) && $model->load(Yii::$app->request->post()) && $model->validate()) {
         $date = Carbon::createFromFormat('d.m.Y', $model->date);
         $updatedCount = 0;
         $needUpdate = PaidEmployment::find()->asArray()->joinWith(['timetable', 'pay'])->where(['date' => "{$date->toDateString()}", 'timetable_id' => $model->ids])->all();
         foreach ($needUpdate as $needRow) {
             $maxPaidDateOnGroupForUser = PaidEmployment::find()->asArray()->select('MAX(date) as max_date')->joinWith(['timetable', 'pay'])->where(['pay.user_id' => $needRow['pay']['user_id'], 'timetable_id' => $needRow['timetable_id']])->one();
             if (!$maxPaidDateOnGroupForUser) {
                 continue;
             }
             $carbonMaxDate = Carbon::createFromFormat('Y-m-d', $maxPaidDateOnGroupForUser['max_date']);
             //Все элементы расписания для группы
             $allTimetableOnGroup = Timetable::find()->asArray()->where(['group_id' => $needRow['timetable']['group_id']])->all();
             $allTimetableWeekDaysOnGroup = ArrayHelper::getColumn($allTimetableOnGroup, function ($array) {
                 return ArrayHelper::getValue($array, 'week_day');
             });
             $i = 0;
             while (7 >= $i) {
                 ++$i;
                 $currDate = clone $carbonMaxDate;
                 $carbonMaxDate->addDay();
                 if (!in_array($currDate->dayOfWeek, $allTimetableWeekDaysOnGroup)) {
                     continue;
                 }
                 $notPaidTimetables = Timetable::find()->asArray()->where(['week_day' => $currDate->dayOfWeek, 'group_id' => $needRow['timetable']['group_id']])->andWhere('NOT EXISTS ' . '( ' . 'SELECT * FROM paid_employment pe ' . 'LEFT JOIN pay p ON p.id = pe.pay_id ' . 'WHERE p.user_id = ' . $needRow['pay']['user_id'] . ' ' . 'AND `pe`.`date` = "' . $currDate->toDateString() . '" ' . 'AND timetable_id = timetable.id' . ')')->orderBy('start, end')->all();
                 if ($notPaidTimetables) {
                     $command = Yii::$app->db->createCommand('UPDATE paid_employment SET `date` = "' . $currDate->toDateString() . '", ' . 'timetable_id = ' . $notPaidTimetables[0]['id'] . ' ' . 'WHERE id = ' . $needRow['id']);
                     if (is_int($command->execute())) {
                         ++$updatedCount;
                         break;
                     }
                 } else {
                 }
             }
         }
         if (0 < $updatedCount) {
             Yii::$app->session->setFlash('success', Module::t('timetable-admin', 'Moved training schedule clients: {sum}', ['sum' => $updatedCount]));
         } else {
             Yii::$app->session->setFlash('danger', Module::t('timetable-admin', Module::t('timetable-admin', 'No records found in the timetable of customers at that date.')));
         }
         return $this->render('cancellessons', ['model' => $model]);
     } elseif ($parents !== null) {
         $out = [];
         $date = Carbon::createFromFormat('d.m.Y', $parents[0]);
         $condition = 'week_day = :week_day';
         $params = [':week_day' => $date->dayOfWeek];
         $groups = Timetable::find()->with('group')->where($condition, $params)->orderBy('start, end')->all();
         foreach ($groups as $row) {
             $out[] = ['id' => $row->id, 'name' => $row->group->name . ' ' . $row->start . ' - ' . $row->end];
         }
         echo Json::encode(['output' => $out, 'selected' => '']);
         return;
     } else {
         return $this->render('cancellessons', ['model' => $model]);
     }
 }