From 88a8a0c6d2a4f0171f263b9c12d5983c2439afc3 Mon Sep 17 00:00:00 2001 From: marina Date: Tue, 28 Jan 2025 13:20:34 +0300 Subject: [PATCH] =?utf8?q?ERP-282=20=D0=9D=D1=83=D0=B6=D0=BD=D0=BE=20?= =?utf8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D0=B8=D0=BD=D1=82?= =?utf8?q?=D0=B5=D1=80=D1=84=D0=B5=D0=B9=D1=81=20=D0=BD=D0=B0=D1=81=D1=82?= =?utf8?q?=D1=80=D0=BE=D0=B9=D0=BA=D0=B8=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7?= =?utf8?q?=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../controllers/CityStoreParamsController.php | 146 +++++++++++------- ...82745_drop_column_on_city_store_params.php | 27 ++++ erp24/records/CityStoreParams.php | 4 +- erp24/views/city-store-params/index.php | 9 +- .../js/city-store-params/city-store-params.js | 9 ++ 5 files changed, 133 insertions(+), 62 deletions(-) create mode 100644 erp24/migrations/m250128_082745_drop_column_on_city_store_params.php diff --git a/erp24/controllers/CityStoreParamsController.php b/erp24/controllers/CityStoreParamsController.php index 76691b91..70a9c23e 100644 --- a/erp24/controllers/CityStoreParamsController.php +++ b/erp24/controllers/CityStoreParamsController.php @@ -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 index 00000000..892fc0c6 --- /dev/null +++ b/erp24/migrations/m250128_082745_drop_column_on_city_store_params.php @@ -0,0 +1,27 @@ +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('Кустовой шеф-флорист')); + } +} diff --git a/erp24/records/CityStoreParams.php b/erp24/records/CityStoreParams.php index adc9150f..231c26d4 100644 --- a/erp24/records/CityStoreParams.php +++ b/erp24/records/CityStoreParams.php @@ -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} */ diff --git a/erp24/views/city-store-params/index.php b/erp24/views/city-store-params/index.php index f3cd0a03..28f06598 100644 --- a/erp24/views/city-store-params/index.php +++ b/erp24/views/city-store-params/index.php @@ -144,13 +144,18 @@ $this->registerJsFile('/js/city-store-params/city-store-params.js', ['position'
-
+
session->hasFlash($flash)): ?> session->getFlash($flash), [ 'class' => 'alert text-center flash-message mb-1 py-1 alert-' . Html::encode($flash), ]) ?>
+
+
+ 'btn btn-secondary w-100', 'id' => 'reset-button']); ?> +
+
'btn btn-primary w-100', 'id' => 'apply-button']); ?> @@ -283,7 +288,7 @@ $this->registerJsFile('/js/city-store-params/city-store-params.js', ['position' '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', diff --git a/erp24/web/js/city-store-params/city-store-params.js b/erp24/web/js/city-store-params/city-store-params.js index 778c08ef..a2dedb4d 100644 --- a/erp24/web/js/city-store-params/city-store-params.js +++ b/erp24/web/js/city-store-params/city-store-params.js @@ -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 -- 2.39.5