];
}
}
- return $weeklyForecasts;
- }
+ $grouped = [];
+ foreach ($weeklyForecasts as $item) {
+ $storeItem = (int)$item['store_id'];
+ $guid = (string)$item['product_guid'];
+ $group = (string)$item['matrix_group'];
+ $type = (string)$item['type'];
+ $forecastValue = (float)$item['week_forecast'];
+ $grouped[$storeItem][$guid][$type][$group] = $forecastValue;
+ }
+
+ return $grouped;
+ }
+ public function getWeeklyProductsWriteoffsForecast($month, $year, $storeId = null, $weekNumber = null)
+ {
+ $weeksProductForecast = [];
+ $filters = [];
+
+ $dateFrom = date("Y-m-d 00:00:00", strtotime(sprintf('%04d-%02d-01', $year, $month)));
+ $monthYear = date("m-Y", strtotime($dateFrom));
+ $filters['store_id'] = $storeId;
+ $filters['type'] = self::TYPE_WRITE_OFFS;
+ $filters['plan_date'] = $dateFrom;
+
+ $monthSpeciesGoals = $this->calculateFullGoalChain($filters);
+ $monthSpeciesGoalsMap = [];
+ foreach ($monthSpeciesGoals as $monthSpeciesGoal) {
+ $monthSpeciesGoalsMap[$monthSpeciesGoal['store_id']]
+ [$monthSpeciesGoal['category']]
+ [$monthSpeciesGoal['subcategory']]
+ [$monthSpeciesGoal['species']] = $monthSpeciesGoal['goal'];
+ }
+
+ $weeksShareResult = $this->getHistoricalWeeklySpeciesShare($monthYear, $filters, null, 'writeOffs');
+ $weeksData = $this->calculateWeeklySpeciesGoals($weeksShareResult, $monthSpeciesGoals);
+
+ foreach ($weeksData as $r) {
+ $forecasts = $this->calculateWeekForecastSpeciesProducts($r['category'], $r['subcategory'], $r['species'], $r['store_id'], $r['weekly_goal']);
+
+ foreach ($forecasts as $forecast) {
+ $weeksProductForecast[] = [
+ 'category' => $forecast['category'] ?? '',
+ 'subcategory' => $forecast['subcategory'] ?? '',
+ 'species' => $forecast['species'] ?? '',
+ 'product_id' => $forecast['product_id'] ?? '',
+ 'name' => $forecast['name'] ?? '',
+ 'price' => $forecast['price'] ?? '',
+ 'goal' => $forecast['goal'] ?? 0,
+ 'forecast' => $forecast['forecast'] ?? 0,
+ 'week' => $r['week'],
+ ];
+ }
+ }
+
+ return $weeksProductForecast;
+ }
+
}