From: marina Date: Fri, 21 Feb 2025 12:32:03 +0000 (+0300) Subject: ERP-302 Редактирование букета X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=48a57c80a79633aa4e60fdb37119e23dcd57a8e5;p=erp24_rep%2Fyii-erp24%2F.git ERP-302 Редактирование букета --- diff --git a/erp24/controllers/BouquetController.php b/erp24/controllers/BouquetController.php index 6bf01a54..4a8ff53b 100644 --- a/erp24/controllers/BouquetController.php +++ b/erp24/controllers/BouquetController.php @@ -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; diff --git a/erp24/records/BouquetComposition.php b/erp24/records/BouquetComposition.php index f006ed7f..12320e4d 100644 --- a/erp24/records/BouquetComposition.php +++ b/erp24/records/BouquetComposition.php @@ -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.' + ], ]; } diff --git a/erp24/records/BouquetCompositionMatrixTypeHistory.php b/erp24/records/BouquetCompositionMatrixTypeHistory.php index cfab41e8..316aca9f 100644 --- a/erp24/records/BouquetCompositionMatrixTypeHistory.php +++ b/erp24/records/BouquetCompositionMatrixTypeHistory.php @@ -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(); } } diff --git a/erp24/records/BouquetForecast.php b/erp24/records/BouquetForecast.php index b6d6111b..8c553ae3 100644 --- a/erp24/records/BouquetForecast.php +++ b/erp24/records/BouquetForecast.php @@ -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 diff --git a/erp24/views/bouquet/_form.php b/erp24/views/bouquet/_form.php index 477cb5d0..6e68bcb2 100644 --- a/erp24/views/bouquet/_form.php +++ b/erp24/views/bouquet/_form.php @@ -37,9 +37,19 @@ $form = ActiveForm::begin([ ]); ?>

-
+
- 'btn btn-primary ms-3 mt-4 w-45']) ?> + 'btn btn-primary ms-3 mt-4 w-75']) ?> +
+
+ session->hasFlash('danger') ? 'danger' : (Yii::$app->session->hasFlash('success') ? 'success' : null); + if ($flashType): + ?> + session->getFlash($flashType), [ + 'class' => 'alert text-center flash-message py-1 mt-6 alert-' . $flashType, + ]) ?> +
diff --git a/erp24/views/bouquet/create.php b/erp24/views/bouquet/create.php index b9f3e0a0..eb08e6e1 100644 --- a/erp24/views/bouquet/create.php +++ b/erp24/views/bouquet/create.php @@ -32,6 +32,7 @@ $this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::PO 'processUrls' => [], 'availableItems' => $availableItems, 'model' => null, + 'flash' => $flash, 'id' => null ]); ?>