141 lines
5.9 KiB
PHP
141 lines
5.9 KiB
PHP
<?php
|
|
namespace App\Filament\Pages\RevisioneContabile;
|
|
|
|
use App\Models\RevisionePrecedenteAmministratore;
|
|
use App\Models\RubricaUniversale;
|
|
use App\Models\Stabile;
|
|
use App\Models\User;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\WithPagination;
|
|
use UnitEnum;
|
|
|
|
class PrecedentiAmministratoriElenco extends Page
|
|
{
|
|
use WithPagination;
|
|
|
|
protected static ?string $title = 'Revisione · Precedente amministratore';
|
|
|
|
protected static ?string $slug = 'revisione-contabile/precedente-amministratore';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-clipboard-document-check';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Revisione Contabile';
|
|
|
|
protected static ?int $navigationSort = 10;
|
|
|
|
protected static bool $shouldRegisterNavigation = true;
|
|
|
|
protected string $view = 'filament.pages.revisione-contabile.precedenti-amministratori-elenco';
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('nuova_revisione')
|
|
->label('Nuova revisione')
|
|
->icon('heroicon-o-plus')
|
|
->color('success')
|
|
->modalHeading('Nuova revisione (precedente amministratore)')
|
|
->form([
|
|
Select::make('stabile_id')
|
|
->label('Stabile')
|
|
->required()
|
|
->searchable()
|
|
->options(fn() => Stabile::query()->orderBy('denominazione')->pluck('denominazione', 'id')->all()),
|
|
|
|
Select::make('rubrica_id')
|
|
->label('Precedente amministratore (Rubrica)')
|
|
->required()
|
|
->searchable()
|
|
->getSearchResultsUsing(function (string $search): array {
|
|
return RubricaUniversale::query()
|
|
->ricerca($search)
|
|
->limit(30)
|
|
->get()
|
|
->mapWithKeys(fn(RubricaUniversale $r) => [$r->id => $r->nome_completo . ($r->email ? (' · ' . $r->email) : '')])
|
|
->all();
|
|
})
|
|
->getOptionLabelUsing(function ($value): ?string {
|
|
if (! $value) {
|
|
return null;
|
|
}
|
|
$r = RubricaUniversale::query()->find($value);
|
|
return $r ? ($r->nome_completo . ($r->email ? (' · ' . $r->email) : '')) : null;
|
|
}),
|
|
|
|
TextInput::make('gestionale')
|
|
->label('Gestionale')
|
|
->placeholder('Es. Danea, TeamSystem, Gestionale X')
|
|
->maxLength(255),
|
|
|
|
FileUpload::make('passaggio_consegne_path')
|
|
->label('Passaggio di consegne (file)')
|
|
->disk('local')
|
|
->directory('revisione/passaggi-consegne')
|
|
->preserveFilenames()
|
|
->openable()
|
|
->downloadable(),
|
|
|
|
Textarea::make('note')
|
|
->label('Note')
|
|
->rows(4),
|
|
])
|
|
->action(function (array $data): void {
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
Notification::make()->title('Utente non valido')->danger()->send();
|
|
return;
|
|
}
|
|
|
|
$rubrica = RubricaUniversale::query()->find((int) ($data['rubrica_id'] ?? 0));
|
|
if (! $rubrica) {
|
|
Notification::make()->title('Contatto rubrica non trovato')->danger()->send();
|
|
return;
|
|
}
|
|
|
|
$record = RevisionePrecedenteAmministratore::query()->create([
|
|
'stabile_id' => (int) $data['stabile_id'],
|
|
'rubrica_id' => (int) $rubrica->id,
|
|
'precedente_amministratore' => (string) ($rubrica->nome_completo ?? '—'),
|
|
'gestionale' => isset($data['gestionale']) ? trim((string) $data['gestionale']) : null,
|
|
'passaggio_consegne_path' => isset($data['passaggio_consegne_path']) ? (string) $data['passaggio_consegne_path'] : null,
|
|
'note' => isset($data['note']) ? trim((string) $data['note']) : null,
|
|
'creato_da' => (int) $user->id,
|
|
'modificato_da' => (int) $user->id,
|
|
]);
|
|
|
|
Notification::make()
|
|
->title('Revisione creata')
|
|
->body('Record creato correttamente.')
|
|
->success()
|
|
->send();
|
|
|
|
$this->redirect(PrecedentiAmministratoriDettaglio::getUrl(['record' => (int) $record->id], panel: 'admin-filament'));
|
|
}),
|
|
];
|
|
}
|
|
|
|
public function getRows(): LengthAwarePaginator
|
|
{
|
|
return RevisionePrecedenteAmministratore::query()
|
|
->with(['stabile'])
|
|
->orderByDesc('created_at')
|
|
->paginate(15);
|
|
}
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']);
|
|
}
|
|
}
|