From: Vladimir Fomichev Date: Mon, 4 Aug 2025 06:57:29 +0000 (+0300) Subject: Распределение матрицы X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=42d870d90b08a1eff6c8d35ba2519284f8cb6dd3;p=erp24_rep%2Fyii-erp24%2F.git Распределение матрицы --- diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index a580d627..45fb204c 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -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);