220 lines
7.2 KiB
PHP
220 lines
7.2 KiB
PHP
<?php
|
|
namespace App\Filament\Pages\Gescon;
|
|
|
|
use App\Models\Fornitore;
|
|
use App\Models\User;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use UnitEnum;
|
|
|
|
class FornitoriArchivio extends Page
|
|
{
|
|
public int $fornitoriCount = 0;
|
|
public string $search = '';
|
|
public string $activeTab = 'elenco';
|
|
public ?int $selectedFornitoreId = null;
|
|
|
|
/** @var array<string, string> */
|
|
public array $automazioni = [
|
|
'fornitore_acqua' => '0',
|
|
'abilita_ticket' => '1',
|
|
'abilita_allegati_camera' => '1',
|
|
];
|
|
|
|
public ?string $contoIban = null;
|
|
public ?string $contoIntestazioneEsatta = null;
|
|
|
|
protected static ?string $navigationLabel = 'Fornitori';
|
|
|
|
protected static ?string $title = 'Fornitori';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-building-storefront';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'NetGescon';
|
|
|
|
protected static ?int $navigationSort = 12;
|
|
|
|
protected static ?string $slug = 'gescon/anagrafica/fornitori';
|
|
|
|
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
|
|
{
|
|
try {
|
|
$this->fornitoriCount = (int) $this->getTableQuery()->count();
|
|
} catch (\Throwable) {
|
|
$this->fornitoriCount = 0;
|
|
}
|
|
}
|
|
|
|
protected function getTableQuery(): Builder
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return Fornitore::query()->whereRaw('1 = 0');
|
|
}
|
|
|
|
if ($user->hasAnyRole(['super-admin', 'admin'])) {
|
|
return Fornitore::query();
|
|
}
|
|
|
|
$amministratoreId = $user->amministratore?->id;
|
|
if (! $amministratoreId) {
|
|
$stabile = StabileContext::getActiveStabile($user);
|
|
$amministratoreId = $stabile?->amministratore_id;
|
|
}
|
|
|
|
if (! $amministratoreId) {
|
|
return Fornitore::query()->whereRaw('1 = 0');
|
|
}
|
|
|
|
return Fornitore::query()->where('amministratore_id', (int) $amministratoreId);
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Fornitore>
|
|
*/
|
|
public function getFornitoriRowsProperty(): Collection
|
|
{
|
|
$query = $this->getTableQuery()
|
|
->withCount('dipendenti')
|
|
->orderBy('ragione_sociale')
|
|
->orderBy('cognome')
|
|
->orderBy('nome');
|
|
|
|
$term = trim($this->search);
|
|
if ($term !== '') {
|
|
$query->where(function (Builder $sub) use ($term): void {
|
|
$like = '%' . $term . '%';
|
|
$sub->where('ragione_sociale', 'like', $like)
|
|
->orWhere('nome', 'like', $like)
|
|
->orWhere('cognome', 'like', $like)
|
|
->orWhere('partita_iva', 'like', $like)
|
|
->orWhere('codice_fiscale', 'like', $like)
|
|
->orWhere('email', 'like', $like)
|
|
->orWhere('telefono', 'like', $like)
|
|
->orWhere('tags', 'like', $like);
|
|
});
|
|
}
|
|
|
|
return $query->limit(400)->get();
|
|
}
|
|
|
|
public function getFornitoreLabel(Fornitore $fornitore): string
|
|
{
|
|
$label = trim((string) ($fornitore->ragione_sociale ?: trim(($fornitore->nome ?? '') . ' ' . ($fornitore->cognome ?? ''))));
|
|
|
|
return $label !== '' ? $label : ('Fornitore #' . $fornitore->id);
|
|
}
|
|
|
|
public function getFornitoreHubUrl(int $fornitoreId): string
|
|
{
|
|
return FornitoreScheda::getUrl(['record' => $fornitoreId], panel: 'admin-filament');
|
|
}
|
|
|
|
public function getFornitoreAnagraficaUrl(int $fornitoreId): string
|
|
{
|
|
return url('/admin-filament/anagrafica/fornitori/' . $fornitoreId);
|
|
}
|
|
|
|
public function getSelectedFornitoreProperty(): ?Fornitore
|
|
{
|
|
if ((int) ($this->selectedFornitoreId ?? 0) <= 0) {
|
|
return null;
|
|
}
|
|
|
|
return $this->getTableQuery()
|
|
->withCount('dipendenti')
|
|
->find((int) $this->selectedFornitoreId);
|
|
}
|
|
|
|
public function apriElenco(): void
|
|
{
|
|
$this->activeTab = 'elenco';
|
|
}
|
|
|
|
public function apriScheda(int $fornitoreId): void
|
|
{
|
|
$fornitore = $this->getTableQuery()->find($fornitoreId);
|
|
if (! $fornitore instanceof Fornitore) {
|
|
Notification::make()->title('Fornitore non trovato')->danger()->send();
|
|
return;
|
|
}
|
|
|
|
$this->selectedFornitoreId = (int) $fornitore->id;
|
|
$this->activeTab = 'scheda';
|
|
$this->loadSchedaState($fornitore);
|
|
}
|
|
|
|
public function saveSchedaAutomazioni(): void
|
|
{
|
|
$fornitore = $this->selectedFornitore;
|
|
if (! $fornitore instanceof Fornitore) {
|
|
Notification::make()->title('Seleziona un fornitore')->danger()->send();
|
|
return;
|
|
}
|
|
|
|
$features = is_array($fornitore->fe_features ?? null) ? $fornitore->fe_features : [];
|
|
$features['automazioni'] = [
|
|
'fornitore_acqua' => ($this->automazioni['fornitore_acqua'] ?? '0') === '1',
|
|
'abilita_ticket' => ($this->automazioni['abilita_ticket'] ?? '0') === '1',
|
|
'abilita_allegati_camera' => ($this->automazioni['abilita_allegati_camera'] ?? '0') === '1',
|
|
];
|
|
|
|
$fornitore->fe_features = $features;
|
|
$fornitore->save();
|
|
|
|
Notification::make()->title('Automazioni aggiornate')->success()->send();
|
|
}
|
|
|
|
public function saveSchedaBancaria(): void
|
|
{
|
|
$fornitore = $this->selectedFornitore;
|
|
if (! $fornitore instanceof Fornitore) {
|
|
Notification::make()->title('Seleziona un fornitore')->danger()->send();
|
|
return;
|
|
}
|
|
|
|
$fornitore->iban = $this->cleanNullable($this->contoIban);
|
|
$fornitore->intestazione_cc_esatta = $this->cleanNullable($this->contoIntestazioneEsatta);
|
|
$fornitore->save();
|
|
|
|
Notification::make()->title('Dati conto aggiornati')->success()->send();
|
|
}
|
|
|
|
private function loadSchedaState(Fornitore $fornitore): void
|
|
{
|
|
$features = is_array($fornitore->fe_features ?? null) ? $fornitore->fe_features : [];
|
|
$auto = (array) ($features['automazioni'] ?? []);
|
|
|
|
$this->automazioni = [
|
|
'fornitore_acqua' => ! empty($auto['fornitore_acqua']) ? '1' : '0',
|
|
'abilita_ticket' => ! empty($auto['abilita_ticket']) ? '1' : '0',
|
|
'abilita_allegati_camera' => ! empty($auto['abilita_allegati_camera']) ? '1' : '0',
|
|
];
|
|
|
|
$this->contoIban = is_string($fornitore->iban) ? $fornitore->iban : null;
|
|
$this->contoIntestazioneEsatta = is_string($fornitore->intestazione_cc_esatta ?? null)
|
|
? $fornitore->intestazione_cc_esatta
|
|
: null;
|
|
}
|
|
|
|
private function cleanNullable(?string $value): ?string
|
|
{
|
|
$value = trim((string) $value);
|
|
return $value !== '' ? $value : null;
|
|
}
|
|
}
|