diff --git a/app/Filament/Pages/UnitaImmobiliarePage.php b/app/Filament/Pages/UnitaImmobiliarePage.php index c325dab..ca95944 100755 --- a/app/Filament/Pages/UnitaImmobiliarePage.php +++ b/app/Filament/Pages/UnitaImmobiliarePage.php @@ -1359,7 +1359,57 @@ private function populateNominativiStoriciFallback(): void return; } - if (DbSchema::hasTable('persone_unita_relazioni') && DbSchema::hasTable('persone')) { + if ($this->hasImportConnection() && DbSchema::connection('gescon_import')->hasTable('condomin_mirror')) { + $codStabile = trim((string) ($this->unita->stabile?->codice_stabile ?? '')); + $scala = trim((string) ($this->unita->scala ?? '')); + $interno = trim((string) ($this->unita->interno ?? '')); + + if ($codStabile !== '' && $interno !== '') { + $q = DB::connection('gescon_import') + ->table('condomin_mirror') + ->where('cod_stabile', $codStabile) + ->where('interno', $interno); + if ($scala !== '') { + $q->where('scala', $scala); + } + + $mirrorRows = $q->orderBy('source_year')->get(); + foreach ($mirrorRows as $m) { + $yLabel = match ($m->source_year) { + '0004' => '2026 (Gestione 0004)', + '0003' => '2025 (Gestione 0003)', + '0001' => '2024 (Gestione 0001)', + default => 'Gestione ' . $m->source_year, + }; + + $owner = trim((string) ($m->nom_cond ?? $m->proprietario ?? '')); + if ($owner !== '') { + $fallbackList[] = [ + 'ruolo' => 'Proprietario', + 'nominativo' => $owner, + 'periodo' => $yLabel, + 'percentuale' => '100,00%', + 'fonte' => 'Archivio storico legacy', + 'detail' => ! empty($m->codice_fiscale) ? ('CF: ' . $m->codice_fiscale) : null, + ]; + } + + $inquil = trim((string) ($m->inquilino ?? $m->inquil_nome ?? '')); + if ($inquil !== '') { + $fallbackList[] = [ + 'ruolo' => 'Inquilino', + 'nominativo' => $inquil, + 'periodo' => $yLabel, + 'percentuale' => '100,00%', + 'fonte' => 'Archivio storico legacy', + 'detail' => null, + ]; + } + } + } + } + + if (empty($fallbackList) && DbSchema::hasTable('persone_unita_relazioni') && DbSchema::hasTable('persone')) { $purRows = DB::table('persone_unita_relazioni as pur') ->join('persone as p', 'pur.persona_id', '=', 'p.id') ->where('pur.unita_id', $this->unita->id) @@ -3201,16 +3251,38 @@ protected function hydrateRelazioni(): void $inquilini = $inquilini->filter(fn($r) => $isRelazioneCorrenteNelAnno($r, $activeAnno))->values(); } - $inqArr = $inquilini->values()->all(); + $legacyRow = $this->getLegacyCondominRow(); + $legacyInqName = trim((string) ($legacyRow?->inquil_nome ?? $legacyRow?->inquilino ?? '')); + if ($legacyInqName !== '') { + $inqNameUpper = strtoupper($legacyInqName); + + $matchedInquilini = $inquilini->filter(function (array $i) use ($inqNameUpper): bool { + $nome = strtoupper(trim((string) ($i['nome'] ?? ''))); + return $nome !== '' && (str_contains($nome, $inqNameUpper) || str_contains($inqNameUpper, $nome)); + })->values(); + + $unmatchedInquilini = $inquilini->reject(function (array $i) use ($inqNameUpper): bool { + $nome = strtoupper(trim((string) ($i['nome'] ?? ''))); + return $nome !== '' && (str_contains($nome, $inqNameUpper) || str_contains($inqNameUpper, $nome)); + })->values()->all(); + + if ($matchedInquilini->isNotEmpty()) { + $inquilini = $matchedInquilini; + $relazioniStoriche = array_values(array_unique(array_merge($relazioniStoriche, $unmatchedInquilini), SORT_REGULAR)); + } + } + + $inqArr = $inquilini->values()->all(); $inqCount = count($inqArr); if ($inqCount > 0) { - $equalQuota = round(100.0 / $inqCount, 2); + $equalQuota = round(100.0 / $inqCount, 2); $startOfYearDate = '01/01/' . $activeAnno; foreach ($inqArr as $idx => $inq) { - if ($inq['quota'] === null || (float)$inq['quota'] <= 0) { - $inqArr[$idx]['quota'] = $equalQuota; - $inqArr[$idx]['quota_label'] = number_format($equalQuota, 2, ',', '.'); + if ($inq['quota'] === null || (float) $inq['quota'] <= 0 || $inqCount === 1) { + $inqQuota = $inqCount === 1 ? 100.0 : $equalQuota; + $inqArr[$idx]['quota'] = $inqQuota; + $inqArr[$idx]['quota_label'] = number_format($inqQuota, 2, ',', '.'); } $dInizio = trim((string) ($inq['data_inizio'] ?? '')); if ($dInizio === '' || $dInizio === 'Da 01/01/2000' || $dInizio === '01/01/2000') { @@ -3447,6 +3519,8 @@ private function getLegacyCondominRow(): ?object 'inquil_dal', 'inquil_al', 'nom_cond', + 'proprietario', + 'inquilino', 'codice_fiscale', 'cond_cod_fisc', 'inquil_nome',