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 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-26 18:01:32 -05:00
co-authored by Claude Sonnet 4.6
parent f0514641b3
commit 3017c7956e
6 changed files with 17 additions and 14 deletions
+4 -4
View File
@@ -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", "")
+1 -1
View File
@@ -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*+"),
+6 -2
View File
@@ -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)"
+2 -4
View File
@@ -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
+3 -2
View File
@@ -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,
+1 -1
View File
@@ -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">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Especialidad Default <span class="text-gray-400 font-normal">(código Firebird, ej: BAC)</span></label>
<label class="block text-sm font-medium text-gray-700 mb-1">Especialidad Default <span class="text-gray-400 font-normal">(código TNS, ej: 02)</span></label>
<input type="text" name="config_especialidad_default"
value="{{ configs|selectattr('key', 'equalto', 'especialidad_default')|map(attribute='value')|first|default('') }}"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 text-sm">