fix: reenviar-venta now fetches all receptions of the factura
Previously used IDRECEPCION to fetch only one reception's services. Now resolves PREFIJO+NUM_FACTURA from the reception, then fetches all receptions sharing that factura so the full detallePedido is sent. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
090890fa46
commit
33bd25c6c8
+24
-12
@@ -231,7 +231,7 @@ WHERE ps.ID_PS = :id_ps
|
||||
ORDER BY rel.COD_EXAMEN
|
||||
"""
|
||||
|
||||
_SQL_VENTAS_BY_ID = """
|
||||
_SQL_VENTAS_BY_FACTURA = """
|
||||
SELECT
|
||||
r.IDRECEPCION, r.PREFIJO, r.NUM_FACTURA, r.FECHA_RECEPCION,
|
||||
r.COD_PACIENTE, r.NIT_EMPRESA, r.VALORTOTAL, r.VALORDESC,
|
||||
@@ -244,6 +244,13 @@ JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
|
||||
LEFT JOIN EXAMEN ex ON TRIM(ex.CODIGO) = TRIM(rel.COD_EXAMEN)
|
||||
LEFT JOIN EMPRESA e ON e.NIT = r.NIT_EMPRESA
|
||||
LEFT JOIN TARIFA t ON TRIM(t.COD_EXAMEN) = TRIM(rel.COD_EXAMEN) AND t.TARIFA = e.TARIFA
|
||||
WHERE r.PREFIJO = :prefijo AND r.NUM_FACTURA = :num_factura
|
||||
ORDER BY r.IDRECEPCION
|
||||
"""
|
||||
|
||||
_SQL_VENTAS_BY_IDRECEP = """
|
||||
SELECT r.PREFIJO, r.NUM_FACTURA
|
||||
FROM RECEPCION r
|
||||
WHERE r.IDRECEPCION = :idrecepcion
|
||||
"""
|
||||
|
||||
@@ -1002,7 +1009,20 @@ async def reenviar_venta(
|
||||
if not fb_ok:
|
||||
return JSONResponse({"success": False, "message": f"Error Firebird: {fb_msg}"})
|
||||
|
||||
ok, err, rows = fb.execute_query(_SQL_VENTAS_BY_ID, {"idrecepcion": idrecepcion})
|
||||
# Primero obtener PREFIJO y NUM_FACTURA de esa recepción
|
||||
ok0, err0, ref = fb.execute_query(_SQL_VENTAS_BY_IDRECEP, {"idrecepcion": idrecepcion})
|
||||
if not ok0 or not ref:
|
||||
fb.disconnect()
|
||||
return JSONResponse({"success": False, "message": err0 or "Recepción no encontrada"})
|
||||
prefijo = str(ref[0].get("PREFIJO") or "").strip()
|
||||
num_factura_int = int(ref[0].get("NUM_FACTURA") or 0)
|
||||
if num_factura_int == 0:
|
||||
fb.disconnect()
|
||||
return JSONResponse({"success": False, "message": "Factura sin número asignado (00000)"})
|
||||
|
||||
# Traer TODAS las recepciones de esa factura para incluir todos los servicios
|
||||
ok, err, rows = fb.execute_query(_SQL_VENTAS_BY_FACTURA,
|
||||
{"prefijo": prefijo, "num_factura": num_factura_int})
|
||||
fb.disconnect()
|
||||
|
||||
if not ok or not rows:
|
||||
@@ -1012,18 +1032,10 @@ async def reenviar_venta(
|
||||
contrato_vta = str(rows[0].get("CODCONTRATO") or "").strip()
|
||||
if contrato_vta in excluded_ventas:
|
||||
return JSONResponse({"success": False, "message": f"Contrato {contrato_vta} excluido"})
|
||||
if int(rows[0].get("NUM_FACTURA") or 0) == 0:
|
||||
return JSONResponse({"success": False, "message": "Factura sin número asignado (00000)"})
|
||||
|
||||
from collections import defaultdict
|
||||
grupos: dict = defaultdict(list)
|
||||
for row in rows:
|
||||
grupos[row.get("IDRECEPCION")].append(dict(row))
|
||||
grupo_rows = list(grupos.values())[0]
|
||||
|
||||
grupo_rows = [dict(r) for r in rows]
|
||||
prefijo_def = configs.get("prefijo_tns_default", "00")
|
||||
num_fac = str(grupo_rows[0].get("NUM_FACTURA") or "").strip()
|
||||
prefijo = str(grupo_rows[0].get("PREFIJO") or "").strip()
|
||||
num_fac = str(num_factura_int)
|
||||
venta_json = generar_factura_venta(grupo_rows, default_vendedor="00",
|
||||
default_prefijo=prefijo_def, numero_override=num_fac)
|
||||
factura_display = f"{prefijo}-{num_fac}"
|
||||
|
||||
Reference in New Issue
Block a user