feat(puesto-control): paridad completa de busqueda/seleccion de lote, validaciones estrictas y control de cacheo de garita

This commit is contained in:
Carlos Tello
2026-10-14 09:09:39 -03:00
parent 1f2b029e39
commit ffa8a2a684
10 changed files with 1030 additions and 100 deletions
+129 -9
View File
@@ -435,7 +435,8 @@ class AccessRepository:
COALESCE(p.Nombre, '') AS Nombre,
COALESCE(d.Documento, '') AS Documento,
pat.Valor AS Patente,
lot.Valor AS LoteCodigo
lot.Valor AS LoteCodigo,
COALESCE(cch.Valor, '0') AS CacheoSalida
FROM tck t
LEFT JOIN mdl m ON t.ModuloIngresoID = m.ModuloID
LEFT JOIN tckper tp ON t.TicketID = tp.TicketID
@@ -443,6 +444,7 @@ class AccessRepository:
LEFT JOIN PerDocCatVal d ON p.PersonaID = d.PersonaID
LEFT JOIN tckprx pat ON t.TicketID = pat.TicketID AND pat.PropiedadID = 4
LEFT JOIN tckprx lot ON t.TicketID = lot.TicketID AND lot.PropiedadID = 7
LEFT JOIN tckprx cch ON t.TicketID = cch.TicketID AND cch.PropiedadID = 6
ORDER BY t.FechaHoraIngreso DESC
"""
with db_session() as conn:
@@ -503,18 +505,136 @@ class AccessRepository:
return cursor.fetchall()
@staticmethod
def buscar_lotes_predictivo(term: str) -> List[Dict[str, Any]]:
"""Búsqueda predictiva de lotes por código."""
patron = f"%{term.strip()}%"
def buscar_lote_por_codigo(codigo: str) -> Optional[Dict[str, Any]]:
"""Busca un lote por coincidencia exacta o normalizada de código/número o LoteID."""
query = """
SELECT TOP 15 l.LoteID, l.Lote AS Codigo
FROM lot l
WHERE l.Lote LIKE %s
ORDER BY l.Lote
SELECT TOP 1 l.LoteID, l.Lote AS Codigo
FROM lot l
WHERE UPPER(LTRIM(RTRIM(l.Lote))) = UPPER(%s)
OR (ISNUMERIC(%s) = 1 AND l.LoteID = CAST(%s AS INT))
"""
with db_session() as conn:
cursor = conn.cursor(as_dict=True)
cursor.execute(query, (patron,))
cod_clean = codigo.strip()
cursor.execute(query, (cod_clean, cod_clean, cod_clean))
return cursor.fetchone()
@staticmethod
def buscar_lotes_predictivo(term: str) -> List[Dict[str, Any]]:
"""
Búsqueda completa de lotes por número, UF, calle o residentes asociados.
Padrón enriquecido y ponderado idéntico a DialogoBuscarLote (pct_dialogs.py).
"""
f_clean = term.strip() if term else ""
if not f_clean:
query = """
SELECT TOP 50
l.LoteID,
l.Lote AS Codigo,
l.UF,
l.Calle,
l.Numero,
COALESCE(
(SELECT TOP 1 cv.Nombre FROM LotCatVal lcv JOIN CatVal cv ON lcv.ValorID = cv.ValorID AND cv.CategoriaID = 26 WHERE lcv.LoteID = l.LoteID),
'Habilitado'
) AS Estado,
(
SELECT TOP 1 CONCAT(p.Apellido, ', ', p.Nombre)
FROM LotPer lp
JOIN Per p ON lp.PersonaID = p.PersonaID
WHERE lp.LoteID = l.LoteID
ORDER BY lp.PerfilID
) AS Propietario
FROM lot l
ORDER BY
CASE WHEN ISNUMERIC(l.Lote) = 1 THEN 1 ELSE 2 END,
CASE WHEN ISNUMERIC(l.Lote) = 1 THEN CAST(l.Lote AS INT) ELSE 0 END,
l.Lote
"""
params = ()
else:
f_exact = f_clean
f_start = f"{f_clean}%"
f_contains = f"%{f_clean}%"
query = """
SELECT TOP 50
l.LoteID,
l.Lote AS Codigo,
l.UF,
l.Calle,
l.Numero,
COALESCE(
(SELECT TOP 1 cv.Nombre FROM LotCatVal lcv JOIN CatVal cv ON lcv.ValorID = cv.ValorID AND cv.CategoriaID = 26 WHERE lcv.LoteID = l.LoteID),
'Habilitado'
) AS Estado,
(
SELECT TOP 1 CONCAT(p.Apellido, ', ', p.Nombre)
FROM LotPer lp
JOIN Per p ON lp.PersonaID = p.PersonaID
WHERE lp.LoteID = l.LoteID
ORDER BY lp.PerfilID
) AS Propietario
FROM lot l
WHERE l.Lote LIKE %s OR l.Calle LIKE %s OR l.UF LIKE %s
OR EXISTS (
SELECT 1 FROM LotPer lp
JOIN Per p ON lp.PersonaID = p.PersonaID
WHERE lp.LoteID = l.LoteID
AND (p.Nombre LIKE %s OR p.Apellido LIKE %s)
)
ORDER BY
CASE
WHEN UPPER(LTRIM(RTRIM(l.Lote))) = UPPER(%s) THEN 1
WHEN UPPER(LTRIM(RTRIM(l.UF))) = UPPER(%s) THEN 2
WHEN UPPER(l.Lote) LIKE UPPER(%s) THEN 3
WHEN UPPER(l.UF) LIKE UPPER(%s) THEN 4
WHEN UPPER(l.Lote) LIKE UPPER(%s) THEN 5
WHEN UPPER(l.Calle) LIKE UPPER(%s) THEN 6
ELSE 7
END,
CASE WHEN ISNUMERIC(l.Lote) = 1 THEN 1 ELSE 2 END,
CASE WHEN ISNUMERIC(l.Lote) = 1 THEN CAST(l.Lote AS INT) ELSE 0 END,
l.Lote
"""
params = (
f_contains, f_contains, f_contains, f_contains, f_contains,
f_exact, f_exact, f_start, f_start, f_contains, f_contains
)
with db_session() as conn:
cursor = conn.cursor(as_dict=True)
cursor.execute(query, params)
return cursor.fetchall()
@staticmethod
def obtener_residentes_lote(lote_id: int) -> List[Dict[str, Any]]:
"""
Obtiene el padrón de residentes y teléfonos vinculados a un lote (LotPer JOIN Per).
Paridad exacta con pct_repository.py:1752.
"""
query = """
SELECT TOP 100
p.PersonaID,
p.Apellido,
p.Nombre,
(SELECT TOP 1 pd.Documento FROM PerDocCatVal pd WHERE pd.PersonaID = p.PersonaID) AS Documento,
COALESCE(pr.Nombre, 'Residente') AS Perfil,
COALESCE(c.Nombre, 'Activo') AS Estado,
(
SELECT STRING_AGG(pt.Telefono, ' / ')
FROM PerTelCatVal pt
WHERE pt.PersonaID = p.PersonaID
) AS Telefonos
FROM LotPer lp
JOIN Per p ON lp.PersonaID = p.PersonaID
LEFT JOIN Prf pr ON lp.PerfilID = pr.PerfilID
LEFT JOIN CatVal c ON lp.ValorID = c.ValorID AND c.CategoriaID = 18
WHERE lp.LoteID = %s
ORDER BY p.Apellido, p.Nombre
"""
with db_session() as conn:
cursor = conn.cursor(as_dict=True)
cursor.execute(query, (lote_id,))
return cursor.fetchall()
@staticmethod