From f6a2c38bbb1b27280aa95c76b2830de2a2c908a5 Mon Sep 17 00:00:00 2001 From: Vladimir Fomichev Date: Fri, 31 Oct 2025 09:07:40 +0300 Subject: [PATCH] =?utf8?q?CRUD=20=D0=B4=D0=BB=D1=8F=20=D0=B3=D1=80=D1=83?= =?utf8?q?=D0=BF=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/actions/grade/AdminUpdateAction.php | 49 +++++- erp24/controllers/AdminGroupController.php | 174 +++++++++++++++++++++ erp24/views/admin-group/_form.php | 49 ++++++ erp24/views/admin-group/create.php | 20 +++ erp24/views/admin-group/index.php | 54 +++++++ erp24/views/admin-group/update.php | 21 +++ erp24/views/admin-group/view.php | 50 ++++++ erp24/views/grade/admin-update.php | 74 ++++++++- 8 files changed, 483 insertions(+), 8 deletions(-) create mode 100644 erp24/controllers/AdminGroupController.php create mode 100644 erp24/views/admin-group/_form.php create mode 100644 erp24/views/admin-group/create.php create mode 100644 erp24/views/admin-group/index.php create mode 100644 erp24/views/admin-group/update.php create mode 100644 erp24/views/admin-group/view.php diff --git a/erp24/actions/grade/AdminUpdateAction.php b/erp24/actions/grade/AdminUpdateAction.php index e81eefdd..cc4c8a1a 100644 --- a/erp24/actions/grade/AdminUpdateAction.php +++ b/erp24/actions/grade/AdminUpdateAction.php @@ -25,6 +25,11 @@ class AdminUpdateAction extends Action if (Yii::$app->user->can("updateAdminSettings", ['id' => $model->id])) { if (Yii::$app->request->isPost) { $attributes = Yii::$app->request->post()['Admin']; + + // Обработка employee_position_id - если приходит пустая строка, устанавливаем null + if (isset($attributes['employee_position_id']) && $attributes['employee_position_id'] === '') { + $attributes['employee_position_id'] = null; + } foreach (['parent_admin_id', 'org_id'] as $fieldName) { if (empty($attributes[$fieldName])) { $attributes[$fieldName] = 0; @@ -75,12 +80,46 @@ class AdminUpdateAction extends Action Yii::$app->cache->set("dirtyAuthSettings", true); } - // Обновляем group_name на основе выбранной группы - if (isset($attributes['group_id'])) { - $adminGroup = AdminGroup::findOne($attributes['group_id']); - if ($adminGroup) { - $attributes['group_name'] = $adminGroup->name; + // Определяем специальные группы с parent_id = 50 + $specialGroups = [ + AdminGroup::GROUP_FLORIST_DAY, // 30 + AdminGroup::GROUP_FLORIST_NIGHT, // 35 + AdminGroup::GROUP_FLORIST_SUPPORT_DAY, // 40 + AdminGroup::GROUP_WORKERS, // 45 + AdminGroup::GROUP_ADMINISTRATORS, // 50 + AdminGroup::GROUP_FLORIST_SUPPORT_NIGHT, // 72 + ]; + + // Ищем группу "Работники магазинов" по имени + $workersGroup = AdminGroup::find()->where(['name' => 'Работники магазинов'])->one(); + if ($workersGroup) { + $specialGroups[] = $workersGroup->id; + } + + $isSpecialGroup = in_array((int)$attributes['group_id'], $specialGroups); + + if ($isSpecialGroup) { + // Для специальных групп формируем group_name из employee_position + shift + if (!empty($attributes['employee_position_id'])) { + $employeePosition = EmployeePosition::findOne($attributes['employee_position_id']); + if ($employeePosition) { + $groupName = $employeePosition->name; + if (!empty($attributes['shift'])) { + $groupName .= ' ' . $attributes['shift']; + } + $attributes['group_name'] = $groupName; + } + } + + unset($attributes['shift']); + } else { + // Для остальных групп group_name берем из текстового поля + if (isset($attributes['custom_position'])) { + $attributes['group_name'] = $attributes['custom_position']; + unset($attributes['custom_position']); } + // Очищаем employee_position_id для не-специальных групп + $attributes['employee_position_id'] = null; } $model->setAttributes($attributes, false); diff --git a/erp24/controllers/AdminGroupController.php b/erp24/controllers/AdminGroupController.php new file mode 100644 index 00000000..6f2af080 --- /dev/null +++ b/erp24/controllers/AdminGroupController.php @@ -0,0 +1,174 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ] + ); + } + + /** + * Lists all AdminGroup models. + * + * @return string + */ + public function actionIndex() + { + if (($resp = $this->checkAccess()) !== null) { + return $resp; + } + $dataProvider = new ActiveDataProvider([ + 'query' => AdminGroup::find(), + /* + 'pagination' => [ + 'pageSize' => 50 + ], + 'sort' => [ + 'defaultOrder' => [ + 'id' => SORT_DESC, + ] + ], + */ + ]); + + return $this->render('index', [ + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single AdminGroup model. + * @param int $id ID + * @return string + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + if (($resp = $this->checkAccess()) !== null) { + return $resp; + } + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new AdminGroup model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return string|\yii\web\Response + */ + public function actionCreate() + { + if (($resp = $this->checkAccess()) !== null) { + return $resp; + } + $model = new AdminGroup(); + + if ($this->request->isPost) { + if ($model->load($this->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + } else { + $model->loadDefaultValues(); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing AdminGroup 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) + { + if (($resp = $this->checkAccess()) !== null) { + return $resp; + } + $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 AdminGroup 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) + { + if (($resp = $this->checkAccess()) !== null) { + return $resp; + } + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the AdminGroup 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 AdminGroup the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = AdminGroup::findOne(['id' => $id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } + + private function checkAccess() { + $groupId = Yii::$app->user->identity->group_id; + + if (!in_array($groupId, [ + AdminGroup::GROUP_IT, + AdminGroup::GROUP_HR, + + ], true)) { + return $this->render('/site/index'); + } + + return null; + } +} diff --git a/erp24/views/admin-group/_form.php b/erp24/views/admin-group/_form.php new file mode 100644 index 00000000..9e73b3e1 --- /dev/null +++ b/erp24/views/admin-group/_form.php @@ -0,0 +1,49 @@ + + +
+ + + + field($model, 'name')->textInput(['maxlength' => true]) ?> + + field($model, 'parent_id')->textInput() ?> + + field($model, 'message')->textarea(['rows' => 6]) ?> + + field($model, 'dostup')->textarea(['rows' => 6]) ?> + + field($model, 'dostup_array')->textarea(['rows' => 6]) ?> + + field($model, 'status_dostup_arr')->textarea(['rows' => 6]) ?> + + field($model, 'status_arr')->textarea(['rows' => 6]) ?> + + field($model, 'istochnik_dostup_all')->textInput() ?> + + field($model, 'istochnik_dostup_arr')->textarea(['rows' => 6]) ?> + + field($model, 'istochnik_id_default')->textInput() ?> + + field($model, 'info_block')->textarea(['rows' => 6]) ?> + + field($model, 'posit')->textInput() ?> + + field($model, 'orders_dostup')->textInput(['maxlength' => true]) ?> + + field($model, 'admin_group_add_arr')->textarea(['rows' => 6]) ?> + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/erp24/views/admin-group/create.php b/erp24/views/admin-group/create.php new file mode 100644 index 00000000..70e747a4 --- /dev/null +++ b/erp24/views/admin-group/create.php @@ -0,0 +1,20 @@ +title = 'Создать группу'; +$this->params['breadcrumbs'][] = ['label' => 'Admin Groups', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/erp24/views/admin-group/index.php b/erp24/views/admin-group/index.php new file mode 100644 index 00000000..b70932ca --- /dev/null +++ b/erp24/views/admin-group/index.php @@ -0,0 +1,54 @@ +title = 'Группы пользователей'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

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

+ + + $dataProvider, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'name', + 'parent_id', + 'message:ntext', + 'dostup:ntext', + //'dostup_array:ntext', + //'status_dostup_arr:ntext', + //'status_arr:ntext', + //'istochnik_dostup_all', + //'istochnik_dostup_arr:ntext', + //'istochnik_id_default', + //'info_block:ntext', + //'posit', + //'orders_dostup', + //'admin_group_add_arr:ntext', + [ + 'class' => ActionColumn::className(), + 'urlCreator' => function ($action, AdminGroup $model, $key, $index, $column) { + return Url::toRoute([$action, 'id' => $model->id]); + } + ], + ], + ]); ?> + + +
diff --git a/erp24/views/admin-group/update.php b/erp24/views/admin-group/update.php new file mode 100644 index 00000000..8354f9de --- /dev/null +++ b/erp24/views/admin-group/update.php @@ -0,0 +1,21 @@ +title = 'Изменить группу: ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Admin Groups', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/erp24/views/admin-group/view.php b/erp24/views/admin-group/view.php new file mode 100644 index 00000000..6ceb4d8d --- /dev/null +++ b/erp24/views/admin-group/view.php @@ -0,0 +1,50 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Admin Groups', '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' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'name', + 'parent_id', + 'message:ntext', + 'dostup:ntext', + 'dostup_array:ntext', + 'status_dostup_arr:ntext', + 'status_arr:ntext', + 'istochnik_dostup_all', + 'istochnik_dostup_arr:ntext', + 'istochnik_id_default', + 'info_block:ntext', + 'posit', + 'orders_dostup', + 'admin_group_add_arr:ntext', + ], + ]) ?> + +
diff --git a/erp24/views/grade/admin-update.php b/erp24/views/grade/admin-update.php index 9ad62f9e..f1c6a3b3 100644 --- a/erp24/views/grade/admin-update.php +++ b/erp24/views/grade/admin-update.php @@ -48,9 +48,43 @@ use yii_app\services\FileService; } ?> - field($model, 'employee_position_id')->dropDownList( - ArrayHelper::map($positions, 'id', 'name'), ['prompt' => 'Выберите должность'] - )->label(false)) ?> + where(['name' => 'Работники магазинов'])->one(); + if ($workersGroup) { + $specialGroups[] = $workersGroup->id; + } + + $isSpecialGroup = in_array((int)$model->group_id, $specialGroups); + ?> + +
+ field($model, 'employee_position_id')->dropDownList( + ArrayHelper::map($positions, 'id', 'name'), ['prompt' => 'Выберите должность'] + )->label(false)) ?> + + 'Не выбрана', + 'день' => 'День', + 'ночь' => 'Ночь' + ], ['class' => 'form-control'])) ?> +
+ +
+ field($model, 'custom_position')->textInput([ + 'value' => $model->group_name + ])->label(false)) ?> +
field($model, 'work_rate')->dropDownList([1 => '5/2', 2 => '2/2', 3 => '3/3'])->label(false)) ?> @@ -235,5 +269,39 @@ use yii_app\services\FileService; } else { $('#workRate').hide(); } + + // Переключаем тип поля должности + changePositionFieldVisibility(t.value); + } + + function changePositionFieldVisibility(groupId) { + var specialGroups = [30, 35, 40, 45, 50, 72]; // Известные ID специальных групп + + // Можно добавить динамический поиск группы "Работники магазинов" через AJAX, но пока используем статический список + + var isSpecialGroup = specialGroups.includes(parseInt(groupId)); + + if (isSpecialGroup) { + $('#positionFieldSpecial').show(); + $('#positionFieldRegular').hide(); + } else { + $('#positionFieldSpecial').hide(); + $('#positionFieldRegular').show(); + // Очищаем поле employee_position_id для не-специальных групп + $('select[name="Admin[employee_position_id]"]').val(''); + } } + + // Инициализируем поле при загрузке страницы + $(document).ready(function() { + var initialGroupId = $('select[name="Admin[group_id]"]').val(); + changePositionFieldVisibility(initialGroupId); + + // Для не-специальных групп очищаем employee_position_id при загрузке + var specialGroups = [30, 35, 40, 45, 50, 72]; + var isSpecialGroup = specialGroups.includes(parseInt(initialGroupId)); + if (!isSpecialGroup) { + $('select[name="Admin[employee_position_id]"]').val(''); + } + }); \ No newline at end of file -- 2.39.5