From cf4078756dea4ac073af278e86510b2f94cee08b Mon Sep 17 00:00:00 2001 From: noahjacob Date: Tue, 25 May 2021 16:40:39 +0530 Subject: [PATCH 1/5] fix: fixed fetching sales order of item variant in production plan --- .../production_plan/production_plan.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py index 6a024f275aa..99b69263e05 100644 --- a/erpnext/manufacturing/doctype/production_plan/production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py @@ -118,16 +118,20 @@ class ProductionPlan(Document): item_condition = "" if self.item_code: + if frappe.db.exists('Item', self.item_code): + is_variant = frappe.db.get_value('Item', self.item_code, ['variant_of']) + if is_variant: + variant_of = is_variant item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) - + items = frappe.db.sql("""select distinct parent, item_code, warehouse, (qty - work_order_qty) * conversion_factor as pending_qty, description, name from `tabSales Order Item` so_item where parent in (%s) and docstatus = 1 and qty > work_order_qty - and exists (select name from `tabBOM` bom where bom.item=so_item.item_code + and exists (select name from `tabBOM` bom where bom.item= "%s" and bom.is_active = 1) %s""" % \ - (", ".join(["%s"] * len(so_list)), item_condition), tuple(so_list), as_dict=1) - + (", ".join(["%s"] * len(so_list)), variant_of or "so_items.item_code", item_condition), tuple(so_list), as_dict=1) + if self.item_code: item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) @@ -695,6 +699,10 @@ def get_sales_orders(self): so_filter += "and so.status = %(sales_order_status)s" if self.item_code: + if frappe.db.exists('Item', self.item_code): + is_variant = frappe.db.get_value('Item', self.item_code, ['variant_of']) + if is_variant: + variant_of = is_variant item_filter += " and so_item.item_code = %(item)s" open_so = frappe.db.sql(""" @@ -704,7 +712,7 @@ def get_sales_orders(self): and so.docstatus = 1 and so.status not in ("Stopped", "Closed") and so.company = %(company)s and so_item.qty > so_item.work_order_qty {0} {1} - and (exists (select name from `tabBOM` bom where bom.item=so_item.item_code + and (exists (select name from `tabBOM` bom where bom.item=%(item_code)s and bom.is_active = 1) or exists (select name from `tabPacked Item` pi where pi.parent = so.name and pi.parent_item = so_item.item_code @@ -715,6 +723,7 @@ def get_sales_orders(self): "to_date": self.to_date, "customer": self.customer, "project": self.project, + "item_code": variant_of or "so_item.item_code", "item": self.item_code, "company": self.company, "sales_order_status": self.sales_order_status From 9b0b2daf4a049991dd47e93fa1202189c676110b Mon Sep 17 00:00:00 2001 From: noahjacob Date: Wed, 26 May 2021 17:52:08 +0530 Subject: [PATCH 2/5] refactor: updated sql query for item variants --- .../production_plan/production_plan.py | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py index 99b69263e05..efb2d9235fd 100644 --- a/erpnext/manufacturing/doctype/production_plan/production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py @@ -116,21 +116,21 @@ class ProductionPlan(Document): so_list = self.get_so_mr_list("sales_order", "sales_orders") - item_condition = "" + item_condition = variant_of_bom = "" if self.item_code: if frappe.db.exists('Item', self.item_code): - is_variant = frappe.db.get_value('Item', self.item_code, ['variant_of']) - if is_variant: - variant_of = is_variant + has_variant_bom = frappe.db.exists({'doctype': 'BOM', 'item': self.item_code}) + if not has_variant_bom: + variant_of_bom = "'%s'" % frappe.db.get_value('Item', self.item_code, ['variant_of']) item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) - + items = frappe.db.sql("""select distinct parent, item_code, warehouse, (qty - work_order_qty) * conversion_factor as pending_qty, description, name from `tabSales Order Item` so_item where parent in (%s) and docstatus = 1 and qty > work_order_qty - and exists (select name from `tabBOM` bom where bom.item= "%s" + and exists (select name from `tabBOM` bom where bom.item= %s and bom.is_active = 1) %s""" % \ - (", ".join(["%s"] * len(so_list)), variant_of or "so_items.item_code", item_condition), tuple(so_list), as_dict=1) + (", ".join(["%s"] * len(so_list)), variant_of_bom or "so_item.item_code", item_condition), tuple(so_list), as_dict=1) if self.item_code: item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) @@ -686,7 +686,7 @@ def get_material_request_items(row, sales_order, company, } def get_sales_orders(self): - so_filter = item_filter = "" + so_filter = item_filter = variant_of_bom = "" if self.from_date: so_filter += " and so.transaction_date >= %(from_date)s" if self.to_date: @@ -700,9 +700,9 @@ def get_sales_orders(self): if self.item_code: if frappe.db.exists('Item', self.item_code): - is_variant = frappe.db.get_value('Item', self.item_code, ['variant_of']) - if is_variant: - variant_of = is_variant + has_variant_bom = frappe.db.exists({'doctype': 'BOM', 'item': self.item_code}) + if not has_variant_bom: + variant_of_bom = "'%s'" % frappe.db.get_value('Item', self.item_code, ['variant_of']) item_filter += " and so_item.item_code = %(item)s" open_so = frappe.db.sql(""" @@ -712,18 +712,17 @@ def get_sales_orders(self): and so.docstatus = 1 and so.status not in ("Stopped", "Closed") and so.company = %(company)s and so_item.qty > so_item.work_order_qty {0} {1} - and (exists (select name from `tabBOM` bom where bom.item=%(item_code)s + and (exists (select name from `tabBOM` bom where bom.item= {2} and bom.is_active = 1) or exists (select name from `tabPacked Item` pi where pi.parent = so.name and pi.parent_item = so_item.item_code and exists (select name from `tabBOM` bom where bom.item=pi.item_code and bom.is_active = 1))) - """.format(so_filter, item_filter), { + """.format(so_filter, item_filter, variant_of_bom or "so_item.item_code"), { "from_date": self.from_date, "to_date": self.to_date, "customer": self.customer, "project": self.project, - "item_code": variant_of or "so_item.item_code", "item": self.item_code, "company": self.company, "sales_order_status": self.sales_order_status From b10465eebe4ef6839ad6b9c12bc0536c3abb7a91 Mon Sep 17 00:00:00 2001 From: Noah Jacob Date: Thu, 3 Jun 2021 17:57:23 +0530 Subject: [PATCH 3/5] refactor: created function to get bom_item for query --- .../production_plan/production_plan.py | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py index efb2d9235fd..c2a365adb1d 100644 --- a/erpnext/manufacturing/doctype/production_plan/production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py @@ -109,6 +109,15 @@ class ProductionPlan(Document): so_mr_list = [d.get(field) for d in self.get(table) if d.get(field)] return so_mr_list + def get_bom_item(self): + """Check if Item or if its Template has a BOM.""" + bom_item = None + has_bom = frappe.db.exists({'doctype': 'BOM', 'item': self.item_code, 'docstatus': 1}) + if not has_bom: + template_item = frappe.db.get_value('Item', self.item_code, ['variant_of']) + bom_item = "bom.item = {0}".format(frappe.db.escape(template_item)) if template_item else bom_item + return bom_item + def get_so_items(self): # Check for empty table or empty rows if not self.get("sales_orders") or not self.get_so_mr_list("sales_order", "sales_orders"): @@ -116,21 +125,20 @@ class ProductionPlan(Document): so_list = self.get_so_mr_list("sales_order", "sales_orders") - item_condition = variant_of_bom = "" - if self.item_code: - if frappe.db.exists('Item', self.item_code): - has_variant_bom = frappe.db.exists({'doctype': 'BOM', 'item': self.item_code}) - if not has_variant_bom: - variant_of_bom = "'%s'" % frappe.db.get_value('Item', self.item_code, ['variant_of']) + item_condition = "" + bom_item = "bom.item = so_item.item_code" + if self.item_code and frappe.db.exists('Item', self.item_code): + bom_item = self.get_bom_item() or bom_item item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) items = frappe.db.sql("""select distinct parent, item_code, warehouse, - (qty - work_order_qty) * conversion_factor as pending_qty, description, name - from `tabSales Order Item` so_item - where parent in (%s) and docstatus = 1 and qty > work_order_qty - and exists (select name from `tabBOM` bom where bom.item= %s - and bom.is_active = 1) %s""" % \ - (", ".join(["%s"] * len(so_list)), variant_of_bom or "so_item.item_code", item_condition), tuple(so_list), as_dict=1) + (qty - work_order_qty) * conversion_factor as pending_qty, description, name + from `tabSales Order Item` so_item + where + parent in (%s) and docstatus = 1 and qty > work_order_qty + and exists (select name from `tabBOM` bom where %s + and bom.is_active = 1) %s""" % \ + (", ".join(["%s"] * len(so_list)), bom_item, item_condition), tuple(so_list), as_dict=1) if self.item_code: item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) @@ -686,7 +694,8 @@ def get_material_request_items(row, sales_order, company, } def get_sales_orders(self): - so_filter = item_filter = variant_of_bom = "" + so_filter = item_filter = "" + bom_item = "bom.item = so_item.item_code" if self.from_date: so_filter += " and so.transaction_date >= %(from_date)s" if self.to_date: @@ -698,11 +707,8 @@ def get_sales_orders(self): if self.sales_order_status: so_filter += "and so.status = %(sales_order_status)s" - if self.item_code: - if frappe.db.exists('Item', self.item_code): - has_variant_bom = frappe.db.exists({'doctype': 'BOM', 'item': self.item_code}) - if not has_variant_bom: - variant_of_bom = "'%s'" % frappe.db.get_value('Item', self.item_code, ['variant_of']) + if self.item_code and frappe.db.exists('Item', self.item_code): + bom_item = self.get_bom_item() or bom_item item_filter += " and so_item.item_code = %(item)s" open_so = frappe.db.sql(""" @@ -712,13 +718,13 @@ def get_sales_orders(self): and so.docstatus = 1 and so.status not in ("Stopped", "Closed") and so.company = %(company)s and so_item.qty > so_item.work_order_qty {0} {1} - and (exists (select name from `tabBOM` bom where bom.item= {2} + and (exists (select name from `tabBOM` bom where {2} and bom.is_active = 1) or exists (select name from `tabPacked Item` pi where pi.parent = so.name and pi.parent_item = so_item.item_code and exists (select name from `tabBOM` bom where bom.item=pi.item_code and bom.is_active = 1))) - """.format(so_filter, item_filter, variant_of_bom or "so_item.item_code"), { + """.format(so_filter, item_filter, bom_item), { "from_date": self.from_date, "to_date": self.to_date, "customer": self.customer, From 041ac339b1ba0ac04724b888fad6748b7a2f47b3 Mon Sep 17 00:00:00 2001 From: Noah Jacob Date: Sun, 6 Jun 2021 18:08:39 +0530 Subject: [PATCH 4/5] style: improved formatting of sql query --- .../production_plan/production_plan.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/erpnext/manufacturing/doctype/production_plan/production_plan.py b/erpnext/manufacturing/doctype/production_plan/production_plan.py index c2a365adb1d..8c27d6ccc96 100644 --- a/erpnext/manufacturing/doctype/production_plan/production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/production_plan.py @@ -131,15 +131,22 @@ class ProductionPlan(Document): bom_item = self.get_bom_item() or bom_item item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) - items = frappe.db.sql("""select distinct parent, item_code, warehouse, - (qty - work_order_qty) * conversion_factor as pending_qty, description, name - from `tabSales Order Item` so_item - where + items = frappe.db.sql(""" + select + distinct parent, item_code, warehouse, + (qty - work_order_qty) * conversion_factor as pending_qty, + description, name + from + `tabSales Order Item` so_item + where parent in (%s) and docstatus = 1 and qty > work_order_qty and exists (select name from `tabBOM` bom where %s - and bom.is_active = 1) %s""" % \ - (", ".join(["%s"] * len(so_list)), bom_item, item_condition), tuple(so_list), as_dict=1) - + and bom.is_active = 1) %s""" % + (", ".join(["%s"] * len(so_list)), + bom_item, + item_condition), + tuple(so_list), as_dict=1) + if self.item_code: item_condition = ' and so_item.item_code = {0}'.format(frappe.db.escape(self.item_code)) From cb44aed78b3e8a427bd8d1387664e9c3037745ce Mon Sep 17 00:00:00 2001 From: Noah Jacob Date: Thu, 5 Aug 2021 16:11:36 +0530 Subject: [PATCH 5/5] test: get sales order with variant --- .../production_plan/test_production_plan.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py index 93e6d7a97f4..af8de8ee0e3 100644 --- a/erpnext/manufacturing/doctype/production_plan/test_production_plan.py +++ b/erpnext/manufacturing/doctype/production_plan/test_production_plan.py @@ -11,6 +11,7 @@ from erpnext.manufacturing.doctype.production_plan.production_plan import get_sa from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import create_stock_reconciliation from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order from erpnext.manufacturing.doctype.production_plan.production_plan import get_items_for_material_requests, get_warehouse_list +from erpnext.controllers.item_variant import create_variant class TestProductionPlan(unittest.TestCase): def setUp(self): @@ -271,6 +272,60 @@ class TestProductionPlan(unittest.TestCase): self.assertEqual(warehouses, expected_warehouses) + def test_get_sales_order_with_variant(self): + if not frappe.db.exists('Item', {"item_code": 'PIV'}): + item = create_item('PIV', valuation_rate = 100) + variant_settings = { + "attributes": [ + { + "attribute": "Colour" + }, + ], + "has_variants": 1 + } + item.update(variant_settings) + item.save() + parent_bom = make_bom(item = 'PIV', raw_materials = ['PIV']) + if not frappe.db.exists('BOM', {"item": 'PIV'}): + parent_bom = make_bom(item = 'PIV', raw_materials = ['PIV']) + else: + parent_bom = frappe.get_doc('BOM', {"item": 'PIV'}) + + if not frappe.db.exists('Item', {"item_code": 'PIV-RED'}): + variant = create_variant("PIV", {"Colour": "Red"}) + variant.save() + variant_bom = make_bom(item = variant.item_code, raw_materials = [variant.item_code]) + else: + variant = frappe.get_doc('Item', 'PIV-RED') + if not frappe.db.exists('BOM', {"item": 'PIV-RED'}): + variant_bom = make_bom(item = variant.item_code, raw_materials = [variant.item_code]) + + """Testing when item variant has a BOM""" + so = make_sales_order(item_code="PIV-RED", qty=5) + pln = frappe.new_doc('Production Plan') + pln.company = so.company + pln.get_items_from = 'Sales Order' + pln.item_code = 'PIV-RED' + pln.get_open_sales_orders() + self.assertEqual(pln.sales_orders[0].sales_order, so.name) + pln.get_so_items() + self.assertEqual(pln.po_items[0].item_code, 'PIV-RED') + self.assertEqual(pln.po_items[0].bom_no, variant_bom.name) + so.cancel() + frappe.delete_doc('Sales Order', so.name) + variant_bom.cancel() + frappe.delete_doc('BOM', variant_bom.name) + + """Testing when item variant doesn't have a BOM""" + so = make_sales_order(item_code="PIV-RED", qty=5) + pln.get_open_sales_orders() + self.assertEqual(pln.sales_orders[0].sales_order, so.name) + pln.po_items = [] + pln.get_so_items() + self.assertEqual(pln.po_items[0].item_code, 'PIV-RED') + self.assertEqual(pln.po_items[0].bom_no, parent_bom.name) + + frappe.db.rollback() def create_production_plan(**args): args = frappe._dict(args)