]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Распределение матрицы
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 4 Aug 2025 06:57:29 +0000 (09:57 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 4 Aug 2025 06:57:29 +0000 (09:57 +0300)
erp24/services/AutoPlannogrammaService.php

index a580d627d645a300d4f1614cb507ab9213dde126..45fb204c2156012e6037af7aefe9fd8c1e87b3ca 100644 (file)
@@ -853,29 +853,62 @@ class AutoPlannogrammaService
         //$monthCategoryGoal = $this->getMonthCategoryGoal($monthCategoryShare, $datePlan, $filters['type']);
         $monthCategoryGoal = [];
         $categoryPlan = CategoryPlan::find()
-            ->where(
-            ['year' => date('Y', strtotime($datePlan)),
+            ->where([
+                'year' => date('Y', strtotime($datePlan)),
                 'month' => date('m', strtotime($datePlan)),
-                'store_id' => $filters['store_id']])->indexBy('category')
+                'store_id' => $filters['store_id'],
+            ])
+            ->indexBy('category')
             ->asArray()
             ->all();
-        foreach ($categoryPlan as $category) {
-            if ($category['category'] === 'Матрица') {
-                continue;
-            }
+
+        $rawGoals = [];
+        foreach ($categoryPlan as $categoryName => $category) {
             if ($filters['type'] == AutoPlannogrammaService::TYPE_SALES) {
-                $fullGoal  = $category['offline'] + $category['internet_shop'] + $category['marketplace'];
+                $rawGoals[$categoryName] = (float)$category['offline'] + (float)$category['internet_shop'] + (float)$category['marketplace'];
             } else {
-                $fullGoal  = $category['write_offs'];
+                $rawGoals[$categoryName] = (float)$category['write_offs'];
             }
+        }
 
-            $monthCategoryGoal[] = [
-                'category' => $category['category'],
-                'store_id' => $filters['store_id'],
-                'goal' => $fullGoal
-            ];
+        $matrixGoal = $rawGoals['Матрица'] ?? 0.0;
+        $nonMatrixGoals = array_filter(
+            $rawGoals,
+            fn($v, $k) => $k !== 'Матрица',
+            ARRAY_FILTER_USE_BOTH
+        );
+        $sumWithoutMatrix = array_sum($nonMatrixGoals);
+
+        if ($sumWithoutMatrix <= 0) {
+            foreach ($nonMatrixGoals as $categoryName => $_) {
+                $monthCategoryGoal[] = [
+                    'category' => $categoryName,
+                    'store_id' => $filters['store_id'],
+                    'share' => 0.0,
+                    'goal' => 0.0,
+                ];
+            }
+        } else {
+            foreach ($nonMatrixGoals as $categoryName => $value) {
+                $share = $value / $sumWithoutMatrix;
+
+                if ($filters['type'] == AutoPlannogrammaService::TYPE_SALES) {
+                    $distributable = max(0.0, $sumWithoutMatrix - $matrixGoal);
+                } else {
+                    $distributable = $sumWithoutMatrix;
+                }
 
+                $newGoal = $share * $distributable;
+
+                $monthCategoryGoal[] = [
+                    'category' => $categoryName,
+                    'store_id' => $filters['store_id'],
+                    'share' => $share,
+                    'goal' => $newGoal,
+                ];
+            }
         }
+
         $monthSubcategoryShare = $this->getMonthSubcategoryShareOrWriteOff($datePlan, $filters);
         $monthSubcategoryGoal = $this->getMonthSubcategoryGoal($monthSubcategoryShare, $monthCategoryGoal);