From ef71a1db56c028dc3f6508916a5abfab6be51d75 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 26 Jun 2026 19:32:53 -0500 Subject: [PATCH] =?UTF-8?q?test-rda:=20add=20preview,=20ver=20m=C3=A1s=20p?= =?UTF-8?q?agination,=20sent=20tracking,=20default=20contrato=3D011?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- app/database.py | 10 ++ app/routes/test_rda.py | 53 ++++++++- app/templates/test_rda.html | 217 +++++++++++++++++++++++++++++------- 3 files changed, 233 insertions(+), 47 deletions(-) diff --git a/app/database.py b/app/database.py index 5cc8b73..94b3674 100644 --- a/app/database.py +++ b/app/database.py @@ -39,6 +39,16 @@ def init_db(): created_at TEXT NOT NULL DEFAULT (datetime('now')) ); + CREATE TABLE IF NOT EXISTS rda_test_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + idrecepcion INTEGER NOT NULL, + factura TEXT, + contrato TEXT, + ok INTEGER NOT NULL DEFAULT 0, + mensaje TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')) + ); + CREATE TABLE IF NOT EXISTS envios ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, diff --git a/app/routes/test_rda.py b/app/routes/test_rda.py index 05d103b..0265d31 100644 --- a/app/routes/test_rda.py +++ b/app/routes/test_rda.py @@ -70,9 +70,21 @@ async def test_page(request: Request, user: dict = Depends(get_current_user)): ) +@router.get("/enviados") +async def get_enviados(request: Request, user: dict = Depends(get_current_user)): + db = get_connection() + rows = db.execute( + "SELECT idrecepcion, ok FROM rda_test_log ORDER BY created_at DESC" + ).fetchall() + db.close() + enviados = {r["idrecepcion"]: bool(r["ok"]) for r in rows} + return JSONResponse({"enviados": enviados}) + + @router.get("/candidatos") async def get_candidatos(request: Request, user: dict = Depends(get_current_user), - contrato: str = "", factura: str = "", articulos: str = ""): + contrato: str = "", factura: str = "", articulos: str = "", + offset: int = 0): db = get_connection() cfg = {r[0]: r[1] for r in db.execute("SELECT key, value FROM config").fetchall()} db.close() @@ -102,8 +114,8 @@ async def get_candidatos(request: Request, user: dict = Depends(get_current_user filtros.append("r.NUM_FACTURA = :factura") params["factura"] = int(factura) - sql_final = """ - SELECT FIRST 10 + sql_final = f""" + SELECT FIRST 10 SKIP {int(offset)} r.IDRECEPCION, r.NUM_FACTURA, r.FECHA_RECEPCION, r.COD_PACIENTE, r.NIT_EMPRESA, e.CODCONTRATO, LIST(DISTINCT rel.COD_EXAMEN, ', ') AS EXAMENES, @@ -111,7 +123,7 @@ async def get_candidatos(request: Request, user: dict = Depends(get_current_user FROM RECEPCION r JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION LEFT JOIN EMPRESA e ON e.NIT = r.NIT_EMPRESA - WHERE """ + " AND ".join(filtros) + """ + WHERE {" AND ".join(filtros)} GROUP BY r.IDRECEPCION, r.NUM_FACTURA, r.FECHA_RECEPCION, r.COD_PACIENTE, r.NIT_EMPRESA, e.CODCONTRATO ORDER BY r.IDRECEPCION DESC """ @@ -138,6 +150,32 @@ async def get_candidatos(request: Request, user: dict = Depends(get_current_user return JSONResponse({"candidatos": candidatos}) +@router.get("/preview") +async def preview_rda(request: Request, user: dict = Depends(get_current_user), + idrecepcion: int = 0): + if not idrecepcion: + return JSONResponse({"error": "Falta idrecepcion"}) + db = get_connection() + cfg = {r[0]: r[1] for r in db.execute("SELECT key, value FROM config").fetchall()} + db.close() + prof_def = cfg.get("profesional_default", "XIMENA") + esp_def = cfg.get("especialidad_default", "02") + remis_def = cfg.get("remisionante_default", "00") + prefijo_def = cfg.get("prefijo_tns_default", "00") + fb, fb_ok, fb_msg = get_firebird_from_config(cfg) + if not fb_ok: + return JSONResponse({"error": fb_msg}) + ok2, err2, rows_rda = fb.execute_query( + _SQL_RDA_BY_WHERE.format(where="r.IDRECEPCION = :val"), {"val": idrecepcion} + ) + fb.disconnect() + if not ok2 or not rows_rda: + return JSONResponse({"error": err2 or "Sin registros"}) + grupos = agrupar_por_recepcion(rows_rda) + rda_json = generar_rda_paciente(list(grupos.values())[0], prof_def, esp_def, remis_def, prefijo_def) + return JSONResponse({"json": rda_json}) + + @router.post("/enviar") async def test_enviar(request: Request, user: dict = Depends(get_current_user)): form = await request.form() @@ -259,6 +297,13 @@ async def test_enviar(request: Request, user: dict = Depends(get_current_user)): "respuesta_tns": raw_resp, "json_enviado": rda_json, }) + db_log = get_connection() + db_log.execute( + "INSERT OR REPLACE INTO rda_test_log (idrecepcion, factura, contrato, ok, mensaje) VALUES (?,?,?,?,?)", + (id_rec, rda_json.get("numero", ""), rda_json.get("numeroContrato", ""), 1 if ok_rda else 0, msg_rda) + ) + db_log.commit() + db_log.close() except Exception as ex: pasos.append({ "tipo": "rda", "idrecepcion": id_rec, "ok": False, diff --git a/app/templates/test_rda.html b/app/templates/test_rda.html index 3a27f83..fc0697a 100644 --- a/app/templates/test_rda.html +++ b/app/templates/test_rda.html @@ -6,15 +6,15 @@
-
+

Candidatos disponibles

- - @@ -35,23 +35,47 @@ Paciente Contrato Exámenes - + Acciones +
+ +
+ + +
-

Enviar RDA

+

Enviar RDA directo

-
+
+
{% endblock %}