]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
CRUD для групп
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 31 Oct 2025 06:07:40 +0000 (09:07 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 31 Oct 2025 06:07:40 +0000 (09:07 +0300)
erp24/actions/grade/AdminUpdateAction.php
erp24/controllers/AdminGroupController.php [new file with mode: 0644]
erp24/views/admin-group/_form.php [new file with mode: 0644]
erp24/views/admin-group/create.php [new file with mode: 0644]
erp24/views/admin-group/index.php [new file with mode: 0644]
erp24/views/admin-group/update.php [new file with mode: 0644]
erp24/views/admin-group/view.php [new file with mode: 0644]
erp24/views/grade/admin-update.php

index e81eefddfb0162c7fc6f67c15242f30699fffb73..cc4c8a1adfac8d3071e3662cd884084267ce48cf 100644 (file)
@@ -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 (file)
index 0000000..6f2af08
--- /dev/null
@@ -0,0 +1,174 @@
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use yii_app\records\AdminGroup;
+use yii\data\ActiveDataProvider;
+use yii\web\Controller;
+use yii\web\NotFoundHttpException;
+use yii\filters\VerbFilter;
+
+/**
+ * AdminGroupController implements the CRUD actions for AdminGroup model.
+ */
+class AdminGroupController extends Controller
+{
+    /**
+     * @inheritDoc
+     */
+    public function behaviors()
+    {
+        return array_merge(
+            parent::behaviors(),
+            [
+                'verbs' => [
+                    '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 (file)
index 0000000..9e73b3e
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\AdminGroup $model */
+/** @var yii\widgets\ActiveForm $form */
+?>
+
+<div class="admin-group-form">
+
+    <?php $form = ActiveForm::begin(); ?>
+
+    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
+
+    <?= $form->field($model, 'parent_id')->textInput() ?>
+
+    <?= $form->field($model, 'message')->textarea(['rows' => 6]) ?>
+
+    <?= $form->field($model, 'dostup')->textarea(['rows' => 6]) ?>
+
+    <?= $form->field($model, 'dostup_array')->textarea(['rows' => 6]) ?>
+
+    <?= $form->field($model, 'status_dostup_arr')->textarea(['rows' => 6]) ?>
+
+    <?= $form->field($model, 'status_arr')->textarea(['rows' => 6]) ?>
+
+    <?= $form->field($model, 'istochnik_dostup_all')->textInput() ?>
+
+    <?= $form->field($model, 'istochnik_dostup_arr')->textarea(['rows' => 6]) ?>
+
+    <?= $form->field($model, 'istochnik_id_default')->textInput() ?>
+
+    <?= $form->field($model, 'info_block')->textarea(['rows' => 6]) ?>
+
+    <?= $form->field($model, 'posit')->textInput() ?>
+
+    <?= $form->field($model, 'orders_dostup')->textInput(['maxlength' => true]) ?>
+
+    <?= $form->field($model, 'admin_group_add_arr')->textarea(['rows' => 6]) ?>
+
+    <div class="form-group">
+        <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
+    </div>
+
+    <?php ActiveForm::end(); ?>
+
+</div>
diff --git a/erp24/views/admin-group/create.php b/erp24/views/admin-group/create.php
new file mode 100644 (file)
index 0000000..70e747a
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+use yii\helpers\Html;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\AdminGroup $model */
+
+$this->title = 'Создать группу';
+$this->params['breadcrumbs'][] = ['label' => 'Admin Groups', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="admin-group-create p-4">
+
+    <h1><?= Html::encode($this->title) ?></h1>
+
+    <?= $this->render('_form', [
+        'model' => $model,
+    ]) ?>
+
+</div>
diff --git a/erp24/views/admin-group/index.php b/erp24/views/admin-group/index.php
new file mode 100644 (file)
index 0000000..b70932c
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+use yii_app\records\AdminGroup;
+use yii\helpers\Html;
+use yii\helpers\Url;
+use yii\grid\ActionColumn;
+use yii\grid\GridView;
+
+/** @var yii\web\View $this */
+/** @var yii\data\ActiveDataProvider $dataProvider */
+
+$this->title = 'Группы пользователей';
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="admin-group-index p-4">
+
+    <h1><?= Html::encode($this->title) ?></h1>
+
+    <p>
+        <?= Html::a('Создать новую группу', ['create'], ['class' => 'btn btn-success']) ?>
+    </p>
+
+
+    <?= GridView::widget([
+        'dataProvider' => $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]);
+                 }
+            ],
+        ],
+    ]); ?>
+
+
+</div>
diff --git a/erp24/views/admin-group/update.php b/erp24/views/admin-group/update.php
new file mode 100644 (file)
index 0000000..8354f9d
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+use yii\helpers\Html;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\AdminGroup $model */
+
+$this->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';
+?>
+<div class="admin-group-update p-4">
+
+    <h1><?= Html::encode($this->title) ?></h1>
+
+    <?= $this->render('_form', [
+        'model' => $model,
+    ]) ?>
+
+</div>
diff --git a/erp24/views/admin-group/view.php b/erp24/views/admin-group/view.php
new file mode 100644 (file)
index 0000000..6ceb4d8
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\DetailView;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\AdminGroup $model */
+
+$this->title = $model->name;
+$this->params['breadcrumbs'][] = ['label' => 'Admin Groups', 'url' => ['index']];
+$this->params['breadcrumbs'][] = $this->title;
+\yii\web\YiiAsset::register($this);
+?>
+<div class="admin-group-view p-4">
+
+    <h1><?= Html::encode($this->title) ?></h1>
+
+    <p>
+        <?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
+        <?= Html::a('Удалить', ['delete', 'id' => $model->id], [
+            'class' => 'btn btn-danger',
+            'data' => [
+                'confirm' => 'Are you sure you want to delete this item?',
+                'method' => 'post',
+            ],
+        ]) ?>
+    </p>
+
+    <?= DetailView::widget([
+        'model' => $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',
+        ],
+    ]) ?>
+
+</div>
index 9ad62f9eb08f0b8702bece7d10eca49acfbdaa62..f1c6a3b3659d4e7dba143e2c1734a9d73898754b 100644 (file)
@@ -48,9 +48,43 @@ use yii_app\services\FileService;
     }
     ?>
 
-    <?php PrintBlockHelper::printBlock('Должность', $form->field($model, 'employee_position_id')->dropDownList(
-        ArrayHelper::map($positions, 'id', 'name'), ['prompt' => 'Выберите должность']
-    )->label(false)) ?>
+    <?php
+    // Определяем специальные группы с parent_id = 50
+    $specialGroups = [
+        \yii_app\records\AdminGroup::GROUP_FLORIST_DAY,      // 30
+        \yii_app\records\AdminGroup::GROUP_FLORIST_NIGHT,    // 35
+        \yii_app\records\AdminGroup::GROUP_FLORIST_SUPPORT_DAY, // 40
+        \yii_app\records\AdminGroup::GROUP_WORKERS,          // 45
+        \yii_app\records\AdminGroup::GROUP_ADMINISTRATORS,   // 50
+        \yii_app\records\AdminGroup::GROUP_FLORIST_SUPPORT_NIGHT, // 72
+    ];
+
+    // Ищем группу "Работники магазинов" по имени
+    $workersGroup = \yii_app\records\AdminGroup::find()->where(['name' => 'Работники магазинов'])->one();
+    if ($workersGroup) {
+        $specialGroups[] = $workersGroup->id;
+    }
+
+    $isSpecialGroup = in_array((int)$model->group_id, $specialGroups);
+    ?>
+
+    <div id="positionFieldSpecial" style="display: <?= $isSpecialGroup ? 'block' : 'none' ?>;">
+        <?php PrintBlockHelper::printBlock('Должность', $form->field($model, 'employee_position_id')->dropDownList(
+            ArrayHelper::map($positions, 'id', 'name'), ['prompt' => 'Выберите должность']
+        )->label(false)) ?>
+
+        <?php PrintBlockHelper::printBlock('Смена', Html::dropDownList('shift', '', [
+            '' => 'Не выбрана',
+            'день' => 'День',
+            'ночь' => 'Ночь'
+        ], ['class' => 'form-control'])) ?>
+    </div>
+
+    <div id="positionFieldRegular" style="display: <?= !$isSpecialGroup ? 'block' : 'none' ?>;">
+        <?php PrintBlockHelper::printBlock('Должность', $form->field($model, 'custom_position')->textInput([
+            'value' => $model->group_name
+        ])->label(false)) ?>
+    </div>
 
     <div id="workRate" style="display: <?= $model->group_id == \yii_app\records\AdminGroup::GROUP_ADMINISTRATORS ? 'block' : 'none'?>">
         <?php PrintBlockHelper::printBlock('Рабочий график', $form->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('');
+        }
+    });
 </script>
\ No newline at end of file