feat(logs): filtro por cédula de paciente en historial de envíos

- Agrega columna cedula a tabla envios (migración automática)
- _guardar_envio almacena la cédula en los 4 pasos de automation
- pac_map (COD_PACIENTE→DOCIDENT) para RDA/preserv/ventas
- logs.py busca por cedula LIKE en vez de idrecepcion/contrato
- logs.html etiqueta el input como "Cédula paciente"

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-22 22:37:40 -05:00
co-authored by Claude Sonnet 4.6
parent 03655427a5
commit 599391739f
4 changed files with 21 additions and 13 deletions
+2
View File
@@ -25,6 +25,8 @@ def _migrate(conn):
conn.execute("ALTER TABLE envios ADD COLUMN contrato TEXT")
if "mensaje_tns" not in cols:
conn.execute("ALTER TABLE envios ADD COLUMN mensaje_tns TEXT")
if "cedula" not in cols:
conn.execute("ALTER TABLE envios ADD COLUMN cedula TEXT")
tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
if "activity_log" not in tables:
+16 -10
View File
@@ -559,6 +559,8 @@ async def run_automation(
api_sucursal = configs.get("api_sucursal", "")
timeout = int(configs.get("api_timeout", 30))
pac_map = {str(r.get("CODIGO", "")): str(r.get("DOCIDENT") or "").strip() for r in rows_pac_todos}
pasos_set = {p.strip() for p in pasos.split(",")}
resultado = {
"fecha": fecha,
@@ -597,7 +599,8 @@ async def run_automation(
"codigo": codigo_pac, "doc": doc, "nombre": nombre, "ok": ok, "msg": msg,
})
_guardar_envio(user["user_id"], "terceros", fecha, tercero_json, msg, ok)
_guardar_envio(user["user_id"], "terceros", fecha, tercero_json, msg, ok,
cedula=str(row.get("DOCIDENT") or "").strip())
# ── Sync paralelo a WhatsApp Lab (silencioso) ─────────────────────────────
wa_url = configs.get("whatsapp_url", "").rstrip("/")
@@ -654,8 +657,9 @@ async def run_automation(
})
_guardar_envio(user["user_id"], "transaccion", factura, rda_json, msg, ok,
fecha_inicio=fecha, fecha_fin=fecha,
servicios=len(grupo_rows))
fecha_inicio=fecha, fecha_fin=fecha, servicios=len(grupo_rows),
idrecepcion=id_recepcion, contrato=str(grupo_rows[0].get("CODCONTRATO") or "").strip(),
cedula=pac_map.get(str(grupo_rows[0].get("COD_PACIENTE", "")), ""))
# ── PASO 3: Enviar Pre-servicios (RCXC / SC) ──────────────────────────────
grupos_ps_all = _agrupar_por_presserv(rows_ps)
@@ -698,8 +702,9 @@ async def run_automation(
})
_guardar_envio(user["user_id"], "transaccion", factura_ps, rda_json, msg, ok,
fecha_inicio=fecha, fecha_fin=fecha,
servicios=len(grupo_rows))
fecha_inicio=fecha, fecha_fin=fecha, servicios=len(grupo_rows),
idrecepcion=id_ps, contrato=str(grupo_rows[0].get("CODCONTRATO") or "").strip(),
cedula=pac_map.get(str(grupo_rows[0].get("COD_PACIENTE", "")), ""))
# ── PASO 4: Enviar Facturas Venta ─────────────────────────────────────────
excluded_ventas = load_excluded_ventas_set()
@@ -742,7 +747,8 @@ async def run_automation(
_guardar_envio(user["user_id"], "ventas", factura_display, venta_json, msg, ok,
fecha_inicio=fecha, fecha_fin=fecha, servicios=len(grupo_rows),
idrecepcion=id_rec, contrato=contrato_vta)
idrecepcion=id_rec, contrato=contrato_vta,
cedula=pac_map.get(str(grupo_rows[0].get("COD_PACIENTE", "")), ""))
r1 = resultado["paso1_terceros"]
r2 = resultado["paso2_rda"]
@@ -759,16 +765,16 @@ async def run_automation(
def _guardar_envio(user_id, tipo, factura, json_data, respuesta, ok,
fecha_inicio=None, fecha_fin=None, servicios=0,
idrecepcion=None, contrato=None):
idrecepcion=None, contrato=None, cedula=None):
try:
conn = get_connection()
conn.execute("""
INSERT INTO envios (user_id, tipo, factura, idrecepcion, contrato,
INSERT INTO envios (user_id, tipo, factura, idrecepcion, contrato, cedula,
fecha_inicio, fecha_fin,
pacientes_count, servicios_count, status, json_enviado, respuesta_api, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
user_id, tipo, factura, idrecepcion, contrato,
user_id, tipo, factura, idrecepcion, contrato, cedula,
fecha_inicio, fecha_fin,
1, servicios,
"success" if ok else "error",
+1 -1
View File
@@ -49,7 +49,7 @@ async def logs_page(
where.append("e.factura LIKE ?")
params.append(f"%{factura}%")
if paciente:
where.append("(CAST(e.idrecepcion AS TEXT) LIKE ? OR e.contrato LIKE ?)")
where.append("(e.cedula LIKE ? OR e.factura LIKE ?)")
params += [f"%{paciente}%", f"%{paciente}%"]
if fecha_desde:
where.append("DATE(e.created_at) >= ?")
+2 -2
View File
@@ -62,8 +62,8 @@
class="px-3 py-1.5 border border-gray-300 rounded-lg text-sm w-28">
</div>
<div>
<label class="block text-xs font-medium text-gray-500 mb-1">IDRECEP / Contrato</label>
<input type="text" name="paciente" value="{{ filtro_paciente }}" placeholder="ID o contrato"
<label class="block text-xs font-medium text-gray-500 mb-1">Cédula paciente</label>
<input type="text" name="paciente" value="{{ filtro_paciente }}" placeholder="Nro. documento"
class="px-3 py-1.5 border border-gray-300 rounded-lg text-sm w-36">
</div>
<div class="flex gap-2">