hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']); } public function mount(): void { $user = Auth::user(); if (! $user instanceof User) { return; } $activeId = StabileContext::resolveActiveStabileId($user); if (! $activeId) { return; } $this->stabileAttivo = StabileContext::accessibleStabili($user)->firstWhere('id', $activeId); } protected function getHeaderActions(): array { return [ Action::make('apri_documenti') ->label('Archivio documenti') ->icon('heroicon-o-folder-open') ->url(fn(): string => DocumentiArchivio::getUrl(panel: 'admin-filament')), ]; } public function getStatsProperty(): array { $stabileId = (int) ($this->stabileAttivo?->id ?? 0); if ($stabileId <= 0 || ! $this->canQueryAssemblee()) { return ['totale' => 0, 'convocate' => 0, 'in_calendario' => 0, 'con_verbale' => 0]; } try { $query = Assemblea::query()->where('stabile_id', $stabileId); $hasVerbali = class_exists(AssembleaVerbale::class) && Schema::hasTable('assemblee_verbali'); return [ 'totale' => (clone $query)->count(), 'convocate' => (clone $query)->where('stato', 'convocata')->count(), 'in_calendario' => (clone $query) ->where(function ($builder): void { $today = now(); $builder->whereDate('data_prima_convocazione', '>=', $today->toDateString()) ->orWhereDate('data_seconda_convocazione', '>=', $today->toDateString()); }) ->count(), 'con_verbale' => $hasVerbali ? (clone $query)->whereHas('verbale')->count() : 0, ]; } catch (QueryException) { return ['totale' => 0, 'convocate' => 0, 'in_calendario' => 0, 'con_verbale' => 0]; } } public function getAssembleeRowsProperty(): Collection { $stabileId = (int) ($this->stabileAttivo?->id ?? 0); if ($stabileId <= 0 || ! $this->canQueryAssemblee()) { return collect(); } try { $query = Assemblea::query() ->where('stabile_id', $stabileId) ->orderByDesc('data_seconda_convocazione') ->orderByDesc('data_prima_convocazione') ->orderByDesc('id') ->limit(16); if (Schema::hasTable('assemblee_verbali')) { $query->with(['verbale:id,assemblea_id']); } $withCount = []; if (Schema::hasTable('ordine_giorno')) { $withCount[] = 'ordineGiorno'; } if (Schema::hasTable('convocazioni')) { $withCount[] = 'convocazioni'; } if ($withCount !== []) { $query->withCount($withCount); } return $query->get()->map(function (Assemblea $assemblea): Assemblea { $assemblea->ordine_giorno_count = (int) ($assemblea->ordine_giorno_count ?? 0); $assemblea->convocazioni_count = (int) ($assemblea->convocazioni_count ?? 0); $assemblea->presenze_count = 0; $assemblea->documenti_count = 0; if (! $assemblea->relationLoaded('verbale')) { $assemblea->setRelation('verbale', null); } return $assemblea; }); } catch (QueryException) { return collect(); } } private function canQueryAssemblee(): bool { try { return Schema::hasTable((new Assemblea())->getTable()); } catch (QueryException) { return false; } } }