]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-302 Редактирование букета
authormarina <m.zozirova@gmail.com>
Fri, 21 Feb 2025 12:32:03 +0000 (15:32 +0300)
committermarina <m.zozirova@gmail.com>
Fri, 21 Feb 2025 12:32:03 +0000 (15:32 +0300)
erp24/controllers/BouquetController.php
erp24/records/BouquetComposition.php
erp24/records/BouquetCompositionMatrixTypeHistory.php
erp24/records/BouquetForecast.php
erp24/views/bouquet/_form.php
erp24/views/bouquet/create.php

index 6bf01a541f34cab94d5f12eefd45766b53d871c0..4a8ff53ba0f67f6eb5436cf2922ee49174e6a3e0 100644 (file)
@@ -68,7 +68,8 @@ class BouquetController extends Controller
     public function actionCreate()
     {
         $model = new BouquetComposition();
-
+        $errors = [];
+        $flash = null;
 
         if (Yii::$app->request->isPost) {
             $data = Yii::$app->request->post();
@@ -79,7 +80,9 @@ class BouquetController extends Controller
                 $year = $data['year'];
 
                 if ($data['matrix_type_id']) {
-                    BouquetCompositionMatrixTypeHistory::setData($data['matrix_type_id'], $model->id);
+                    if (!empty(BouquetCompositionMatrixTypeHistory::setData($data['matrix_type_id'], $model->id))) {
+                        $errors = array_merge($errors, BouquetCompositionMatrixTypeHistory::setData($data['matrix_type_id'], $model->id));
+                    }
                 }
 
                 $model->photo_bouquet = UploadedFile::getInstances($model, 'photo_bouquet');
@@ -105,25 +108,38 @@ class BouquetController extends Controller
                         FileService::saveUploadedFile($video, BouquetComposition::VIDEO_BUILD_PROCESS, $model->id);
                     }
                 }
-
                 if (!empty($data['BouquetForecast']['type_sales_value'])) {
                     $salesData = $data['BouquetForecast']['type_sales_value'];
+                    $errors = [];
 
                     if (!empty($salesData['offline'])) {
-                        BouquetForecast::processSalesData($model->id, $year, $month, $salesData['offline'], BouquetForecast::OFFLINE_STORES);
+                        $offlineErrors = BouquetForecast::processSalesData($model->id, $year, $month, $salesData['offline'], BouquetForecast::OFFLINE_STORES);
+                        if (!empty($offlineErrors)) {
+                            $errors = array_merge($errors, $offlineErrors);
+                        }
                     }
 
                     if (!empty($salesData['online'])) {
-                        BouquetForecast::processSalesData($model->id, $year, $month, $salesData['online'], BouquetForecast::ONLINE_STORES);
+                        $onlineErrors = BouquetForecast::processSalesData($model->id, $year, $month, $salesData['online'], BouquetForecast::ONLINE_STORES);
+                        if (!empty($onlineErrors)) {
+                            $errors = array_merge($errors, $onlineErrors);
+                        }
                     }
 
                     if (!empty($salesData['marketplace'])) {
-                        BouquetForecast::processSalesData($model->id, $year, $month, $salesData['marketplace'], BouquetForecast::MARKETPLACE);
+                        $marketplaceErrors = BouquetForecast::processSalesData($model->id, $year, $month, $salesData['marketplace'], BouquetForecast::MARKETPLACE);
+                        if (!empty($marketplaceErrors)) {
+                            $errors = array_merge($errors, $marketplaceErrors);
+                        }
+                    }
+
+                    if (!empty($errors)) {
+                        Yii::$app->session->setFlash('danger', implode(' ', array_map('json_encode', $errors)));
                     }
                 }
 
-                if (array_key_exists('products_quantity', $data) ) {
-                    BouquetCompositionProducts::deleteAll([ 'bouquet_id' => $model->id]);
+                if (array_key_exists('products_quantity', $data)) {
+                    BouquetCompositionProducts::deleteAll(['bouquet_id' => $model->id]);
                     $bouquetProducts = Yii::$app->request->post('products_quantity');
                     foreach ($bouquetProducts as $key => $value) {
                         $product = new BouquetCompositionProducts([
@@ -132,12 +148,14 @@ class BouquetController extends Controller
                             'count' => $value
                         ]);
                         if (!$product->save()) {
-                            var_dump($model->getErrors());
-                            die();
+                            $errors = array_merge($errors, $product->getErrors());
                         }
                     }
                 }
+                Yii::$app->session->setFlash('success', 'Данные успешно сохранены');
                 return $this->redirect(['view', 'id' => $model->id]);
+            } else {
+                $errors = $model->getErrors();
             }
         }
 
@@ -155,65 +173,101 @@ class BouquetController extends Controller
         $marketplaceList = BouquetForecast::getStoresList(null, BouquetForecast::MARKETPLACE, CityStore::class, ['visible' => CityStore::IS_VISIBLE]);
         $onlineStoresList = BouquetForecast::getStoresList(null, BouquetForecast::ONLINE_STORES, CityStore::class, ['visible' => CityStore::IS_VISIBLE]);
 
+        if (!empty($errors)) {
+            $flatErrors = array_merge([], ...array_values($errors));
+            $flashMessage = !empty($flatErrors) ? implode(' ', $flatErrors) : 'Произошла ошибка.';
+            Yii::$app->session->setFlash('danger', $flashMessage);
+        }
+
         return $this->render('create', [
             'onlineStoresList' => $onlineStoresList,
             'marketplaceList' => $marketplaceList,
             'storesTypeList' => $storesTypeList,
             'availableItems' => $availableItems,
+            'flash' => $flash
         ]);
+
     }
 
     public function actionView($id)
     {
         $model = BouquetComposition::findOne($id);
+        $errors = [];
+        $flash = null;
 
         if (Yii::$app->request->isPost) {
             $data = Yii::$app->request->post();
-            $month = $data['month'];
-            $year = $data['year'];
+            $model->load($data);
+            if ($model->save()) {
 
-            if ($data['matrix_type_id']) {
+                $month = $data['month'];
+                $year = $data['year'];
 
-                BouquetCompositionMatrixTypeHistory::setData($data['matrix_type_id'], $model->id);
-            }
+                if ($data['matrix_type_id']) {
+                    if (!empty(BouquetCompositionMatrixTypeHistory::setData($data['matrix_type_id'], $model->id))) {
+                        $errors = array_merge($errors, BouquetCompositionMatrixTypeHistory::setData($data['matrix_type_id'], $model->id));
+                    }
+                }
 
-            $model->photo_bouquet = UploadedFile::getInstances($model, 'photo_bouquet');
-            if ($model->photo_bouquet) {
-                Files::deleteAll(['file_type' => 'image', 'entity_id' => $id, 'entity' => BouquetComposition::PHOTO_BOUQUET]);
-                foreach ($model->photo_bouquet as $photo) {
-                    FileService::saveUploadedFile($photo, BouquetComposition::PHOTO_BOUQUET, $model->id);
+                $model->photo_bouquet = UploadedFile::getInstances($model, 'photo_bouquet');
+                if (!empty($model->photo_bouquet)) {
+                    Files::deleteAll(['file_type' => 'image', 'entity_id' => $model->id, 'entity' => BouquetComposition::PHOTO_BOUQUET]);
+                    foreach ($model->photo_bouquet as $photo) {
+                        FileService::saveUploadedFile($photo, BouquetComposition::PHOTO_BOUQUET, $model->id);
+                    }
                 }
-            }
 
-            $model->video_presentation = UploadedFile::getInstances($model, 'video_presentation');
-            if ($model->video_presentation) {
-                Files::deleteAll(['file_type' => 'video', 'entity_id' => $id, 'entity' => BouquetComposition::VIDEO_PRESENTATION]);
-                FileService::saveUploadedFile($model->video_presentation, BouquetComposition::VIDEO_PRESENTATION, $model->id);
-            }
+                $model->video_presentation = UploadedFile::getInstances($model, 'video_presentation');
+                if (!empty($model->video_presentation)) {
+                    Files::deleteAll(['file_type' => 'video', 'entity_id' => $model->id, 'entity' => BouquetComposition::VIDEO_PRESENTATION]);
+                    foreach ($model->video_presentation as $video) {
+                        FileService::saveUploadedFile($video, BouquetComposition::VIDEO_PRESENTATION, $model->id);
+                    }
+                }
 
-            $model->video_build_process = UploadedFile::getInstances($model, 'video_build_process');
-            if ($model->video_build_process) {
-                Files::deleteAll(['file_type' => 'video', 'entity_id' => $id, 'entity' => BouquetComposition::VIDEO_BUILD_PROCESS]);
-                FileService::saveUploadedFile($model->video_build_process, BouquetComposition::VIDEO_BUILD_PROCESS, $model->id);
-            }
+                $model->video_build_process = UploadedFile::getInstances($model, 'video_build_process');
+                if (!empty($model->video_build_process)) {
+                    Files::deleteAll(['file_type' => 'video', 'entity_id' => $model->id, 'entity' => BouquetComposition::VIDEO_BUILD_PROCESS]);
+                    foreach ($model->video_build_process as $video) {
+                        FileService::saveUploadedFile($video, BouquetComposition::VIDEO_BUILD_PROCESS, $model->id);
+                    }
+                }
 
 
-            if (!empty($data['BouquetForecast']['type_sales_value'])) {
-                $salesData = $data['BouquetForecast']['type_sales_value'];
+                if (!empty($data['BouquetForecast']['type_sales_value'])) {
+                    $salesData = $data['BouquetForecast']['type_sales_value'];
+                    $errors = [];
 
-                if (!empty($salesData['offline'])) {
-                    BouquetForecast::processSalesData($id, $year, $month, $salesData['offline'], BouquetForecast::OFFLINE_STORES);
-                }
+                    if (!empty($salesData['offline'])) {
+                        $offlineErrors = BouquetForecast::processSalesData($model->id, $year, $month, $salesData['offline'], BouquetForecast::OFFLINE_STORES);
+                        if (!empty($offlineErrors)) {
+                            $errors = array_merge($errors, $offlineErrors);
+                        }
+                    }
 
-                if (!empty($salesData['online'])) {
-                    BouquetForecast::processSalesData($id, $year, $month, $salesData['online'], BouquetForecast::ONLINE_STORES);
-                }
+                    if (!empty($salesData['online'])) {
+                        $onlineErrors = BouquetForecast::processSalesData($model->id, $year, $month, $salesData['online'], BouquetForecast::ONLINE_STORES);
+                        if (!empty($onlineErrors)) {
+                            $errors = array_merge($errors, $onlineErrors);
+                        }
+                    }
+
+                    if (!empty($salesData['marketplace'])) {
+                        $marketplaceErrors = BouquetForecast::processSalesData($model->id, $year, $month, $salesData['marketplace'], BouquetForecast::MARKETPLACE);
+                        if (!empty($marketplaceErrors)) {
+                            $errors = array_merge($errors, $marketplaceErrors);
+                        }
+                    }
 
-                if (!empty($salesData['marketplace'])) {
-                    BouquetForecast::processSalesData($id, $year, $month, $salesData['marketplace'], BouquetForecast::MARKETPLACE);
+                    if (!empty($errors)) {
+                        Yii::$app->session->setFlash('danger', implode(' ', array_map('json_encode', $errors)));
+                    }
                 }
-            }
 
+                Yii::$app->session->setFlash('success', 'Данные успешно сохранены');
+            } else {
+                $errors = $model->getErrors();
+            }
         }
 
 
@@ -231,6 +285,13 @@ class BouquetController extends Controller
         $onlineStoresList = BouquetForecast::getStoresList($id, BouquetForecast::ONLINE_STORES, CityStore::class, ['visible' => CityStore::IS_VISIBLE]);
 
 
+        if (!empty($errors)) {
+            $flatErrors = array_merge([], ...array_values($errors));
+            $flashMessage = !empty($flatErrors) ? implode(' ', $flatErrors) : 'Произошла ошибка.';
+            Yii::$app->session->setFlash('danger', $flashMessage);
+        }
+
+
         return $this->render('view', [
             'model' => $model,
             'onlineStoresList' => $onlineStoresList,
@@ -241,6 +302,7 @@ class BouquetController extends Controller
             'photoFiles' => $photoFiles,
             'videoUrls' => $videoUrls,
             'processUrls' => $processUrls,
+            'flash' => $flash
         ]);
     }
 
@@ -271,7 +333,7 @@ class BouquetController extends Controller
         if (Yii::$app->request->isPost) {
             try {
                 if (Yii::$app->request->post('products_quantity')) {
-                    BouquetCompositionProducts::deleteAll([ 'bouquet_id' => $model->id]);
+                    BouquetCompositionProducts::deleteAll(['bouquet_id' => $model->id]);
                     $bouquetProducts = Yii::$app->request->post('products_quantity');
                     foreach ($bouquetProducts as $key => $value) {
                         $product = new BouquetCompositionProducts([
@@ -391,6 +453,7 @@ class BouquetController extends Controller
             'marketplace' => $marketplaceList,
         ];
     }
+
     public function actionGetCalculates()
     {
         Yii::$app->response->format = Response::FORMAT_JSON;
index f006ed7f6cc318f594d20be6fc3f38891251e4d8..12320e4d5910318f28747037791f958ffbf465c4 100644 (file)
@@ -58,12 +58,21 @@ class BouquetComposition extends ActiveRecord
     public function rules()
     {
         return [
-            [['name'], 'required'],
+            [['name'], 'required', 'message' => 'Поле "Название" не может быть пустым.'],
             [['created_by', 'updated_by'], 'integer'],
             [['created_at', 'updated_at'], 'safe'],
             [['guid', 'name'], 'string', 'max' => 255],
-            [['photo_bouquet'], 'file', 'extensions' => 'jpg, jpeg, png, gif', 'maxFiles' => 10],
-            [['video_presentation', 'video_build_process'], 'file', 'extensions' => 'mkv, mov, avi, mp4'],
+
+            [['photo_bouquet'], 'file',
+                'extensions' => 'jpg, jpeg, png, gif',
+                'maxFiles' => 10,
+                'message' => 'Допустимые форматы: jpg, jpeg, png, gif. Максимум 10 файлов.'
+            ],
+
+            [['video_presentation', 'video_build_process'], 'file',
+                'extensions' => 'mkv, mov, avi, mp4',
+                'message' => 'Допустимые форматы видео: mkv, mov, avi, mp4.'
+            ],
         ];
     }
 
index cfab41e8f9e999f983497d629a2751b4a2fbf678..316aca9fa45cfa72973fcbf17cf1a2d3b6cf65c1 100644 (file)
@@ -92,8 +92,8 @@ class BouquetCompositionMatrixTypeHistory extends ActiveRecord
             'matrix_type_id' => $value,
             'date_from' => new Expression('now()'),
         ]);
-        if(!$matrixHistoryType->save()) {
-            var_dump($matrixHistoryType->getErrors(), $matrixHistoryType->date_from);
+        if (!$matrixHistoryType->save()) {
+            return $matrixHistoryType->errors();
         }
     }
 
index b6d6111bd6c226b6b4ab7e645aa17d46b79bb2f1..8c553ae34d36ef05947888defaad42b5cdc38c67 100644 (file)
@@ -121,6 +121,8 @@ class BouquetForecast extends ActiveRecord
 
     public static function processSalesData($id, $year, $month, $salesData, $salesType)
     {
+        $errors = [];
+
         foreach ($salesData as $key => $value) {
             $model = BouquetForecast::findOne([
                 'bouquet_id' => $id,
@@ -139,10 +141,17 @@ class BouquetForecast extends ActiveRecord
                     'type_sales_id' => $key,
                     'type_sales_value' => $value
                 ]);
-                $model->save();
+
+                if (!$model->save()) {
+                    $errors[] = $model->errors;
+                }
             } elseif ($model->type_sales_value != $value) {
-                $model->updateAttributes(['type_sales_value' => $value]);
+                if (!$model->updateAttributes(['type_sales_value' => $value])) {
+                    $errors[] = $model->errors;
+                }
             }
         }
+
+        return $errors;
     }
 }
\ No newline at end of file
index 477cb5d0426192b2bacc2ab652b4d86e7e6d9cbe..6e68bcb2092a5b4f896f06eeba4bb27c6e71a755 100644 (file)
@@ -37,9 +37,19 @@ $form = ActiveForm::begin([
 ]);
 ?>
     <h1 class="ms-3 mb-0"><?= $model ? Html::encode("Редактирование букета") : Html::encode("Создание букета") ?></h1>
-    <div class="row">
+    <div class="row d-flex align-items-center">
         <div class="col-md-2">
-            <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary ms-3 mt-4 w-45']) ?>
+            <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary ms-3 mt-4 w-75']) ?>
+        </div>
+        <div class="col-md-10 flash-column">
+            <?php
+            $flashType = Yii::$app->session->hasFlash('danger') ? 'danger' : (Yii::$app->session->hasFlash('success') ? 'success' : null);
+            if ($flashType):
+                ?>
+                <?= Html::tag('div', Yii::$app->session->getFlash($flashType), [
+                'class' => 'alert text-center flash-message py-1 mt-6 alert-' . $flashType,
+            ]) ?>
+            <?php endif; ?>
         </div>
     </div>
     <div class="row d-flex border-bottom align-items-center py-4">
index b9f3e0a0a3d9c41417d7e76d8849baf7d00a7d69..eb08e6e195a6ee18a6fbabf7b951483cc51f8ad5 100644 (file)
@@ -32,6 +32,7 @@ $this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::PO
         'processUrls' => [],
         'availableItems' => $availableItems,
         'model' => null,
+        'flash' => $flash,
         'id' => null
     ]); ?>
 </div>