diff --git a/app/routes/test_rda.py b/app/routes/test_rda.py index 8981e96..3b86264 100644 --- a/app/routes/test_rda.py +++ b/app/routes/test_rda.py @@ -13,6 +13,7 @@ from app.services.json_generator import ( agrupar_por_recepcion, ) from app.services.api_client import get_tns_token, TNS_BASE +from app.routes.contratos import load_contrato_map, load_excluded_set router = APIRouter(prefix="/test-rda", tags=["test-rda"]) @@ -161,15 +162,19 @@ async def get_candidatos(request: Request, user: dict = Depends(get_current_user if not ok2: return JSONResponse({"error": err2}) + excluded = load_excluded_set() candidatos = [] for row in rows: + cod_c = str(row.get("CODCONTRATO") or "").strip() + if cod_c in excluded: + continue candidatos.append({ "idrecepcion": row.get("IDRECEPCION"), "factura": row.get("NUM_FACTURA"), "fecha": str(row.get("FECHA_RECEPCION", ""))[:10], "paciente": row.get("COD_PACIENTE", ""), "empresa": row.get("NIT_EMPRESA", ""), - "contrato": row.get("CODCONTRATO", ""), + "contrato": cod_c, "examenes": row.get("EXAMENES", ""), "total": row.get("TOTAL_EXAMENES", 0), }) @@ -199,8 +204,12 @@ async def preview_rda(request: Request, user: dict = Depends(get_current_user), 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}) + grupo = list(grupos.values())[0] + cod_c = str(grupo[0].get("CODCONTRATO") or "").strip() + excluido = cod_c in load_excluded_set() + rda_json = generar_rda_paciente(grupo, prof_def, esp_def, remis_def, prefijo_def, + contrato_map=load_contrato_map()) + return JSONResponse({"json": rda_json, "excluido": excluido, "contrato": cod_c}) @router.post("/enviar") @@ -252,6 +261,12 @@ async def test_enviar(request: Request, user: dict = Depends(get_current_user)): fb.disconnect() return JSONResponse({"error": "Sin registros (¿NUM_FACTURA = 0 o contrato vacío?)"}) + # Bloquear si el contrato está excluido + cod_c_check = str(rows_rda[0].get("CODCONTRATO") or "").strip() + if cod_c_check in load_excluded_set(): + fb.disconnect() + return JSONResponse({"error": f"Contrato {cod_c_check} está excluido del envío TNS"}) + # Pacientes únicos pacientes_unicos = {} for row in rows_rda: @@ -301,12 +316,14 @@ async def test_enviar(request: Request, user: dict = Depends(get_current_user)): # Paso 2: enviar RDA grupos = agrupar_por_recepcion(rows_rda) + contrato_map = load_contrato_map() async with httpx.AsyncClient(timeout=30) as client: for id_rec, grupo_rows in grupos.items(): num_factura = str(grupo_rows[0].get("NUM_FACTURA") or "").strip() numero_override = num_factura - rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def, remis_def, prefijo_def, numero_override) + rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def, remis_def, prefijo_def, numero_override, + contrato_map=contrato_map) examenes = [r.get("COD_EXAMEN", "") for r in grupo_rows] try: r = await client.post(endpoint_rda, json=rda_json, headers=headers)