hasAnyRole(['super-admin', 'admin'])) { return true; } return $user->can('contabilita.prima-nota'); } public function mount(): void { $this->mountInteractsWithTable(); } protected function getHeaderActions(): array { return [ Action::make('vai_prima_nota') ->label('Vai a prima nota') ->icon('heroicon-o-book-open') ->url(fn (): string => PrimaNotaArchivio::getUrl()), ]; } protected function getTableQuery(): Builder { $user = Auth::user(); if (! $user instanceof User) { return ChiusuraGestione::query()->whereRaw('1 = 0'); } $stabileId = StabileContext::resolveActiveStabileId($user); if (! $stabileId) { return ChiusuraGestione::query()->whereRaw('1 = 0'); } return ChiusuraGestione::query() ->where('stabile_id', $stabileId) ->orderByDesc('data_chiusura') ->orderByDesc('id'); } public function table(Table $table): Table { return $table ->striped() ->columns([ TextColumn::make('gestioneContabile.denominazione') ->label('Gestione') ->wrap(), TextColumn::make('data_chiusura') ->label('Data chiusura') ->date('d/m/Y') ->sortable(), TextColumn::make('stato') ->label('Stato') ->badge(), TextColumn::make('registrazione_chiusura_id') ->label('Reg. chiusura') ->toggleable(isToggledHiddenByDefault: true), TextColumn::make('registrazione_storno_id') ->label('Reg. storno') ->toggleable(isToggledHiddenByDefault: true), ]) ->actions([ Action::make('genera_apertura') ->label('Genera apertura') ->icon('heroicon-o-arrow-path') ->visible(fn (ChiusuraGestione $record) => $record->stato === 'chiusa') ->form([ Select::make('gestione_destinazione_id') ->label('Gestione destinazione') ->options(fn () => $this->getGestioniOptions()) ->searchable() ->required(), DatePicker::make('data_apertura') ->label('Data apertura') ->required() ->default(fn () => now()->toDateString()), ]) ->action(function (ChiusuraGestione $record, array $data): void { $user = Auth::user(); if (! $user instanceof User) { Notification::make()->title('Utente non valido')->danger()->send(); return; } $stabileId = StabileContext::resolveActiveStabileId($user); if (! $stabileId) { Notification::make()->title('Seleziona uno stabile')->danger()->send(); return; } $gestioneDest = GestioneContabile::query()->where('stabile_id', $stabileId)->find((int) $data['gestione_destinazione_id']); if (! $gestioneDest) { Notification::make()->title('Gestione destinazione non trovata')->danger()->send(); return; } try { $service = app(ChiusuraGestioneService::class); $service->generaAperturaDaChiusura($user, $stabileId, $record, $gestioneDest, (string) $data['data_apertura']); Notification::make()->title('Apertura generata in prima nota')->success()->send(); } catch (\Throwable $e) { Notification::make()->title('Errore apertura')->body($e->getMessage())->danger()->send(); } }), Action::make('riapri_controllata') ->label('Riapri (storno)') ->icon('heroicon-o-lock-open') ->color('danger') ->visible(fn (ChiusuraGestione $record) => $record->stato === 'chiusa') ->requiresConfirmation() ->action(function (ChiusuraGestione $record): void { $user = Auth::user(); if (! $user instanceof User) { Notification::make()->title('Utente non valido')->danger()->send(); return; } $stabileId = StabileContext::resolveActiveStabileId($user); if (! $stabileId) { Notification::make()->title('Seleziona uno stabile')->danger()->send(); return; } try { $service = app(ChiusuraGestioneService::class); $service->stornaChiusura($user, $stabileId, $record); Notification::make()->title('Gestione riaperta (storno registrato)')->success()->send(); } catch (\Throwable $e) { Notification::make()->title('Errore riapertura')->body($e->getMessage())->danger()->send(); } }), ]); } public function getActiveStabile(): ?Stabile { $user = Auth::user(); if (! $user instanceof User) { return null; } return StabileContext::getActiveStabile($user); } /** * @return array */ private function getGestioniOptions(): array { if (! Schema::hasTable('gestioni_contabili')) { return []; } $user = Auth::user(); if (! $user instanceof User) { return []; } $stabileId = StabileContext::resolveActiveStabileId($user); if (! $stabileId) { return []; } return GestioneContabile::query() ->where('stabile_id', $stabileId) ->orderByDesc('anno_gestione') ->orderBy('tipo_gestione') ->orderBy('numero_straordinaria') ->get() ->mapWithKeys(fn (GestioneContabile $g) => [ (string) $g->id => $g->denominazione . ' (' . $g->tipo_gestione . ' ' . $g->anno_gestione . ($g->numero_straordinaria ? ' #' . $g->numero_straordinaria : '') . ')', ]) ->all(); } }