From 30b4f89a7bc78ed0bf1e8956b3f3c732ada4129f Mon Sep 17 00:00:00 2001 From: michele Date: Tue, 7 Jul 2026 19:03:47 +0200 Subject: [PATCH] fix(gescon-import): correct date formats and description mapping, remove read window and show registered badge --- .../RiscaldamentoStabileArchivio.php | 17 ++++---- .../Import/MultiYearGesconImportService.php | 42 +++++++++++-------- .../riscaldamento-stabile-archivio.blade.php | 12 ++---- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/app/Filament/Pages/Condomini/RiscaldamentoStabileArchivio.php b/app/Filament/Pages/Condomini/RiscaldamentoStabileArchivio.php index f561337..c30d99c 100755 --- a/app/Filament/Pages/Condomini/RiscaldamentoStabileArchivio.php +++ b/app/Filament/Pages/Condomini/RiscaldamentoStabileArchivio.php @@ -3867,10 +3867,18 @@ public function getRiscaldamentoTariffeRowsProperty(): array $activeYear = $this->resolveActiveAnnoGestione(); - // Carica le voci di spesa collegate al riscaldamento per lo stabile + // Trova le gestioni attive per questo stabile ed anno + $gestioneIds = \App\Models\GestioneContabile::query() + ->where('stabile_id', $stabileId) + ->where('anno_gestione', $activeYear) + ->pluck('id') + ->all(); + + // Carica le voci di spesa collegate al riscaldamento per lo stabile e la gestione dell'anno attivo $voci = \App\Models\VoceSpesa::query() ->where('stabile_id', $stabileId) ->where('attiva', true) + ->whereIn('gestione_contabile_id', $gestioneIds) ->where(function ($query): void { $query->where('tipo_gestione', 'riscaldamento') ->orWhere('tipo', 'riscaldamento') @@ -3884,13 +3892,6 @@ public function getRiscaldamentoTariffeRowsProperty(): array ->orderBy('codice') ->get(); - // Trova le gestioni attive per questo stabile ed anno - $gestioneIds = \App\Models\GestioneContabile::query() - ->where('stabile_id', $stabileId) - ->where('anno_gestione', $activeYear) - ->pluck('id') - ->all(); - $rows = []; foreach ($voci as $voce) { $consuntivo = 0.0; diff --git a/app/Services/Import/MultiYearGesconImportService.php b/app/Services/Import/MultiYearGesconImportService.php index 6426c6a..7303a2c 100755 --- a/app/Services/Import/MultiYearGesconImportService.php +++ b/app/Services/Import/MultiYearGesconImportService.php @@ -176,7 +176,7 @@ private function scanGestioniFromMdb(string $mdbPath, int $year): array $straordinarie = []; foreach ($operazioni as $op) { - $g = strtoupper(trim($op['Gestione'] ?? '')); + $g = strtoupper(trim($op['gestione'] ?? '')); if ($g === 'R') { $hasRiscaldamento = true; } elseif ($g === 'S') { @@ -384,12 +384,12 @@ private function importRitenuteFOrGestione(GestioneContabile $gestione, string $ RegistroRitenuteAcconto::create([ 'tenant_id' => $this->tenantId, 'gestione_id' => $gestione->id, - 'numero_progressivo' => $ra['Rif_RDA'], + 'numero_progressivo' => $ra['rif_rda'], 'data_competenza' => $this->parseGesconDate($ra['data_oper']), 'imponibile' => (float) ($ra['imponibile_operazione'] ?? 0), 'aliquota_ritenuta' => (float) ($ra['aliquota'] ?? 20), 'importo_ritenuta' => (float) ($ra['ritenuta'] ?? 0), - 'rif_rda' => $ra['Rif_RDA'], + 'rif_rda' => $ra['rif_rda'], 'stato_versamento' => 'da_versare', 'metadati_gescon' => json_encode($ra), ]); @@ -537,11 +537,11 @@ private function importOperazioni(string $year, string $mdbPath): int { $imported = 0; - // Query MDB per operazioni $operazioni = $this->queryMdb($mdbPath, " SELECT * FROM Operazioni "); + echo "DEBUG: Found " . count($operazioni) . " operations in MDB for year {$year}\n"; usort($operazioni, function($a, $b) { $idA = (int) ($a['id_operaz'] ?? 0); @@ -589,7 +589,7 @@ private function importOperazioni(string $year, string $mdbPath): int 'tenant_id' => $this->tenantId, 'gestione_id' => $gestioneId, 'legacy_id' => $opId, - 'descrizione' => $op['note'] ?? $op['descrizione'] ?? $op['natura'] ?? 'Operazione', + 'descrizione' => (trim($op['benef'] ?? '') !== '') ? trim($op['benef']) : ((trim($op['note'] ?? '') !== '') ? trim($op['note']) : ((trim($op['descrizione'] ?? '') !== '') ? trim($op['descrizione']) : 'Operazione')), 'data_operazione' => $this->parseGesconDate($dtSpe), 'compet' => $competClean, 'natura2' => $natura2Clean, @@ -609,6 +609,7 @@ private function importOperazioni(string $year, string $mdbPath): int $imported++; } catch (\Exception $e) { + echo "Error importing operazione " . ($op['id_operaz'] ?? 'unknown') . " for year {$year}: " . $e->getMessage() . "\n"; Log::error("Error importing operazione", [ 'year' => $year, 'operazione' => $op, @@ -646,7 +647,7 @@ private function importIncassi(string $year, string $mdbPath): int 'gestione_id' => $gestioneId, 'condominio_id' => $this->gestioni[$year]['ordinaria']->stabile_id ?? 1, 'conto_bancario_id' => $this->resolveContoBancarioId($mdbPath, $inc['cod_cassa'] ?? null), - 'id_incasso_gescon' => $inc['ID_incasso'] ?? null, + 'id_incasso_gescon' => $inc['id_incasso'] ?? null, 'cod_cond_gescon' => $inc['cod_cond'] ?? null, 'cond_inquil' => $inc['cond_inquil'] ?? null, 'n_riferimento' => $inc['n_riferimento'] ?? null, @@ -704,12 +705,12 @@ private function importRitenuteAcconto(string $year, string $mdbPath): int RegistroRitenuteAcconto::create([ 'tenant_id' => $this->tenantId, 'gestione_id' => $gestioneId, - 'numero_progressivo' => $ra['Rif_RDA'], + 'numero_progressivo' => $ra['rif_rda'], 'data_competenza' => $this->parseGesconDate($ra['dt_spe'] ?? null), 'imponibile' => (float) ($ra['imponibile_operazione'] ?? 0), 'aliquota_ritenuta' => (float) ($ra['aliquota'] ?? 20), 'importo_ritenuta' => (float) ($ra['ritenuta'] ?? 0), - 'rif_rda' => $ra['Rif_RDA'], + 'rif_rda' => $ra['rif_rda'], 'stato_versamento' => 'da_versare', // Default 'metadati_gescon' => json_encode($ra), ]); @@ -753,13 +754,13 @@ private function importIncassiEstrattoConto(string $year, string $mdbPath): int 'tenant_id' => $this->tenantId, 'gestione_id' => $gestioneId, 'id_gescon' => $inc['protocollo'] ?? null, - 'data_operazione' => $this->parseGesconDate($inc['Data_pag'] ?? null), - 'data_valuta' => $this->parseGesconDate($inc['Data_pag'] ?? null), - 'importo' => (float) ($inc['Importo'] ?? 0), - 'descrizione' => trim(($inc['Descrizione_Aggiuntiva'] ?? '') . ' ' . ($inc['Nome_condomino'] ?? '')), - 'ordinante' => $inc['Nome_condomino'] ?? null, - 'riferimento_bancario' => $inc['Num_incasso'] ?? null, - 'file_origine' => $inc['Nome_file_pdf'] ?? null, + 'data_operazione' => $this->parseGesconDate($inc['data_pag'] ?? null), + 'data_valuta' => $this->parseGesconDate($inc['data_pag'] ?? null), + 'importo' => (float) ($inc['importo'] ?? 0), + 'descrizione' => trim(($inc['descrizione_aggiuntiva'] ?? '') . ' ' . ($inc['nome_condomino'] ?? '')), + 'ordinante' => $inc['nome_condomino'] ?? null, + 'riferimento_bancario' => $inc['num_incasso'] ?? null, + 'file_origine' => $inc['nome_file_pdf'] ?? null, 'stato' => 'da_riconciliare', 'metadati_gescon' => json_encode($inc), ]); @@ -820,7 +821,7 @@ private function importRateEmesse(string $year, string $mdbPath): int private function determineGestioneId(string $year, array $operazione): int { $gestione = $operazione['gestione'] ?? 'O'; - $nStra = (int) ($operazione['N_STRA'] ?? 0); + $nStra = (int) ($operazione['n_stra'] ?? 0); switch (strtoupper($gestione)) { case 'R': @@ -940,7 +941,7 @@ private function parseMdbOutput(string $output): array return []; } - $headers = array_map('trim', explode("\t", array_shift($lines))); + $headers = array_map('strtolower', array_map('trim', explode("\t", array_shift($lines)))); $headerCount = count($headers); $data = []; @@ -1019,7 +1020,12 @@ private function parseGesconDate($dateValue): ?string } // Prova vari formati date Gescon/Access - $formats = ['Y-m-d', 'd/m/Y', 'm/d/Y', 'Y-m-d H:i:s']; + $formats = [ + 'm/d/y H:i:s', 'd/m/y H:i:s', + 'd/m/y', 'm/d/y', 'y-m-d', + 'Y-m-d H:i:s', 'd/m/Y H:i:s', 'm/d/Y H:i:s', + 'Y-m-d', 'd/m/Y', 'm/d/Y', + ]; foreach ($formats as $format) { $date = \DateTime::createFromFormat($format, $dateValue); diff --git a/resources/views/filament/pages/condomini/riscaldamento-stabile-archivio.blade.php b/resources/views/filament/pages/condomini/riscaldamento-stabile-archivio.blade.php index 0ea925b..e60a5c3 100755 --- a/resources/views/filament/pages/condomini/riscaldamento-stabile-archivio.blade.php +++ b/resources/views/filament/pages/condomini/riscaldamento-stabile-archivio.blade.php @@ -213,7 +213,6 @@ class="inline-flex items-center rounded-md border border-cyan-300 bg-white px-3 Fattura Servizio / codici Periodo FE - Finestra lettura Mc Spesa Azioni @@ -241,6 +240,8 @@ class="rounded border-cyan-300 text-cyan-600 focus:ring-cyan-500"> Pagata il {{ $row['registrata_data_pagamento'] }} @elseif($row['registrata_stato'] === 'da_pagare') Debito (€ {{ number_format($row['registrata_netto_da_pagare'], 2, ',', '.') }}) + @elseif(!empty($row['legacy_id'])) + Registrata in Prima Nota @else Da registrare @endif @@ -270,13 +271,6 @@ class="rounded border-cyan-300 text-cyan-600 focus:ring-cyan-500"> @endif - - @if($row['ha_finestra']) - {{ $row['finestra_label'] }} - @else - - @endif - {{ number_format((float) ($row['totale_mc'] ?? 0), 3, ',', '.') }} {{ number_format((float) ($row['totale_spesa'] ?? 0), 2, ',', '.') }} @@ -290,7 +284,7 @@ class="rounded border-cyan-300 text-cyan-600 focus:ring-cyan-500"> @empty - Nessuna FE riscaldamento estratta nello storico attivo. + Nessuna FE riscaldamento estratta nello storico attivo. @endforelse