feat(unita): add search by nominativo in unit picker dropdown and resolve active owners across all units (task-d095b0e23b)

This commit is contained in:
michele 2026-08-26 18:40:31 +02:00
parent df09bc67b2
commit cfc1c07b91

View File

@ -790,6 +790,80 @@ protected function refreshUnitaOptions(): void
}
}
if (DbSchema::hasTable('persone_unita_relazioni') && DbSchema::hasTable('persone')) {
try {
$today = now()->toDateString();
$rows = DB::table('persone_unita_relazioni as pur')
->join('persone as p', 'pur.persona_id', '=', 'p.id')
->whereIn('pur.unita_id', $unita->pluck('id'))
->where(function ($query) use ($today): void {
$query->whereNull('pur.data_inizio')->orWhere('pur.data_inizio', '<=', $today);
})
->where(function ($query) use ($today): void {
$query->whereNull('pur.data_fine')->orWhere('pur.data_fine', '>=', $today);
})
->where('pur.attivo', 1)
->orderByRaw("CASE WHEN pur.tipo_relazione = 'proprietario' THEN 0 ELSE 1 END")
->orderByDesc('pur.data_inizio')
->select('pur.unita_id', 'p.cognome', 'p.nome', 'p.ragione_sociale')
->get();
foreach ($rows as $row) {
$unitId = (int) ($row->unita_id ?? 0);
if ($unitId <= 0 || isset($currentNominativoByUnita[$unitId])) {
continue;
}
$name = trim((string) ($row->ragione_sociale ?? ''));
if ($name === '') {
$name = trim((string) ($row->cognome ?? '') . ' ' . (string) ($row->nome ?? ''));
}
if ($name !== '') {
$currentNominativoByUnita[$unitId] = $name;
}
}
} catch (\Throwable $e) {
// Ignore fallback error
}
}
if (DbSchema::hasTable('rubrica_ruoli') && DbSchema::hasTable('rubrica_universale')) {
try {
$today = now()->toDateString();
$rows = DB::table('rubrica_ruoli as rr')
->join('rubrica_universale as ru', 'rr.rubrica_id', '=', 'ru.id')
->whereIn('rr.unita_id', $unita->pluck('id'))
->where(function ($query) use ($today): void {
$query->whereNull('rr.data_inizio')->orWhere('rr.data_inizio', '<=', $today);
})
->where(function ($query) use ($today): void {
$query->whereNull('rr.data_fine')->orWhere('rr.data_fine', '>=', $today);
})
->where('rr.attivo', 1)
->orderByRaw("CASE WHEN rr.ruolo = 'proprietario' THEN 0 ELSE 1 END")
->orderByDesc('rr.data_inizio')
->select('rr.unita_id', 'ru.cognome', 'ru.nome', 'ru.ragione_sociale')
->get();
foreach ($rows as $row) {
$unitId = (int) ($row->unita_id ?? 0);
if ($unitId <= 0 || isset($currentNominativoByUnita[$unitId])) {
continue;
}
$name = trim((string) ($row->ragione_sociale ?? ''));
if ($name === '') {
$name = trim((string) ($row->cognome ?? '') . ' ' . (string) ($row->nome ?? ''));
}
if ($name !== '') {
$currentNominativoByUnita[$unitId] = $name;
}
}
} catch (\Throwable $e) {
// Ignore fallback error
}
}
$sorted = $unita->sort(function (UnitaImmobiliare $a, UnitaImmobiliare $b): int {
$pa = (int) ($a->palazzina_id ?? 0);
$pb = (int) ($b->palazzina_id ?? 0);
@ -834,6 +908,20 @@ protected function refreshUnitaOptions(): void
$codice = substr($codice, strlen($stabileCode . '-'));
}
$details = [];
if ($u->scala) {
$details[] = 'Scala ' . $u->scala;
}
if ($u->interno) {
$details[] = 'Int. ' . $u->interno;
}
if ($u->tipo_unita) {
$details[] = '(' . ucfirst(strtolower($u->tipo_unita)) . ')';
}
$detStr = implode(' - ', array_filter($details));
$label = $detStr !== '' ? ($detStr . ' [' . $codice . ']') : trim($codice);
$owner = $currentNominativoByUnita[(int) $u->id] ?? ($this->ownerByUnitaLocal[(int) $u->id] ?? null);
if ($this->unita && (int) $u->id === (int) $this->unita->id) {
$legacyOwner = $this->getLegacyCondominoRiferimentoNome();
@ -841,7 +929,6 @@ protected function refreshUnitaOptions(): void
$owner = $legacyOwner;
}
}
$label = trim($codice);
if (is_string($owner) && trim($owner) !== '') {
$label .= ' — ' . trim($owner);
}