feat: add /debug-fb/tarifas endpoint to explore price tables in Firebird

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-18 14:29:43 -05:00
co-authored by Claude Sonnet 4.6
parent 268451decc
commit 85ae3a0b3e
+52
View File
@@ -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: