From 7d663c1a95d49b7d7b433c10152eabdd479fa996 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 7 Dec 2023 11:12:14 +0530 Subject: [PATCH] fix(shopify): handle multiple instance of same item in delivery Earlier the code assumed item can only be used once in order. This change now uses fifo type assignment of shopify line items to delivery note line items to only map item code once and then pop it. --- ecommerce_integrations/shopify/fulfillment.py | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/ecommerce_integrations/shopify/fulfillment.py b/ecommerce_integrations/shopify/fulfillment.py index e8cbb453..97a6706e 100644 --- a/ecommerce_integrations/shopify/fulfillment.py +++ b/ecommerce_integrations/shopify/fulfillment.py @@ -1,3 +1,5 @@ +from copy import deepcopy + import frappe from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note from frappe.utils import cint, cstr, getdate @@ -62,13 +64,26 @@ def get_fulfillment_items(dn_items, fulfillment_items, location_id=None): # local import to avoid circular imports from ecommerce_integrations.shopify.product import get_item_code + fulfillment_items = deepcopy(fulfillment_items) + setting = frappe.get_cached_doc(SETTING_DOCTYPE) wh_map = setting.get_integration_to_erpnext_wh_mapping() warehouse = wh_map.get(str(location_id)) or setting.warehouse - return [ - dn_item.update({"qty": item.get("quantity"), "warehouse": warehouse}) - for item in fulfillment_items - for dn_item in dn_items - if get_item_code(item) == dn_item.item_code - ] + final_items = [] + + def find_matching_fullfilement_item(dn_item): + nonlocal fulfillment_items + + for item in fulfillment_items: + if get_item_code(item) == dn_item.item_code: + fulfillment_items.remove(item) + return item + + for dn_item in dn_items: + if shopify_item := find_matching_fullfilement_item(dn_item): + final_items.append( + dn_item.update({"qty": shopify_item.get("quantity"), "warehouse": warehouse}) + ) + + return final_items