/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = PaidEmployment::find()->joinWith(['pay.profile', 'pay', 'timetable', 'timetable.group']); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['date' => SORT_DESC]]]); $dataProvider->sort->attributes['pay.profile.fullName'] = ['asc' => [Profile::tableName() . '.name' => SORT_ASC, Profile::tableName() . '.surname' => SORT_ASC], 'desc' => [Profile::tableName() . '.name' => SORT_DESC, Profile::tableName() . '.surname' => SORT_DESC]]; $dataProvider->sort->attributes['timetable.group.name'] = ['asc' => [Group::tableName() . '.name' => SORT_ASC], 'desc' => [Group::tableName() . '.name' => SORT_DESC]]; $dataProvider->sort->attributes['timetable.start'] = ['asc' => [Timetable::tableName() . '.start' => SORT_ASC], 'desc' => [Timetable::tableName() . '.start' => SORT_DESC]]; $dataProvider->sort->attributes['timetable.end'] = ['asc' => [Timetable::tableName() . '.end' => SORT_ASC], 'desc' => [Timetable::tableName() . '.end' => SORT_DESC]]; $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, 'date' => $this->date, 'pay_id' => $this->pay_id, 'pay.user_id' => $this->userFullName, 'timetable.group_id' => $this->timetableGroupId]); return $dataProvider; }
/** * Displays a single Pay model. * @param integer $id * @return mixed */ public function actionView($id) { $paidEmployment = new ActiveDataProvider(['query' => PaidEmployment::find()->select(['{{%paid_employment}}.id', '{{%paid_employment}}.timetable_id', '{{%paid_employment}}.date', '{{%timetable}}.start', '{{%timetable}}.end', '{{%group}}.name'])->joinWith('timetable.group')->where(['pay_id' => $id])->orderBy('date'), 'pagination' => ['pageSize' => 50], 'sort' => false]); return $this->render('view', ['model' => $this->findModel($id), 'paidEmployment' => $paidEmployment]); }
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; } }
/** * Finds the PaidEmployment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return PaidEmployment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModelAll($id) { if (($model = PaidEmployment::find()->where(['id' => $id])->all()) !== null) { return $model; } else { throw new HttpException(404); } }
/** * Displays a single User model. * @return mixed */ public function actionViewPay($id) { $paidEmployment = new ActiveDataProvider(['query' => PaidEmployment::find()->select(['{{%paid_employment}}.timetable_id', '{{%paid_employment}}.date', '{{%timetable}}.start', '{{%timetable}}.end', '{{%group}}.name'])->joinWith('timetable.group')->where(['pay_id' => $id])->orderBy('date'), 'pagination' => ['pageSize' => 30], 'sort' => false]); $model = Pay::find()->where('id = :id AND user_id=' . Yii::$app->user->id, [':id' => $id])->one(); if (!$model) { throw new NotFoundHttpException('The requested page does not exist.'); } return $this->render('view_pay', ['model' => $model, 'paidEmployment' => $paidEmployment]); }
/** * 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]); } }