185 lines
8.4 KiB
PHP
185 lines
8.4 KiB
PHP
<?php
|
|
namespace App\Filament\Pages\Gescon;
|
|
|
|
use App\Models\RubricaUniversale;
|
|
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\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use UnitEnum;
|
|
|
|
class RubricaUniversaleArchivio extends Page implements HasTable
|
|
{
|
|
use InteractsWithTable;
|
|
|
|
protected static bool $shouldRegisterNavigation = true;
|
|
|
|
protected static ?string $navigationLabel = 'Anagrafica unica (Rubrica)';
|
|
|
|
protected static ?string $title = 'Anagrafica unica (Rubrica)';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-book-open';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'NetGescon';
|
|
|
|
protected static ?int $navigationSort = 3;
|
|
|
|
protected static ?string $slug = 'gescon/anagrafica/rubrica-universale';
|
|
|
|
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 getTableQuery(): Builder
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return RubricaUniversale::query()->whereRaw('1 = 0');
|
|
}
|
|
|
|
$activeStabile = StabileContext::getActiveStabile($user);
|
|
$adminId = (int) ($activeStabile?->amministratore_id ?: 0);
|
|
if ($adminId <= 0) {
|
|
return RubricaUniversale::query()->whereRaw('1 = 0');
|
|
}
|
|
|
|
$query = RubricaUniversale::query();
|
|
|
|
// Allineamento con Anagrafica Unica: include solo contatti realmente collegati al tenant.
|
|
$query->where(function (Builder $q) use ($adminId): void {
|
|
$q->whereRaw('1 = 0');
|
|
|
|
if (Schema::hasColumn('rubrica_universale', 'amministratore_id')) {
|
|
$q->orWhere('rubrica_universale.amministratore_id', $adminId);
|
|
}
|
|
|
|
if (Schema::hasTable('fornitori')) {
|
|
$q->orWhereExists(function ($sub) use ($adminId) {
|
|
$sub->select(DB::raw(1))
|
|
->from('fornitori as f')
|
|
->whereColumn('f.rubrica_id', 'rubrica_universale.id')
|
|
->where('f.amministratore_id', $adminId);
|
|
});
|
|
}
|
|
|
|
if (Schema::hasTable('stabili') && Schema::hasColumn('stabili', 'rubrica_id')) {
|
|
$q->orWhereExists(function ($sub) use ($adminId) {
|
|
$sub->select(DB::raw(1))
|
|
->from('stabili as s')
|
|
->whereColumn('s.rubrica_id', 'rubrica_universale.id')
|
|
->where('s.amministratore_id', $adminId);
|
|
});
|
|
}
|
|
|
|
if (Schema::hasTable('rubrica_ruoli') && Schema::hasTable('stabili')) {
|
|
$q->orWhereExists(function ($sub) use ($adminId) {
|
|
$sub->select(DB::raw(1))
|
|
->from('rubrica_ruoli as rr')
|
|
->join('stabili as s2', 's2.id', '=', 'rr.stabile_id')
|
|
->whereColumn('rr.rubrica_id', 'rubrica_universale.id')
|
|
->where('s2.amministratore_id', $adminId);
|
|
});
|
|
}
|
|
});
|
|
|
|
$query->where(function (Builder $q): void {
|
|
$q->whereNotNull('ragione_sociale')->where('ragione_sociale', '!=', '')
|
|
->orWhereNotNull('cognome')->where('cognome', '!=', '')
|
|
->orWhereNotNull('nome')->where('nome', '!=', '')
|
|
->orWhereNotNull('codice_fiscale')->where('codice_fiscale', '!=', '')
|
|
->orWhereNotNull('partita_iva')->where('partita_iva', '!=', '');
|
|
});
|
|
|
|
return $query
|
|
->orderByRaw("COALESCE(ragione_sociale, '')")
|
|
->orderByRaw("COALESCE(cognome, '')")
|
|
->orderByRaw("COALESCE(nome, '')")
|
|
->orderBy('id');
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->striped()
|
|
->paginationPageOptions([10, 25, 50, 100])
|
|
->defaultPaginationPageOption(25)
|
|
->columns([
|
|
TextColumn::make('codice_univoco')->label('Codice')->sortable()->searchable()->toggleable(),
|
|
TextColumn::make('nome_completo')
|
|
->label('Nominativo')
|
|
->wrap()
|
|
->searchable(query: function (Builder $query, string $search): Builder {
|
|
$search = trim($search);
|
|
if ($search === '') {
|
|
return $query;
|
|
}
|
|
|
|
$tokens = preg_split('/(?:[,;|+]|
|
|
\s(?:e|ed|and)\s|
|
|
\s+
|
|
)+/ux', $search) ?: [];
|
|
$tokens = array_values(array_filter(array_map(static fn(string $value): string => trim($value), $tokens), static fn(string $value): bool => $value !== ''));
|
|
|
|
if ($tokens === []) {
|
|
$tokens = [$search];
|
|
}
|
|
|
|
foreach ($tokens as $token) {
|
|
$like = '%' . mb_strtolower($token) . '%';
|
|
$digits = preg_replace('/\D+/', '', $token) ?: '';
|
|
$query->where(function (Builder $q) use ($like, $digits) {
|
|
$q->whereRaw("LOWER(COALESCE(ragione_sociale, '')) LIKE ?", [$like])
|
|
->orWhereRaw("LOWER(COALESCE(nome, '')) LIKE ?", [$like])
|
|
->orWhereRaw("LOWER(COALESCE(cognome, '')) LIKE ?", [$like])
|
|
->orWhereRaw("LOWER(TRIM(CONCAT(COALESCE(nome, ''), ' ', COALESCE(cognome, '')))) LIKE ?", [$like])
|
|
->orWhereRaw("LOWER(COALESCE(email, '')) LIKE ?", [$like])
|
|
->orWhereRaw("LOWER(COALESCE(pec, '')) LIKE ?", [$like])
|
|
->orWhereRaw("LOWER(COALESCE(codice_fiscale, '')) LIKE ?", [$like])
|
|
->orWhereRaw("LOWER(COALESCE(partita_iva, '')) LIKE ?", [$like]);
|
|
|
|
if ($digits !== '') {
|
|
$digitLike = '%' . $digits . '%';
|
|
$q->orWhereRaw("REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(telefono_ufficio, ''), ' ', ''), '+', ''), '-', ''), '(', ''), ')', '') LIKE ?", [$digitLike])
|
|
->orWhereRaw("REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(telefono_cellulare, ''), ' ', ''), '+', ''), '-', ''), '(', ''), ')', '') LIKE ?", [$digitLike])
|
|
->orWhereRaw("REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(telefono_casa, ''), ' ', ''), '+', ''), '-', ''), '(', ''), ')', '') LIKE ?", [$digitLike]);
|
|
}
|
|
});
|
|
}
|
|
|
|
return $query;
|
|
}),
|
|
TextColumn::make('categoria')->label('Categoria')->sortable()->toggleable(),
|
|
TextColumn::make('partita_iva')->label('P.IVA')->searchable()->toggleable(isToggledHiddenByDefault: true),
|
|
TextColumn::make('codice_fiscale')->label('CF')->searchable()->toggleable(isToggledHiddenByDefault: true),
|
|
TextColumn::make('email')->label('Email')->searchable()->toggleable(isToggledHiddenByDefault: true),
|
|
TextColumn::make('pec')->label('PEC')->searchable()->toggleable(isToggledHiddenByDefault: true),
|
|
TextColumn::make('stato')->label('Stato')->sortable()->toggleable(isToggledHiddenByDefault: true),
|
|
])
|
|
->actions([
|
|
Action::make('apri_scheda')
|
|
->label('Apri scheda')
|
|
->icon('heroicon-o-identification')
|
|
->url(fn(RubricaUniversale $record) => RubricaUniversaleScheda::getUrl(['record' => $record->id], panel: 'admin-filament')),
|
|
]);
|
|
}
|
|
}
|