@php $user = \Illuminate\Support\Facades\Auth::user(); $stabileHasRiscaldamento = static function (?\App\Models\Stabile $stabile): bool { if (! $stabile) { return false; } if ((bool) ($stabile->riscaldamento_centralizzato ?? false)) { return true; } $rateRiscaldamento = $stabile->rate_riscaldamento_mesi ?? []; if (is_string($rateRiscaldamento)) { $rateRiscaldamento = json_decode($rateRiscaldamento, true); } if (is_array($rateRiscaldamento) && $rateRiscaldamento !== []) { return true; } $config = $stabile->configurazione_avanzata ?? []; if (is_string($config)) { $config = json_decode($config, true); } if (is_array($config) && array_key_exists('mostra_riscaldamento', $config) && $config['mostra_riscaldamento']) { return true; } return \Illuminate\Support\Facades\DB::table('gestioni_contabili') ->where('stabile_id', $stabile->id) ->where('tipo_gestione', 'riscaldamento') ->exists(); }; $stabileLabel = static function (?\App\Models\Stabile $stabile): string { if (! $stabile) { return 'Seleziona stabile'; } $primary = trim((string) ($stabile->codice_operatore ?: $stabile->codice_stabile)); $mnemo = trim((string) ($stabile->cod_stabile ?? '')); if ($mnemo !== '' && $primary !== '' && $mnemo !== $primary) { return $mnemo . ' · ' . $primary; } return $primary !== '' ? $primary : ($mnemo !== '' ? $mnemo : ('#' . (string) $stabile->id)); }; $stabili = collect(); $stabileAttivo = null; if ($user instanceof \App\Models\User) { $stabili = \App\Support\StabileContext::accessibleStabili($user); $activeId = \App\Support\StabileContext::resolveActiveStabileId($user); $stabileAttivo = $activeId ? $stabili->firstWhere('id', $activeId) : null; } $gestioni = \App\Support\GestioneContext::availableGestioni(); $gestioneAttiva = \App\Support\GestioneContext::resolveActiveGestione($user instanceof \App\Models\User ? $user : null); $annoGestione = \App\Support\AnnoGestioneContext::resolveActiveAnno($user instanceof \App\Models\User ? $user : null); $labelGestione = $gestioni[$gestioneAttiva] ?? (string) $gestioneAttiva; $haRiscaldamento = $stabileHasRiscaldamento($stabileAttivo); @endphp