From: fomichev Date: Thu, 24 Oct 2024 19:11:05 +0000 (+0300) Subject: Перенос кустовых X-Git-Tag: 1.7~228^2~24 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=75b937c6e1770b7e2b50337f4228301624ebb6f7;p=erp24_rep%2Fyii-erp24%2F.git Перенос кустовых --- diff --git a/erp24/controllers/crud/ClusterAdminController.php b/erp24/controllers/crud/ClusterAdminController.php index fbe70593..fd6b62d0 100644 --- a/erp24/controllers/crud/ClusterAdminController.php +++ b/erp24/controllers/crud/ClusterAdminController.php @@ -52,6 +52,12 @@ class ClusterAdminController extends Controller ->asArray() ->all(); + // Получаем список администраторов + $admins = ArrayHelper::map(Admin::find() + ->select(['id', 'name']) + ->where(['IN', 'group_id', [7]]) + ->all(), 'id', 'name'); + // Создаем массив для сопоставления кустов и их статуса $clusterMapping = []; foreach ($clusters as $cluster) { @@ -71,13 +77,13 @@ class ClusterAdminController extends Controller if ($adminRecord['active'] == 1) { $clusterMapping[$cluster['id']]['admin'] = $adminRecord['admin']['name'] . ' (' . $adminRecord['admin_id'] . ')'; $clusterMapping[$cluster['id']]['status'] = 'Активная запись'; + $clusterMapping[$cluster['id']]['admin_id'] = $adminRecord['admin_id']; // Добавляем admin_id $clusterMapping[$cluster['id']]['hasActive'] = true; break; } } } - if (!$clusterMapping[$cluster['id']]['hasActive'] && $clusterMapping[$cluster['id']]['hasAny']) { $clusterMapping[$cluster['id']]['status'] = 'Нет активных записей'; } @@ -85,9 +91,64 @@ class ClusterAdminController extends Controller return $this->render('index', [ 'clusterMapping' => $clusterMapping, + 'clustersList' => $clusters, + 'admins' => $admins, // Передаем список администраторов ]); } + public function actionMoveAdmin() + { + $newAdminId = Yii::$app->request->post('admin_id'); + $clusterId = Yii::$app->request->post('cluster_id'); + $currentDate = date('Y-m-d H:i:s'); + + $currentClusterAdmin = ClusterAdmin::find() + ->where(['cluster_id' => $clusterId, 'active' => 1]) + ->one(); + + $oldClusterAdmin = ClusterAdmin::find() + ->where(['admin_id' => $newAdminId, 'active' => 1]) + ->one(); + + if ($currentClusterAdmin) { + // Завершаем старую запись + $currentClusterAdmin->active = 0; + $currentClusterAdmin->date_end = $currentDate; + + if (!$currentClusterAdmin->save(false)) { + return $this->asJson(['success' => false, 'message' => 'Администратор не найден или уже не активен.']); + } + ClusterManagerService::clearClusterManagerStores($currentClusterAdmin->cluster_id, $currentClusterAdmin->admin_id); + } + if ($oldClusterAdmin) { + // Завершаем старую запись + $oldClusterAdmin->active = 0; + $oldClusterAdmin->date_end = $currentDate; + + if (!$oldClusterAdmin->save(false)) { + return $this->asJson(['success' => false, 'message' => 'Администратор не найден или уже не активен.']); + } + ClusterManagerService::clearClusterManagerStores($oldClusterAdmin->cluster_id, $oldClusterAdmin->admin_id); + } + + + // Создаем новую запись + $newClusterAdmin = new ClusterAdmin(); + $newClusterAdmin->admin_id = $newAdminId; + $newClusterAdmin->cluster_id = $clusterId; + $newClusterAdmin->date_start = $currentDate; + $newClusterAdmin->active = 1; + + + + if (!$newClusterAdmin->save(false)) { + return $this->asJson(['success' => false, 'message' => 'Администратор не найден или уже не активен.']); + } + ClusterManagerService::syncClusterManagers($newClusterAdmin->cluster_id, $newClusterAdmin->admin_id); + return $this->asJson(['success' => true]); + + } + /** * Displays a single ClusterAdmin model. * @param int $id ID записи @@ -268,9 +329,9 @@ 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]) @@ -304,12 +365,12 @@ class ClusterAdminController extends Controller if ($model->active == 0) { - $existingInactiveRecords = ClusterAdmin::find() + $existingRecords = ClusterAdmin::find() ->where(['admin_id' => $model->admin_id,]) ->andWhere(['!=', 'id', $model->id]) ->all(); - foreach ($existingInactiveRecords as $record) { + foreach ($existingRecords as $record) { $modelStart = strtotime($model->date_start); $modelEnd = strtotime($model->date_end); $recordStart = strtotime($record->date_start); diff --git a/erp24/views/crud/cluster-admin/index.php b/erp24/views/crud/cluster-admin/index.php index aacb358a..64b6aad5 100644 --- a/erp24/views/crud/cluster-admin/index.php +++ b/erp24/views/crud/cluster-admin/index.php @@ -1,65 +1,124 @@ title = 'Привязка Куст-Кустовой'; -$this->params['breadcrumbs'][] = $this->title; ?> -
-

title) ?>

+
- new \yii\data\ArrayDataProvider([ - 'allModels' => $clusterMapping, - 'pagination' => [ - 'pageSize' => 20, - ], - ]), - 'columns' => [ - [ - 'attribute' => 'name', - 'label' => 'Куст', - 'value' => function ($model) { - return Html::encode($model['name']); - }, - 'format' => 'raw', - ], - [ - 'attribute' => 'admin', - 'label' => 'Кустовой', - 'value' => function ($model) { - if ($model['status'] === 'Нет записей') { - return Html::tag('span', 'Нет записей', ['style' => 'color: red;']); - } elseif ($model['status'] === 'Нет активных записей') { - return Html::tag('span', 'Нет активных записей', ['style' => 'color: orange;']); - } else { - return Html::encode($model['admin']); - } - }, - 'format' => 'raw', - ], - [ - 'class' => 'yii\grid\ActionColumn', - 'template' => '{view} {create}', - 'buttons' => [ - 'view' => function ($url, $model, $key) { - return Html::a('Просмотреть', ['view', 'id' => $key], ['class' => 'btn btn-primary']); +

title) ?>

+ + new \yii\data\ArrayDataProvider([ + 'allModels' => $clusterMapping, + 'pagination' => [ + 'pageSize' => 20, + ], + ]), + 'columns' => [ + [ + 'attribute' => 'name', + 'label' => 'Куст', + 'value' => function ($model) { + return Html::encode($model['name']); }, - 'create' => function ($url, $model, $key) { - // Показываем кнопку только если нет записей для кластера + 'format' => 'raw', + ], + [ + 'attribute' => 'admin', + 'label' => 'Кустовой', + 'value' => function ($model) { if ($model['status'] === 'Нет записей') { - return Html::a('Создать новую привязку', ['create', 'cluster_id' => $key], ['class' => 'btn btn-success']); + return Html::tag('span', 'Нет записей', ['style' => 'color: red;']); + } elseif ($model['status'] === 'Нет активных записей') { + return Html::tag('span', 'Нет активных записей', ['style' => 'color: orange;']); + } else { + return Html::encode($model['admin']); + } + }, + 'format' => 'raw', + ], + [ + 'attribute' => 'transfer', + 'label' => 'Назначить кустового', + 'format' => 'raw', + 'value' => function ($model, $key) use ($admins) { + // Проверяем, существует ли admin_id в массиве + $adminId = isset($model['admin_id']) ? $model['admin_id'] : null; + + // Если есть привязка, показываем имя, иначе показываем Select2 + if ($model['status'] !== 'Нет записей') { + return Select2::widget([ + 'name' => 'admin_id_' . ($adminId ?? 'new'), // Если admin_id не задан, используем 'new' + 'data' => $admins, // Список администраторов + 'value' => $adminId, // Текущий привязанный администратор или null + 'options' => [ + 'placeholder' => 'Выберите администратора...', + 'class' => 'form-control transfer-select', + 'data-id' => $key, + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]); } return null; }, ], + [ + 'class' => 'yii\grid\ActionColumn', + 'template' => '{view} {create}', + 'buttons' => [ + 'view' => function ($url, $model, $key) { + return Html::a('Просмотреть', ['view', 'id' => $key], ['class' => 'btn btn-primary']); + }, + 'create' => function ($url, $model, $key) { + // Показываем кнопку только если нет записей для кластера + if ($model['status'] === 'Нет записей') { + return Html::a('Создать новую привязку', ['create', 'cluster_id' => $key], ['class' => 'btn btn-success']); + } + return null; + }, + ], + ], ], - ], - ]); ?> + ]); ?> + +
+ +registerJs(" + $('.transfer-select').on('change', function () { + let adminId = $(this).val(); + let clusterId = $(this).data('id'); -
\ No newline at end of file + if (adminId) { + $.post({ + url: 'move-admin', // Укажите правильный маршрут для назначения администратора + data: { admin_id: adminId, cluster_id: clusterId }, + success: function(response) { + if (response.success) { + alert('Администратор успешно назначен'); + window.location.reload(); + } else { + alert('Ошибка при назначении администратора'); + } + }, + error: function() { + alert('Ошибка при запросе'); + } + }); + } +}); +"); +?> \ No newline at end of file