int $selectedMonth,
int $selectedYear,
array $medianProductsWithoutHistory
- ): array
- {
+ ): array {
+ $accumulator = [];
+ $prices = [];
+ $guidToGroup = [];
- $accumulator = [];
- // var_dump($productsWithoutHistory); die();
- foreach ($medianProductsWithoutHistory as $guid => $info) {
- $quantity = (float)$info;
- if ($quantity <= 0) {
- continue;
- }
- // цена за два месяца назад
+ foreach ($medianProductsWithoutHistory as $guid => $qty) {
+ $q = (float)$qty;
+ if ($q <= 0) continue;
+
$price = self::getPriceForProductAtOffsetMonthWeekly(
- $guid,
- $selectedYear,
- $selectedMonth,
- $storeId,
- 2
+ $guid, $selectedYear, $selectedMonth, $storeId, 2
);
+ $prices[$guid] = $price;
- $cost = $quantity * $price;
-
- /** @var Products1cNomenclature $nom */
$nom = Products1cNomenclature::findOne($guid);
$cat = $nom->category ?? '';
$sub = $nom->subcategory ?? '';
$sp = $nom->species ?? '';
+ $groupKey = implode('|', [$cat,$sub,$sp]);
+ $guidToGroup[$guid] = $groupKey;
- $key = implode('|', [$cat, $sub, $sp]);
- if (!isset($accumulator[$key])) {
- $accumulator[$key] = [
+ if (!isset($accumulator[$groupKey])) {
+ $accumulator[$groupKey] = [
'store_id' => $storeId,
'category' => $cat,
'subcategory' => $sub,
'species' => $sp,
'month' => $selectedMonth,
'year' => $selectedYear,
- 'goal' => 0.0,
+ 'goal' => 0.0,
+ 'forecasts' => [],
];
}
- $accumulator[$key]['goal'] += $cost;
+ $accumulator[$groupKey]['goal'] += $q * $price;
+ }
+
+ foreach ($medianProductsWithoutHistory as $guid => $_) {
+ $groupKey = $guidToGroup[$guid];
+ $goal = $accumulator[$groupKey]['goal'];
+ $price = $prices[$guid] ?? 0.0;
+ $accumulator[$groupKey]['forecasts'][$guid] =
+ $price > 0.0
+ ? ($goal / $price)
+ : 0.0;
}
return array_values($accumulator);