orderBy('name')->get(); return view('municipalities.index', compact('municipalities')); } public function show(\App\Models\Municipality $municipality) { if (!$municipality->is_active) { abort(404); } $municipality->load(['contracts.meta']); $contracts = $municipality->contracts; // M365 Statistics $m365Contracts = $contracts->where('type', 'microsoft_365'); // Fetch all active license levels to initialize stats keys $licenseLevels = \App\Models\LicenseLevel::active()->pluck('name')->toArray(); $m365Stats = array_fill_keys($licenseLevels, 0); $m365Stats['Autre'] = 0; // Fallback category foreach ($m365Contracts as $contract) { $level = $contract->meta->where('key', 'm365_license_level')->first()?->value; $quantity = (int) $contract->meta->where('key', 'm365_quantity')->first()?->value ?? 0; if ($level && array_key_exists($level, $m365Stats)) { $m365Stats[$level] += $quantity; } else { $m365Stats['Autre'] += $quantity; } } return view('municipalities.show', compact('municipality', 'contracts', 'm365Stats')); } }