public function actionCreate()
{
$model = new BouquetComposition();
-
+ $errors = [];
+ $flash = null;
if (Yii::$app->request->isPost) {
$data = Yii::$app->request->post();
$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');
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([
'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();
}
}
$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();
+ }
}
$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,
'photoFiles' => $photoFiles,
'videoUrls' => $videoUrls,
'processUrls' => $processUrls,
+ 'flash' => $flash
]);
}
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([
'marketplace' => $marketplaceList,
];
}
+
public function actionGetCalculates()
{
Yii::$app->response->format = Response::FORMAT_JSON;