foreach (['offline', 'marketplace', 'online'] as $channel) {
$channelData = $forecast[$storeId][$channel] ?? [];
+ $titleData = [];
- // Prioritize share if it exists
+ // Add share-based value if it exists
if (isset($channelData['share']) && is_numeric($channelData['share'])) {
- $value['title'][$channel] = round($quantity * $channelData['share'], 2);
- continue; // Skip product-specific data if share exists
+ $titleData = [round($quantity * $channelData['share'], 2)];
}
- // Use product-specific forecast if available
+ // Add product-specific details if available
if (isset($productData['product_id']) && isset($forecast[$storeId][$productData['product_id']][$channel])) {
$details = $forecast[$storeId][$productData['product_id']][$channel];
$total = array_sum($details);
- $value['title'][$channel] = [
- round($quantity * $total, 2),
+ $titleData = [
+ isset($titleData[0]) ? $titleData[0] : round($quantity * $total, 2),
'details' => array_map(fn($val) => round($val, 2), $details),
];
- } else {
- $value['title'][$channel] = []; // Empty if neither share nor details exist
}
+
+ $value['title'][$channel] = $titleData ?: [];
}
}
}