From: fomichev Date: Thu, 24 Oct 2024 14:20:45 +0000 (+0300) Subject: Редактирование записи X-Git-Tag: 1.7~228^2~28 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=472b3b91ce2e740de1ecf000a0c25ec64ebad332;p=erp24_rep%2Fyii-erp24%2F.git Редактирование записи --- diff --git a/erp24/controllers/crud/ClusterAdminController.php b/erp24/controllers/crud/ClusterAdminController.php index b0dc4540..8a674145 100644 --- a/erp24/controllers/crud/ClusterAdminController.php +++ b/erp24/controllers/crud/ClusterAdminController.php @@ -126,14 +126,14 @@ class ClusterAdminController extends Controller { $model = new ClusterAdmin(); - // Проверяем, существует ли указанный кластер + $cluster = Cluster::findOne($cluster_id); if (!$cluster) { throw new NotFoundHttpException('Кластер не найден.'); } if ($this->request->isPost && $model->load($this->request->post())) { - $model->cluster_id = $cluster_id; // Устанавливаем cluster_id + $model->cluster_id = $cluster_id; // Ищем активную запись для данного кластера $existingRecord = ClusterAdmin::find() @@ -190,6 +190,10 @@ class ClusterAdminController extends Controller { $model = $this->findModel($id); + $admins = ArrayHelper::map(Admin::find() + ->select(['id', 'name']) + ->where(['IN', 'group_id', [7]]) + ->all(), 'id', 'name'); $cluster = Cluster::findOne($model->cluster_id); if (!$cluster) { @@ -197,6 +201,67 @@ class ClusterAdminController extends Controller } if ($this->request->isPost && $model->load($this->request->post())) { + + // Проверяем изменение admin_id + if ($model->admin_id) { + // Если активность = 1 + if ($model->active == 1) { + $existingActiveRecords = ClusterAdmin::find() + ->where(['admin_id' => $model->admin_id, 'active' => 1]) + ->andWhere(['!=', 'cluster_id', $model->cluster_id]) + ->all(); + + if (!empty($existingActiveRecords)) { + $conflictCluster = $existingActiveRecords[0]; + Yii::$app->session->setFlash('error', 'Этот пользователь ' . $admins[$model->admin_id] . ' уже привязан к кластеру ' . $conflictCluster->cluster_id . ' с активным статусом.'); + return $this->redirect(['update', 'id' => $model->id]); + } + + // Проверка на конфликт с неактивными записями + $existingInactiveRecords = ClusterAdmin::find() + ->where(['admin_id' => $model->admin_id, 'active' => 0]) + ->andWhere(['!=', 'cluster_id', $model->cluster_id]) + ->all(); + + foreach ($existingInactiveRecords as $record) { + $modelStart = strtotime($model->date_start); + $recordStart = strtotime($record->date_start); + $recordEnd = strtotime($record->date_end); + + if ($modelStart < $recordStart && $modelStart < $recordEnd) { + Yii::$app->session->setFlash('error', 'Ошибка: активная запись для ' . $admins[$model->admin_id] . 'не может начинаться раньше, чем неактивная запись (' . $record->date_start . ' - ' . $record->date_end . ') для другого кластера.'); + return $this->redirect(['update', 'id' => $model->id]); + } + } + + } + + // Если активность = 0 + if ($model->active == 0) { + $existingInactiveRecords = ClusterAdmin::find() + ->where(['admin_id' => $model->admin_id,]) + // ->andWhere(['!=', 'cluster_id', $model->cluster_id]) + ->all(); + + foreach ($existingInactiveRecords as $record) { + $modelStart = strtotime($model->date_start); + $modelEnd = strtotime($model->date_end); + $recordStart = strtotime($record->date_start); + $recordEnd = strtotime($record->date_end); + + if ( + ($modelStart > $recordStart && $modelStart < $recordEnd) || // Начало новой записи внутри существующей + ($modelEnd > $recordStart && $modelEnd < $recordEnd) || // Конец новой записи внутри существующей + ($modelStart <= $recordStart && $modelEnd >= $recordEnd) // Новая запись полностью охватывает существующую + ) { + Yii::$app->session->setFlash('error', 'Пересечение дат с записью от ' . $record->date_start . ' до ' . $record->date_end . ' для другого кластера.'); + return $this->redirect(['update', 'id' => $model->id]); + } + } + } + } + + // Сохраняем модель, если нет ошибок if ($model->save()) { Yii::$app->session->setFlash('success', 'Запись успешно обновлена.'); return $this->redirect(['view', 'id' => $model->cluster_id]); @@ -205,10 +270,7 @@ class ClusterAdminController extends Controller } } - $admins = ArrayHelper::map(Admin::find() - ->select(['id', 'name']) - ->where(['IN', 'group_id', [7]]) - ->all(), 'id', 'name'); + $user = Yii::$app->user->identity; @@ -219,7 +281,6 @@ class ClusterAdminController extends Controller 'user' => $user, ]); } - /** * Deletes an existing ClusterAdmin model. * If deletion is successful, the browser will be redirected to the 'index' page. diff --git a/erp24/records/ClusterAdmin.php b/erp24/records/ClusterAdmin.php index f5bd42ab..e4d87e95 100644 --- a/erp24/records/ClusterAdmin.php +++ b/erp24/records/ClusterAdmin.php @@ -65,14 +65,6 @@ class ClusterAdmin extends \yii\db\ActiveRecord return $this->hasOne(Admin::class, ['id' => 'admin_id']); } - /** - * Устанавливаем значение активности по умолчанию - */ - public function init() - { - parent::init(); - if ($this->isNewRecord) { - $this->active = 1; // По умолчанию активная запись - } - } + + } \ No newline at end of file diff --git a/erp24/views/crud/cluster-admin/_form.php b/erp24/views/crud/cluster-admin/_form.php index 54b9f3ba..a2135ed2 100644 --- a/erp24/views/crud/cluster-admin/_form.php +++ b/erp24/views/crud/cluster-admin/_form.php @@ -15,60 +15,96 @@ use yii_app\records\ClusterAdmin; ?> -
- session->hasFlash('error')) { ?> - - +
+ session->hasFlash('error')) { ?> + + -
- +
+ + + +
+ + + field($model, 'cluster_id')->hiddenInput(['value' => $cluster->id])->label(false) ?> +
+ + field($model, 'admin_id')->widget(Select2::class, [ + 'data' => $admins, + 'options' => ['placeholder' => 'Выберите кустового...'], + 'pluginOptions' => [ + 'allowClear' => true + ], + ]); ?> + + + field($model, 'active')->checkbox([ + 'label' => 'Активность', + 'checked' => $model->isNewRecord ? true : boolval($model->active), + 'id' => 'active-checkbox', + ]) ?> - -
- - - field($model, 'cluster_id')->hiddenInput(['value' => $cluster->id])->label(false) ?> -
- field($model, 'admin_id')->widget(Select2::class, [ - 'data' => $admins, - 'options' => ['placeholder' => 'Выберите кустового...'], - 'pluginOptions' => [ - 'allowClear' => true - ], - ]); ?> - - - field($model, 'active')->checkbox([ - 'label' => 'Активность', - 'checked' => $model->isNewRecord ? true : boolval($model->active), - ]) ?> - - - - field($model, 'date_start')->input('date', [ - 'value' => $model->date_start ?? date('Y-m-d'), - ]) ?> - - field($model, 'date_end')->input('date', [ - 'value' => $model->date_end ?? date('Y-m-d'), - ]) ?> - -
- 'btn btn-success']) ?> - 'btn btn-danger my-2']) ?> -
+ ]) ?> + - + field($model, 'date_end')->input('date', [ + 'value' => $model->date_end ?? date('Y-m-d'), + 'id' => 'date-end-field', + 'style' => $model->active ? 'display: none;' : '', + ]) ?> +
+ 'btn btn-success']) ?> + 'btn btn-danger my-2']) ?> +
+ + +
-
\ No newline at end of file + +registerJs($js); +?> \ No newline at end of file