From dc0db3a652d2be5f71518ff5da9a84eda5294879 Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Wed, 17 Jan 2018 16:12:55 +0530 Subject: [PATCH] Exploded view for BOM Stock Report (#12506) * Multilevel BOM Stock Report prototype * Rechristened multilevel to exploded view * Removed trailing whitespace in line 16. Replaced spaces with tabs for indentation * Used BOM Explosion item in query for exploded view * Removed trailing whitespaces for Codacy compliance --- .../bom_stock_report/bom_stock_report.js | 4 + .../bom_stock_report/bom_stock_report.py | 89 ++++++++++--------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.js b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.js index 95c78e6f1f..049a822ec0 100644 --- a/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.js +++ b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.js @@ -12,6 +12,10 @@ frappe.query_reports["BOM Stock Report"] = { "fieldtype": "Link", "options": "Warehouse", "reqd": 1 + }, { + "fieldname": "show_exploded_view", + "label": __("Show exploded view"), + "fieldtype": "Check" } ] } diff --git a/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py index ab9f83d522..32368395c0 100644 --- a/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py +++ b/erpnext/manufacturing/report/bom_stock_report/bom_stock_report.py @@ -6,52 +6,61 @@ import frappe from frappe import _ def execute(filters=None): - if not filters: filters = {} - columns = get_columns() - data = get_bom_stock(filters) - return columns, data + if not filters: filters = {} + columns = get_columns() + + data = get_bom_stock(filters) + + return columns, data def get_columns(): - """return columns""" - columns = [ - _("Item") + ":Link/Item:150", - _("Description") + "::500", - _("Required Qty") + ":Float:100", - _("In Stock Qty") + ":Float:100", - _("Enough Parts to Build") + ":Float:200", - ] + """return columns""" + columns = [ + _("Item") + ":Link/Item:150", + _("Description") + "::500", + _("Required Qty") + ":Float:100", + _("In Stock Qty") + ":Float:100", + _("Enough Parts to Build") + ":Float:200", + ] - return columns + return columns def get_bom_stock(filters): - conditions = "" - bom = filters.get("bom") + conditions = "" + bom = filters.get("bom") - if filters.get("warehouse"): - warehouse_details = frappe.db.get_value("Warehouse", filters.get("warehouse"), ["lft", "rgt"], as_dict=1) - if warehouse_details: - conditions += " and exists (select name from `tabWarehouse` wh \ - where wh.lft >= %s and wh.rgt <= %s and ledger.warehouse = wh.name)" % (warehouse_details.lft, - warehouse_details.rgt) - else: - conditions += " and ledger.warehouse = '%s'" % frappe.db.escape(filters.get("warehouse")) + table = "`tabBOM Item`" + qty_field = "qty" - else: - conditions += "" + if filters.get("show_exploded_view"): + table = "`tabBOM Explosion Item`" + qty_field = "stock_qty" - return frappe.db.sql(""" - SELECT - bom_item.item_code , - bom_item.description , - bom_item.qty, - sum(ledger.actual_qty) as actual_qty, - sum(FLOOR(ledger.actual_qty /bom_item.qty))as to_build - FROM - `tabBOM Item` AS bom_item - LEFT JOIN `tabBin` AS ledger - ON bom_item.item_code = ledger.item_code - %s - WHERE - bom_item.parent = '%s' and bom_item.parenttype='BOM' + if filters.get("warehouse"): + warehouse_details = frappe.db.get_value("Warehouse", filters.get("warehouse"), ["lft", "rgt"], as_dict=1) + if warehouse_details: + conditions += " and exists (select name from `tabWarehouse` wh \ + where wh.lft >= %s and wh.rgt <= %s and ledger.warehouse = wh.name)" % (warehouse_details.lft, + warehouse_details.rgt) + else: + conditions += " and ledger.warehouse = '%s'" % frappe.db.escape(filters.get("warehouse")) - GROUP BY bom_item.item_code""" % (conditions, bom)) + else: + conditions += "" + + return frappe.db.sql(""" + SELECT + bom_item.item_code , + bom_item.description , + bom_item.{qty_field}, + sum(ledger.actual_qty) as actual_qty, + sum(FLOOR(ledger.actual_qty / bom_item.{qty_field}))as to_build + FROM + {table} AS bom_item + LEFT JOIN `tabBin` AS ledger + ON bom_item.item_code = ledger.item_code + {conditions} + WHERE + bom_item.parent = '{bom}' and bom_item.parenttype='BOM' + + GROUP BY bom_item.item_code""".format(qty_field=qty_field, table=table, conditions=conditions, bom=bom))