diff --git a/app/database.py b/app/database.py index a204a42..9e1de7e 100644 --- a/app/database.py +++ b/app/database.py @@ -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: diff --git a/app/routes/automation.py b/app/routes/automation.py index 8951468..a3f478c 100644 --- a/app/routes/automation.py +++ b/app/routes/automation.py @@ -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", diff --git a/app/routes/logs.py b/app/routes/logs.py index 7573e60..eef1ed5 100644 --- a/app/routes/logs.py +++ b/app/routes/logs.py @@ -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) >= ?") diff --git a/app/templates/logs.html b/app/templates/logs.html index a66bb91..f7f647c 100644 --- a/app/templates/logs.html +++ b/app/templates/logs.html @@ -62,8 +62,8 @@ class="px-3 py-1.5 border border-gray-300 rounded-lg text-sm w-28">