feat: queries reales Firebird y generadores JSON para API ERP
- json_generator: nuevas funciones generar_tercero_api y generar_rda_paciente mapeando PACIENTE/RECEPCION/RELACION al formato de los endpoints Tercero/Crear y RdaPaciente/Insertar - queries: reemplaza placeholders por consultas reales sobre PACIENTE, RECEPCION, RELACION, MEDICO y CIUDAD - config: agrega api_version y api_sucursal; actualiza url y credenciales Firebird - terceros/transaccion routes: usan nuevos generadores y endpoints correctos Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
26243aa2bc
commit
774ce7d728
+25
-26
@@ -6,7 +6,7 @@ from fastapi.responses import JSONResponse
|
||||
from app.database import get_connection
|
||||
from app.auth import get_current_user
|
||||
from app.services.firebird_service import get_firebird_from_config
|
||||
from app.services.json_generator import generar_transaccion, agrupar_por_factura
|
||||
from app.services.json_generator import generar_rda_paciente, agrupar_por_recepcion
|
||||
|
||||
router = APIRouter(prefix="/transaccion", tags=["transaccion"])
|
||||
|
||||
@@ -53,8 +53,11 @@ async def preview_transaccion(
|
||||
return JSONResponse({"success": False, "message": f"Error Firebird: {msg}"})
|
||||
|
||||
params = {"fecha_ini": fecha_inicio, "fecha_fin": fecha_fin}
|
||||
if ":factura" in q["query_text"] and factura:
|
||||
params["factura"] = factura
|
||||
if ":num_factura" in q["query_text"] and factura:
|
||||
params["num_factura"] = factura
|
||||
prefijo = configs.get("api_prefijo", "")
|
||||
if prefijo:
|
||||
params["prefijo"] = prefijo
|
||||
|
||||
success, error, rows = fb.execute_query(q["query_text"], params)
|
||||
fb.disconnect()
|
||||
@@ -62,14 +65,8 @@ async def preview_transaccion(
|
||||
if not success:
|
||||
return JSONResponse({"success": False, "message": error})
|
||||
|
||||
grupos = agrupar_por_factura(rows, factura)
|
||||
json_result = []
|
||||
for (fact, doc_key), grupo in grupos.items():
|
||||
paciente_data = {"tipoDocumentoIdentificacion": doc_key[0], "numDocumentoIdentificacion": doc_key[1]}
|
||||
json_result.append(generar_transaccion(
|
||||
fact, configs.get("num_documento_obligado", ""),
|
||||
paciente_data, grupo["procedimientos"],
|
||||
))
|
||||
grupos = agrupar_por_recepcion(rows)
|
||||
json_result = [generar_rda_paciente(grupo) for grupo in grupos.values()]
|
||||
|
||||
return JSONResponse({
|
||||
"success": True,
|
||||
@@ -104,8 +101,11 @@ async def send_transaccion(
|
||||
return JSONResponse({"success": False, "message": f"Error Firebird: {msg}"})
|
||||
|
||||
params = {"fecha_ini": fecha_inicio, "fecha_fin": fecha_fin}
|
||||
if ":factura" in q["query_text"] and factura:
|
||||
params["factura"] = factura
|
||||
if ":num_factura" in q["query_text"] and factura:
|
||||
params["num_factura"] = factura
|
||||
prefijo = configs.get("api_prefijo", "")
|
||||
if prefijo:
|
||||
params["prefijo"] = prefijo
|
||||
|
||||
success, error, rows = fb.execute_query(q["query_text"], params)
|
||||
fb.disconnect()
|
||||
@@ -115,33 +115,31 @@ async def send_transaccion(
|
||||
if not rows:
|
||||
return JSONResponse({"success": False, "message": "No se encontraron datos"})
|
||||
|
||||
grupos = agrupar_por_factura(rows, factura)
|
||||
grupos = agrupar_por_recepcion(rows)
|
||||
api_url = configs.get("api_url", "")
|
||||
api_version = configs.get("api_version", "1")
|
||||
api_sucursal = configs.get("api_sucursal", "")
|
||||
api_key = configs.get("api_key", "")
|
||||
api_method = configs.get("api_method", "POST")
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if api_key:
|
||||
headers["Authorization"] = f"Bearer {api_key}"
|
||||
|
||||
endpoint = f"{api_url}/v{api_version}/rda/RdaPaciente/Insertar"
|
||||
if api_sucursal:
|
||||
endpoint += f"?codigosucursal={api_sucursal}"
|
||||
|
||||
total_enviados = 0
|
||||
total_errores = 0
|
||||
resultados = []
|
||||
|
||||
async with httpx.AsyncClient(timeout=int(configs.get("api_timeout", 30))) as client:
|
||||
for (fact, doc_key), grupo in grupos.items():
|
||||
paciente_data = {"tipoDocumentoIdentificacion": doc_key[0], "numDocumentoIdentificacion": doc_key[1]}
|
||||
trans_json = generar_transaccion(
|
||||
fact, configs.get("num_documento_obligado", ""),
|
||||
paciente_data, grupo["procedimientos"],
|
||||
)
|
||||
for id_recepcion, grupo_rows in grupos.items():
|
||||
trans_json = generar_rda_paciente(grupo_rows)
|
||||
|
||||
status_ok = False
|
||||
response_text = ""
|
||||
try:
|
||||
if api_method == "POST":
|
||||
resp = await client.post(api_url + "/transaccion", json=trans_json, headers=headers)
|
||||
else:
|
||||
resp = await client.put(api_url + "/transaccion", json=trans_json, headers=headers)
|
||||
resp = await client.post(endpoint, json=trans_json, headers=headers)
|
||||
status_ok = resp.is_success
|
||||
response_text = resp.text[:1000]
|
||||
except Exception as e:
|
||||
@@ -152,6 +150,7 @@ async def send_transaccion(
|
||||
else:
|
||||
total_errores += 1
|
||||
|
||||
fact = str(grupo_rows[0].get("NUM_FACTURA", id_recepcion))
|
||||
resultados.append({"factura": fact, "success": status_ok})
|
||||
|
||||
conn = get_connection()
|
||||
@@ -162,7 +161,7 @@ async def send_transaccion(
|
||||
""", (
|
||||
user["user_id"], "transaccion", fact,
|
||||
fecha_inicio, fecha_fin,
|
||||
1, len(grupo["procedimientos"]),
|
||||
1, len(grupo_rows),
|
||||
"success" if status_ok else "error",
|
||||
json_lib.dumps(trans_json, indent=2, ensure_ascii=False)[:5000],
|
||||
response_text,
|
||||
|
||||
Reference in New Issue
Block a user