diff --git a/app/services/json_generator.py b/app/services/json_generator.py index 13f9fcc..160389a 100644 --- a/app/services/json_generator.py +++ b/app/services/json_generator.py @@ -182,29 +182,34 @@ def generar_rda_paciente(rows: list, default_profesional: str = "", default_espe nit_empresa = str(h.get("NIT_EMPRESA") or "").strip() cod_contrato_raw = str(h.get("CODCONTRATO") or "").strip() - # Particular: por NIT_EMPRESA "PART" o por contrato "12"/"012" + + # Resolver tipo desde contrato_map primero (tiene prioridad sobre heurísticas) + _TIPOUSU_MAP = {"1": "11", "5": "07"} # EPS→11, Póliza→07 + _nc_s = cod_contrato_raw.lstrip("0") or cod_contrato_raw + _map_tipo = "" + if contrato_map and cod_contrato_raw: + _map_tipo = contrato_map.get(cod_contrato_raw) or contrato_map.get(_nc_s) or "" + + # es_particular: NIT "PART", o mapa dice "12", o heurística solo si no está en el mapa es_particular = ( nit_empresa.upper() in ("PART", "PARTICULAR", "", "0") - or cod_contrato_raw.lstrip("0") == "12" + or (_map_tipo == "12") + or (not _map_tipo and cod_contrato_raw.lstrip("0") == "12") ) cod_contrato = cod_contrato_raw or ("012" if es_particular else None) cod_forma_pago = "CLIP" if es_particular else "INST" autorizacion = None if es_particular else (str(h.get("AUTORIZACION") or "").strip() or None) - # tipousuario: prioridad 1→tabla contratos, 2→particular, 3→TIPOUSUSISPRO/TIPOUSU - _TIPOUSU_MAP = {"1": "11", "5": "07"} # EPS→11, Póliza→07 - tipoususispro = "" - if contrato_map and cod_contrato_raw: - _nc_s = cod_contrato_raw.lstrip("0") or cod_contrato_raw - tipoususispro = contrato_map.get(cod_contrato_raw) or contrato_map.get(_nc_s) or "" - if not tipoususispro: - if es_particular: - tipoususispro = "12" - else: - tipoususispro = str(h.get("TIPOUSUSISPRO") or "").strip() - if not tipoususispro: - tipousu = str(h.get("TIPOUSU") or "").strip() - tipoususispro = _TIPOUSU_MAP.get(tipousu, "11") + # tipousuario: prioridad 1→mapa contratos, 2→particular, 3→TIPOUSUSISPRO/TIPOUSU + if _map_tipo: + tipoususispro = _map_tipo + elif es_particular: + tipoususispro = "12" + else: + tipoususispro = str(h.get("TIPOUSUSISPRO") or "").strip() + if not tipoususispro: + tipousu = str(h.get("TIPOUSU") or "").strip() + tipoususispro = _TIPOUSU_MAP.get(tipousu, "11") via_ingreso = "01" modalidad = "01"