]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
проверка недель
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 26 Sep 2025 15:11:28 +0000 (18:11 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 26 Sep 2025 15:11:28 +0000 (18:11 +0300)
erp24/services/AutoPlannogrammaService.php

index 16077d7ca51ed3995d8144b60f49d32b251a23bf..03be5c990184b9db246fb56fd22773f0668a3de2 100644 (file)
@@ -1326,6 +1326,68 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $
             }
         }
 
+        $monthStart = sprintf('%04d-%02d-01 00:00:00', $year, $month);
+        $monthEnd   = date('Y-m-d 23:59:59', strtotime("$monthStart +1 month -1 second"));
+
+        $actualGuids = Products1cNomenclatureActuality::find()
+            ->select(['guid'])
+            ->andWhere(['<=', 'date_from', $monthEnd])
+            ->andWhere(['or', ['date_to' => null], ['>=', 'date_to', $monthStart]])
+            ->asArray()
+            ->column();
+
+        if (!empty($actualGuids)) {
+            $allSpeciesTuples = (new \yii\db\Query())
+                ->select([
+                    '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' => $actualGuids])
+                ->andWhere(['p1.components' => ''])
+                ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']])
+                ->andWhere(['not', ['p1c.species' => null]])
+                ->andWhere(['<>', 'p1c.species', ''])
+                ->groupBy(['p1c.category', 'p1c.subcategory', 'p1c.species'])
+                ->all();
+
+            $existingKeys = [];
+            foreach ($result as $row) {
+                $existingKeys[$row['store_id'] . '|' . $row['category'] . '|' . $row['subcategory'] . '|' . $row['species']] = true;
+            }
+
+            $weeksCount = max(1, count($targetRanges));
+            $evenShare  = round(1.0 / $weeksCount, 6);
+
+            foreach ($storeIds as $sid) {
+                foreach ($allSpeciesTuples as $tpl) {
+                    $cat = $tpl['category'];
+                    $sub = $tpl['subcategory'];
+                    $spec = $tpl['species'];
+
+                    $baseKey = $sid . '|' . $cat . '|' . $sub . '|' . $spec;
+
+                    if (!isset($existingKeys[$baseKey])) {
+                        foreach ($targetRanges as $posIndex => $range) {
+                            $isoWeekNumber = $range['index'];
+                            $result[] = [
+                                'store_id'  => $sid,
+                                'category'  => $cat,
+                                'subcategory'=> $sub,
+                                'species'   => $spec,
+                                'week'      => $isoWeekNumber,
+                                'share'     => $evenShare,
+                                'sumMonth'  => 0.0,
+                                'sumWeek'   => 0.0,
+                            ];
+                        }
+                    }
+                }
+            }
+        }
+
         $grouped = [];
         foreach ($result as $idx => $row) {
             $key = "{$row['store_id']}|{$row['category']}|{$row['subcategory']}|{$row['species']}";
@@ -1336,10 +1398,11 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $
             foreach ($indices as $i) {
                 $sumPercent += $result[$i]['share'];
             }
+
             if ($sumPercent < 1.0) {
-                $diff = 1.0 - round($sumPercent, 4);
+                $diff  = 1.0 - round($sumPercent, 4);
                 $count = count($indices);
-                $add = round($diff / $count, 4);
+                $add   = round($diff / $count, 4);
                 foreach ($indices as $i) {
                     $result[$i]['share'] = round($result[$i]['share'] + $add, 6);
                 }
@@ -2558,9 +2621,9 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $
         $salesProductForecastShare = $this->calculateProductForecastShare($noHistoryProductData, $historyProductData);
 
         $productForecastSpecies = $this->calculateProductSalesBySpecies($salesProductForecastShare, $cleanedSpeciesGoals);
-        var_dump($productForecastSpecies); die();
-        $weeklySales = $this->getHistoricalSpeciesShareByWeek($filters['plan_date'], $filters);
 
+        $weeklySales = $this->getHistoricalSpeciesShareByWeek($filters['plan_date'], $filters);
+        var_dump($weeklySales);die();
         $weeklySalesForecast = $this->calculateWeeklyProductForecastPieces($productForecastSpecies, $weeklySales, $filters);
 
         return $weeklySalesForecast;