diff --git a/app/routes/automation.py b/app/routes/automation.py index a3f478c..cd6beec 100644 --- a/app/routes/automation.py +++ b/app/routes/automation.py @@ -571,6 +571,9 @@ async def run_automation( "paso4_ventas": {"enviados": 0, "errores": 0, "detalle": []}, } + # Pacientes cuyo tercero falló → no enviar RDA para ellos + terceros_fallidos: set[str] = set() + # ── PASO 1: Enviar Terceros (particulares + EPS) ───────────────────────── if "terceros" in pasos_set: async with httpx.AsyncClient(timeout=timeout) as client: @@ -594,6 +597,7 @@ async def run_automation( resultado["paso1_terceros"]["enviados"] += 1 else: resultado["paso1_terceros"]["errores"] += 1 + terceros_fallidos.add(codigo_pac) resultado["paso1_terceros"]["detalle"].append({ "codigo": codigo_pac, "doc": doc, "nombre": nombre, "ok": ok, "msg": msg, @@ -631,6 +635,17 @@ async def run_automation( if "rda" in pasos_set: async with httpx.AsyncClient(timeout=timeout) as client: for id_recepcion, grupo_rows in grupos.items(): + cod_pac = str(grupo_rows[0].get("COD_PACIENTE", "")) + if cod_pac in terceros_fallidos: + resultado["paso2_rda"]["detalle"].append({ + "idrecepcion": id_recepcion, + "factura": str(grupo_rows[0].get("NUM_FACTURA") or id_recepcion), + "paciente": cod_pac, + "examenes": len(grupo_rows), + "ok": False, "msg": "Omitido: tercero no creado en TNS", + }) + resultado["paso2_rda"]["errores"] += 1 + continue num_fac = str(grupo_rows[0].get("NUM_FACTURA") or "").strip() num_override = num_fac rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def, remis_def, prefijo_def, num_override, contrato_map=contrato_map, sin_contrato_set=sin_contrato_set) @@ -670,6 +685,17 @@ async def run_automation( if "preserv" in pasos_set: async with httpx.AsyncClient(timeout=timeout) as client: for id_ps, grupo_rows in grupos_ps.items(): + cod_pac = str(grupo_rows[0].get("COD_PACIENTE", "")) + if cod_pac in terceros_fallidos: + resultado["paso3_preserv"]["detalle"].append({ + "idrecepcion": id_ps, + "factura": str(grupo_rows[0].get("PS_NUMERO") or id_ps), + "paciente": cod_pac, + "examenes": len(grupo_rows), + "ok": False, "msg": "Omitido: tercero no creado en TNS", + }) + resultado["paso3_preserv"]["errores"] += 1 + continue ps_prefijo = str(grupo_rows[0].get("PS_PREFIJO") or "SC").strip() ps_numero = str(grupo_rows[0].get("PS_NUMERO") or "").strip() rda_json = generar_rda_paciente(