fix: defensive grouping filter in preview and run ventas endpoints

Replace dict comprehension with explicit loop so that non-list or empty
grupo values are safely skipped, preventing KeyError: 0 on v[0].

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-28 15:05:09 -05:00
co-authored by Claude Sonnet 4.6
parent 3a0964e4ec
commit c58ecc1757
+18 -6
View File
@@ -455,9 +455,15 @@ async def preview_automation(
]
grupos_vta_all = agrupar_por_factura(rows_vta)
grupos_vta = {k: v for k, v in grupos_vta_all.items()
if str(v[0].get("CODCONTRATO") or "").strip() not in excluded_ventas
and int(v[0].get("NUM_FACTURA") or 0) != 0}
grupos_vta = {}
for k, v in grupos_vta_all.items():
if not isinstance(v, list) or not v:
continue
if int(v[0].get("NUM_FACTURA") or 0) == 0:
continue
if str(v[0].get("CODCONTRATO") or "").strip() in excluded_ventas:
continue
grupos_vta[k] = v
ventas_preview = []
for num_fac_key, grupo_rows in grupos_vta.items():
@@ -734,9 +740,15 @@ async def run_automation(
# ── PASO 4: Enviar Facturas Venta ─────────────────────────────────────────
excluded_ventas = load_excluded_ventas_set()
grupos_vta_all = agrupar_por_factura(rows_vta)
grupos_vta = {k: v for k, v in grupos_vta_all.items()
if str(v[0].get("CODCONTRATO") or "").strip() not in excluded_ventas
and int(v[0].get("NUM_FACTURA") or 0) != 0}
grupos_vta = {}
for k, v in grupos_vta_all.items():
if not isinstance(v, list) or not v:
continue
if int(v[0].get("NUM_FACTURA") or 0) == 0:
continue
if str(v[0].get("CODCONTRATO") or "").strip() in excluded_ventas:
continue
grupos_vta[k] = v
resultado["paso4_ventas"]["excluidos"] = len(grupos_vta_all) - len(grupos_vta)
endpoint_venta = f"{TNS_BASE}/v2/facturacion/Ventas/Crear?codigosucursal={api_sucursal or '00'}"