From: fomichev Date: Thu, 5 Jun 2025 09:57:14 +0000 (+0300) Subject: Управление порядком X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=8397dd970f9bf8a12da33ba46e21f19a501e0fff;p=erp24_rep%2Fyii-erp24%2F.git Управление порядком --- diff --git a/erp24/controllers/crud/MarketplaceOrder1cStatusesController.php b/erp24/controllers/crud/MarketplaceOrder1cStatusesController.php index 8696b4b0..e6908e4e 100644 --- a/erp24/controllers/crud/MarketplaceOrder1cStatusesController.php +++ b/erp24/controllers/crud/MarketplaceOrder1cStatusesController.php @@ -95,18 +95,22 @@ class MarketplaceOrder1cStatusesController extends Controller $model = new MarketplaceOrder1cStatuses(); $relationModels = [new MarketplaceOrder1cStatusesRelations()]; - - $statusOptions = MarketplaceOrder1cStatuses::find() - ->select(['status']) - ->indexBy('id') - ->column(); + $allStatuses = MarketplaceOrder1cStatuses::find() + ->select(['id','status','marketplace_id']) + ->asArray() + ->all(); + $statusOptionsByMarketplace = []; + foreach ($allStatuses as $row) { + $mp = (int)$row['marketplace_id']; + $statusOptionsByMarketplace[$mp][$row['id']] = $row['status']; + } if (Yii::$app->request->isPost) { if ($model->load(Yii::$app->request->post())) { $transaction = Yii::$app->db->beginTransaction(Transaction::SERIALIZABLE); try { if (!$model->save()) { - throw new \Exception('Не удалось сохранить MarketplaceOrder1cStatuses'); + throw new \Exception('Не удалось сохранить основной статус'); } $postRelations = Yii::$app->request->post('Relations', []); @@ -118,12 +122,15 @@ class MarketplaceOrder1cStatusesController extends Controller $rel = new MarketplaceOrder1cStatusesRelations(); $rel->status_id_from = $model->id; - $rel->status_id_to = $relData['status_id_to']; - $rel->description = $relData['description']; - $rel->button_text = $relData['button_text']; + $rel->status_id_to = $relData['status_id_to']; + $rel->description = $relData['description']; + $rel->button_text = $relData['button_text']; + $rel->order = (int)$relData['order']; if (!$rel->save()) { - throw new \Exception('Не удалось сохранить Relation #' . $i); + $errors = $rel->getFirstErrors(); + $message = implode("; ", $errors); + throw new \Exception("Ошибка при сохранении связи #".($i+1).": ".$message); } } @@ -137,11 +144,10 @@ class MarketplaceOrder1cStatusesController extends Controller } } - return $this->render('create', [ 'model' => $model, 'relationModels' => $relationModels, - 'statusOptions' => $statusOptions, + 'statusOptions' => $statusOptionsByMarketplace, ]); } @@ -159,49 +165,73 @@ class MarketplaceOrder1cStatusesController extends Controller $existingRelations = $model->getRelationsFrom()->all(); $relationModels = count($existingRelations) ? $existingRelations : [new MarketplaceOrder1cStatusesRelations()]; - $statusOptions = MarketplaceOrder1cStatuses::find() - ->select(['status']) - ->indexBy('id') - ->column(); - if (Yii::$app->request->isPost) { - if ($model->load(Yii::$app->request->post())) { - $transaction = Yii::$app->db->beginTransaction(Transaction::SERIALIZABLE); - try { - if (!$model->save()) { - throw new \Exception('Не удалось сохранить основной статус'); - } + $allStatuses = MarketplaceOrder1cStatuses::find() + ->select(['id','status','marketplace_id']) + ->asArray() + ->all(); - MarketplaceOrder1cStatusesRelations::deleteAll(['status_id_from' => $model->id]); + $statusOptionsByMarketplace = []; + foreach ($allStatuses as $row) { + $mp = (int)$row['marketplace_id']; + $statusOptionsByMarketplace[$mp][$row['id']] = $row['status']; + } - $postRelations = Yii::$app->request->post('Relations', []); - foreach ($postRelations as $i => $relData) { - if (empty($relData['status_id_to'])) { - continue; - } - $rel = new MarketplaceOrder1cStatusesRelations(); - $rel->status_id_from = $model->id; - $rel->status_id_to = $relData['status_id_to']; - $rel->description = $relData['description']; - $rel->button_text = $relData['button_text']; - if (!$rel->save()) { - throw new \Exception('Не удалось сохранить Relation #' . $i); - } - } + $currentId = $model->id; + if ($currentId) { + foreach ($statusOptionsByMarketplace as $mp => &$arr) { + if (isset($arr[$currentId])) { + unset($arr[$currentId]); + } + } + unset($arr); + } - $transaction->commit(); - return $this->redirect(['view', 'id' => $model->id]); - } catch (\Exception $e) { - $transaction->rollBack(); - Yii::$app->session->setFlash('error', 'Ошибка при сохранении отношений: ' . $e->getMessage()); + $existingRelations = $model->getRelationsFrom() + ->orderBy(['order' => SORT_ASC]) + ->all(); + + if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) { + $transaction = Yii::$app->db->beginTransaction(Transaction::SERIALIZABLE); + try { + if (!$model->save()) { + throw new \Exception('Не удалось сохранить основной статус'); + } + + MarketplaceOrder1cStatusesRelations::deleteAll(['status_id_from' => $model->id]); + + $postRelations = Yii::$app->request->post('Relations', []); + foreach ($postRelations as $i => $relData) { + if (empty($relData['status_id_to'])) { + continue; + } + $rel = new MarketplaceOrder1cStatusesRelations(); + $rel->status_id_from = $model->id; + $rel->status_id_to = $relData['status_id_to']; + $rel->description = $relData['description']; + $rel->button_text = $relData['button_text']; + $rel->order = (int)$relData['order']; + + if (!$rel->save()) { + $errors = $rel->getFirstErrors(); + $message = implode("; ", $errors); + throw new \Exception("Ошибка при сохранении связи #".($i+1).": ".$message); + } } + + $transaction->commit(); + return $this->redirect(['view', 'id' => $model->id]); + + } catch (\Exception $e) { + $transaction->rollBack(); + Yii::$app->session->setFlash('error', 'Ошибка при сохранении отношений: ' . $e->getMessage()); } } return $this->render('update', [ 'model' => $model, 'relationModels' => $relationModels, - 'statusOptions' => $statusOptions, + 'statusOptions' => $statusOptionsByMarketplace, ]); } diff --git a/erp24/migrations/m250604_141110_create_marketplace_order_1c_statuses_relations_table.php b/erp24/migrations/m250604_141110_create_marketplace_order_1c_statuses_relations_table.php index 8833354e..34e90f7e 100644 --- a/erp24/migrations/m250604_141110_create_marketplace_order_1c_statuses_relations_table.php +++ b/erp24/migrations/m250604_141110_create_marketplace_order_1c_statuses_relations_table.php @@ -23,6 +23,7 @@ class m250604_141110_create_marketplace_order_1c_statuses_relations_table extend 'status_id_to' => $this->integer()->comment('Cтатус для перехода - ссылка на id из таблицы marketplace_order_1c_statuses'), 'description' => $this->text()->null()->comment('Описание для 1С'), 'button_text' => $this->string()->null()->comment('Текст кнопки'), + 'order' => $this->integer()->comment('Порядок статусов перехода'), 'created_at' => $this->dateTime()->comment('Дата создания'), 'updated_at' => $this->dateTime()->comment('Дата обновления'), diff --git a/erp24/records/MarketplaceOrder1cStatusesRelations.php b/erp24/records/MarketplaceOrder1cStatusesRelations.php index c035c6e1..45a13923 100644 --- a/erp24/records/MarketplaceOrder1cStatusesRelations.php +++ b/erp24/records/MarketplaceOrder1cStatusesRelations.php @@ -12,6 +12,7 @@ use Yii; * @property int|null $status_id_to Cтатус для перехода - ссылка на id из таблицы marketplace_order_1c_statuses * @property string|null $description Описание для 1С * @property string|null $button_text Текст кнопки + * @property int $order Порядок статусов перехода * @property string|null $created_at Дата создания * @property string|null $updated_at Дата обновления */ @@ -33,29 +34,52 @@ class MarketplaceOrder1cStatusesRelations extends \yii\db\ActiveRecord public function rules() { return [ - [['status_id_from', 'status_id_to', 'description', 'button_text', 'created_at', 'updated_at'], 'default', 'value' => null], - [['status_id_from', 'status_id_to'], 'default', 'value' => null], - [['status_id_from', 'status_id_to'], 'integer'], + [['status_id_from', 'status_id_to', 'order'], 'integer'], [['description'], 'string'], - [['created_at', 'updated_at'], 'safe'], [['button_text'], 'string', 'max' => 255], + [['created_at', 'updated_at'], 'safe'], + [['status_id_from', 'status_id_to'], 'unique', + 'targetAttribute' => ['status_id_from', 'status_id_to'], + 'message' => 'Для данного статуса уже существует связь с таким же status_id_to.' + ], + [['status_id_from', 'order'], 'unique', + 'targetAttribute' => ['status_id_from', 'order'], + 'message' => 'Для данного статуса уже существует связь с таким же порядком.' + ], ]; } + /** * {@inheritdoc} */ public function attributeLabels() { return [ - 'id' => 'ID', - 'status_id_from' => 'Status Id From', - 'status_id_to' => 'Status Id To', - 'description' => 'Description', - 'button_text' => 'Button Text', - 'created_at' => 'Created At', - 'updated_at' => 'Updated At', + 'id' => 'ID', + 'status_id_from' => 'Статус (откуда)', + 'status_id_to' => 'Статус (куда)', + 'description' => 'Описание', + 'button_text' => 'Текст кнопки', + 'order' => 'Порядок перехода', + 'created_at' => 'Дата создания', + 'updated_at' => 'Дата обновления', ]; } + public function beforeValidate() + { + if (parent::beforeValidate()) { + if ($this->status_id_from !== null && $this->order === null) { + // Получаем из БД максимальный order для этого status_id_from + $maxOrder = (int) self::find() + ->where(['status_id_from' => $this->status_id_from]) + ->max('`order`'); + $this->order = $maxOrder + 1; + } + return true; + } + return false; + } + } diff --git a/erp24/views/crud/marketplace-order-1c-statuses/_form.php b/erp24/views/crud/marketplace-order-1c-statuses/_form.php index ed005517..98a26a06 100644 --- a/erp24/views/crud/marketplace-order-1c-statuses/_form.php +++ b/erp24/views/crud/marketplace-order-1c-statuses/_form.php @@ -1,5 +1,7 @@ registerJs($js); +$this->registerJs( + "window.statusesByMarketplace = $jsonStatuses;", + View::POS_HEAD +); + +$this->registerJsFile( + '@web/js/Sortable.js', + ['position' => \yii\web\View::POS_END] +); +$this->registerJsFile( + '@web/js/crud/marketplace-order1c-statuses/index.js', + ['depends' => [\yii\web\JqueryAsset::class], 'position' => \yii\web\View::POS_END] +); + ?>
+ session->hasFlash('error')): ?> + + - field($model, 'marketplace_id') - ->dropDownList([1 => 'ФлауВау', 2 => 'ЯндексМаркет'], ['prompt' => 'Выберите маркетплейс']) ?> + ->dropDownList( + [1 => 'ФлауВау', 2 => 'ЯндексМаркет'], + [ + 'prompt' => 'Выберите маркетплейс', + 'id' => 'marketplace-order1cstatuses-marketplace_id', + ] + ) ?> field($model, 'status')->textInput(['maxlength' => true]) ?> @@ -39,40 +53,53 @@ $this->registerJs($js);
-

Связанные статусы (relations)

- $rel): ?> -
- - 'control-label']) ?> - status_id_to, - $statusOptions, - ['prompt' => '— выберите статус —', 'class' => 'form-control'] - ); ?> - - - 'control-label', 'style' => 'margin-top:8px;']) ?> - description, - ['class' => 'form-control'] - ); ?> - - - 'control-label', 'style' => 'margin-top:8px;']) ?> - button_text, - ['class' => 'form-control'] - ); ?> - + $rel): + $initialOrder = $rel->order ?: ($i + 1); + ?> +
+ ☰ + Порядок: + + 'rel-order-input']) ?> + +
+ 'control-label']) ?> + status_id_to, + [], + [ + 'prompt' => '— выберите статус —', + 'class' => 'form-control status-to-select', + + 'data-current' => $rel->status_id_to, + ] + ); ?> +
+ +
+ 'control-label']) ?> + description, + ['class' => 'form-control'] + ); ?> +
+ +
+ 'control-label']) ?> + button_text, + ['class' => 'form-control'] + ); ?> +
@@ -89,26 +116,40 @@ $this->registerJs($js); -