$value['title'] = [];
foreach (['offline', 'marketplace', 'online'] as $channel) {
- // 1. Обработка доли (share)
- if (
- isset($forecast[$storeId][$channel]['share']) &&
- is_numeric($forecast[$storeId][$channel]['share'])
- ) {
- $share = $forecast[$storeId][$channel]['share'];
- $value['title'][$channel . '_share'] = round($quantity * $share, 2);
+ // Ищем forecast по store и каналу
+ $channelData = $forecast[$storeId][$channel] ?? [];
+
+ // 1. Если есть share — сохраняем отдельно
+ if (isset($channelData['share']) && is_numeric($channelData['share'])) {
+ $value['title'][$channel . '_share'] = round($quantity * $channelData['share'], 2);
}
- // 2. Обработка подтипов
- if (
- isset($forecast[$storeId][$channel]) &&
- is_array($forecast[$storeId][$channel])
- ) {
- // Исключаем случай, когда это просто ['share' => ...]
- $subtypes = $forecast[$storeId][$channel];
- $subtypesWithoutShare = array_filter($subtypes, fn($k) => $k !== 'share', ARRAY_FILTER_USE_KEY);
-
- if (!empty($subtypesWithoutShare)) {
- $total = array_sum($subtypesWithoutShare);
- $value['title'][$channel] = [
- 'total' => round($quantity * $total, 2),
- 'details' => $subtypesWithoutShare,
- ];
- }
+ // 2. Если есть подтипы (любые ключи, кроме 'share')
+ $details = array_filter(
+ $channelData,
+ fn($k) => $k !== 'share',
+ ARRAY_FILTER_USE_KEY
+ );
+
+ if (!empty($details)) {
+ $total = array_sum($details);
+ $value['title'][$channel] = [
+ 'total' => round($quantity * $total, 2),
+ 'details' => $details,
+ ];
}
}
}