]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Метод недельных прогнозов по букетам и группам матриц
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 16 Jun 2025 10:03:22 +0000 (13:03 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 16 Jun 2025 10:03:22 +0000 (13:03 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/services/AutoPlannogrammaService.php
erp24/services/StorePlanService.php

index 2bfec0d8e8cb85d71c99145c0cc0ef3201317235..16f3ee0966ecca14814147e5b180608fd95108d7 100644 (file)
@@ -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),
+        ];
+    }
 
 
 }
index 829b2b3ea977c5f9b0df2787e54cf6b6be1d97b8..15d9c773fd2fe2d40d1f563fc2e82a71234c9551 100644 (file)
@@ -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
index 6c9792eda988e2618dc162f4b9771065b9417795..c40da43b370bf5e77d91e72583f022b528ddb5aa 100755 (executable)
@@ -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,
         ];
     }