netgescon-day0/app/Filament/Pages/Fornitore/LavorazioniOperative.php

191 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 LavorazioniOperative extends Page
{
use ResolvesOperatoreContext;
protected static ?string $navigationLabel = 'Lavorazioni';
protected static ?string $title = 'Lavorazioni operative';
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-queue-list';
protected static UnitEnum|string|null $navigationGroup = 'Fornitore';
protected static ?int $navigationSort = 1;
protected static ?string $slug = 'fornitore/lavorazioni';
protected string $view = 'filament.pages.fornitore.lavorazioni-operative';
public ?int $fornitoreId = null;
public ?string $fornitoreLabel = null;
public bool $missingAdminContext = false;
public string $scope = 'tutte';
/** @var array<int, array<string, mixed>> */
public array $rows = [];
/** @var array<string, int> */
public array $totals = [
'tutte' => 0,
'ticket_amministratore' => 0,
'interne' => 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->scope = (string) request()->query('scope', 'tutte');
[$fornitore] = $this->resolveOperatoreContext(allowAdminWithoutSupplier: true);
if (! $fornitore instanceof Fornitore) {
$this->missingAdminContext = true;
return;
}
$this->fornitoreId = (int) $fornitore->id;
$this->fornitoreLabel = $this->getFornitoreLabel($fornitore);
$this->refreshData();
}
public function updatedScope(): void
{
$this->refreshData();
}
public function setScope(string $scope): void
{
$this->scope = $scope;
$this->refreshData();
}
public function refreshData(): void
{
if (! $this->fornitoreId) {
$this->rows = [];
return;
}
[$fornitore, $dipendente] = $this->resolveOperatoreContext($this->fornitoreId);
$baseQuery = $this->buildBaseQuery($fornitore, $dipendente);
$this->totals = [
'tutte' => (clone $baseQuery)->count(),
'ticket_amministratore' => (clone $baseQuery)->count(),
'interne' => 0,
];
if ($this->scope === 'interne') {
$this->rows = [];
return;
}
$this->rows = $baseQuery
->limit(120)
->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') ?: '-',
'origine' => 'Ticket amministratore',
'url' => TicketInterventoScheda::getUrl(['record' => (int) $intervento->id], panel: 'admin-filament'),
]);
})
->all();
}
public function getTicketsUrl(): string
{
if ($this->fornitoreId) {
return TicketOperativi::getUrl(['fornitore' => $this->fornitoreId], panel: 'admin-filament');
}
return TicketOperativi::getUrl(panel: 'admin-filament');
}
public function getCollaboratoriUrl(): string
{
if ($this->fornitoreId) {
return Collaboratori::getUrl(['fornitore' => $this->fornitoreId], panel: 'admin-filament');
}
return Collaboratori::getUrl(panel: 'admin-filament');
}
public function getRubricaUrl(): string
{
if ($this->fornitoreId) {
return RubricaClienti::getUrl(['fornitore' => $this->fornitoreId], panel: 'admin-filament');
}
return RubricaClienti::getUrl(panel: 'admin-filament');
}
public function getProdottiUrl(): string
{
if ($this->fornitoreId) {
return ProdottiCatalogo::getUrl(['fornitore' => $this->fornitoreId], panel: 'admin-filament');
}
return ProdottiCatalogo::getUrl(panel: 'admin-filament');
}
public function getImpostazioniUrl(): string
{
if ($this->fornitoreId) {
return ImpostazioniArchivio::getUrl(['fornitore' => $this->fornitoreId], panel: 'admin-filament');
}
return ImpostazioniArchivio::getUrl(panel: 'admin-filament');
}
protected function buildBaseQuery(Fornitore $fornitore, ?FornitoreDipendente $dipendente): Builder
{
$query = TicketIntervento::query()
->with(['ticket.stabile', 'ticket.unitaImmobiliare', 'ticket.soggettoRichiedente', '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;
}
}