232 lines
8.4 KiB
PHP
232 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Contabilita;
|
|
|
|
use App\Models\GestioneContabile;
|
|
use App\Models\Stabile;
|
|
use App\Models\User;
|
|
use App\Modules\Contabilita\Models\ChiusuraGestione;
|
|
use App\Modules\Contabilita\Services\ChiusuraGestioneService;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Concerns\InteractsWithTable;
|
|
use Filament\Tables\Contracts\HasTable;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use UnitEnum;
|
|
|
|
class ChiusureGestioneArchivio extends Page implements HasTable
|
|
{
|
|
use InteractsWithTable;
|
|
|
|
protected static ?string $navigationLabel = 'Chiusure gestione';
|
|
|
|
protected static ?string $title = 'Chiusure gestione';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-lock-closed';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Contabilità';
|
|
|
|
protected static ?int $navigationSort = 11;
|
|
|
|
protected static ?string $slug = 'contabilita/chiusure-gestione';
|
|
|
|
protected string $view = 'filament.pages.contabilita.chiusure-gestione-archivio';
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
if (! $user instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
if ($user->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<string,string>
|
|
*/
|
|
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();
|
|
}
|
|
}
|