int $selectedYear,
array $medianProductsWithoutHistory
): array {
- $accumulator = [];
- $prices = [];
- $guidToGroup = [];
-
+ $accumulator = [];
foreach ($medianProductsWithoutHistory as $guid => $data) {
- $q = (float)$data['weightedValue'];
- if ($q <= 0) continue;
-
- $price = self::getPriceForProductAtOffsetMonthWeekly(
- $guid, $selectedYear, $selectedMonth, $storeId, 2
- );
- $prices[$guid] = $price;
-
- $cat = $data['category'];
- $sub = $data['subcategory'];
- $sp = $data['species'];
- $groupKey = implode('|', [$cat,$sub,$sp]);
- $guidToGroup[$guid] = $groupKey;
+ $cat = $data['category'];
+ $sub = $data['subcategory'];
+ $sp = $data['species'];
+ $groupKey = implode('|', [$cat, $sub, $sp]);
+ $q = (float)$data['weightedValue'];
if (!isset($accumulator[$groupKey])) {
$accumulator[$groupKey] = [
'forecasts' => [],
];
}
- $accumulator[$groupKey]['goal'] += $q * $price;
- }
- foreach ($medianProductsWithoutHistory as $guid => $qty) {
- $groupKey = $guidToGroup[$guid];
- $goal = $accumulator[$groupKey]['goal'];
- $price = $prices[$guid] ?? 0.0;
- $accumulator[$groupKey]['forecasts'][$guid] = $qty['weightedValue'];
+ $accumulator[$groupKey]['forecasts'][$guid] = $q;
+ if ($q > 0) {
+ $price = self::getPriceForProductAtOffsetMonthWeekly(
+ $guid, $selectedYear, $selectedMonth, $storeId, 2
+ );
+ $accumulator[$groupKey]['goal'] += $q * $price;
+ }
}
return array_values($accumulator);