183 lines
6.0 KiB
PHP
183 lines
6.0 KiB
PHP
<?php
|
|
namespace App\Filament\Pages\Fornitore;
|
|
|
|
use App\Filament\Pages\Fornitore\Concerns\ResolvesOperatoreContext;
|
|
use App\Models\Fornitore;
|
|
use App\Models\FornitoreDipendente;
|
|
use App\Models\TicketIntervento;
|
|
use App\Models\User;
|
|
use BackedEnum;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use UnitEnum;
|
|
|
|
class TicketOperativi extends Page
|
|
{
|
|
use ResolvesOperatoreContext;
|
|
|
|
protected static ?string $navigationLabel = 'Ticket operativi';
|
|
|
|
protected static ?string $title = 'Ticket fornitore';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-wrench-screwdriver';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Fornitore';
|
|
|
|
protected static ?int $navigationSort = 1;
|
|
|
|
protected static ?string $slug = 'fornitore/tickets';
|
|
|
|
protected string $view = 'filament.pages.fornitore.ticket-operativi';
|
|
|
|
public ?int $fornitoreId = null;
|
|
|
|
public ?string $fornitoreLabel = null;
|
|
|
|
public string $status = 'aperti';
|
|
|
|
public bool $missingAdminContext = false;
|
|
|
|
/** @var array<int, array<string, mixed>> */
|
|
public array $rows = [];
|
|
|
|
/** @var array<string, int> */
|
|
public array $totals = [
|
|
'aperti' => 0,
|
|
'chiusi' => 0,
|
|
'fatturabili' => 0,
|
|
];
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'fornitore']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->status = (string) request()->query('stato', 'aperti');
|
|
[$fornitore] = $this->resolveOperatoreContext(allowAdminWithoutSupplier: true);
|
|
|
|
if (! $fornitore instanceof Fornitore) {
|
|
$this->missingAdminContext = true;
|
|
$this->rows = [];
|
|
return;
|
|
}
|
|
|
|
$this->fornitoreId = (int) $fornitore->id;
|
|
$this->fornitoreLabel = $this->getFornitoreLabel($fornitore);
|
|
|
|
$this->refreshData();
|
|
}
|
|
|
|
public function updatedStatus(): void
|
|
{
|
|
$this->refreshData();
|
|
}
|
|
|
|
public function refreshData(): void
|
|
{
|
|
if (! $this->fornitoreId) {
|
|
$this->rows = [];
|
|
$this->totals = ['aperti' => 0, 'chiusi' => 0, 'fatturabili' => 0];
|
|
return;
|
|
}
|
|
|
|
[$fornitore, $dipendente] = $this->resolveOperatoreContext($this->fornitoreId);
|
|
|
|
$query = $this->buildBaseQuery($fornitore, $dipendente);
|
|
$this->applyStatusFilter($query, $this->status);
|
|
|
|
$this->rows = $query
|
|
->limit(100)
|
|
->get()
|
|
->map(function (TicketIntervento $intervento): array {
|
|
$base = $this->buildInterventoRow($intervento);
|
|
|
|
return array_merge($base, [
|
|
'id' => (int) $intervento->id,
|
|
'ticket_id' => (int) $intervento->ticket_id,
|
|
'titolo' => (string) ($intervento->ticket->titolo ?? '-'),
|
|
'stato' => (string) $intervento->stato,
|
|
'stabile' => (string) ($intervento->ticket->stabile->denominazione ?? '-'),
|
|
'operatore' => (string) $intervento->operatore_assegnato_label,
|
|
'tempo_minuti' => (int) ($intervento->tempo_minuti ?? 0),
|
|
'updated_at' => optional($intervento->updated_at)->format('d/m/Y H:i') ?: '-',
|
|
'url' => TicketInterventoScheda::getUrl(['record' => $intervento->id], panel: 'admin-filament'),
|
|
]);
|
|
})
|
|
->all();
|
|
|
|
$this->totals = [
|
|
'aperti' => (clone $this->buildBaseQuery($fornitore, $dipendente))
|
|
->whereNotIn('stato', ['chiuso'])
|
|
->count(),
|
|
'chiusi' => (clone $this->buildBaseQuery($fornitore, $dipendente))
|
|
->whereIn('stato', ['chiuso', 'fatturato'])
|
|
->count(),
|
|
'fatturabili' => (clone $this->buildBaseQuery($fornitore, $dipendente))
|
|
->whereIn('stato', ['fatturabile', 'fatturato'])
|
|
->count(),
|
|
];
|
|
}
|
|
|
|
public function setStatus(string $status): void
|
|
{
|
|
$this->status = $status;
|
|
$this->refreshData();
|
|
}
|
|
|
|
public function getCollaboratoriUrl(): string
|
|
{
|
|
if ($this->fornitoreId && Auth::user()?->hasAnyRole(['super-admin', 'admin', 'amministratore'])) {
|
|
return Collaboratori::getUrl(['fornitore' => $this->fornitoreId], panel: 'admin-filament');
|
|
}
|
|
|
|
return Collaboratori::getUrl(panel: 'admin-filament');
|
|
}
|
|
|
|
public function getContabilitaUrl(): string
|
|
{
|
|
if ($this->fornitoreId && Auth::user()?->hasAnyRole(['super-admin', 'admin', 'amministratore'])) {
|
|
return Contabilita::getUrl(['fornitore' => $this->fornitoreId], panel: 'admin-filament');
|
|
}
|
|
|
|
return Contabilita::getUrl(panel: 'admin-filament');
|
|
}
|
|
|
|
protected function buildBaseQuery(Fornitore $fornitore, ?FornitoreDipendente $dipendente): Builder
|
|
{
|
|
$query = TicketIntervento::query()
|
|
->with(['ticket.stabile', 'eseguitoDaDipendente'])
|
|
->where('fornitore_id', (int) $fornitore->id)
|
|
->orderByDesc('created_at');
|
|
|
|
if ($dipendente instanceof FornitoreDipendente) {
|
|
$query->where(function (Builder $builder) use ($dipendente): void {
|
|
$builder->whereNull('eseguito_da_dipendente_id')
|
|
->orWhere('eseguito_da_dipendente_id', (int) $dipendente->id);
|
|
});
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
protected function applyStatusFilter(Builder $query, string $status): void
|
|
{
|
|
if ($status === 'chiusi') {
|
|
$query->whereIn('stato', ['chiuso', 'fatturato']);
|
|
return;
|
|
}
|
|
|
|
if ($status === 'fatturabili') {
|
|
$query->whereIn('stato', ['fatturabile', 'fatturato']);
|
|
return;
|
|
}
|
|
|
|
$query->whereNotIn('stato', ['chiuso']);
|
|
}
|
|
}
|