From 42d870d90b08a1eff6c8d35ba2519284f8cb6dd3 Mon Sep 17 00:00:00 2001 From: Vladimir Fomichev Date: Mon, 4 Aug 2025 09:57:29 +0300 Subject: [PATCH] =?utf8?q?=D0=A0=D0=B0=D1=81=D0=BF=D1=80=D0=B5=D0=B4=D0=B5?= =?utf8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?utf8?q?=D1=86=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/services/AutoPlannogrammaService.php | 61 +++++++++++++++++----- 1 file changed, 47 insertions(+), 14 deletions(-) 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); -- 2.39.5