fix: ripristina preview ticket mobile e matching rubrica

This commit is contained in:
michele 2026-04-06 21:04:31 +00:00
parent 1ca7314f6b
commit 372ed81bdd
2 changed files with 87 additions and 3 deletions

View File

@ -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 ?? ''))));

View File

@ -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()"