Files
BRISTO/app/Http/Controllers/MunicipalityController.php
jeremy bayse 89a369964d Premier commit
2026-02-09 11:27:21 +01:00

47 lines
1.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MunicipalityController extends Controller
{
public function index()
{
$municipalities = \App\Models\Municipality::active()->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'));
}
}