146 lines
5.0 KiB
PHP
146 lines
5.0 KiB
PHP
<?php
|
|
namespace App\Filament\Pages\Supporto;
|
|
|
|
use App\Models\InsuranceClaim;
|
|
use App\Models\User;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use UnitEnum;
|
|
|
|
class GestioneAssicurazioni extends Page
|
|
{
|
|
protected static ?string $navigationLabel = 'Pratiche Assicurative';
|
|
|
|
protected static ?string $title = 'Gestione Pratiche Assicurative';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-shield-check';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Supporto';
|
|
|
|
protected static ?int $navigationSort = 27;
|
|
|
|
protected static ?string $slug = 'supporto/pratiche-assicurative';
|
|
|
|
protected string $view = 'filament.pages.supporto.gestione-assicurazioni';
|
|
|
|
public string $status = 'aperte';
|
|
|
|
public ?int $focusTicketId = null;
|
|
|
|
/** @var array<int,array<string,mixed>> */
|
|
public array $rows = [];
|
|
|
|
/** @var array<string,int> */
|
|
public array $totals = [
|
|
'aperte' => 0,
|
|
'chiuse' => 0,
|
|
'tutte' => 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->status = (string) request()->query('status', 'aperte');
|
|
$this->focusTicketId = (int) request()->query('ticket', 0) ?: null;
|
|
$this->refreshData();
|
|
}
|
|
|
|
public function updatedStatus(): void
|
|
{
|
|
$this->refreshData();
|
|
}
|
|
|
|
public function setStatus(string $status): void
|
|
{
|
|
$this->status = in_array($status, ['aperte', 'chiuse', 'tutte'], true) ? $status : 'aperte';
|
|
$this->refreshData();
|
|
}
|
|
|
|
public function refreshData(): void
|
|
{
|
|
$stabileIds = $this->resolveAccessibleStabileIds();
|
|
if ($stabileIds === []) {
|
|
$this->rows = [];
|
|
$this->totals = ['aperte' => 0, 'chiuse' => 0, 'tutte' => 0];
|
|
return;
|
|
}
|
|
|
|
$base = InsuranceClaim::query()
|
|
->with(['ticket.stabile:id,denominazione', 'insurancePolicy:id,company_name,policy_number'])
|
|
->whereIn('stabile_id', $stabileIds);
|
|
|
|
if ($this->focusTicketId) {
|
|
$base->where('ticket_id', (int) $this->focusTicketId);
|
|
}
|
|
|
|
$query = clone $base;
|
|
if ($this->status === 'aperte') {
|
|
$query->whereNotIn('status', ['chiusa', 'chiuso', 'archiviata']);
|
|
} elseif ($this->status === 'chiuse') {
|
|
$query->whereIn('status', ['chiusa', 'chiuso', 'archiviata']);
|
|
}
|
|
|
|
$this->rows = $query
|
|
->latest('opened_at')
|
|
->latest('id')
|
|
->limit(120)
|
|
->get()
|
|
->map(function (InsuranceClaim $claim): array {
|
|
return [
|
|
'id' => (int) $claim->id,
|
|
'ticket_id' => (int) $claim->ticket_id,
|
|
'ticket_title' => (string) ($claim->ticket->titolo ?? '-'),
|
|
'stabile' => (string) ($claim->ticket->stabile->denominazione ?? '-'),
|
|
'claim_number' => (string) ($claim->claim_number ?? ''),
|
|
'policy_reference' => (string) ($claim->policy_reference ?? ''),
|
|
'policy_label' => trim(implode(' · ', array_filter([
|
|
(string) ($claim->insurancePolicy->company_name ?? ''),
|
|
(string) ($claim->insurancePolicy->policy_number ?? ''),
|
|
]))),
|
|
'status' => (string) ($claim->status ?? 'aperta'),
|
|
'opened_at' => optional($claim->opened_at)->format('d/m/Y H:i') ?: '-',
|
|
'next_action' => (string) (data_get($claim->metadata, 'next_action') ?? ''),
|
|
'documents_sent_at' => filled(data_get($claim->metadata, 'documents_sent_at')) ? (string) data_get($claim->metadata, 'documents_sent_at') : '',
|
|
'ticket_url' => TicketGestione::getUrl([
|
|
'ticket' => (int) $claim->ticket_id,
|
|
'tab' => 'assicurazione',
|
|
], panel: 'admin-filament'),
|
|
];
|
|
})
|
|
->all();
|
|
|
|
$this->totals = [
|
|
'aperte' => (clone $base)->whereNotIn('status', ['chiusa', 'chiuso', 'archiviata'])->count(),
|
|
'chiuse' => (clone $base)->whereIn('status', ['chiusa', 'chiuso', 'archiviata'])->count(),
|
|
'tutte' => (clone $base)->count(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<int,int>
|
|
*/
|
|
private function resolveAccessibleStabileIds(): array
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return [];
|
|
}
|
|
|
|
return StabileContext::accessibleStabili($user)
|
|
->pluck('id')
|
|
->map(fn($value) => (int) $value)
|
|
->filter(fn(int $value) => $value > 0)
|
|
->values()
|
|
->all();
|
|
}
|
|
}
|