From 885e2659b1bb5b5ad93adfba80078b5391d52de5 Mon Sep 17 00:00:00 2001 From: Vladimir Fomichev Date: Mon, 16 Jun 2025 13:03:22 +0300 Subject: [PATCH] =?utf8?q?=D0=9C=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BD=D0=B5?= =?utf8?q?=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D1=85=20=D0=BF=D1=80=D0=BE?= =?utf8?q?=D0=B3=D0=BD=D0=BE=D0=B7=D0=BE=D0=B2=20=D0=BF=D0=BE=20=D0=B1?= =?utf8?q?=D1=83=D0=BA=D0=B5=D1=82=D0=B0=D0=BC=20=D0=B8=20=D0=B3=D1=80?= =?utf8?q?=D1=83=D0=BF=D0=BF=D0=B0=D0=BC=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?utf8?q?=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../AutoPlannogrammaController.php | 30 ++++++++++ erp24/services/AutoPlannogrammaService.php | 57 +++++++++++++++++++ erp24/services/StorePlanService.php | 21 ++++++- 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index 2bfec0d8..16f3ee09 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -1745,6 +1745,36 @@ class AutoPlannogrammaController extends BaseController ]); } + public function actionWeeklyBouquetProductsForecast() + { + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + + $request = Yii::$app->request; + $storeId = $request->get('storeId'); + $month = $request->get('month'); + $year = $request->get('year'); + $week = $request->get('week'); + + if (!$month || !$year || $week === null) { + return ['success' => false, 'message' => 'Нет параметров']; + } + + $service = new AutoPlannogrammaService(); + $result = $service->getWeeklyBouquetProductsForecast($month, $year, $storeId); + + if (!is_array($result)) { + return ['success' => false, 'message' => 'Ошибка структуры данных']; + } + + $filtered = array_filter($result, function ($item) use ($week) { + return (int)$item['week'] === (int)$week; + }); + + return [ + 'success' => true, + 'data' => array_values($filtered), + ]; + } } diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 829b2b3e..15d9c773 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -2786,4 +2786,61 @@ class AutoPlannogrammaService return $pricesMap; } + public function getWeeklyBouquetProductsForecast($month, $year, $storeId) + { + $matrixGroups = ArrayHelper::map( + MatrixBouquetForecast::find()->select(['group'])->distinct()->asArray()->all(), + 'group', + 'group' + ); + $date = $year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT) . '-01'; + $result = StorePlanService::getBouquetSpiecesMonthGoalFromForecast($month, $year, $storeId, $matrixGroups); + $weekShares = $this->getHistoricalSpeciesShareByWeek($date); + $flatData = $result['flatData']; + $weekIndex = []; + foreach ($weekShares as $shareRow) { + $key = implode('|', [ + $shareRow['store_id'], + $shareRow['category'], + $shareRow['subcategory'], + $shareRow['species'], + ]); + $weekIndex[$key][] = $shareRow; + } + + $weeklyForecasts = []; + + foreach ($flatData as $item) { + $key = implode('|', [ + $item['store_id'], + $item['category'], + $item['subcategory'], + $item['species'], + ]); + + if (!isset($weekIndex[$key])) { + continue; + } + + foreach ($weekIndex[$key] as $weekShare) { + $weeklyForecasts[] = [ + 'store_id' => $item['store_id'], + 'category' => $item['category'], + 'subcategory' => $item['subcategory'], + 'species' => $item['species'], + 'type' => $item['type'], + 'product_guid' => $item['product_guid'], + 'week_forecast' => round($item['full_forecast'] * $weekShare['share'], 2), + 'full_forecast' => $item['full_forecast'], + 'week' => $weekShare['week'], + 'matrix_group' => $item['matrix_group'], + 'month' => $item['month'], + 'year' => $item['year'], + ]; + } + } + return $weeklyForecasts; + + } + } \ No newline at end of file diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index 6c9792ed..c40da43b 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -1257,7 +1257,7 @@ class StorePlanService $resultData = []; $detailData = []; $priceCache = []; - $speciesCache = []; + $flatData = []; foreach ($matrixGroups as $matrixGroup) { $forecasts = MatrixBouquetForecast::find() @@ -1335,16 +1335,30 @@ class StorePlanService $price = $priceCache[$regionId][$guid] ?? 0; $raw = $price * $product['count'] * $typeSalesValue; $cost = round($raw * BouquetCompositionPrice::SURCHARGE_ASSEMBLY, 2); - + $fullForecast = $product['count'] * $typeSalesValue; $resultData[$sid][$category][$subcategory][$species][$typeSales] = ($resultData[$sid][$category][$subcategory][$species][$typeSales] ?? 0) + $cost; $detailData[$sid][$species][$typeSales][] = [ 'product_guid' => $guid, 'price' => $price, 'count' => $product['count'], 'forecast' => $typeSalesValue, + 'full_forecast' => $fullForecast, 'guid' => $forecast['guid'], 'raw_calculation' => $raw, 'rounded' => $cost, + 'matrix_group' => $matrixGroup, + ]; + $flatData[] = [ + 'store_id' => $sid, + 'category' => $category, + 'subcategory' => $subcategory, + 'species' => $species, + 'type' => $typeSales, + 'product_guid' => $guid, + 'full_forecast' => $fullForecast, + 'matrix_group' => $matrixGroup, + 'month' => $month, + 'year' => $year, ]; } } @@ -1366,7 +1380,8 @@ class StorePlanService return [ 'detail' => $resultData, 'final' => $finalResult, - 'fullDetail' => $detailData + 'fullDetail' => $detailData, + 'flatData' => $flatData, ]; } -- 2.39.5