$weeksData = [];
$weeksShareResult = [];
$weeksGoalResult = [];
-
+ $monthCategoryShareResult = [];
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$filters = [];
);
}
- $monthResult = $service->getMonthSpeciesShareOrWriteOffDate(
- $dateFrom,
- $dateTo,
- $filters,
- null,
- $model->type
- );
- $speciesMonthTotals = [];
- foreach ($monthResult as $monthRow) {
- $speciesMonthTotals[$monthRow['store_id']]
- [$monthRow['category']]
- [$monthRow['subcategory']]
- [$monthRow['species']] = $monthRow['total_sum'] ;
- }
+
$monthSpeciesGoals = $service->calculateFullGoalChain($filters);
$monthSpeciesGoalsMap = [];
$monthSpeciesGoalsMap[$monthSpeciesGoal['store_id']]
[$monthSpeciesGoal['category']]
[$monthSpeciesGoal['subcategory']]
- [$monthSpeciesGoal['species']] = $monthSpeciesGoal['goal'] ; // переименовать
+ [$monthSpeciesGoal['species']] = $monthSpeciesGoal['goal'] ;
}
- foreach (range(1, 5) as $ind) {
- $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($dateFrom)));
- $weekEnd = date("Y-m-d 23:59:59", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($dateFrom)));
- if ($weekEnd > $dateTo) {
- $weekEnd = $dateTo;
+
+
+ $weeksShareResult = $service->getHistoricalWeeklySpeciesShare($model->month, $filters, null, 'writeOffs');
+ $weeksData = $service->calculateWeeklySpeciesGoals($weeksShareResult['weeksData'], $monthSpeciesGoals) ;
+
+ $datePlan = $filters['plan_date'];
+ $monthCategoryShare = $service->getMonthCategoryShareOrWriteOffWeighted($datePlan, $filters, null, $filters['type']);
+ $monthCategoryGoal = $service->getMonthCategoryGoal($monthCategoryShare, $datePlan, $filters);
+ foreach ($monthCategoryShare as $sid => $cats) {
+ foreach($cats as $cat) {
+ $monthCategoryShareResult[$sid][$cat['category']]['total_sum_cat'] = $cat['total_sum_cat'];
+ $monthCategoryShareResult[$sid][$cat['category']]['share_of_total'] = $cat['share_of_total'];
}
- if ($weekStart > $dateTo) {
- continue;
+
+ }
+ //var_dump($monthCategoryShareResult); die();
+ $monthSubcategoryShare = $service->getMonthSubcategoryShareOrWriteOffWeighted($datePlan, $filters, null, $filters['type']);
+ $monthSubcategoryGoal = $service->getMonthSubcategoryGoal($monthSubcategoryShare, $monthCategoryGoal);
+
+ if ($filters['type'] === 'writeOffs') {
+ $salesSubShare = $service->getMonthSubcategoryShareOrWriteOffWeighted($datePlan, $filters, null, 'sales');
+ $salesSubGoal = $service->getMonthSubcategoryGoal($salesSubShare, $monthCategoryGoal);
+
+ $catGoalMap = [];
+ foreach ($monthCategoryGoal as $row) {
+ $catGoalMap[$row['category']] = $row['goal'];
}
- $weekResult = $service->getMonthSpeciesShareOrWriteOffDate(
- $weekStart,
- $weekEnd,
- $filters,
- null,
- $model->type
- );
- $weeksData[$ind] = $weekResult;
- if (!$weekResult) {
- continue;
+ $salesSubGoalMap = [];
+ foreach ($salesSubGoal as $row) {
+ $salesSubGoalMap[$row['category']][$row['subcategory']] = $row['goal'];
}
- $weeksShareResult[$ind] = [];
- $weeksGoalResult[$ind] = [];
- foreach ($weekResult as $weekRow) {
- $monthSum = $speciesMonthTotals
- [$weekRow['store_id']]
- [$weekRow['category']]
- [$weekRow['subcategory']]
- [$weekRow['species']] ?? null;
-
- $monthGoal = $monthSpeciesGoalsMap
- [$weekRow['store_id']]
- [$weekRow['category']]
- [$weekRow['subcategory']]
- [$weekRow['species']] ?? null;
-
-
- if ($monthSum) {
- $weeksShareResult[$ind]
- [$weekRow['category']]
- [$weekRow['subcategory']]
- [$weekRow['species']] = $weekRow['total_sum'] / $monthSum;
- }
- if ($monthSum && $monthGoal) {
- $weeksGoalResult[$ind]
- [$weekRow['category']]
- [$weekRow['subcategory']]
- [$weekRow['species']] = ($weekRow['total_sum'] / $monthSum) * $monthGoal;
- }
+ foreach ($monthSubcategoryShare as &$row) {
+ $cat = $row['category'];
+ $sub = $row['subcategory'];
- }
+ $writeShare = $row['percent_of_month'];
+ $writeGoal = ($catGoalMap[$cat] ?? 0) * $writeShare;
+ $saleGoal = $salesSubGoalMap[$cat][$sub] ?? 0;
+
+ if ($saleGoal > 0 && $writeGoal > 0.1 * $saleGoal) {
+ $row['share'] = 0.1;
+ }
+ }
+ unset($row);
+ $monthSubcategoryGoal = $service->getMonthSubcategoryGoal($monthSubcategoryShare, $monthCategoryGoal);
}
- $weeksShareResult = $service->getHistoricalWeeklySpeciesShare($model->month, $filters, null, 'writeOffs');
- $weeksData = $service->calculateWeeklySpeciesGoals($weeksShareResult['weeksData'], $monthSpeciesGoals) ;
+ $monthSpeciesShare = $service->getMonthSpeciesShareOrWriteOffWeighted($datePlan, $datePlan, $filters, null, $filters['type']);
+ $monthSpeciesGoal = $service->getMonthSpeciesGoalDirty($monthSpeciesShare, $monthSubcategoryGoal);
+ if ($filters['type'] === 'writeOffs') {
+ $salesSpecShare = $service->getMonthSpeciesShareOrWriteOffWeighted($datePlan, $datePlan, $filters, null, 'sales');
+ $salesSpecGoal = $service->getMonthSpeciesGoalDirty($salesSpecShare, $monthSubcategoryGoal);
+ $subGoalMap = [];
+ foreach ($monthSubcategoryGoal as $row) {
+ $subGoalMap[$row['category']][$row['subcategory']] = $row['goal'];
+ }
+ $salesSpecGoalMap = [];
+ foreach ($salesSpecGoal as $row) {
+ $salesSpecGoalMap[$row['category']][$row['subcategory']][$row['species']] = $row['goal'];
+ }
+ foreach ($monthSpeciesShare as &$row) {
+ $cat = $row['category'];
+ $sub = $row['subcategory'];
+ $spec = $row['species'];
+ $writeShare = $row['percent_of_month'];
+ $writeGoal = ($subGoalMap[$cat][$sub] ?? 0) * $writeShare;
+ $saleGoal = $salesSpecGoalMap[$cat][$sub][$spec] ?? 0;
+
+ if ($saleGoal > 0 && $writeGoal > 0.1 * $saleGoal) {
+ $row['share'] = 0.1;
+ }
+ }
+ unset($row);
+
+ $monthSpeciesGoal = $service->getMonthSpeciesGoalDirty($monthSpeciesShare, $monthSubcategoryGoal);
+ }
'model' => $model,
'result' => $monthResult,
'weeksData' => $weeksData,
- //'monthGoal' => $monthSpeciesGoalsMap,
+ 'monthCategoryShare' => $monthCategoryShareResult,
// 'weeksGoalResult' => $weeksGoalResult,
'totals' => $totals,
'storeList' => $storeList,
$start = date('Y-m-01 00:00:00', strtotime("-{$offsetMonths} months", $baseMonth));
$end = date('Y-m-t 23:59:59', strtotime($start));
- $monthStoreTotals = $this->getStoreTotals(
- $storeIds, $start, $productFilter, $type, $end
- );
+
$q = (new Query())
->select([
$sum = (float)$r['month_sum'] * $w;
$weightedSums[$sid][$cat] = ($weightedSums[$sid][$cat] ?? 0) + $sum;
}
- foreach ($monthStoreTotals as $sid => $monthStoreTotal) {
- $monthStoreTotalsWeighted[$sid] = ($monthStoreTotalsWeighted[$sid] ?? 0) + ($monthStoreTotal * $w);
-
- }
}
$result = [];
foreach ($weightedSums as $storeId => $cats) {
$grand = array_sum($cats) ?: 1;
+
+ $unlabeledSum = $cats[''] ?? 0;
+ $labeledCount = count($cats) - (isset($cats['']) ? 1 : 0);
+
+ $distribution = $labeledCount > 0 ? $unlabeledSum / $labeledCount : 0;
+
foreach ($cats as $category => $weightedSum) {
+ if ($category === '') {
+ continue;
+ }
+
+ $adjustedSum = $weightedSum + $distribution;
+
$result[$storeId][] = [
'category' => $category,
'total_sum_cat' => $weightedSum,
- 'total_sum_store' => $monthStoreTotalsWeighted[$storeId],
- 'query_total_sum_store' => $grand,
- 'share_of_total' => round($weightedSum / $grand, 4),
- 'share_of_total_control' => round($weightedSum / $monthStoreTotalsWeighted[$storeId], 4),
+ 'total_sum_store' => $grand,
+ 'share_of_total' => round($adjustedSum / $grand, 4),
];
}
}
-
return $result;
}