//$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);