165 lines
4.7 KiB
PHP
165 lines
4.7 KiB
PHP
<?php
|
|
namespace App\Filament\Pages\Supporto;
|
|
|
|
use App\Filament\Pages\Gescon\FornitoreScheda;
|
|
use App\Models\Amministratore;
|
|
use App\Models\AssistenzaTecnorepairScheda;
|
|
use App\Models\User;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use UnitEnum;
|
|
|
|
class AssistenzaLegacy extends Page
|
|
{
|
|
protected static ?string $navigationLabel = 'Assistenza Legacy';
|
|
|
|
protected static ?string $title = 'Assistenza Legacy';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-wrench-screwdriver';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Supporto';
|
|
|
|
protected static ?int $navigationSort = 28;
|
|
|
|
protected static ?string $slug = 'supporto/assistenza-legacy';
|
|
|
|
protected string $view = 'filament.pages.supporto.assistenza-legacy';
|
|
|
|
public string $status = 'all';
|
|
|
|
public string $search = '';
|
|
|
|
public ?int $selectedSchedaId = null;
|
|
|
|
/** @var array<string,int> */
|
|
public array $counters = [
|
|
'all' => 0,
|
|
'open' => 0,
|
|
'waiting' => 0,
|
|
'closed' => 0,
|
|
];
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->selectedSchedaId = (int) request()->query('scheda', 0) ?: null;
|
|
$this->refreshCounters();
|
|
}
|
|
|
|
public function updatedStatus(): void
|
|
{
|
|
$this->refreshCounters();
|
|
}
|
|
|
|
public function updatedSearch(): void
|
|
{
|
|
$this->refreshCounters();
|
|
}
|
|
|
|
public function apriScheda(int $schedaId): void
|
|
{
|
|
$this->selectedSchedaId = $schedaId;
|
|
}
|
|
|
|
public function getRowsProperty()
|
|
{
|
|
return $this->baseQuery()
|
|
->with(['fornitore:id,ragione_sociale,is_tecnorepair_primary', 'allegati'])
|
|
->search($this->search)
|
|
->statusBucket($this->status)
|
|
->orderByRaw("CASE status_bucket WHEN 'open' THEN 0 WHEN 'waiting' THEN 1 WHEN 'closed' THEN 2 ELSE 3 END")
|
|
->orderByDesc('date_received')
|
|
->orderByDesc('id')
|
|
->limit(250)
|
|
->get();
|
|
}
|
|
|
|
public function getSelectedSchedaProperty(): ?AssistenzaTecnorepairScheda
|
|
{
|
|
if ((int) ($this->selectedSchedaId ?? 0) <= 0) {
|
|
return null;
|
|
}
|
|
|
|
return $this->baseQuery()
|
|
->with(['fornitore:id,ragione_sociale,is_tecnorepair_primary', 'allegati', 'productSerials'])
|
|
->find((int) $this->selectedSchedaId);
|
|
}
|
|
|
|
public function getFornitoreUrl(?int $fornitoreId): ?string
|
|
{
|
|
if ((int) $fornitoreId <= 0) {
|
|
return null;
|
|
}
|
|
|
|
return FornitoreScheda::getUrl(['record' => $fornitoreId], panel: 'admin-filament');
|
|
}
|
|
|
|
public function getImportCommandHintProperty(): string
|
|
{
|
|
$admin = $this->resolveAmministratore();
|
|
|
|
return $admin instanceof Amministratore
|
|
? 'php artisan tecnorepair:import-legacy ' . ((string) ($admin->codice_amministratore ?: $admin->id)) . ' --force-primary'
|
|
: 'php artisan tecnorepair:import-legacy {amministratore} --force-primary';
|
|
}
|
|
|
|
private function refreshCounters(): void
|
|
{
|
|
$query = $this->baseQuery()->search($this->search);
|
|
|
|
$this->counters = [
|
|
'all' => (int) (clone $query)->count(),
|
|
'open' => (int) (clone $query)->where('status_bucket', 'open')->count(),
|
|
'waiting' => (int) (clone $query)->where('status_bucket', 'waiting')->count(),
|
|
'closed' => (int) (clone $query)->where('status_bucket', 'closed')->count(),
|
|
];
|
|
}
|
|
|
|
private function baseQuery(): Builder
|
|
{
|
|
$query = AssistenzaTecnorepairScheda::query();
|
|
$user = Auth::user();
|
|
|
|
if (! $user instanceof User) {
|
|
return $query->whereRaw('1 = 0');
|
|
}
|
|
|
|
if ($user->hasAnyRole(['super-admin', 'admin'])) {
|
|
return $query;
|
|
}
|
|
|
|
$amministratore = $this->resolveAmministratore();
|
|
if (! $amministratore instanceof Amministratore) {
|
|
return $query->whereRaw('1 = 0');
|
|
}
|
|
|
|
return $query->forAmministratore((int) $amministratore->id);
|
|
}
|
|
|
|
private function resolveAmministratore(): ?Amministratore
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return null;
|
|
}
|
|
|
|
if ($user->amministratore instanceof Amministratore) {
|
|
return $user->amministratore;
|
|
}
|
|
|
|
$stabile = StabileContext::getActiveStabile($user);
|
|
|
|
return $stabile?->amministratore;
|
|
}
|
|
}
|