From 06b9c484b26a9ccca3c33a9aabe066aa2300ef5b Mon Sep 17 00:00:00 2001 From: ljain112 Date: Mon, 6 Jul 2026 17:37:09 +0530 Subject: [PATCH] refactor: move functionality in postprocess for mapped doc (cherry picked from commit 0691c7c7bc6c23bb05ed764d01192ff733da72fb) # Conflicts: # erpnext/subcontracting/doctype/subcontracting_inward_order/subcontracting_inward_order.py --- .../subcontracting_inward_order.py | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/erpnext/subcontracting/doctype/subcontracting_inward_order/subcontracting_inward_order.py b/erpnext/subcontracting/doctype/subcontracting_inward_order/subcontracting_inward_order.py index 9687a070bda..1b225db401c 100644 --- a/erpnext/subcontracting/doctype/subcontracting_inward_order/subcontracting_inward_order.py +++ b/erpnext/subcontracting/doctype/subcontracting_inward_order/subcontracting_inward_order.py @@ -345,6 +345,26 @@ class SubcontractingInwardOrder(SubcontractingController): if target_doc and target_doc.get("items"): target_doc.items = [] + def postprocess(source, target): + target.purpose = "Receive from Customer" + target.subcontracting_inward_order = source.name + target.set_stock_entry_type() + + for rm_item in source.received_items: + if not rm_item.required_qty or not rm_item.is_customer_provided_item: + continue + + target.append( + "items", + { + "scio_detail": rm_item.get("name"), + "item_code": rm_item.get("rm_item_code"), + "qty": calculate_qty_as_per_bom(rm_item), + "t_warehouse": rm_item.get("warehouse"), + "stock_uom": rm_item.get("stock_uom"), + }, + ) + stock_entry = get_mapped_doc( "Subcontracting Inward Order", self.name, @@ -357,9 +377,11 @@ class SubcontractingInwardOrder(SubcontractingController): }, }, target_doc, + postprocess=postprocess, ignore_child_tables=True, ) +<<<<<<< HEAD stock_entry.purpose = "Receive from Customer" stock_entry.subcontracting_inward_order = self.name @@ -380,6 +402,8 @@ class SubcontractingInwardOrder(SubcontractingController): stock_entry.add_to_stock_entry_detail(items_dict) +======= +>>>>>>> 0691c7c7bc (refactor: move functionality in postprocess for mapped doc) if target_doc: return stock_entry else: @@ -390,6 +414,27 @@ class SubcontractingInwardOrder(SubcontractingController): if target_doc and target_doc.get("items"): target_doc.items = [] + def postprocess(source, target): + target.purpose = "Return Raw Material to Customer" + target.subcontracting_inward_order = source.name + target.set_stock_entry_type() + + for rm_item in source.received_items: + qty = rm_item.received_qty - rm_item.work_order_qty - rm_item.returned_qty + if not qty: + continue + + target.append( + "items", + { + "scio_detail": rm_item.get("name"), + "item_code": rm_item.get("rm_item_code"), + "qty": qty, + "s_warehouse": rm_item.get("warehouse"), + "stock_uom": rm_item.get("stock_uom"), + }, + ) + stock_entry = get_mapped_doc( "Subcontracting Inward Order", self.name, @@ -402,9 +447,11 @@ class SubcontractingInwardOrder(SubcontractingController): }, }, target_doc, + postprocess=postprocess, ignore_child_tables=True, ) +<<<<<<< HEAD stock_entry.purpose = "Return Raw Material to Customer" stock_entry.set_stock_entry_type() stock_entry.subcontracting_inward_order = self.name @@ -421,6 +468,8 @@ class SubcontractingInwardOrder(SubcontractingController): stock_entry.add_to_stock_entry_detail(items_dict) +======= +>>>>>>> 0691c7c7bc (refactor: move functionality in postprocess for mapped doc) if target_doc: return stock_entry else: @@ -431,6 +480,58 @@ class SubcontractingInwardOrder(SubcontractingController): if target_doc and target_doc.get("items"): target_doc.items = [] + def postprocess(source, target): + target.purpose = "Subcontracting Delivery" + target.subcontracting_inward_order = source.name + target.set_stock_entry_type() + + scio_details = [] + allow_over = frappe.get_single_value("Selling Settings", "allow_delivery_of_overproduced_qty") + for fg_item in source.items: + qty = ( + fg_item.produced_qty + if allow_over + else min(fg_item.qty, fg_item.produced_qty) - fg_item.delivered_qty + ) + if qty < 0: + continue + + scio_details.append(fg_item.name) + target.append( + "items", + { + "qty": qty, + "item_code": fg_item.item_code, + "s_warehouse": fg_item.delivery_warehouse, + "stock_uom": fg_item.stock_uom, + "scio_detail": fg_item.name, + "is_finished_item": 1, + }, + ) + + if ( + frappe.get_single_value("Selling Settings", "deliver_secondary_items") + and source.secondary_items + and scio_details + ): + for secondary_item in source.secondary_items: + if secondary_item.reference_name not in scio_details: + continue + + qty = secondary_item.produced_qty - secondary_item.delivered_qty + if qty > 0: + target.append( + "items", + { + "qty": qty, + "item_code": secondary_item.item_code, + "s_warehouse": secondary_item.warehouse, + "stock_uom": secondary_item.stock_uom, + "scio_detail": secondary_item.name, + "secondary_item_type": secondary_item.secondary_item_type, + }, + ) + stock_entry = get_mapped_doc( "Subcontracting Inward Order", self.name, @@ -443,9 +544,11 @@ class SubcontractingInwardOrder(SubcontractingController): }, }, target_doc, + postprocess=postprocess, ignore_child_tables=True, ) +<<<<<<< HEAD stock_entry.purpose = "Subcontracting Delivery" stock_entry.set_stock_entry_type() stock_entry.subcontracting_inward_order = self.name @@ -499,6 +602,8 @@ class SubcontractingInwardOrder(SubcontractingController): stock_entry.add_to_stock_entry_detail(items_dict) +======= +>>>>>>> 0691c7c7bc (refactor: move functionality in postprocess for mapped doc) if target_doc: return stock_entry else: @@ -509,6 +614,26 @@ class SubcontractingInwardOrder(SubcontractingController): if target_doc and target_doc.get("items"): target_doc.items = [] + def postprocess(source, target): + target.purpose = "Subcontracting Return" + target.set_stock_entry_type() + + for fg_item in source.items: + qty = fg_item.delivered_qty - fg_item.returned_qty + if qty < 0: + continue + + target.append( + "items", + { + "qty": qty, + "item_code": fg_item.item_code, + "stock_uom": fg_item.stock_uom, + "scio_detail": fg_item.name, + "is_finished_item": 1, + }, + ) + stock_entry = get_mapped_doc( "Subcontracting Inward Order", self.name, @@ -522,9 +647,11 @@ class SubcontractingInwardOrder(SubcontractingController): }, }, target_doc, + postprocess=postprocess, ignore_child_tables=True, ) +<<<<<<< HEAD stock_entry.purpose = "Subcontracting Return" stock_entry.set_stock_entry_type() @@ -544,6 +671,8 @@ class SubcontractingInwardOrder(SubcontractingController): stock_entry.add_to_stock_entry_detail(items_dict) +======= +>>>>>>> 0691c7c7bc (refactor: move functionality in postprocess for mapped doc) if target_doc: return stock_entry else: