152 lines
5.1 KiB
PHP
152 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Strumenti;
|
|
|
|
use App\Models\DocumentoCollegato;
|
|
use App\Models\Stabile;
|
|
use App\Models\User;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Pages\Page;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Concerns\InteractsWithTable;
|
|
use Filament\Tables\Contracts\HasTable;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use UnitEnum;
|
|
|
|
class DocumentiCollegatiArchivio extends Page implements HasTable
|
|
{
|
|
use InteractsWithTable;
|
|
|
|
protected static bool $shouldRegisterNavigation = false;
|
|
|
|
protected static ?string $navigationLabel = 'Documenti collegati';
|
|
|
|
protected static ?string $title = 'Documenti collegati';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-folder-open';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Strumenti';
|
|
|
|
protected static ?int $navigationSort = 60;
|
|
|
|
protected static ?string $slug = 'strumenti/documenti-collegati';
|
|
|
|
protected string $view = 'filament.pages.gescon.table';
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->mountInteractsWithTable();
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
protected function getTableQuery(): Builder
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return DocumentoCollegato::query()->whereRaw('1 = 0');
|
|
}
|
|
|
|
$query = DocumentoCollegato::query()->with(['stabile', 'contatto']);
|
|
|
|
// Se c'è uno stabile attivo in contesto, filtro su quello (più utile per operatore)
|
|
$activeStabile = StabileContext::getActiveStabile($user);
|
|
if ($activeStabile instanceof Stabile) {
|
|
$query->where('stabile_id', (int) $activeStabile->id);
|
|
return $query;
|
|
}
|
|
|
|
// Altrimenti limito agli stabili accessibili (non super-admin/admin)
|
|
if ($user->hasAnyRole(['super-admin', 'admin'])) {
|
|
return $query;
|
|
}
|
|
|
|
$allowedIds = StabileContext::accessibleStabili($user)
|
|
->pluck('id')
|
|
->map(fn ($v) => (int) $v)
|
|
->values();
|
|
|
|
if ($allowedIds->isEmpty()) {
|
|
return DocumentoCollegato::query()->whereRaw('1 = 0');
|
|
}
|
|
|
|
return $query->whereIn('stabile_id', $allowedIds);
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->striped()
|
|
->defaultSort('created_at', 'desc')
|
|
->columns([
|
|
TextColumn::make('protocollo_formattato')
|
|
->label('Protocollo')
|
|
->searchable(['numero_protocollo'])
|
|
->copyable()
|
|
->toggleable(),
|
|
TextColumn::make('stabile.denominazione')
|
|
->label('Stabile')
|
|
->searchable()
|
|
->toggleable(),
|
|
TextColumn::make('titolo')
|
|
->label('Titolo')
|
|
->searchable()
|
|
->limit(50),
|
|
TextColumn::make('tipo_documento')
|
|
->label('Tipo')
|
|
->badge()
|
|
->toggleable(),
|
|
TextColumn::make('data_sottoscrizione')
|
|
->label('Data')
|
|
->date('d-m-Y')
|
|
->toggleable(),
|
|
TextColumn::make('stato_documento')
|
|
->label('Stato')
|
|
->badge()
|
|
->toggleable(),
|
|
])
|
|
->filters([
|
|
SelectFilter::make('tipo_documento')
|
|
->label('Tipo')
|
|
->options([
|
|
'contratto' => 'Contratto',
|
|
'assicurazione' => 'Assicurazione',
|
|
'certificato' => 'Certificato',
|
|
'preventivo' => 'Preventivo',
|
|
'fattura' => 'Fattura',
|
|
'verbale' => 'Verbale',
|
|
'altro' => 'Altro',
|
|
]),
|
|
])
|
|
->actions([
|
|
Action::make('apri')
|
|
->label('Apri')
|
|
->icon('heroicon-o-eye')
|
|
->url(fn (DocumentoCollegato $record) => route('admin.documenti-collegati.show', $record->id), true),
|
|
Action::make('etichetta_11354')
|
|
->label('Etichetta 11354')
|
|
->icon('heroicon-o-tag')
|
|
->url(fn (DocumentoCollegato $record) => route('admin.documenti-collegati.etichetta', $record->id), true),
|
|
Action::make('etichetta_99014')
|
|
->label('Etichetta 99014')
|
|
->icon('heroicon-o-tag')
|
|
->url(fn (DocumentoCollegato $record) => route('admin.documenti-collegati.etichetta', $record->id) . '?format=99014', true),
|
|
]);
|
|
}
|
|
}
|