netgescon-day0/app/Filament/Pages/Contabilita/RegolePrimaNota.php

172 lines
6.3 KiB
PHP
Executable File

<?php
namespace App\Filament\Pages\Contabilita;
use App\Models\User;
use App\Modules\Contabilita\Models\PianoConti;
use App\Modules\Contabilita\Models\RegolaPrimaNota;
use App\Support\StabileContext;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Tables\Columns\IconColumn;
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\DB;
use UnitEnum;
class RegolePrimaNota extends Page implements HasTable
{
use InteractsWithTable;
protected static ?string $navigationLabel = 'Regole prima nota';
protected static ?string $title = 'Regole prima nota';
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-adjustments-vertical';
protected static UnitEnum|string|null $navigationGroup = 'Contabilità';
protected static ?int $navigationSort = 11;
protected static ?string $slug = 'contabilita/regole-prima-nota';
protected string $view = 'filament.pages.gescon.table';
public static function canAccess(): bool
{
$user = Auth::user();
if (! $user instanceof User) {
return false;
}
return $user->hasRole('super-admin');
}
public function mount(): void
{
$this->mountInteractsWithTable();
}
protected function getHeaderActions(): array
{
return [
Action::make('nuova_regola')
->label('Nuova regola')
->icon('heroicon-o-plus')
->form([
Select::make('origine')
->label('Origine')
->options([
'banca' => 'Banca',
'fattura' => 'Fattura',
'rata' => 'Rata',
'incasso' => 'Incasso',
'altro' => 'Altro',
])
->default('banca')
->required(),
TextInput::make('chiave')
->label('Chiave')
->helperText('Esempio banca: causale 048/208/219/013/008')
->required(),
TextInput::make('label')
->label('Nome regola')
->nullable(),
Select::make('conto_dare_id')
->label('Conto Dare')
->options(fn() => PianoConti::query()->orderBy('codice')->get()->mapWithKeys(fn(PianoConti $c) => [(string) $c->id => $c->codice . ' - ' . $c->descrizione])->all())
->searchable()
->nullable(),
Select::make('conto_avere_id')
->label('Conto Avere')
->options(fn() => PianoConti::query()->orderBy('codice')->get()->mapWithKeys(fn(PianoConti $c) => [(string) $c->id => $c->codice . ' - ' . $c->descrizione])->all())
->searchable()
->nullable(),
Textarea::make('note')
->label('Note')
->rows(2)
->nullable(),
])
->action(function (array $data): void {
$user = Auth::user();
if (! $user instanceof User) {
return;
}
$stabileId = StabileContext::resolveActiveStabileId($user);
if (! $stabileId) {
Notification::make()->title('Seleziona uno stabile')->danger()->send();
return;
}
try {
DB::transaction(function () use ($stabileId, $data) {
RegolaPrimaNota::query()->create([
'stabile_id' => $stabileId,
'origine' => (string) $data['origine'],
'chiave' => trim((string) $data['chiave']),
'label' => isset($data['label']) ? trim((string) $data['label']) : null,
'note' => $data['note'] ?? null,
'conto_dare_id' => $data['conto_dare_id'] ? (int) $data['conto_dare_id'] : null,
'conto_avere_id' => $data['conto_avere_id'] ? (int) $data['conto_avere_id'] : null,
'attiva' => true,
]);
});
} catch (\Throwable $e) {
Notification::make()->title('Errore')->body($e->getMessage())->danger()->send();
return;
}
Notification::make()->title('Regola creata')->success()->send();
}),
];
}
protected function getTableQuery(): Builder
{
$user = Auth::user();
if (! $user instanceof User) {
return RegolaPrimaNota::query()->whereRaw('1 = 0');
}
$activeStabileId = StabileContext::resolveActiveStabileId($user);
if (! $activeStabileId) {
return RegolaPrimaNota::query()->whereRaw('1 = 0');
}
return RegolaPrimaNota::query()
->where('stabile_id', $activeStabileId)
->orderBy('origine')
->orderBy('chiave');
}
public function table(Table $table): Table
{
return $table
->striped()
->columns([
TextColumn::make('origine')->label('Origine')->badge(),
TextColumn::make('chiave')->label('Chiave')->sortable()->searchable(),
TextColumn::make('label')->label('Nome')->wrap()->toggleable(),
TextColumn::make('contoDare.codice')->label('Dare')->toggleable(),
TextColumn::make('contoAvere.codice')->label('Avere')->toggleable(),
IconColumn::make('attiva')->label('Attiva')->boolean(),
]);
}
}