Shows recent recepciones without filters, CURRENT_DATE from Firebird, and counts with/without NUM_FACTURA filter to diagnose zero-result sync. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
114 lines
4.4 KiB
Python
114 lines
4.4 KiB
Python
import traceback
|
|
from fastapi import APIRouter, Request, Depends
|
|
from fastapi.responses import JSONResponse
|
|
from app.database import get_connection
|
|
from app.auth import get_current_user
|
|
from app.services.firebird_service import get_firebird_from_config
|
|
|
|
router = APIRouter(prefix="/debug-fb", tags=["debug"])
|
|
|
|
|
|
@router.get("/recepcion")
|
|
async def debug_recepcion(user: dict = Depends(get_current_user)):
|
|
"""Diagnóstico: qué hay en RECEPCION hoy sin filtros restrictivos."""
|
|
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({"error": msg})
|
|
|
|
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
|
|
)
|
|
result["ultimas_5_sin_filtro"] = list(rows1) if ok1 and rows1 else f"error: {rows1}"
|
|
|
|
# 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
|
|
)
|
|
result["hoy_sin_filtro_factura"] = rows3[0] if ok3 and rows3 else "error"
|
|
|
|
# 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"
|
|
|
|
# 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
|
|
)
|
|
result["fechas_recientes"] = list(rows5) if ok5 and rows5 else "error"
|
|
|
|
fb.disconnect()
|
|
return JSONResponse(result)
|
|
except Exception:
|
|
return JSONResponse({"traceback": traceback.format_exc()})
|
|
|
|
|
|
@router.get("/cups")
|
|
async def find_cups(request: Request, user: dict = Depends(get_current_user)):
|
|
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({"error": msg})
|
|
|
|
results = {}
|
|
|
|
BUSCAR = "903967"
|
|
|
|
# Tablas y sus campos de texto para buscar
|
|
tablas_candidatas = [
|
|
("PROTOCOLO", ["CODIGO", "NOMBRE"]),
|
|
("ITEM", ["COD_PROTOCOLO", "CUPS_DETALLE", "CODITEMINTERFACE"]),
|
|
("RELACION", ["COD_EXAMEN"]),
|
|
("RELACION_HIMS", ["CUPS_REF"]),
|
|
("ARTICULO", ["CODIGO", "CUPS"]),
|
|
("SERVICIO", ["CODIGO", "CUPS", "COD_CUPS"]),
|
|
("PROCEDIMIENTO", ["CODIGO", "CUPS"]),
|
|
]
|
|
|
|
encontrado = {}
|
|
for tabla, campos in tablas_candidatas:
|
|
for campo in campos:
|
|
ok_t, _, rows_t = fb.execute_query(
|
|
f"SELECT FIRST 3 t.* FROM {tabla} t "
|
|
f"WHERE TRIM(t.{campo}) = '{BUSCAR}'"
|
|
)
|
|
if ok_t and rows_t:
|
|
encontrado[f"{tabla}.{campo}"] = list(rows_t)
|
|
|
|
results["valor_903967_encontrado_en"] = encontrado if encontrado else "no encontrado en tablas comunes"
|
|
|
|
# Buscar en campos de tipo char/varchar de PROTOCOLO e ITEM que no conocemos
|
|
for tabla in ["PROTOCOLO", "ITEM"]:
|
|
ok_c, _, rows_c = fb.execute_query(
|
|
f"SELECT TRIM(f.RDB$FIELD_NAME) AS c FROM RDB$RELATION_FIELDS f "
|
|
f"WHERE TRIM(f.RDB$RELATION_NAME)='{tabla}' ORDER BY f.RDB$FIELD_POSITION"
|
|
)
|
|
if ok_c:
|
|
results[f"columnas_{tabla.lower()}"] = [r["c"] for r in rows_c]
|
|
|
|
fb.disconnect()
|
|
return JSONResponse(results)
|
|
except Exception:
|
|
return JSONResponse({"traceback": traceback.format_exc()})
|