From: vladfo Date: Thu, 12 Sep 2024 15:18:16 +0000 (+0300) Subject: добавил просмотр кустов и сопоставление с шефами X-Git-Tag: 1.5~4^2~10 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=2300e2155eb69e85e85a67a60e2115b3b7553344;p=erp24_rep%2Fyii-erp24%2F.git добавил просмотр кустов и сопоставление с шефами --- diff --git a/erp24/controllers/ClusterLinkEditController.php b/erp24/controllers/ClusterLinkEditController.php index 6fd51fad..ee37d362 100644 --- a/erp24/controllers/ClusterLinkEditController.php +++ b/erp24/controllers/ClusterLinkEditController.php @@ -15,6 +15,7 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii_app\records\PlanStore; +use yii_app\records\StoreDynamic; /** * ClusterController implements the CRUD actions for Cluster model. @@ -46,12 +47,83 @@ class ClusterLinkEditController extends Controller */ public function actionIndex() { + // Поиск моделей кустов $searchModel = new ClusterSearch(); $dataProvider = $searchModel->search($this->request->queryParams); + // Получаем кустовых директоров из таблицы Admin + $clusterManagers = Admin::find() + ->select(['name', 'id', 'store_arr']) + ->where(['group_id' => 7, 'group_name' => 'Кустовой директор']) // Условия для группы кустовых директоров + ->indexBy('id') // Индексируем по идентификатору + ->all(); + + // Данные по динамике магазинов, связанных с кустами + $storeData = StoreDynamic::find() + ->select([ + 'value_int AS cluster_id', + 'string_agg(store_id::text, \',\') AS stores', // Используем string_agg для PostgreSQL + 'COUNT(store_id) AS store_count', + 'MAX(date_from) AS last_update' + ]) + ->where(['active' => 1]) + ->groupBy('value_int') // Группируем по идентификатору куста + ->asArray() + ->all(); + + // Преобразуем данные о магазинах в удобный формат + $storeLists = ArrayHelper::map($storeData, 'cluster_id', 'stores'); // Список магазинов для каждого куста + + // Инициализируем массив соответствий кустов и менеджеров + $clusterToManager = []; + $matchedManagers = []; + + // Сопоставляем менеджеров и кусты по спискам магазинов + foreach ($storeData as $store) { + $clusterId = $store['cluster_id']; + $storeIds = explode(',', $store['stores']); // Магазины, прикрепленные к кусту + $assignedManager = null; + + foreach ($clusterManagers as $manager) { + if (!empty($manager->store_arr)) { + $managerStores = explode(',', $manager->store_arr); // Магазины, прикрепленные к менеджеру + $intersection = array_intersect($storeIds, $managerStores); // Находим пересечение + + // Если большинство магазинов совпадают, то менеджер сопоставляется с кустом + if (count($intersection) >= count($storeIds) / 2) { + $assignedManager = $manager->name; + $matchedManagers[] = $manager->id; // Добавляем менеджера в список использованных + break; // Останавливаем поиск для этого куста + } + } + } + + // Если менеджер не найден по магазину, оставляем его "не назначенным" + $clusterToManager[$clusterId] = $assignedManager ?? 'Не назначен'; + } + + // Проверка на оставшегося менеджера и куст + $unassignedClusters = array_filter($clusterToManager, fn($manager) => $manager === 'Не назначен'); // Кусты без менеджера + $unassignedManagers = array_filter($clusterManagers, fn($manager) => !in_array($manager->id, $matchedManagers)); // Менеджеры без кластера + + // Если остался один менеджер и один куст, их нужно сопоставить + if (count($unassignedClusters) === 1 && count($unassignedManagers) === 1) { + $unassignedClusterId = array_key_first($unassignedClusters); // ID куста без менеджера + $unassignedManager = reset($unassignedManagers); // Менеджер без куста + $clusterToManager[$unassignedClusterId] = $unassignedManager->name; // Назначаем последнего менеджера на куст + + + } + + // Передача данных в представление return $this->render('/cluster_link_edit/index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, + 'clusterManagers' => $clusterManagers, + 'storeCounts' => ArrayHelper::map($storeData, 'cluster_id', 'store_count'), + 'lastUpdates' => ArrayHelper::map($storeData, 'cluster_id', 'last_update'), + 'storeLists' => $storeLists, + 'clusterToManager' => $clusterToManager, // Передаем соответствия куста и менеджера ]); } @@ -120,7 +192,7 @@ class ClusterLinkEditController extends Controller */ public function actionViewAll($id) { - return $this->render('view', [ + return $this->render('/cluster_link_edit/view-all', [ 'model' => $this->findModel($id), ]); } diff --git a/erp24/views/cluster_link_edit/index.php b/erp24/views/cluster_link_edit/index.php index 20bc4ab4..03f0b7c8 100644 --- a/erp24/views/cluster_link_edit/index.php +++ b/erp24/views/cluster_link_edit/index.php @@ -1,14 +1,18 @@ title = 'Кусты'; $this->params['breadcrumbs'][] = $this->title; @@ -18,11 +22,10 @@ $this->params['breadcrumbs'][] = $this->title;

title) ?>

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

- render('_search', ['model' => $searchModel]); ?> $dataProvider, @@ -33,10 +36,36 @@ $this->params['breadcrumbs'][] = $this->title; 'id', 'name', [ - 'class' => ActionColumn::className(), - 'urlCreator' => function ($action, Cluster $model, $key, $index, $column) { - return Url::toRoute([$action, 'id' => $model->id]); - } + 'label' => 'Количество магазинов', + 'value' => function ($model) use ($storeCounts) { + // Отображаем количество магазинов из маппинга $storeCounts + return isset($storeCounts[$model->id]) ? $storeCounts[$model->id] : 0; + }, + ], + [ + 'label' => 'Шеф кустовой', + 'value' => function ($model) use ($clusterToManager) { + return $clusterToManager[$model->id] ?? 'Не назначен'; + }, + ], + [ + 'label' => 'Дата обновления', + 'value' => function ($model) use ($lastUpdates) { + // Отображаем дату последнего обновления из маппинга $lastUpdates + return isset($lastUpdates[$model->id]) ? date('d.m.Y', strtotime($lastUpdates[$model->id])) : 'Неизвестно'; + }, + ], + [ + 'class' => 'yii\grid\ActionColumn', + 'template' => '{view-all} {update}', + 'buttons' => [ + 'view-all' => function ($url, $model, $key) { + return Html::a('Просмотр', $url, ['class' => 'btn btn-primary']); + }, + 'update' => function ($url, $model, $key) { + return Html::a('Редактировать', $url, ['class' => 'btn btn-warning']); + }, + ], ], ], ]); ?> @@ -44,3 +73,7 @@ $this->params['breadcrumbs'][] = $this->title; + + \ No newline at end of file diff --git a/erp24/views/cluster_link_edit/view-all.php b/erp24/views/cluster_link_edit/view-all.php new file mode 100644 index 00000000..707ddd34 --- /dev/null +++ b/erp24/views/cluster_link_edit/view-all.php @@ -0,0 +1,38 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => 'Clusters', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ +

title) ?>

+ +

+ 'btn btn-primary']) ?> + $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', + ], + ]) ?> +

Просмотр куста 1

+
diff --git a/erp24/views/cluster_link_edit/view.php b/erp24/views/cluster_link_edit/view.php deleted file mode 100644 index f6dfe018..00000000 --- a/erp24/views/cluster_link_edit/view.php +++ /dev/null @@ -1,38 +0,0 @@ -title = $model->name; -$this->params['breadcrumbs'][] = ['label' => 'Clusters', 'url' => ['index']]; -$this->params['breadcrumbs'][] = $this->title; -\yii\web\YiiAsset::register($this); -?> -
- -

title) ?>

- -

- 'btn btn-primary']) ?> - $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', - ], - ]) ?> - -