# ============================================================================
def generate_email_html(
data: dict,
cardcode: str,
cardname: str | None,
item_catalog: dict[str, str],
prices: dict[str, float],
) -> str:
cab = data.get("cabecera", {})
det = data.get("detalle", [])
num_oc = cab.get("numero_oc", "") or ""
fecha_entrega = (cab.get("fecha_embarque") or cab.get("fecha_generacion") or "")[:10]
cliente = f"{cardcode} — {cardname or ''}"
rows_html = ""
for d in det:
upc = d.get("cod_upc", "") or ""
desc = d.get("descripcion", "") or ""
empaques = d.get("empaques", 0) or 0
color_talla = d.get("color_talla_um", "") or ""
unid_emp = d.get("unid_empaque", 0) or 0
cantidad_unid = d.get("cantidad_unid", 0) or 0
importe = d.get("importe", 0) or 0
precio_unidad = round(importe / cantidad_unid) if cantidad_unid > 0 else 0
sap_item = item_catalog.get(upc, "")
sap_price = prices.get(sap_item) if sap_item else None
if not sap_item:
texto = "Sin catálogo"
elif sap_price is None:
texto = "Sin precio SAP"
elif sap_price != precio_unidad:
texto = f"Precio difiere: ${precio_unidad:,} vs ${sap_price:,.0f}"
else:
texto = "OK"
rows_html += (
"
"
f"| {upc} | "
f"{empaques:,} | "
f"{color_talla} | "
f"{unid_emp:,} | "
f"{cantidad_unid:,} | "
f"{importe:,} | "
f"{precio_unidad:,} | "
f"{sap_item or '—'} | "
f"{f'${sap_price:,.0f}' if sap_price is not None else '—'} | "
f"{texto} | "
f"{desc} | "
"
\n"
)
html = f"""
Orden de Compra — {num_oc}
| N° OC: | {num_oc} |
| Fecha Entrega: | {fecha_entrega} |
| Cliente: | {cliente} |
| cod_upc |
empaques |
color_talla_um |
unid_empaque |
cantidad_unid |
Importe |
precio_unidad |
ItemCode |
Precio SAP |
Texto |
descripción |
{rows_html}
"""
return html
# ============================================================================