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
+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",