From f01856d7a999704c2f7bef4d15af5bdb8d673a7f Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Mon, 14 Sep 2020 20:51:20 +0530 Subject: [PATCH] fix: production plan incorrect work order qty (#23264) * fix: production plan incorrect work order qty * added test case * Update test_production_plan.py --- .../production_plan/production_plan.py | 11 ++--- .../production_plan/test_production_plan.py | 44 ++++++++++++++++++- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py index 5b14d054af..b6552d5d6b 100644 --- a/erpnext/manufacturing/doctype/production_plan/production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py @@ -322,12 +322,13 @@ class ProductionPlan(Document): work_orders = [] bom_data = {} - get_sub_assembly_items(item.get("bom_no"), bom_data) + get_sub_assembly_items(item.get("bom_no"), bom_data, item.get("qty")) for key, data in bom_data.items(): data.update({ - 'qty': data.get("stock_qty") * item.get("qty"), + 'qty': data.get("stock_qty"), 'production_plan': self.name, + 'use_multi_level_bom': item.get("use_multi_level_bom"), 'company': self.company, 'fg_warehouse': item.get("fg_warehouse"), 'update_consumed_material_cost_in_project': 0 @@ -781,7 +782,7 @@ def get_item_data(item_code): # "description": item_details.get("description") } -def get_sub_assembly_items(bom_no, bom_data): +def get_sub_assembly_items(bom_no, bom_data, to_produce_qty): data = get_children('BOM', parent = bom_no) for d in data: if d.expandable: @@ -798,6 +799,6 @@ def get_sub_assembly_items(bom_no, bom_data): }) bom_item = bom_data.get(key) - bom_item["stock_qty"] += d.stock_qty / d.parent_bom_qty + bom_item["stock_qty"] += (d.stock_qty / d.parent_bom_qty) * flt(to_produce_qty) - get_sub_assembly_items(bom_item.get("bom_no"), bom_data) + get_sub_assembly_items(bom_item.get("bom_no"), bom_data, bom_item["stock_qty"]) diff --git a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py index ca67d71bb0..e728cc2d95 100644 --- a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py @@ -158,6 +158,46 @@ class TestProductionPlan(unittest.TestCase): self.assertTrue(mr.material_request_type, 'Customer Provided') self.assertTrue(mr.customer, '_Test Customer') + def test_production_plan_with_multi_level_bom(self): + #|Item Code | Qty | + #|Test BOM 1 | 1 | + #| Test BOM 2 | 2 | + #| Test BOM 3 | 3 | + + for item_code in ["Test BOM 1", "Test BOM 2", "Test BOM 3", "Test RM BOM 1"]: + create_item(item_code, is_stock_item=1) + + # created bom upto 3 level + if not frappe.db.get_value('BOM', {'item': "Test BOM 3"}): + make_bom(item = "Test BOM 3", raw_materials = ["Test RM BOM 1"], rm_qty=3) + + if not frappe.db.get_value('BOM', {'item': "Test BOM 2"}): + make_bom(item = "Test BOM 2", raw_materials = ["Test BOM 3"], rm_qty=3) + + if not frappe.db.get_value('BOM', {'item': "Test BOM 1"}): + make_bom(item = "Test BOM 1", raw_materials = ["Test BOM 2"], rm_qty=2) + + item_code = "Test BOM 1" + pln = frappe.new_doc('Production Plan') + pln.company = "_Test Company" + pln.append("po_items", { + "item_code": item_code, + "bom_no": frappe.db.get_value('BOM', {'item': "Test BOM 1"}), + "planned_qty": 3, + "make_work_order_for_sub_assembly_items": 1 + }) + + pln.submit() + pln.make_work_order() + + #last level sub-assembly work order produce qty + to_produce_qty = frappe.db.get_value("Work Order", + {"production_plan": pln.name, "production_item": "Test BOM 3"}, "qty") + + self.assertEqual(to_produce_qty, 18.0) + pln.cancel() + frappe.delete_doc("Production Plan", pln.name) + def create_production_plan(**args): args = frappe._dict(args) @@ -205,7 +245,7 @@ def make_bom(**args): bom.append('items', { 'item_code': item, - 'qty': 1, + 'qty': args.rm_qty or 1.0, 'uom': item_doc.stock_uom, 'stock_uom': item_doc.stock_uom, 'rate': item_doc.valuation_rate or args.rate, @@ -213,4 +253,4 @@ def make_bom(**args): bom.insert(ignore_permissions=True) bom.submit() - return bom \ No newline at end of file + return bom