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
{
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,
],
return $this->render('index', [
'dataProvider' => $dataProvider,
+ 'storeClusterMap' => $storeClusterMap,
+ 'clusterMap' => $clusterMap,
]);
}
public function actionItemmatrix() { return $this->render('itemmatrix'); }
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;
}
}
-
public function getAdministrator()
{
return $this->hasOne(Admin::class, ['id' => 'administrator_id']);
/** @var yii\web\View $this */
/** @var yii\data\ActiveDataProvider $dataProvider */
+/* @var $storeClusterMap array */
+/* @var $clusterMap array */
$this->title = 'Список магазинов';
$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] ?? 'Не назначен') : 'Не назначен';
},
],
[