Agrega filtro convenio en automation/transaccion/test-rda y fix particular (contrato 12)
- automation: filtro contrato en SQL pacientes+RDA, numero LHXC en run() - transaccion: filtro contrato en preview/send-one/send - test-rda: filtro tipo_usuario (11/07/12), default contrato "12", acepta "12"/"012" - json_generator: detecta particular por CODCONTRATO "12"/"012" además de NIT_EMPRESA PART; fuerza tipousuario="12", codFormaPago="CLIP", plazoDias="0" Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
d7afbe9b7d
commit
69372a5673
@@ -16,7 +16,7 @@ from app.services.api_client import get_tns_token, TNS_BASE
|
||||
|
||||
router = APIRouter(prefix="/automation", tags=["automation"])
|
||||
|
||||
# Query para obtener pacientes únicos por rango de fecha
|
||||
# Query para obtener pacientes únicos por rango de fecha + contrato
|
||||
_SQL_PACIENTES = """
|
||||
SELECT DISTINCT
|
||||
p.CODIGO,
|
||||
@@ -35,11 +35,14 @@ SELECT DISTINCT
|
||||
p.CODETNIA
|
||||
FROM PACIENTE p
|
||||
JOIN RECEPCION r ON r.COD_PACIENTE = p.CODIGO
|
||||
LEFT JOIN EMPRESA e ON e.NIT = r.NIT_EMPRESA
|
||||
LEFT JOIN CIUDAD c ON c.CODIGO = p.CIUDAD
|
||||
WHERE r.FECHA_RECEPCION BETWEEN :fecha_ini AND :fecha_fin
|
||||
AND r.NUM_FACTURA > 0
|
||||
{filtro_contrato_pac}
|
||||
"""
|
||||
|
||||
# Query para obtener recepciones con exámenes por rango de fecha
|
||||
# Query para obtener recepciones con exámenes por rango de fecha + contrato
|
||||
_SQL_RDA = """
|
||||
SELECT
|
||||
r.IDRECEPCION,
|
||||
@@ -68,6 +71,7 @@ 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
|
||||
AND r.NUM_FACTURA > 0
|
||||
{filtro_contrato_rda}
|
||||
ORDER BY r.IDRECEPCION
|
||||
"""
|
||||
|
||||
@@ -80,11 +84,25 @@ async def automation_page(request: Request, user: dict = Depends(get_current_use
|
||||
})
|
||||
|
||||
|
||||
def _build_sql(contrato: str):
|
||||
if contrato:
|
||||
fp = "AND e.CODCONTRATO = :contrato"
|
||||
fr = "AND e.CODCONTRATO = :contrato"
|
||||
else:
|
||||
fp = ""
|
||||
fr = ""
|
||||
return (
|
||||
_SQL_PACIENTES.format(filtro_contrato_pac=fp),
|
||||
_SQL_RDA.format(filtro_contrato_rda=fr),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/preview")
|
||||
async def preview_automation(
|
||||
request: Request,
|
||||
user: dict = Depends(get_current_user),
|
||||
fecha: str = Form(...),
|
||||
contrato: str = Form(""),
|
||||
):
|
||||
conn = get_connection()
|
||||
configs = {row["key"]: row["value"] for row in conn.execute("SELECT * FROM config").fetchall()}
|
||||
@@ -97,13 +115,17 @@ async def preview_automation(
|
||||
fecha_ini = f"{fecha} 00:00:00"
|
||||
fecha_fin = f"{fecha} 23:59:59"
|
||||
params = {"fecha_ini": fecha_ini, "fecha_fin": fecha_fin}
|
||||
if contrato:
|
||||
params["contrato"] = contrato.strip()
|
||||
|
||||
ok1, err1, rows_pac = fb.execute_query(_SQL_PACIENTES, params)
|
||||
sql_pac, sql_rda = _build_sql(contrato.strip())
|
||||
|
||||
ok1, err1, rows_pac = fb.execute_query(sql_pac, params)
|
||||
if not ok1:
|
||||
fb.disconnect()
|
||||
return JSONResponse({"success": False, "message": f"Error BD pacientes: {err1}"})
|
||||
|
||||
ok2, err2, rows_rda = fb.execute_query(_SQL_RDA, params)
|
||||
ok2, err2, rows_rda = fb.execute_query(sql_rda, params)
|
||||
fb.disconnect()
|
||||
if not ok2:
|
||||
return JSONResponse({"success": False, "message": f"Error BD RDA: {err2}"})
|
||||
@@ -152,6 +174,7 @@ async def run_automation(
|
||||
request: Request,
|
||||
user: dict = Depends(get_current_user),
|
||||
fecha: str = Form(...),
|
||||
contrato: str = Form(""),
|
||||
):
|
||||
conn = get_connection()
|
||||
configs = {row["key"]: row["value"] for row in conn.execute("SELECT * FROM config").fetchall()}
|
||||
@@ -161,19 +184,22 @@ async def run_automation(
|
||||
if not fb_ok:
|
||||
return JSONResponse({"success": False, "message": f"Error Firebird: {fb_msg}"})
|
||||
|
||||
# Rango: día completo de la fecha seleccionada
|
||||
fecha_ini = f"{fecha} 00:00:00"
|
||||
fecha_fin = f"{fecha} 23:59:59"
|
||||
params = {"fecha_ini": fecha_ini, "fecha_fin": fecha_fin}
|
||||
if contrato:
|
||||
params["contrato"] = contrato.strip()
|
||||
|
||||
sql_pac, sql_rda = _build_sql(contrato.strip())
|
||||
|
||||
# ── Paso 1: obtener pacientes únicos ─────────────────────────────────────
|
||||
ok1, err1, rows_pac = fb.execute_query(_SQL_PACIENTES, params)
|
||||
ok1, err1, rows_pac = fb.execute_query(sql_pac, params)
|
||||
if not ok1:
|
||||
fb.disconnect()
|
||||
return JSONResponse({"success": False, "message": f"Error Firebird pacientes: {err1}"})
|
||||
|
||||
# ── Paso 2: obtener recepciones ───────────────────────────────────────────
|
||||
ok2, err2, rows_rda = fb.execute_query(_SQL_RDA, params)
|
||||
ok2, err2, rows_rda = fb.execute_query(sql_rda, params)
|
||||
fb.disconnect()
|
||||
if not ok2:
|
||||
return JSONResponse({"success": False, "message": f"Error Firebird RDA: {err2}"})
|
||||
@@ -238,8 +264,11 @@ async def run_automation(
|
||||
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
for id_recepcion, grupo_rows in grupos.items():
|
||||
rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def, remis_def, prefijo_def)
|
||||
factura = str(grupo_rows[0].get("NUM_FACTURA", id_recepcion))
|
||||
num_fac = str(grupo_rows[0].get("NUM_FACTURA") or "").strip()
|
||||
prefijo_fb = str(grupo_rows[0].get("PREFIJO") or "").strip()
|
||||
num_override = f"{prefijo_fb}{num_fac.zfill(5)}" if prefijo_fb and num_fac else num_fac
|
||||
rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def, remis_def, prefijo_def, num_override)
|
||||
factura = num_override or str(id_recepcion)
|
||||
try:
|
||||
resp = await client.post(endpoint, json=rda_json, headers=headers)
|
||||
ok = resp.is_success
|
||||
|
||||
Reference in New Issue
Block a user