159 lines
5.8 KiB
PHP
159 lines
5.8 KiB
PHP
<?php
|
|
namespace App\Filament\Pages\Fornitore;
|
|
|
|
use App\Filament\Pages\Fornitore\Concerns\ResolvesOperatoreContext;
|
|
use App\Models\Fornitore;
|
|
use App\Models\TicketIntervento;
|
|
use App\Models\User;
|
|
use App\Modules\Contabilita\Models\FatturaFornitore;
|
|
use BackedEnum;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use UnitEnum;
|
|
|
|
class Contabilita extends Page
|
|
{
|
|
use ResolvesOperatoreContext;
|
|
|
|
protected static ?string $navigationLabel = 'Contabilita';
|
|
|
|
protected static ?string $title = 'Contabilita fornitore';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-banknotes';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Fornitore';
|
|
|
|
protected static ?int $navigationSort = 3;
|
|
|
|
protected static ?string $slug = 'fornitore/contabilita';
|
|
|
|
protected string $view = 'filament.pages.fornitore.contabilita';
|
|
|
|
public ?Fornitore $fornitore = null;
|
|
|
|
public ?string $fornitoreLabel = null;
|
|
|
|
public bool $missingAdminContext = false;
|
|
|
|
public bool $contabilitaReady = false;
|
|
|
|
/** @var array{aperte:int,pagate:int,registrate:int,totale_aperto:float,totale_registrato:float} */
|
|
public array $stats = [
|
|
'aperte' => 0,
|
|
'pagate' => 0,
|
|
'registrate' => 0,
|
|
'totale_aperto' => 0.0,
|
|
'totale_registrato' => 0.0,
|
|
];
|
|
|
|
/** @var array<int,array<string,mixed>> */
|
|
public array $fattureRows = [];
|
|
|
|
/** @var array<int,array<string,mixed>> */
|
|
public array $queueRows = [];
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'fornitore']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
[$fornitore] = $this->resolveOperatoreContext(allowAdminWithoutSupplier: true);
|
|
|
|
if (! $fornitore instanceof Fornitore) {
|
|
$this->missingAdminContext = true;
|
|
return;
|
|
}
|
|
|
|
$this->fornitore = $fornitore;
|
|
$this->fornitoreLabel = $this->getFornitoreLabel($fornitore);
|
|
$this->loadData();
|
|
}
|
|
|
|
public function getTicketsUrl(): string
|
|
{
|
|
if ($this->fornitore instanceof Fornitore) {
|
|
return TicketOperativi::getUrl(['fornitore' => (int) $this->fornitore->id], panel: 'admin-filament');
|
|
}
|
|
|
|
return TicketOperativi::getUrl(panel: 'admin-filament');
|
|
}
|
|
|
|
public function getCollaboratoriUrl(): string
|
|
{
|
|
if ($this->fornitore instanceof Fornitore) {
|
|
return Collaboratori::getUrl(['fornitore' => (int) $this->fornitore->id], panel: 'admin-filament');
|
|
}
|
|
|
|
return Collaboratori::getUrl(panel: 'admin-filament');
|
|
}
|
|
|
|
protected function loadData(): void
|
|
{
|
|
if (! $this->fornitore instanceof Fornitore) {
|
|
return;
|
|
}
|
|
|
|
$this->queueRows = TicketIntervento::query()
|
|
->with(['ticket.stabile.amministratore'])
|
|
->where('fornitore_id', (int) $this->fornitore->id)
|
|
->whereIn('stato', ['in_verifica', 'fatturabile', 'fatturato'])
|
|
->orderByDesc('updated_at')
|
|
->limit(40)
|
|
->get()
|
|
->map(fn(TicketIntervento $intervento) => [
|
|
'ticket_id' => (int) $intervento->ticket_id,
|
|
'intervento_id' => (int) $intervento->id,
|
|
'stato' => (string) $intervento->stato,
|
|
'stabile' => (string) ($intervento->ticket?->stabile?->denominazione ?? '-'),
|
|
'amministratore' => (string) ($intervento->ticket?->stabile?->amministratore?->denominazione_studio ?? '-'),
|
|
'titolo' => (string) ($intervento->ticket?->titolo ?? '-'),
|
|
'aggiornato' => optional($intervento->updated_at)->format('d/m/Y H:i') ?: '-',
|
|
'url' => TicketInterventoScheda::getUrl(['record' => (int) $intervento->id], panel: 'admin-filament'),
|
|
])
|
|
->all();
|
|
|
|
if (! Schema::hasTable('contabilita_fatture_fornitori')) {
|
|
$this->contabilitaReady = false;
|
|
return;
|
|
}
|
|
|
|
$this->contabilitaReady = true;
|
|
|
|
$fatture = FatturaFornitore::query()
|
|
->with(['stabile.amministratore'])
|
|
->where('fornitore_id', (int) $this->fornitore->id)
|
|
->orderByDesc('data_documento')
|
|
->orderByDesc('id')
|
|
->limit(100)
|
|
->get();
|
|
|
|
$this->stats = [
|
|
'aperte' => $fatture->whereIn('stato', ['inserito', 'contabilizzato'])->count(),
|
|
'pagate' => $fatture->where('stato', 'pagato')->count(),
|
|
'registrate' => $fatture->count(),
|
|
'totale_aperto' => (float) $fatture->whereIn('stato', ['inserito', 'contabilizzato'])->sum('netto_da_pagare'),
|
|
'totale_registrato' => (float) $fatture->sum('totale'),
|
|
];
|
|
|
|
$this->fattureRows = $fatture
|
|
->map(fn(FatturaFornitore $fattura) => [
|
|
'id' => (int) $fattura->id,
|
|
'data_documento' => optional($fattura->data_documento)->format('d/m/Y') ?: '-',
|
|
'numero_documento' => (string) ($fattura->numero_documento ?? '-'),
|
|
'stato' => (string) ($fattura->stato ?? 'inserito'),
|
|
'stabile' => (string) ($fattura->stabile?->denominazione ?? '-'),
|
|
'amministratore' => (string) ($fattura->stabile?->amministratore?->denominazione_studio ?? '-'),
|
|
'descrizione' => (string) ($fattura->descrizione ?? ''),
|
|
'totale' => (float) ($fattura->totale ?? 0),
|
|
'netto' => (float) ($fattura->netto_da_pagare ?? 0),
|
|
])
|
|
->all();
|
|
}
|
|
}
|