From 8b715fe18f8928b222c1e7ae75296fb17d0e3870 Mon Sep 17 00:00:00 2001 From: vladfo Date: Tue, 15 Oct 2024 17:38:53 +0300 Subject: [PATCH] =?utf8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?utf8?q?=20=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BC?= =?utf8?q?=D0=BE=D0=B4=D0=B5=D0=BB=D0=B5=D0=B9=20=D0=B8=20=D0=BA=D0=BE?= =?utf8?q?=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../crud/ClusterAdminController.php | 165 ++++++++++++++++++ ...1015_125203_create_cluster_admin_table.php | 32 ++++ erp24/records/ClusterAdmin.php | 65 +++++++ erp24/records/ClusterAdminSearch.php | 70 ++++++++ erp24/views/crud/cluster-admin/_form.php | 60 +++++++ erp24/views/crud/cluster-admin/_search.php | 35 ++++ erp24/views/crud/cluster-admin/create.php | 24 +++ erp24/views/crud/cluster-admin/index.php | 57 ++++++ erp24/views/crud/cluster-admin/update.php | 25 +++ erp24/views/crud/cluster-admin/view.php | 50 ++++++ 10 files changed, 583 insertions(+) create mode 100644 erp24/controllers/crud/ClusterAdminController.php create mode 100644 erp24/migrations/m241015_125203_create_cluster_admin_table.php create mode 100644 erp24/records/ClusterAdmin.php create mode 100644 erp24/records/ClusterAdminSearch.php create mode 100644 erp24/views/crud/cluster-admin/_form.php create mode 100644 erp24/views/crud/cluster-admin/_search.php create mode 100644 erp24/views/crud/cluster-admin/create.php create mode 100644 erp24/views/crud/cluster-admin/index.php create mode 100644 erp24/views/crud/cluster-admin/update.php create mode 100644 erp24/views/crud/cluster-admin/view.php diff --git a/erp24/controllers/crud/ClusterAdminController.php b/erp24/controllers/crud/ClusterAdminController.php new file mode 100644 index 00000000..ca06e7c7 --- /dev/null +++ b/erp24/controllers/crud/ClusterAdminController.php @@ -0,0 +1,165 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ] + ); + } + + /** + * Lists all ClusterAdmin models. + * + * @return string + */ + public function actionIndex() + { + $searchModel = new ClusterAdminSearch(); + $dataProvider = $searchModel->search($this->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single ClusterAdmin model. + * @param int $id ID записи + * @return string + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new ClusterAdmin model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return string|\yii\web\Response + */ + public function actionCreate() + { + $model = new ClusterAdmin(); + + if ($this->request->isPost && $model->load($this->request->post())) { + // Проверка на существование записи с такими же cluster_id и admin_id и date_end > текущей даты + $existingRecord = ClusterAdmin::find() + ->where(['cluster_id' => $model->cluster_id, 'admin_id' => $model->admin_id]) + ->andWhere(['>', 'date_end', date('Y-m-d')]) + ->one(); + + if ($existingRecord) { + Yii::$app->session->setFlash('error', 'Запись с таким кустом и кустовым уже существует и ещё актуальна.'); + } else { + // Проверка на пересечение дат + $overlappingRecord = ClusterAdmin::find() + ->where(['cluster_id' => $model->cluster_id, 'admin_id' => $model->admin_id]) + ->andWhere(['<=', 'date_start', $model->date_start]) + ->andWhere(['>=', 'date_end', $model->date_start]) + ->one(); + + if ($overlappingRecord) { + Yii::$app->session->setFlash('error', 'Дата начала пересекается с существующей записью.'); + } else { + if ($model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + } + } + } + // Получение данных для выпадающих списков + $clusters = ArrayHelper::map(Cluster::find()->all(), 'id', 'name'); + $admins = ArrayHelper::map(Admin::find() + ->select(['id', 'name']) + ->where(['NOT IN', 'group_id', [-1, 1000]]) // Исключаем сотрудников с group_id = -1 и group_id = 1000 + ->all(), 'id', 'name'); + + return $this->render('create', [ + 'model' => $model, + 'clusters' => $clusters, + 'admins' => $admins, + ]); + } + + /** + * Updates an existing ClusterAdmin model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param int $id ID записи + * @return string|\yii\web\Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing ClusterAdmin model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param int $id ID записи + * @return \yii\web\Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the ClusterAdmin model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param int $id ID записи + * @return ClusterAdmin the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = ClusterAdmin::findOne(['id' => $id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/erp24/migrations/m241015_125203_create_cluster_admin_table.php b/erp24/migrations/m241015_125203_create_cluster_admin_table.php new file mode 100644 index 00000000..ee4e5722 --- /dev/null +++ b/erp24/migrations/m241015_125203_create_cluster_admin_table.php @@ -0,0 +1,32 @@ +createTable(self::TABLE_NAME, [ + 'id' => $this->primaryKey(), + 'cluster_id' => $this->integer()->notNull()->comment('ID куста'), + 'admin_id' => $this->integer()->notNull()->comment('ID пользователя'), + 'date_start' => $this->date()->notNull()->comment('Дата начала привязки'), + 'date_end' => $this->date()->defaultValue('2100-01-01')->notNull()->comment('Дата окончания привязки'), + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable(self::TABLE_NAME); + } +} diff --git a/erp24/records/ClusterAdmin.php b/erp24/records/ClusterAdmin.php new file mode 100644 index 00000000..4fc9ec83 --- /dev/null +++ b/erp24/records/ClusterAdmin.php @@ -0,0 +1,65 @@ + 'php:Y-m-d'], + ['date_end', 'compare', 'compareAttribute' => 'date_start', + 'operator' => '>=', 'type' => 'date', + 'message' => 'Дата окончания должна быть больше или равна дате начала'], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID записи', + 'cluster_id' => 'Куст', + 'admin_id' => 'Кустовой', + 'date_start' => 'Начало привязки', + 'date_end' => 'Окончание привязки', + ]; + } + + public function getCluster() + { + return $this->hasOne(Cluster::class, ['id' => 'cluster_id']); + } + + public function getAdmin() + { + return $this->hasOne(Admin::class, ['id' => 'admin_id']); + } + +} diff --git a/erp24/records/ClusterAdminSearch.php b/erp24/records/ClusterAdminSearch.php new file mode 100644 index 00000000..44c93e31 --- /dev/null +++ b/erp24/records/ClusterAdminSearch.php @@ -0,0 +1,70 @@ + $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; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'cluster_id' => $this->cluster_id, + 'admin_id' => $this->admin_id, + 'date_start' => $this->date_start, + 'date_end' => $this->date_end, + ]); + + return $dataProvider; + } +} diff --git a/erp24/views/crud/cluster-admin/_form.php b/erp24/views/crud/cluster-admin/_form.php new file mode 100644 index 00000000..61216e35 --- /dev/null +++ b/erp24/views/crud/cluster-admin/_form.php @@ -0,0 +1,60 @@ + + +
+ + + session->hasFlash('error')) { ?> + + +
+ + + + + field($model, 'cluster_id')->widget(Select2::class, [ + 'data' => $clusters, + 'options' => ['placeholder' => 'Выберите куст...'], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]); ?> + + field($model, 'admin_id')->widget(Select2::class, [ + 'data' => $admins, + 'options' => ['placeholder' => 'Выберите кустового...'], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]); ?> + + field($model, 'date_start')->input('date', [ + 'value' => date('Y-m-d'), + ]) ?> + + field($model, 'date_end')->input('date') ?> + +
+ 'btn btn-success']) ?> +
+ + +
+
diff --git a/erp24/views/crud/cluster-admin/_search.php b/erp24/views/crud/cluster-admin/_search.php new file mode 100644 index 00000000..532d90a4 --- /dev/null +++ b/erp24/views/crud/cluster-admin/_search.php @@ -0,0 +1,35 @@ + + + diff --git a/erp24/views/crud/cluster-admin/create.php b/erp24/views/crud/cluster-admin/create.php new file mode 100644 index 00000000..a70c6dc6 --- /dev/null +++ b/erp24/views/crud/cluster-admin/create.php @@ -0,0 +1,24 @@ +title = 'Создание привязки Куста к Кустовому'; +$this->params['breadcrumbs'][] = ['label' => 'Cluster Admins', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + 'clusters' => $clusters, + 'admins' => $admins, + ]) ?> + +
diff --git a/erp24/views/crud/cluster-admin/index.php b/erp24/views/crud/cluster-admin/index.php new file mode 100644 index 00000000..ddc22416 --- /dev/null +++ b/erp24/views/crud/cluster-admin/index.php @@ -0,0 +1,57 @@ +title = 'Привязка Куст-Кустовой'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ 'btn btn-success']) ?> +

+ + render('_search', ['model' => $searchModel]); ?> + + $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + [ + 'attribute' => 'cluster_id', + 'value' => function ($model) { + return $model->cluster ? $model->cluster->name . ' (' . $model->cluster_id . ')' : null; + }, + ], + [ + 'attribute' => 'admin_id', + 'value' => function ($model) { + return $model->admin ? $model->admin->name . ' (' . $model->admin_id . ')' : null; + }, + ], + 'date_start', + 'date_end', + [ + 'class' => ActionColumn::class, + 'urlCreator' => function ($action, ClusterAdmin $model, $key, $index, $column) { + return Url::toRoute([$action, 'id' => $model->id]); + } + ], + ], + ]); ?> + + +
diff --git a/erp24/views/crud/cluster-admin/update.php b/erp24/views/crud/cluster-admin/update.php new file mode 100644 index 00000000..b51539c4 --- /dev/null +++ b/erp24/views/crud/cluster-admin/update.php @@ -0,0 +1,25 @@ +title = 'Редактирование привязки куста к кустовому: ' . $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Cluster Admins', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + 'clusters' => $clusters, + 'admins' => $admins, + ]) ?> + +
diff --git a/erp24/views/crud/cluster-admin/view.php b/erp24/views/crud/cluster-admin/view.php new file mode 100644 index 00000000..046aacf4 --- /dev/null +++ b/erp24/views/crud/cluster-admin/view.php @@ -0,0 +1,50 @@ +title = "Привязка кустового к кусту №" . $model->id . " от " . $model->date_start; +$this->params['breadcrumbs'][] = ['label' => 'Cluster Admins', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ +

title) ?>

+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Вы уверены что хотите удалить запись?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + [ + 'attribute' => 'cluster_id', + 'value' => function ($model) { + return $model->cluster ? $model->cluster->name . ' (' . $model->cluster_id . ')' : null; + }, + ], + [ + 'attribute' => 'admin_id', + 'value' => function ($model) { + return $model->admin ? $model->admin->name . ' (' . $model->admin_id . ')' : null; + }, + ], + 'date_start', + 'date_end', + ], + ]) ?> + +
-- 2.39.5