From 1751c822c2457e297ec9e2678282666e2aabd553 Mon Sep 17 00:00:00 2001 From: vladfo Date: Thu, 17 Oct 2024 17:50:14 +0300 Subject: [PATCH] =?utf8?q?=20=D0=A7=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20cluste?= =?utf8?q?r=5Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/StoreController.php | 39 +++++++++++++++++++++++++-- erp24/records/CityStore.php | 3 +-- erp24/views/store/index.php | 11 +++++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/erp24/controllers/StoreController.php b/erp24/controllers/StoreController.php index 71cfe336..b9e8d900 100755 --- a/erp24/controllers/StoreController.php +++ b/erp24/controllers/StoreController.php @@ -3,8 +3,11 @@ namespace app\controllers; use yii\data\ActiveDataProvider; +use yii\helpers\ArrayHelper; use yii\web\Controller; use yii_app\records\CityStore; +use yii_app\records\Cluster; +use yii_app\records\StoreDynamic; class StoreController extends Controller { @@ -12,11 +15,41 @@ class StoreController extends Controller public function actionIndex() { - // Создаем DataProvider, чтобы получить магазины, у которых visible = 1 + + $clusters = Cluster::find() + ->select(['id', 'name']) + ->indexBy('id') + ->asArray() + ->all(); + + + $clusterMap = ArrayHelper::map($clusters, 'id', function ($cluster) { + return $cluster['name'] . ' (' . $cluster['id'] . ')'; + }); + + + $storeData = StoreDynamic::find() + ->select(['value_int AS cluster_id', "string_agg(store_id::text, ',') AS stores"]) + ->where(['active' => 1]) + ->groupBy('value_int') + ->asArray() + ->all(); + + + $storeClusterMap = []; + foreach ($storeData as $storeItem) { + $clusterId = $storeItem['cluster_id']; + $storeIds = explode(',', $storeItem['stores']); + foreach ($storeIds as $storeId) { + $storeClusterMap[$storeId] = $clusterId; // Магазин -> Кластер + } + } + + $dataProvider = new ActiveDataProvider([ 'query' => CityStore::find() ->where(['visible' => 1]) - ->with(['city', 'cluster', 'administrator']), // Загружаем связанные данные + ->with(['city', 'administrator']), 'pagination' => [ 'pageSize' => 20, ], @@ -24,6 +57,8 @@ class StoreController extends Controller return $this->render('index', [ 'dataProvider' => $dataProvider, + 'storeClusterMap' => $storeClusterMap, + 'clusterMap' => $clusterMap, ]); } public function actionItemmatrix() { return $this->render('itemmatrix'); } diff --git a/erp24/records/CityStore.php b/erp24/records/CityStore.php index b57b9086..34bdb099 100755 --- a/erp24/records/CityStore.php +++ b/erp24/records/CityStore.php @@ -330,7 +330,7 @@ class CityStore extends ActiveRecord public function getStoreGuid(): ActiveQuery { - $query = $this->hasOne(ExportImportTable::className(), ['entity_id' => 'id'])->onCondition(['export_id' => 1, 'entity' => 'city_store']); + $query = $this->hasOne(ExportImportTable::class, ['entity_id' => 'id'])->onCondition(['export_id' => 1, 'entity' => 'city_store']); return $query; } @@ -341,7 +341,6 @@ class CityStore extends ActiveRecord } - public function getAdministrator() { return $this->hasOne(Admin::class, ['id' => 'administrator_id']); diff --git a/erp24/views/store/index.php b/erp24/views/store/index.php index cc2e939f..b10bbb07 100644 --- a/erp24/views/store/index.php +++ b/erp24/views/store/index.php @@ -5,6 +5,8 @@ use yii\grid\GridView; /** @var yii\web\View $this */ /** @var yii\data\ActiveDataProvider $dataProvider */ +/* @var $storeClusterMap array */ +/* @var $clusterMap array */ $this->title = 'Список магазинов'; $this->params['breadcrumbs'][] = $this->title; @@ -34,10 +36,11 @@ $this->params['breadcrumbs'][] = $this->title; }, ], [ - 'attribute' => 'cluster.name', - 'label' => 'Куст', - 'value' => function ($model) { - return $model->cluster ? $model->cluster->name : '(Не указан)'; + 'attribute' => 'id', + 'label' => 'Кластер', + 'value' => function ($model) use ($storeClusterMap, $clusterMap) { + $clusterId = $storeClusterMap[$model->id] ?? null; // Получаем cluster_id для магазина + return $clusterId ? ($clusterMap[$clusterId] ?? 'Не назначен') : 'Не назначен'; }, ], [ -- 2.39.5