From 3017c7956ec2fc52e4d610dcbad16d68fd3916da Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 26 Jun 2026 18:01:32 -0500 Subject: [PATCH] Fix numeroContrato: use EMPRESA.CODCONTRATO from Firebird, sucursal default 00 - Join EMPRESA table in all RDA queries to get CODCONTRATO (TNS contract code) - Use EMPRESA.CODCONTRATO as numeroContrato instead of raw NIT_EMPRESA - Especialidad always uses default_especialidad (ignore Firebird COD_ESPECIALIDAD) - sucursal defaults to '00' (not empty) in config and endpoints - Fix config template label: especialidad default is TNS code not Firebird code Co-Authored-By: Claude Sonnet 4.6 --- app/routes/automation.py | 8 ++++---- app/routes/config.py | 2 +- app/routes/queries.py | 8 ++++++-- app/routes/transaccion.py | 6 ++---- app/services/json_generator.py | 5 +++-- app/templates/config.html | 2 +- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/routes/automation.py b/app/routes/automation.py index 5c83152..06fe222 100644 --- a/app/routes/automation.py +++ b/app/routes/automation.py @@ -59,10 +59,12 @@ SELECT rel.PRECIO, rel.FECHA_REPORTADO, m.COD_ESPECIALIDAD, - COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional + COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional, + e.CODCONTRATO FROM RECEPCION r JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO +LEFT JOIN EMPRESA e ON e.NIT = r.NIT_EMPRESA WHERE r.FECHA_RECEPCION BETWEEN :fecha_ini AND :fecha_fin ORDER BY r.IDRECEPCION """ @@ -226,9 +228,7 @@ async def run_automation( # ── PASO 2: Enviar RDA Paciente ─────────────────────────────────────────── grupos = agrupar_por_recepcion(rows_rda) - endpoint = f"{TNS_BASE}/v2/rda/RdaPaciente/Insertar" - if api_sucursal: - endpoint += f"?codigosucursal={api_sucursal}" + endpoint = f"{TNS_BASE}/v2/rda/RdaPaciente/Insertar?codigosucursal={api_sucursal or '00'}" prof_def = configs.get("profesional_default", "") esp_def = configs.get("especialidad_default", "") diff --git a/app/routes/config.py b/app/routes/config.py index 16616bd..d5883a5 100644 --- a/app/routes/config.py +++ b/app/routes/config.py @@ -17,7 +17,7 @@ DEFAULT_KEYS = [ ("api_key", ""), ("api_timeout", "30"), ("api_version", "1"), - ("api_sucursal", ""), + ("api_sucursal", "00"), ("tns_empresa", "9002787299"), ("tns_usuario", "DOCUXER"), ("tns_password", "Nicolas2796*+"), diff --git a/app/routes/queries.py b/app/routes/queries.py index a6e9ee2..9aa2fc4 100644 --- a/app/routes/queries.py +++ b/app/routes/queries.py @@ -51,10 +51,12 @@ WHERE p.DOCIDENT = :doc_num""", rel.PRECIO, rel.FECHA_REPORTADO, m.COD_ESPECIALIDAD, - COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional + COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional, + e.CODCONTRATO FROM RECEPCION r JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO +LEFT JOIN EMPRESA e ON e.NIT = r.NIT_EMPRESA WHERE r.NUM_FACTURA = :num_factura""", "description": "Servicios de una recepción por número de factura" }, @@ -79,10 +81,12 @@ WHERE r.NUM_FACTURA = :num_factura""", rel.PRECIO, rel.FECHA_REPORTADO, m.COD_ESPECIALIDAD, - COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional + COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional, + e.CODCONTRATO FROM RECEPCION r JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO +LEFT JOIN EMPRESA e ON e.NIT = r.NIT_EMPRESA WHERE r.FECHA_RECEPCION BETWEEN :fecha_ini AND :fecha_fin ORDER BY r.IDRECEPCION""", "description": "Servicios en un rango de fechas (para RdaPaciente/Insertar)" diff --git a/app/routes/transaccion.py b/app/routes/transaccion.py index 0d756bd..3f81c26 100644 --- a/app/routes/transaccion.py +++ b/app/routes/transaccion.py @@ -147,10 +147,8 @@ async def send_transaccion( return JSONResponse({"success": False, "message": f"Error login TNS: {token_err}"}) headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"} - api_sucursal = configs.get("api_sucursal", "") - endpoint = f"{TNS_BASE}/v2/rda/RdaPaciente/Insertar" - if api_sucursal: - endpoint += f"?codigosucursal={api_sucursal}" + api_sucursal = configs.get("api_sucursal", "") or "00" + endpoint = f"{TNS_BASE}/v2/rda/RdaPaciente/Insertar?codigosucursal={api_sucursal}" total_enviados = 0 total_errores = 0 diff --git a/app/services/json_generator.py b/app/services/json_generator.py index 8b315bc..30e2f9b 100644 --- a/app/services/json_generator.py +++ b/app/services/json_generator.py @@ -127,7 +127,7 @@ def generar_rda_paciente(rows: list, default_profesional: str = "", default_espe "impConsumo": 0, "observacion": "", "profesional": default_profesional or None, - "especialidad": (lambda e: e if e not in (".", "-", "0", "00") else default_especialidad or None)(str(row.get("COD_ESPECIALIDAD") or "").strip()), + "especialidad": default_especialidad or None, "diagnosticoprincipal": str(h.get("DIAG_PPAL") or "").strip(), "fechaHoraRealizacion": fecha_real or egreso, }) @@ -136,6 +136,7 @@ def generar_rda_paciente(rows: list, default_profesional: str = "", default_espe nit_empresa = str(h.get("NIT_EMPRESA") or "").strip() es_particular = nit_empresa.upper() in ("PART", "PARTICULAR", "", "0") + cod_contrato = str(h.get("CODCONTRATO") or "").strip() or None # Códigos configurados en TNS para este laboratorio (GET /v2/tablas/FormaPago/ObtenerFormasDePago) cod_forma_pago = "CLIP" if es_particular else "INST" @@ -163,7 +164,7 @@ def generar_rda_paciente(rows: list, default_profesional: str = "", default_espe "fechaHoraIngreso": ingreso, "fechaHoraEgreso": egreso, "modalidadAtencion": modalidad, - "numeroContrato": nit_empresa, + "numeroContrato": cod_contrato, "diagnosticoprincipal": str(h.get("DIAG_PPAL") or "").strip(), "tipousuario": tipoususispro, "viaIngreso": via_ingreso, diff --git a/app/templates/config.html b/app/templates/config.html index e68793d..b01327b 100644 --- a/app/templates/config.html +++ b/app/templates/config.html @@ -133,7 +133,7 @@ class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 text-sm">
- +