]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Рассчеты для товаров
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 26 Sep 2025 15:35:02 +0000 (18:35 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 26 Sep 2025 15:35:02 +0000 (18:35 +0300)
erp24/services/AutoPlannogrammaService.php

index 03be5c990184b9db246fb56fd22773f0668a3de2..ad601c8c7131e3711814c5760bba152179c0b1db 100644 (file)
@@ -1430,6 +1430,34 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $
             ->andWhere(['>=', 'date_to', $dt])
             ->asArray()
             ->column();
+
+        $actualBySpec = [];
+        if (!empty($actualProducts)) {
+            $tuples = (new \yii\db\Query())
+                ->select([
+                    'pid'        => 'p1c.id',
+                    'category'   => 'p1c.category',
+                    'subcategory'=> 'p1c.subcategory',
+                    'species'    => 'p1c.species',
+                ])
+                ->from(['p1c' => 'products_1c_nomenclature'])
+                ->leftJoin(['p1' => 'products_1c'], 'p1.id = p1c.id')
+                ->where(['p1c.id' => $actualProducts])
+                ->andWhere(['p1.components' => ''])
+                ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']])
+                ->andWhere(['not', ['p1c.species' => null]])
+                ->andWhere(['<>', 'p1c.species', ''])
+                ->all();
+
+            foreach ($tuples as $t) {
+                $cat = $t['category'];
+                $sub = $t['subcategory'];
+                $spec = $t['species'];
+                $pid = $t['pid'];
+                $actualBySpec[$cat][$sub][$spec][$pid] = true;
+            }
+        }
+
         $forecastMap = [];
         foreach ($productForecastSpecies as $item) {
             $sid = $item['store_id'];
@@ -1452,34 +1480,44 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $
             $week = $w['week'];
             $wShare = (float)$w['share'];
 
-            if (
-                !isset(
-                    $forecastMap[$sid],
-                    $forecastMap[$sid][$cat],
-                    $forecastMap[$sid][$cat][$sub],
-                    $forecastMap[$sid][$cat][$sub][$spec]
-                )
-            ) {
-                continue;
+            $productsInSpec = $forecastMap[$sid][$cat][$sub][$spec] ?? [];
+            if (!empty($productsInSpec)) {
+                foreach ($productsInSpec as $pid => $piecesMon) {
+                    $forecastWeekPieces = round($piecesMon * $wShare, 2);
+                    if (!in_array($pid, $actualProducts)) {
+                        continue;
+                    }
+                    $result[] = [
+                        'week' => $week,
+                        'store_id' => $sid,
+                        'category' => $cat,
+                        'subcategory' => $sub,
+                        'species' => $spec,
+                        'product_id' => $pid,
+                        'forecast_month_pieces' => $piecesMon,
+                        'forecast_week_pieces' => $forecastWeekPieces,
+                    ];
+                }
             }
 
-            $productsInSpec = $forecastMap[$sid][$cat][$sub][$spec];
-
-            foreach ($productsInSpec as $pid => $piecesMon) {
-                $forecastWeekPieces = round($piecesMon * $wShare, 2);
-                if (!in_array($pid, $actualProducts)) {
-                   continue;
+            $specActualSet = $actualBySpec[$cat][$sub][$spec] ?? [];
+            if (!empty($specActualSet)) {
+                $missingPids = array_diff(array_keys($specActualSet), array_keys($productsInSpec));
+                foreach ($missingPids as $pid) {
+                    if (!in_array($pid, $actualProducts, true)) {
+                        continue;
+                    }
+                    $result[] = [
+                        'week'                   => $week,
+                        'store_id'               => $sid,
+                        'category'               => $cat,
+                        'subcategory'            => $sub,
+                        'species'                => $spec,
+                        'product_id'             => $pid,
+                        'forecast_month_pieces'  => 0.0,
+                        'forecast_week_pieces'   => 0.0,
+                    ];
                 }
-                $result[] = [
-                    'week' => $week,
-                    'store_id' => $sid,
-                    'category' => $cat,
-                    'subcategory' => $sub,
-                    'species' => $spec,
-                    'product_id' => $pid,
-                    'forecast_month_pieces' => $piecesMon,
-                    'forecast_week_pieces' => $forecastWeekPieces,
-                ];
             }
         }
 
@@ -2623,7 +2661,7 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $
         $productForecastSpecies = $this->calculateProductSalesBySpecies($salesProductForecastShare, $cleanedSpeciesGoals);
 
         $weeklySales = $this->getHistoricalSpeciesShareByWeek($filters['plan_date'], $filters);
-        var_dump($weeklySales);die();
+
         $weeklySalesForecast = $this->calculateWeeklyProductForecastPieces($productForecastSpecies, $weeklySales, $filters);
 
         return $weeklySalesForecast;