From 372ed81bdd142b9431bb86e7283aa0e18c6bcd2b Mon Sep 17 00:00:00 2001 From: michele Date: Mon, 6 Apr 2026 21:04:31 +0000 Subject: [PATCH] fix: ripristina preview ticket mobile e matching rubrica --- ...sconMaterializePersoneRelazioniCommand.php | 89 ++++++++++++++++++- .../pages/supporto/ticket-mobile.blade.php | 1 + 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/app/Console/Commands/GesconMaterializePersoneRelazioniCommand.php b/app/Console/Commands/GesconMaterializePersoneRelazioniCommand.php index 48828fc..80c6972 100644 --- a/app/Console/Commands/GesconMaterializePersoneRelazioniCommand.php +++ b/app/Console/Commands/GesconMaterializePersoneRelazioniCommand.php @@ -213,13 +213,32 @@ private function findMatchingRubrica(object $row, string $tipoRelazione): ?Rubri return null; } - return RubricaUniversale::query() + $matches = RubricaUniversale::query() ->when($adminId > 0, fn($query) => $query->where('amministratore_id', $adminId)) ->where(function ($query) use ($identityKey): void { $query->whereRaw("UPPER(REGEXP_REPLACE(COALESCE(ragione_sociale, ''), '[^A-Z0-9]', '')) = ?", [$identityKey]) - ->orWhereRaw("UPPER(REGEXP_REPLACE(CONCAT(COALESCE(cognome, ''), COALESCE(nome, '')), '[^A-Z0-9]', '')) = ?", [$identityKey]); + ->orWhereRaw("UPPER(REGEXP_REPLACE(CONCAT(COALESCE(cognome, ''), COALESCE(nome, '')), '[^A-Z0-9]', '')) = ?", [$identityKey]) + ->orWhereRaw("UPPER(REGEXP_REPLACE(CONCAT(COALESCE(nome, ''), COALESCE(cognome, '')), '[^A-Z0-9]', '')) = ?", [$identityKey]) + ->orWhereRaw("UPPER(REGEXP_REPLACE(COALESCE(nome, ''), '[^A-Z0-9]', '')) = ?", [$identityKey]); }) - ->orderBy('id') + ->get(); + + if ($matches->isNotEmpty()) { + return $matches + ->sortByDesc(fn(RubricaUniversale $rubrica): int => $this->scoreRubricaMatch($rubrica, $identityKey)) + ->first(); + } + + $tokenKey = $this->normalizeTokenSetKey($row->nominativo ?? null); + if (! $tokenKey) { + return null; + } + + return RubricaUniversale::query() + ->when($adminId > 0, fn($query) => $query->where('amministratore_id', $adminId)) + ->get() + ->filter(fn(RubricaUniversale $rubrica): bool => $this->normalizeTokenSetKey($this->buildRubricaIdentityLabel($rubrica)) === $tokenKey) + ->sortByDesc(fn(RubricaUniversale $rubrica): int => $this->scoreRubricaMatch($rubrica, $identityKey)) ->first(); } @@ -536,6 +555,70 @@ private function normalizeIdentityKey(mixed $value): ?string return $value !== '' ? $value : null; } + private function normalizeTokenSetKey(mixed $value): ?string + { + if ($value === null) { + return null; + } + + $tokens = preg_split('/\s+/', Str::upper(trim((string) $value))) ?: []; + $tokens = array_values(array_filter(array_map(function (string $token): string { + return (string) preg_replace('/[^A-Z0-9]+/u', '', $token); + }, $tokens))); + + if ($tokens === []) { + return null; + } + + sort($tokens, SORT_STRING); + + return implode('|', $tokens); + } + + private function buildRubricaIdentityLabel(RubricaUniversale $rubrica): string + { + return trim(implode(' ', array_filter([ + (string) ($rubrica->nome ?? ''), + (string) ($rubrica->cognome ?? ''), + (string) ($rubrica->ragione_sociale ?? ''), + ]))); + } + + private function scoreRubricaMatch(RubricaUniversale $rubrica, string $identityKey): int + { + $score = 0; + + $concatCognomeNome = $this->normalizeIdentityKey((string) (($rubrica->cognome ?? '') . ($rubrica->nome ?? ''))); + $concatNomeCognome = $this->normalizeIdentityKey((string) (($rubrica->nome ?? '') . ($rubrica->cognome ?? ''))); + $nomeOnly = $this->normalizeIdentityKey((string) ($rubrica->nome ?? '')); + $ragioneSociale = $this->normalizeIdentityKey((string) ($rubrica->ragione_sociale ?? '')); + + if ($concatCognomeNome === $identityKey || $concatNomeCognome === $identityKey) { + $score += 40; + } + if ($nomeOnly === $identityKey || $ragioneSociale === $identityKey) { + $score += 25; + } + + if (filled($rubrica->telefono_cellulare)) { + $score += 20; + } + if (filled($rubrica->telefono_ufficio)) { + $score += 8; + } + if (filled($rubrica->email)) { + $score += 6; + } + if (filled($rubrica->codice_fiscale) || filled($rubrica->partita_iva)) { + $score += 10; + } + if (filled($rubrica->riferimento_stabile) || filled($rubrica->riferimento_unita)) { + $score += 5; + } + + return $score; + } + private function normalizeNameKey(?string $nome, ?string $cognome): ?string { $full = trim(strtoupper(trim((string) ($cognome ?? '')) . ' ' . trim((string) ($nome ?? '')))); diff --git a/resources/views/filament/pages/supporto/ticket-mobile.blade.php b/resources/views/filament/pages/supporto/ticket-mobile.blade.php index 201f85e..46ddaf3 100644 --- a/resources/views/filament/pages/supporto/ticket-mobile.blade.php +++ b/resources/views/filament/pages/supporto/ticket-mobile.blade.php @@ -308,6 +308,7 @@ } return `${(bytes / 1048576).toFixed(1)} MB`; + }, }" x-on:ticket-mobile-local-previews-clear.window="clearPreviews($event.detail?.source || null)" x-on:ticket-mobile-draft-reset.window="clearPreviews()"