162 lines
5.5 KiB
PHP
162 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Condomini;
|
|
|
|
use App\Models\FatturaEstrattaStaging;
|
|
use App\Models\FatturaElettronica;
|
|
use App\Models\Fornitore;
|
|
use App\Models\Stabile;
|
|
use App\Models\User;
|
|
use App\Support\StabileContext;
|
|
use BackedEnum;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use UnitEnum;
|
|
|
|
class ValidazioneFatturePage extends Page
|
|
{
|
|
protected static ?string $navigationLabel = 'Validazione Fatture';
|
|
|
|
protected static ?string $title = 'Dispo-Campi Estratti';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-clipboard-document-check';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Stabile';
|
|
|
|
protected static ?int $navigationSort = 35;
|
|
|
|
protected static ?string $slug = 'condomini/validazione-fatture';
|
|
|
|
protected string $view = 'filament.pages.condomini.validazione-fatture';
|
|
|
|
public ?Stabile $stabileAttivo = null;
|
|
|
|
public array $records = [];
|
|
public ?int $selectedRecordId = null;
|
|
public string $selectedRecordText = '';
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$user = Auth::user();
|
|
if (! $user instanceof User) {
|
|
return;
|
|
}
|
|
|
|
$activeId = StabileContext::resolveActiveStabileId($user);
|
|
if (! $activeId) {
|
|
return;
|
|
}
|
|
|
|
$this->stabileAttivo = StabileContext::accessibleStabili($user)->firstWhere('id', $activeId);
|
|
$this->loadRecords();
|
|
}
|
|
|
|
public function loadRecords(): void
|
|
{
|
|
if (!$this->stabileAttivo) {
|
|
$this->records = [];
|
|
return;
|
|
}
|
|
|
|
$this->records = FatturaEstrattaStaging::query()
|
|
->where('stabile_id', $this->stabileAttivo->id)
|
|
->where('stato', 'pendente')
|
|
->get()
|
|
->toArray();
|
|
|
|
if (count($this->records) > 0 && !$this->selectedRecordId) {
|
|
$this->selectRecord($this->records[0]['id']);
|
|
}
|
|
}
|
|
|
|
public function selectRecord(int $id): void
|
|
{
|
|
$this->selectedRecordId = $id;
|
|
$record = FatturaEstrattaStaging::find($id);
|
|
if ($record && $record->file_path && file_exists(storage_path('app/' . $record->file_path))) {
|
|
// Estrae testo dal file PDF per visualizzarlo a sinistra
|
|
$output = [];
|
|
$exitCode = 0;
|
|
$cmd = "/usr/bin/pdftotext -layout " . escapeshellarg(storage_path('app/' . $record->file_path)) . " -";
|
|
exec($cmd, $output, $exitCode);
|
|
$this->selectedRecordText = $exitCode === 0 ? implode("\n", $output) : "Impossibile leggere il testo del PDF.";
|
|
} else {
|
|
$this->selectedRecordText = "Nessun testo OCR o PDF disponibile per questo record.";
|
|
}
|
|
}
|
|
|
|
public function updateRecord(int $id, string $field, string $value): void
|
|
{
|
|
$record = FatturaEstrattaStaging::find($id);
|
|
if ($record) {
|
|
$record->update([$field => $value]);
|
|
Notification::make()->title('Campo aggiornato')->success()->send();
|
|
$this->loadRecords();
|
|
}
|
|
}
|
|
|
|
public function validaERegistra(int $id): void
|
|
{
|
|
$staging = FatturaEstrattaStaging::find($id);
|
|
if (!$staging) {
|
|
return;
|
|
}
|
|
|
|
// Rileva o crea il fornitore sulla base dei dati validati
|
|
$fornitore = Fornitore::query()
|
|
->where('partita_iva', $staging->partita_iva)
|
|
->first();
|
|
|
|
if (!$fornitore && $this->stabileAttivo) {
|
|
$fornitore = Fornitore::create([
|
|
'amministratore_id' => $this->stabileAttivo->amministratore_id,
|
|
'denominazione' => $staging->fornitore_nome ?: 'Nuovo Fornitore',
|
|
'partita_iva' => $staging->partita_iva,
|
|
'stato' => 'attivo',
|
|
]);
|
|
}
|
|
|
|
// Registra definitivamente come fattura elettronica reale
|
|
FatturaElettronica::create([
|
|
'stabile_id' => $staging->stabile_id,
|
|
'fornitore_id' => $fornitore?->id,
|
|
'fornitore_piva' => $staging->partita_iva,
|
|
'fornitore_denominazione' => $staging->fornitore_nome,
|
|
'numero_fattura' => $staging->numero_contratto ?: 'ND',
|
|
'data_fattura' => $staging->data_inizio_periodo ?: now(),
|
|
'imponibile' => $staging->importo_monetario,
|
|
'totale' => $staging->importo_monetario,
|
|
'consumo_valore' => $staging->quantita_consumata,
|
|
'consumo_unita' => $staging->unita_misura,
|
|
'consumo_riferimento' => $staging->data_inizio_periodo ? $staging->data_inizio_periodo->format('d/m/Y') . ' - ' . $staging->data_fine_periodo?->format('d/m/Y') : null,
|
|
'cbill' => $staging->cbill,
|
|
'stato' => 'ricevuta',
|
|
]);
|
|
|
|
$staging->update(['stato' => 'validata']);
|
|
Notification::make()->title('Fattura validata e registrata con successo!')->success()->send();
|
|
|
|
$this->selectedRecordId = null;
|
|
$this->loadRecords();
|
|
}
|
|
|
|
public function scartaRecord(int $id): void
|
|
{
|
|
$staging = FatturaEstrattaStaging::find($id);
|
|
if ($staging) {
|
|
$staging->update(['stato' => 'scartata']);
|
|
Notification::make()->title('Record scartato')->warning()->send();
|
|
$this->selectedRecordId = null;
|
|
$this->loadRecords();
|
|
}
|
|
}
|
|
}
|