Files

77 lines
2.7 KiB
Python
Raw Permalink Normal View History

# ============================================================================
def to_draft_payload(
data: dict,
cardcode: str | None = None,
city: str | None = None,
county: str | None = None,
item_catalog: dict[str, str] | None = None,
prices: dict[str, float] | None = None,
) -> dict:
cab = data.get("cabecera", {})
det = data.get("detalle", [])
doc_date = (cab.get("fecha_generacion") or "")[:10]
due_date = (cab.get("fecha_embarque") or doc_date or "")[:10]
resolved_cardcode = cardcode or cab.get("emisor", "")
catalog = item_catalog or {}
price_map = prices or {}
comments = cab.get("condiciones_pago_texto") or cab.get("condiciones_pago") or ""
obs = cab.get("observaciones", "")
if obs:
comments = (comments + " | " + obs) if comments else obs
lines = []
for d in det:
codigo_upc = d.get("cod_upc", "")
sap_item = catalog.get(codigo_upc, "")
precio_empaque = d.get("precio_efectivo",0.00)
sap_price = price_map.get(sap_item,0.00)
#print(f" SAP Item={sap_item}")
#print(f" Tamaño {len(sap_item)}")
if sap_item is not None and len(sap_item)>0:
lines.append({
"ItemCode": sap_item,
# "ItemDescription": d.get("descripcion", "") or "",
"Quantity": d.get("cantidad_unid",0.00),
##"Quantity": d.get("empaques", 1),
"MeasureUnit": "UND", #d.get("color_talla_um",""),
##"UnitPrice": precio_empaque,
##"UnitsOfMeasurment":d.get("unid_empaque", 0.00),
"PackQuantity":d.get("empaques", 1),
"TaxCode": "IVA",
"LineTotal": d.get("importe",0.00),
"FreeText": codigo_upc,
"U_B2B_CANAL": "B2B",
})
shipto_payto = cab.get("emisor","") + "." + cab.get("lugar_entrega","")
address_extension = {
"BillToCity":city,
"BillToCounty":county
}
payload = {
"CardCode": resolved_cardcode,
"DocObjectCode":"oOrders",
"DocDate": doc_date,
"DocDueDate": due_date,
"TaxDate": doc_date,
"NumAtCard": cab.get("numero_oc", "") or "",
"ShipToCode": shipto_payto,
"PayToCode": shipto_payto,
"Comments": comments or "",
"U_VENT_B2B": cab.get("emisor", "") or "",
"DocType": "I",
"DocumentLines": lines,
"AddressExtension":address_extension
}
return payload
# ============================================================================