diff --git a/app/routers/evidencia.py b/app/routers/evidencia.py index 813d894..12826a8 100644 --- a/app/routers/evidencia.py +++ b/app/routers/evidencia.py @@ -23,6 +23,7 @@ from app.models import Cliente, Credito from app.routers import render from app.services import avisos as servicio_avisos from app.services import avisos_24h as servicio_avisos_24h +from app.services import expediente as servicio_expediente from app.services.agregados import tenant_de_sesion from app.services.evidencia import exportar_cliente, nombre_archivo from app.services.reconstruccion import ( @@ -32,6 +33,7 @@ from app.services.reconstruccion import ( ) router = APIRouter(tags=["evidencia"]) +MAXIMO_CANDIDATOS = 10 # clientes que se listan cuando la búsqueda da más de uno def _fecha(texto: str | None) -> date: @@ -54,22 +56,46 @@ def indice( ) -> Response: dia = _fecha(fecha) error = None - if cliente: + candidatos: list[dict] = [] + if cliente and cliente.strip(): + texto = cliente.strip() tenant = tenant_de_sesion(db) + # El identificador exacto va directo; si no, se busca por nombre, RFC o parte del + # identificador (la misma búsqueda del expediente) con la identificación vigente. cliente_id = db.scalar( - select(Cliente.id).where( - Cliente.tenant_id == tenant.id, Cliente.id_externo == cliente.strip() - ) + select(Cliente.id).where(Cliente.tenant_id == tenant.id, Cliente.id_externo == texto) ) + if cliente_id is None: + filas, total = servicio_expediente.buscar_clientes( + db, tenant, texto, 1, MAXIMO_CANDIDATOS + ) + if total == 1: + cliente_id = filas[0][0].id + elif total: + candidatos = [ + { + "id": c.id, + "id_externo": c.id_externo, + "nombre": i.nombre_busqueda if i else None, + "rfc": i.rfc if i else None, + } + for c, i in filas + ] + if total > len(candidatos): + error = ( + f"{total} clientes coinciden con «{texto}»: se muestran los primeros " + f"{len(candidatos)}. Escribe más del nombre o el RFC completo." + ) + else: + error = f"No hay ningún cliente con ese nombre, RFC o identificador: {texto}." if cliente_id is not None: return RedirectResponse( f"/evidencia/{cliente_id}?fecha={dia.isoformat()}", status_code=303 ) - error = f"No hay ningún cliente con identificador {cliente.strip()}." return render( request, "evidencia/index.html", - {"fecha": dia, "cliente": cliente or "", "error": error}, + {"fecha": dia, "cliente": cliente or "", "error": error, "candidatos": candidatos}, ) diff --git a/app/services/expediente.py b/app/services/expediente.py index 700b0ff..baea06c 100644 --- a/app/services/expediente.py +++ b/app/services/expediente.py @@ -805,7 +805,8 @@ def nombres_vigentes( def buscar_clientes( session: Session, tenant: Tenant, q: str | None, pagina: int, por_pagina: int ) -> tuple[list[tuple[Cliente, Identificacion | None]], int]: - """Clientes del tenant por nombre vigente o `id_externo`, paginados.""" + """Clientes del tenant por nombre o RFC de su identificación vigente, o por `id_externo`, + paginados. El texto puede ser una parte del nombre, del RFC o del identificador.""" consulta = ( select(Cliente, Identificacion) .outerjoin( @@ -816,8 +817,13 @@ def buscar_clientes( ) if q: patron = f"%{q.strip()}%" + rfc = f"%{''.join(q.split()).upper()}%" # el RFC se guarda en mayúsculas y sin espacios consulta = consulta.where( - or_(Cliente.id_externo.ilike(patron), Identificacion.nombre_busqueda.ilike(patron)) + or_( + Cliente.id_externo.ilike(patron), + Identificacion.nombre_busqueda.ilike(patron), + Identificacion.rfc.like(rfc), + ) ) total = session.scalar(select(func.count()).select_from(consulta.subquery())) or 0 filas = session.execute( diff --git a/app/templates/evidencia/index.html b/app/templates/evidencia/index.html index 93464e6..1b09651 100644 --- a/app/templates/evidencia/index.html +++ b/app/templates/evidencia/index.html @@ -20,13 +20,25 @@ {% if error %}{{ aviso(error, "peligro") }}{% endif %}
| Cliente | RFC | Identificador | |
|---|---|---|---|
| {{ c.nombre or "—" }} | {{ c.rfc or "—" }} | {{ c.id_externo }} | Reconstruir |