fix: update /debug-fb/recepcion to show all columns and empresa tables

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-18 14:51:07 -05:00
co-authored by Claude Sonnet 4.6
parent 85ae3a0b3e
commit 7aa02fe6ac
+31 -26
View File
@@ -69,7 +69,7 @@ async def listar_diagnosticos(
@router.get("/recepcion")
async def debug_recepcion(user: dict = Depends(get_current_user)):
"""Diagnóstico: qué hay en RECEPCION hoy sin filtros restrictivos."""
"""Diagnóstico: columnas de RECEPCION, tabla EMPRESA y link a tarifa."""
try:
conn = get_connection()
cfg = {r["key"]: r["value"] for r in conn.execute("SELECT * FROM config").fetchall()}
@@ -81,37 +81,42 @@ async def debug_recepcion(user: dict = Depends(get_current_user)):
result = {}
# Últimas 5 recepciones sin ningún filtro
ok1, _, rows1 = fb.execute_query(
"SELECT FIRST 5 r.IDRECEPCION, r.FECHA_RECEPCION, r.HORAINICIORECEPCION, "
"r.NUM_FACTURA, r.COD_PACIENTE "
"FROM RECEPCION r ORDER BY r.IDRECEPCION DESC", None
# Todas las columnas de RECEPCION
ok_c, _, cols = fb.execute_query(
"SELECT TRIM(f.RDB$FIELD_NAME) AS COL "
"FROM RDB$RELATION_FIELDS f "
"WHERE TRIM(f.RDB$RELATION_NAME) = 'RECEPCION' "
"ORDER BY f.RDB$FIELD_POSITION", None
)
result["ultimas_5_sin_filtro"] = list(rows1) if ok1 and rows1 else f"error: {rows1}"
result["columnas_recepcion"] = [r["COL"] for r in (cols or [])] if ok_c else []
# Fecha actual en Firebird
ok2, _, rows2 = fb.execute_query("SELECT CURRENT_DATE AS FH FROM RDB$DATABASE", None)
result["current_date_firebird"] = rows2[0] if ok2 and rows2 else "error"
# Cuántas recepciones de hoy (CURRENT_DATE) sin filtro NUM_FACTURA
ok3, _, rows3 = fb.execute_query(
"SELECT COUNT(*) AS CNT FROM RECEPCION r WHERE r.FECHA_RECEPCION = CURRENT_DATE", None
# Tablas con EMPRESA o CONVENIO en el nombre
ok_e, _, tabs = 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 'EMPRESA' "
" OR UPPER(TRIM(r.RDB$RELATION_NAME)) CONTAINING 'CONVENIO') "
"ORDER BY TABLA", None
)
result["hoy_sin_filtro_factura"] = rows3[0] if ok3 and rows3 else "error"
result["tablas_empresa_convenio"] = [r["TABLA"] for r in (tabs or [])] if ok_e else []
# Cuántas con NUM_FACTURA > 0
ok4, _, rows4 = fb.execute_query(
"SELECT COUNT(*) AS CNT FROM RECEPCION r "
"WHERE r.FECHA_RECEPCION = CURRENT_DATE AND r.NUM_FACTURA > 0", None
)
result["hoy_con_factura"] = rows4[0] if ok4 and rows4 else "error"
# Columnas + muestra de cada tabla encontrada
for t in result["tablas_empresa_convenio"][:4]:
ok_tc, _, tcols = 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_tr, _, trows = fb.execute_query(f"SELECT FIRST 2 * FROM {t}", None)
result[f"tabla_{t}"] = {
"columnas": [c["COL"] for c in (tcols or [])] if ok_tc else [],
"muestra": list(trows or []) if ok_tr else [],
}
# Distintas fechas recientes en la tabla
ok5, _, rows5 = fb.execute_query(
"SELECT FIRST 5 DISTINCT r.FECHA_RECEPCION FROM RECEPCION r "
"ORDER BY r.FECHA_RECEPCION DESC", None
# 1 recepción completa para ver si tiene COD_TARIFA o NIT_EMPRESA
ok_r, _, rec = fb.execute_query(
"SELECT FIRST 1 r.* FROM RECEPCION r ORDER BY r.IDRECEPCION DESC", None
)
result["fechas_recientes"] = list(rows5) if ok5 and rows5 else "error"
result["recepcion_completa"] = dict(rec[0]) if ok_r and rec else {}
fb.disconnect()
return JSONResponse(result)