]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-282 Нужно сделать интерфейс настройки магазина
authormarina <m.zozirova@gmail.com>
Tue, 28 Jan 2025 10:20:34 +0000 (13:20 +0300)
committermarina <m.zozirova@gmail.com>
Tue, 28 Jan 2025 10:20:34 +0000 (13:20 +0300)
erp24/controllers/CityStoreParamsController.php
erp24/migrations/m250128_082745_drop_column_on_city_store_params.php [new file with mode: 0644]
erp24/records/CityStoreParams.php
erp24/views/city-store-params/index.php
erp24/web/js/city-store-params/city-store-params.js

index 76691b910e91f7a21311d7df5028741b9457faca..70a9c23e83357d553f68e9f4c2b5449337bbb45c 100644 (file)
@@ -59,27 +59,32 @@ class CityStoreParamsController extends Controller
     {
         $flash = 'info';
         $value = 'Для редактирования необходимо выбрать магазин!';
-        if (Yii::$app->request->post()) {
-            $model = !empty(Yii::$app->request->post()['CityStoreParams']['id'])
-                ? $this->findModel(Yii::$app->request->post()['CityStoreParams']['id'])
-                : new CityStoreParams();
 
-            if ($model->load(Yii::$app->request->post())) {
-                $model->matrix_type = is_array($model->matrix_type) ? implode(',', $model->matrix_type) : $model->matrix_type;
-            }
+        if (Yii::$app->request->isPost) {
+            $postData = Yii::$app->request->post()['CityStoreParams'];
+            if ($postData) {
+                if (isset($postData['bush_chef_florist']) && $postData['bush_chef_florist']) {
+                    $this->updateStoreDynamic($postData['store_id'], 'bush_chef_florist', $postData['bush_chef_florist']);
+                }
+
+                if (isset($postData['territorial_manager']) && $postData['territorial_manager']) {
+                    $this->updateStoreDynamic($postData['store_id'], 'territorial_manager', $postData['territorial_manager']);
+                }
+
+                $model = !empty($postData['id']) ? $this->findModel($postData['id']) : new CityStoreParams();
+
+                if ($model->load(Yii::$app->request->post())) {
+                    $model->matrix_type = is_array($model->matrix_type) ? implode(',', $model->matrix_type) : $model->matrix_type;
+                }
 
-            if ($model->validate()) {
-                if ($model->save()) {
+                if ($model->validate() && $model->save()) {
                     $flash = 'success';
                     $value = 'Данные успешно сохранены';
                     $selected_store = $model->store_id;
+                } else {
+                    $flash = 'danger';
+                    $value = 'Ошибка при сохранении: ' . implode('. ', array_map(fn($error) => is_array($error) ? implode(', ', $error) : $error, $model->getErrors()));
                 }
-            } else {
-                $flash = 'danger';
-                $value = 'Ошибка при сохранении: ' . implode('. ', array_map(function ($error) {
-                        return is_array($error) ? implode(', ', $error) : $error;
-                    }, $model->getErrors()));
-
             }
         }
 
@@ -91,6 +96,38 @@ class CityStoreParamsController extends Controller
         ]);
     }
 
+    /**
+     * Обновление StoreDynamic для bush_chef_florist и territorial_manager
+     */
+    private function updateStoreDynamic($storeId, $field, $value)
+    {
+        $category = null;
+        if ($field === 'bush_chef_florist') {
+            $category = 2;
+        } else if ($field === 'territorial_manager') {
+            $category = 3;
+        }
+
+        StoreDynamic::updateAll(
+            ['active' => 0, 'date_to' => date('Y-m-d H:i:s')],
+            ['active' => 1, 'category' => $category, 'store_id' => $storeId]
+        );
+
+        $model = new StoreDynamic([
+            'store_id' => $storeId,
+            'value_type' => 'int',
+            'value_int' => $value,
+            'date_from' => date('Y-m-d H:i:s'),
+            'date_to' => '2100-01-01 00:00:00',
+            'active' => 1,
+            'category' => $category,
+        ]);
+
+        if (!$model->save()) {
+            var_dump($model->getErrors());
+        }
+    }
+
     /**
      * Находит модель по первичному ключу
      * @param int $id
@@ -111,50 +148,39 @@ class CityStoreParamsController extends Controller
         Yii::$app->response->format = Response::FORMAT_JSON;
 
         $data = Yii::$app->request->post();
-        $addressCity = $data['address_city'] ?? '';
-        $addressRegion = $data['address_region'] ?? '';
-        $addressDistrict = $data['address_district'] ?? '';
-        $storeType = $data['store_type'] ?? '';
-        $territorialManager = $data['territorial_manager'] ?? '';
-        $bushChefFlorist = $data['bush_chef_florist'] ?? '';
-
-        $query = StoreDynamic::find()->andFilterWhere(['active' => 1]);
-
-        if ($territorialManager) {
-            $query->andFilterWhere([
-                'category' => 3,
-                'value_int' => ClusterAdmin::find()
-                    ->andFilterWhere(['admin_id' => $territorialManager, 'active' => 1])
-                    ->select('cluster_id')
-                    ->column()
-            ]);
+
+        $territorialManager = $data['territorial_manager'] ?? null;
+        $bushChefFlorist = $data['bush_chef_florist'] ?? null;
+
+        $query = CityStore::find()->andWhere(['visible' => CityStore::IS_VISIBLE])
+            ->andFilterWhere(['city' => $data['address_city']])
+            ->andFilterWhere(['region' => $data['address_region']])
+            ->andFilterWhere(['district' => $data['address_district']])
+            ->andFilterWhere(['type' => $data['store_type']]);
+
+        if (!empty($territorialManager)) {
+            $territorialManagerStoreIds = StoreDynamic::find()
+                ->select('store_id')
+                ->where(['category' => 3, 'active' => 1, 'value_int' => $territorialManager])
+                ->column();
+
+            $query->andWhere(['in', 'id', $territorialManagerStoreIds ?: [-1]]);
         }
 
-        if ($bushChefFlorist) {
-            $query->andWhere(['category' => 2, 'value_int' => $bushChefFlorist]);
+        if (!empty($bushChefFlorist)) {
+            $bushChefFloristStoreIds = StoreDynamic::find()
+                ->select('store_id')
+                ->where(['category' => 2, 'active' => 1, 'value_int' => $bushChefFlorist])
+                ->column();
+
+            $query->andWhere(['in', 'id', $bushChefFloristStoreIds ?: [-1]]);
         }
 
-        $territorialManagerStoreIds = $query->select('store_id')->column();
-        $bushChefFloristStoreIds = $query->select('store_id')->column();
-
-        $stores = CityStore::find()
-            ->andFilterWhere([
-                'visible' => CityStore::IS_VISIBLE,
-                'city' => $addressCity,
-                'region' => $addressRegion,
-                'district' => $addressDistrict,
-                'type' => $storeType,
-            ])
-            ->andFilterWhere(['in', 'id', $bushChefFloristStoreIds])
-            ->andFilterWhere(['in', 'id', $territorialManagerStoreIds])
-            ->orderBy('id')
-            ->all();
+        $stores = $query->orderBy('id')->all();
 
         return [
             'success' => true,
-            'data' => ArrayHelper::map($stores, 'id', function ($store) {
-                return $store->id . ' ' . $store->name;
-            }),
+            'data' => ArrayHelper::map($stores, 'id', fn($store) => $store->id . ' ' . $store->name),
         ];
     }
 
@@ -178,17 +204,21 @@ class CityStoreParamsController extends Controller
                 'region' => $params->address_region ?? $store->region ?? null,
                 'city' => $params->address_city ?? $store->city ?? null,
                 'district' => $params->address_district ?? $store->district ?? null,
-                'territorialManager' => $params->territorial_manager ?? StoreDynamic::findOne(['active' => 1, 'category' => 3])->value_int ?? null,
-                'bushChefFloristId' => $params->bush_chef_florist ?? StoreDynamic::findOne(['store_id' => $storeId, 'active' => 1, 'category' => 2])->value_int ?? null,
+                'territorialManager' => StoreDynamic::findOne(['active' => 1, 'category' => 3, 'store_id' => $storeId])->value_int ?? null,
+                'bushChefFloristId' => StoreDynamic::findOne(['store_id' => $storeId, 'active' => 1, 'category' => 2])->value_int ?? null,
                 'storeArea' => $params->store_area ?? null,
                 'showcaseVolume' => $params->showcase_volume ?? null,
                 'freezeArea' => $params->freeze_area ?? null,
                 'freezeVolume' => $params->freeze_volume ?? null,
                 'matrixType' => !empty($params->matrix_type) ? explode(',', $params->matrix_type) : null,
-                'territorialManagerArray' => array_unique(array_merge(
-                    ArrayHelper::map(Admin::findAll(['group_id' => AdminGroup::GROUP_BUSH_DIRECTOR]), 'id', 'name'),
-                    ArrayHelper::map(Admin::findAll(['id' => StoreDynamic::find()->andWhere(['category' => 3, 'active' => 1])->select('value_int')->column()]), 'id', 'name')
-                ), SORT_REGULAR),
+                'territorialManagerArray' => ArrayHelper::map(
+                    array_merge(
+                        Admin::findAll(['group_id' => AdminGroup::GROUP_BUSH_DIRECTOR]),
+                        Admin::findAll(['id' => StoreDynamic::find()->andWhere(['category' => 3, 'active' => 1])->select('value_int')->column()])
+                    ),
+                    'id',
+                    'name',
+                ),
                 'storeTypeArray' => ArrayHelper::map(StoreType::find()->all(), 'id', 'name'),
                 'regionArray' => ArrayHelper::map(StoreCityList::findAll(['type' => StoreCityList::TYPE_REGION]), 'id', 'name'),
                 'cityArray' => ArrayHelper::map(StoreCityList::findAll(['type' => StoreCityList::TYPE_CITY]), 'id', 'name'),
diff --git a/erp24/migrations/m250128_082745_drop_column_on_city_store_params.php b/erp24/migrations/m250128_082745_drop_column_on_city_store_params.php
new file mode 100644 (file)
index 0000000..892fc0c
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m250128_082745_drop_column_on_city_store_params
+ */
+class m250128_082745_drop_column_on_city_store_params extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->dropColumn('city_store_params', 'territorial_manager');
+        $this->dropColumn('city_store_params', 'bush_chef_florist');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+       $this->addColumn('city_store_params', 'territorial_manager',  $this->integer()->comment('Территориально управляющий'));
+       $this->addColumn('city_store_params', 'bush_chef_florist',  $this->integer()->comment('Кустовой шеф-флорист'));
+    }
+}
index adc9150f173fc07a49aaa5163f9461917f942207..231c26d4570be0a665e20d62434901473105a6e9 100644 (file)
@@ -18,8 +18,6 @@ use yii\filters\VerbFilter;
  * @property string|null $address_city
  * @property string|null $address_region
  * @property string|null $address_district
- * @property int|null $territorial_manager
- * @property int|null $bush_chef_florist
  * @property float|null $store_area
  * @property float|null $showcase_volume
  * @property float|null $freeze_area
@@ -32,6 +30,8 @@ use yii\filters\VerbFilter;
  */
 class CityStoreParams extends ActiveRecord
 {
+    public $bush_chef_florist;
+    public $territorial_manager;
     /**
      * {@inheritdoc}
      */
index f3cd0a03e75b0d5dc9978ee5bd79524459e7a8b3..28f06598d1b0eb0dac4a539dab4d29814b74d6fc 100644 (file)
@@ -144,13 +144,18 @@ $this->registerJsFile('/js/city-store-params/city-store-params.js', ['position'
         </div>
     </div>
     <div class="row mt-3">
-        <div class="col-md-10">
+        <div class="col-md-8">
             <?php if (Yii::$app->session->hasFlash($flash)): ?>
                 <?= Html::tag('div', Yii::$app->session->getFlash($flash), [
                     'class' => 'alert text-center flash-message mb-1 py-1 alert-' . Html::encode($flash),
                 ]) ?>
             <?php endif; ?>
         </div>
+        <div class="col-md-2 d-flex justify-content-center align-items-center">
+            <div class="card bg-light shadow-sm w-100">
+                <?= Html::button('Сбросить фильтры', ['class' => 'btn btn-secondary w-100', 'id' => 'reset-button']); ?>
+            </div>
+        </div>
         <div class="col-md-2 d-flex justify-content-center align-items-center">
             <div class="card p-45 bg-light shadow-sm w-100">
                 <?= Html::button('Применить', ['class' => 'btn btn-primary w-100', 'id' => 'apply-button']); ?>
@@ -283,7 +288,7 @@ $this->registerJsFile('/js/city-store-params/city-store-params.js', ['position'
                         <?=  Select2::widget([
                             'name' => 'CityStoreParams[bush_chef_florist]',
                             'value' => null,
-                            'data' =>  ArrayHelper::map(Admin::findAll(['group_id' => AdminGroup::GROUP_BUSH_DIRECTOR]), 'id', 'name_full'),
+                            'data' =>  [],
                             'options' => [
                                 'placeholder' => 'Кустовой шеф-флорист',
                                 'class' => 'form-select',
index 778c08ef2f43e791520463c35610edf597f285c0..a2dedb4d850263bb602a15bc6853d03acb05766f 100644 (file)
@@ -142,4 +142,13 @@ $(document).ready(function () {
             });
         }, 15000);
     }
+});
+
+$('#reset-button').on('click', function() {
+    $('#address_region').val(null).trigger('change');
+    $('#address_city').val(null).trigger('change');
+    $('#address_district').val(null).trigger('change');
+    $('#store_type').val(null).trigger('change');
+    $('#territorial_manager').val(null).trigger('change');
+    $('#bush_chef_florist').val(null).trigger('change');
 });
\ No newline at end of file