* 'without_history' => [...],
* ]
*/
- private static function analyzeHistory($salesHistory, $periods)
+ private static function analyzeHistory(array $salesHistory, array $periods)
{
$productsWithHistory = [];
$productsWithoutHistory = [];
- foreach ($salesHistory as $guid => $monthsData) {
+ foreach ($salesHistory as $guid => $info) {
+ $monthsData = $info['data'];
+ $metaFields = [
+ 'category' => $info['category'],
+ 'subcategory' => $info['subcategory'],
+ 'species' => $info['species'],
+ ];
$hasHistoryInAllPeriods = true;
- $weeklySalesData = [];
+ $weeklySalesData = [];
foreach ($periods as $periodKey => $periodData) {
$weeksCount = count($periodData['weeks']);
if (!isset($monthsData[$periodKey])) {
$hasHistoryInAllPeriods = false;
- // Заполняем пустыми значениями столько недель, сколько их есть в периоде.
$weekData = array_fill(0, $weeksCount, 0);
} else {
- $weekData = [];
+ $weekData = [];
$activeWeeks = 0;
- for ($weekIndex = 0; $weekIndex < $weeksCount; $weekIndex++) {
- $salesCount = isset($monthsData[$periodKey][$weekIndex]) ? $monthsData[$periodKey][$weekIndex] : 0;
- $weekData[$weekIndex] = $salesCount;
- if ($weekIndex == 4) {
- continue; //пропускаем 5 неполную неделю в учете активных продаж
- }
- if ($salesCount > 0) {
+ for ($i = 0; $i < $weeksCount; $i++) {
+ $count = $monthsData[$periodKey][$i] ?? 0;
+ $weekData[$i] = $count;
+
+ if ($i < 4 && $count > 0) {
$activeWeeks++;
}
}
$hasHistoryInAllPeriods = false;
}
}
+
$weeklySalesData[$periodKey] = $weekData;
}
- $productData = [
- 'guid' => $guid,
- 'weekly_sales' => $weeklySalesData,
- ];
+ $item = array_merge(
+ ['guid' => $guid],
+ $metaFields,
+ ['weekly_sales' => $weeklySalesData]
+ );
if ($hasHistoryInAllPeriods) {
- $productsWithHistory[] = $productData;
+ $productsWithHistory[] = $item;
} else {
- $productsWithoutHistory[] = $productData;
+ $productsWithoutHistory[] = $item;
}
}