134 lines
5.7 KiB
PHP
Executable File
134 lines
5.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Filament\Pages\SuperAdmin;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Contabilita\Models\CausaleBancaria;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
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\Schema;
|
|
use UnitEnum;
|
|
|
|
class CausaliBancarie extends Page implements HasTable
|
|
{
|
|
use InteractsWithTable;
|
|
|
|
protected static ?string $navigationLabel = 'Causali bancarie';
|
|
|
|
protected static ?string $title = 'Causali bancarie';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-tag';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Contabilità';
|
|
|
|
protected static ?int $navigationSort = 20;
|
|
|
|
protected static ?string $slug = 'contabilita/causali-bancarie';
|
|
|
|
protected string $view = 'filament.pages.gescon.table';
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user instanceof User
|
|
&& ($user->hasAnyRole(['super-admin', 'admin']) || $user->can('contabilita.causali'))
|
|
&& Schema::hasTable('contabilita_causali_bancarie');
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->mountInteractsWithTable();
|
|
}
|
|
|
|
protected function getTableQuery(): Builder
|
|
{
|
|
return CausaleBancaria::query();
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->striped()
|
|
->defaultSort('codice')
|
|
->columns([
|
|
TextColumn::make('banca')->label('Banca')->sortable()->toggleable(isToggledHiddenByDefault: true),
|
|
TextColumn::make('codice')->label('Causale')->searchable()->sortable(),
|
|
TextColumn::make('interbancaria')->label('Interb.')->toggleable(),
|
|
TextColumn::make('descrizione')->label('Descrizione')->searchable()->sortable()->wrap(),
|
|
IconColumn::make('attivo')->label('Attivo')->boolean()->sortable(),
|
|
TextColumn::make('updated_at')->label('Aggiornato')->dateTime('d/m/Y H:i')->toggleable(isToggledHiddenByDefault: true),
|
|
])
|
|
->headerActions([
|
|
Action::make('create')
|
|
->label('Nuova causale')
|
|
->icon('heroicon-o-plus')
|
|
->form([
|
|
Select::make('banca')
|
|
->label('Banca')
|
|
->native(false)
|
|
->options([
|
|
'UNICREDIT' => 'UNICREDIT',
|
|
'INTESA' => 'INTESA',
|
|
])
|
|
->default('UNICREDIT')
|
|
->required(),
|
|
TextInput::make('codice')->label('Causale')->required()->maxLength(20),
|
|
TextInput::make('interbancaria')->label('Interbancaria')->maxLength(10),
|
|
TextInput::make('descrizione')->label('Descrizione')->required()->maxLength(255),
|
|
Toggle::make('attivo')->label('Attivo')->default(true),
|
|
])
|
|
->action(function (array $data): void {
|
|
CausaleBancaria::query()->create([
|
|
'banca' => trim((string) ($data['banca'] ?? 'UNICREDIT')),
|
|
'codice' => trim((string) ($data['codice'] ?? '')),
|
|
'interbancaria' => isset($data['interbancaria']) && is_string($data['interbancaria']) && trim($data['interbancaria']) !== '' ? trim((string) $data['interbancaria']) : null,
|
|
'descrizione' => trim((string) ($data['descrizione'] ?? '')),
|
|
'attivo' => (bool) ($data['attivo'] ?? true),
|
|
]);
|
|
}),
|
|
])
|
|
->actions([
|
|
Action::make('edit')
|
|
->label('Modifica')
|
|
->icon('heroicon-o-pencil-square')
|
|
->form([
|
|
TextInput::make('interbancaria')->label('Interbancaria')->maxLength(10),
|
|
TextInput::make('descrizione')->label('Descrizione')->required()->maxLength(255),
|
|
Toggle::make('attivo')->label('Attivo')->default(true),
|
|
])
|
|
->fillForm(fn(CausaleBancaria $record): array => [
|
|
'interbancaria' => $record->interbancaria,
|
|
'descrizione' => $record->descrizione,
|
|
'attivo' => (bool) $record->attivo,
|
|
])
|
|
->action(function (CausaleBancaria $record, array $data): void {
|
|
$record->fill([
|
|
'interbancaria' => isset($data['interbancaria']) && is_string($data['interbancaria']) && trim($data['interbancaria']) !== '' ? trim((string) $data['interbancaria']) : null,
|
|
'descrizione' => trim((string) ($data['descrizione'] ?? '')),
|
|
'attivo' => (bool) ($data['attivo'] ?? true),
|
|
]);
|
|
$record->save();
|
|
}),
|
|
|
|
Action::make('delete')
|
|
->label('Elimina')
|
|
->icon('heroicon-o-trash')
|
|
->color('danger')
|
|
->requiresConfirmation()
|
|
->action(fn(CausaleBancaria $record) => $record->delete()),
|
|
]);
|
|
}
|
|
}
|