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(); } } }