From 85ae3a0b3e0186f4673f26fd3cb2c73b21da4a2f Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 18 Jul 2026 14:29:43 -0500 Subject: [PATCH] feat: add /debug-fb/tarifas endpoint to explore price tables in Firebird Co-Authored-By: Claude Sonnet 4.6 --- app/routes/debug_fb.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/app/routes/debug_fb.py b/app/routes/debug_fb.py index d589b7b..5b60e74 100644 --- a/app/routes/debug_fb.py +++ b/app/routes/debug_fb.py @@ -119,6 +119,58 @@ async def debug_recepcion(user: dict = Depends(get_current_user)): return JSONResponse({"traceback": traceback.format_exc()}) +@router.get("/tarifas") +async def debug_tarifas(user: dict = Depends(get_current_user)): + """Explora tablas de tarifas/precios en Firebird.""" + try: + conn = get_connection() + cfg = {r["key"]: r["value"] for r in conn.execute("SELECT * FROM config").fetchall()} + conn.close() + fb, ok, msg = get_firebird_from_config(cfg) + if not ok: + return JSONResponse({"ok": False, "error": msg}) + + # Todas las tablas que contengan TARIF, PRECIO, ARANCEL, CONVENIO, ISS en el nombre + ok_t, _, tablas = fb.execute_query( + "SELECT TRIM(r.RDB$RELATION_NAME) AS TABLA " + "FROM RDB$RELATIONS r " + "WHERE r.RDB$SYSTEM_FLAG = 0 " + " AND (UPPER(TRIM(r.RDB$RELATION_NAME)) CONTAINING 'TARIF' " + " OR UPPER(TRIM(r.RDB$RELATION_NAME)) CONTAINING 'PRECIO' " + " OR UPPER(TRIM(r.RDB$RELATION_NAME)) CONTAINING 'ARANCEL' " + " OR UPPER(TRIM(r.RDB$RELATION_NAME)) CONTAINING 'CONVENIO' " + " OR UPPER(TRIM(r.RDB$RELATION_NAME)) CONTAINING 'VALOR') " + "ORDER BY TABLA", None + ) + + result = {"tablas_encontradas": [r["TABLA"] for r in (tablas or [])] if ok_t else []} + + # Para cada tabla encontrada: columnas + 3 filas de muestra + for t in result["tablas_encontradas"][:6]: + ok_c, _, cols = fb.execute_query( + "SELECT TRIM(f.RDB$FIELD_NAME) AS COL " + "FROM RDB$RELATION_FIELDS f " + f"WHERE TRIM(f.RDB$RELATION_NAME) = '{t}' " + "ORDER BY f.RDB$FIELD_POSITION", None + ) + ok_r, _, rows = fb.execute_query( + f"SELECT FIRST 3 * FROM {t}", None + ) + ok_n, _, cnt = fb.execute_query( + f"SELECT COUNT(*) AS N FROM {t}", None + ) + result[t] = { + "columnas": [c["COL"] for c in (cols or [])] if ok_c else [], + "total": cnt[0]["N"] if ok_n and cnt else "?", + "muestra": list(rows or []) if ok_r else [], + } + + fb.disconnect() + return JSONResponse(result) + except Exception: + return JSONResponse({"ok": False, "traceback": traceback.format_exc()}) + + @router.get("/cups") async def find_cups(request: Request, user: dict = Depends(get_current_user)): try: