Fix profesional/especialidad: use MEDICO.CODIGO first, add config defaults
- SQL query now prioritizes MEDICO.CODIGO over RECEPCION.USUARIO as profesional (USUARIO is the billing clerk; MEDICO is the medical professional) - Add profesional_default and especialidad_default config keys for when Firebird data is empty or contains invalid values (e.g. especialidad = ".") - generar_rda_paciente accepts default_profesional and default_especialidad params - ensure_defaults in queries.py now updates existing query records on startup Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
fbdce1c790
commit
c70f0f1062
@@ -59,7 +59,7 @@ SELECT
|
|||||||
rel.PRECIO,
|
rel.PRECIO,
|
||||||
rel.FECHA_REPORTADO,
|
rel.FECHA_REPORTADO,
|
||||||
m.COD_ESPECIALIDAD,
|
m.COD_ESPECIALIDAD,
|
||||||
COALESCE(NULLIF(TRIM(r.USUARIO), ''), TRIM(m.CODIGO), '') AS profesional
|
COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional
|
||||||
FROM RECEPCION r
|
FROM RECEPCION r
|
||||||
JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
|
JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
|
||||||
LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO
|
LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO
|
||||||
@@ -229,10 +229,12 @@ async def run_automation(
|
|||||||
endpoint = f"{TNS_BASE}/v2/rda/RdaPaciente/Insertar"
|
endpoint = f"{TNS_BASE}/v2/rda/RdaPaciente/Insertar"
|
||||||
if api_sucursal:
|
if api_sucursal:
|
||||||
endpoint += f"?codigosucursal={api_sucursal}"
|
endpoint += f"?codigosucursal={api_sucursal}"
|
||||||
|
prof_def = configs.get("profesional_default", "")
|
||||||
|
esp_def = configs.get("especialidad_default", "")
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||||
for id_recepcion, grupo_rows in grupos.items():
|
for id_recepcion, grupo_rows in grupos.items():
|
||||||
rda_json = generar_rda_paciente(grupo_rows)
|
rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def)
|
||||||
factura = str(grupo_rows[0].get("NUM_FACTURA", id_recepcion))
|
factura = str(grupo_rows[0].get("NUM_FACTURA", id_recepcion))
|
||||||
try:
|
try:
|
||||||
resp = await client.post(endpoint, json=rda_json, headers=headers)
|
resp = await client.post(endpoint, json=rda_json, headers=headers)
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ DEFAULT_KEYS = [
|
|||||||
("tns_password", "Nicolas2796*+"),
|
("tns_password", "Nicolas2796*+"),
|
||||||
("num_documento_obligado", ""),
|
("num_documento_obligado", ""),
|
||||||
("cod_prestador", ""),
|
("cod_prestador", ""),
|
||||||
|
("profesional_default", ""),
|
||||||
|
("especialidad_default", ""),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ WHERE p.DOCIDENT = :doc_num""",
|
|||||||
rel.PRECIO,
|
rel.PRECIO,
|
||||||
rel.FECHA_REPORTADO,
|
rel.FECHA_REPORTADO,
|
||||||
m.COD_ESPECIALIDAD,
|
m.COD_ESPECIALIDAD,
|
||||||
COALESCE(NULLIF(TRIM(r.USUARIO), ''), TRIM(m.CODIGO), '') AS profesional
|
COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional
|
||||||
FROM RECEPCION r
|
FROM RECEPCION r
|
||||||
JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
|
JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
|
||||||
LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO
|
LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO
|
||||||
@@ -79,7 +79,7 @@ WHERE r.NUM_FACTURA = :num_factura""",
|
|||||||
rel.PRECIO,
|
rel.PRECIO,
|
||||||
rel.FECHA_REPORTADO,
|
rel.FECHA_REPORTADO,
|
||||||
m.COD_ESPECIALIDAD,
|
m.COD_ESPECIALIDAD,
|
||||||
COALESCE(NULLIF(TRIM(r.USUARIO), ''), TRIM(m.CODIGO), '') AS profesional
|
COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional
|
||||||
FROM RECEPCION r
|
FROM RECEPCION r
|
||||||
JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
|
JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
|
||||||
LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO
|
LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO
|
||||||
@@ -101,6 +101,11 @@ def ensure_defaults():
|
|||||||
"INSERT INTO queries (name, query_type, query_text, description) VALUES (?, ?, ?, ?)",
|
"INSERT INTO queries (name, query_type, query_text, description) VALUES (?, ?, ?, ?)",
|
||||||
(q["name"], q["query_type"], q["query_text"], q["description"]),
|
(q["name"], q["query_type"], q["query_text"], q["description"]),
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE queries SET query_text = ?, description = ? WHERE name = ?",
|
||||||
|
(q["query_text"], q["description"], q["name"]),
|
||||||
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,9 @@ async def preview_transaccion(
|
|||||||
return JSONResponse({"success": False, "message": error})
|
return JSONResponse({"success": False, "message": error})
|
||||||
|
|
||||||
grupos = agrupar_por_recepcion(rows)
|
grupos = agrupar_por_recepcion(rows)
|
||||||
json_result = [generar_rda_paciente(grupo) for grupo in grupos.values()]
|
prof_def = configs.get("profesional_default", "")
|
||||||
|
esp_def = configs.get("especialidad_default", "")
|
||||||
|
json_result = [generar_rda_paciente(grupo, prof_def, esp_def) for grupo in grupos.values()]
|
||||||
|
|
||||||
return JSONResponse({
|
return JSONResponse({
|
||||||
"success": True,
|
"success": True,
|
||||||
@@ -153,10 +155,12 @@ async def send_transaccion(
|
|||||||
total_enviados = 0
|
total_enviados = 0
|
||||||
total_errores = 0
|
total_errores = 0
|
||||||
resultados = []
|
resultados = []
|
||||||
|
prof_def = configs.get("profesional_default", "")
|
||||||
|
esp_def = configs.get("especialidad_default", "")
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=int(configs.get("api_timeout", 30))) as client:
|
async with httpx.AsyncClient(timeout=int(configs.get("api_timeout", 30))) as client:
|
||||||
for id_recepcion, grupo_rows in grupos.items():
|
for id_recepcion, grupo_rows in grupos.items():
|
||||||
trans_json = generar_rda_paciente(grupo_rows)
|
trans_json = generar_rda_paciente(grupo_rows, prof_def, esp_def)
|
||||||
|
|
||||||
status_ok = False
|
status_ok = False
|
||||||
response_text = ""
|
response_text = ""
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ def agrupar_por_recepcion(rows: list) -> dict:
|
|||||||
return grupos
|
return grupos
|
||||||
|
|
||||||
|
|
||||||
def generar_rda_paciente(rows: list) -> dict:
|
def generar_rda_paciente(rows: list, default_profesional: str = "", default_especialidad: str = "") -> dict:
|
||||||
if not rows:
|
if not rows:
|
||||||
return {}
|
return {}
|
||||||
h = rows[0]
|
h = rows[0]
|
||||||
@@ -126,8 +126,8 @@ def generar_rda_paciente(rows: list) -> dict:
|
|||||||
"porcentajeIva": 0,
|
"porcentajeIva": 0,
|
||||||
"impConsumo": 0,
|
"impConsumo": 0,
|
||||||
"observacion": "",
|
"observacion": "",
|
||||||
"profesional": str(h.get("profesional") or "").strip(),
|
"profesional": str(row.get("profesional") or "").strip() or default_profesional,
|
||||||
"especialidad": (lambda e: e if e not in (".", "-", "0", "00") else "")(str(row.get("COD_ESPECIALIDAD") or "").strip()),
|
"especialidad": (lambda e: e if e not in (".", "-", "0", "00") else default_especialidad)(str(row.get("COD_ESPECIALIDAD") or "").strip()),
|
||||||
"diagnosticoprincipal": str(h.get("DIAG_PPAL") or "").strip(),
|
"diagnosticoprincipal": str(h.get("DIAG_PPAL") or "").strip(),
|
||||||
"fechaHoraRealizacion": fecha_real or egreso,
|
"fechaHoraRealizacion": fecha_real or egreso,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user