From f8676b2058c54f77e23b82f7e5ae8cce0e4c3010 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 17 Jan 2017 14:11:04 +0530 Subject: [PATCH] Create Material Requests considering projected qty of items --- .../production_planning_tool.py | 78 +++++++++++++------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py index e4cee4b4de3..db5b66c579c 100644 --- a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py +++ b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py @@ -344,36 +344,56 @@ class ProductionPlanningTool(Document): self.make_items_dict(item_list) def get_subitems(self,bom_wise_item_details, bom, parent_qty, include_sublevel, only_raw, supply_subs,non_stock_item=0): - for d in frappe.db.sql("""SELECT bom_item.item_code, default_material_request_type, - ifnull(%(parent_qty)s * sum(bom_item.qty/ifnull(bom.quantity, 1)), 0) as qty, - item.is_sub_contracted_item as is_sub_contracted, item.default_bom as default_bom, - bom_item.description as description, bom_item.stock_uom as stock_uom, item.min_order_qty - as min_order_qty FROM `tabBOM Item` bom_item, `tabBOM` bom, tabItem item - where bom.name = bom_item.parent and bom.name = %(bom)s and bom_item.docstatus < 2 - and bom_item.item_code = item.name - """ + ("and item.is_stock_item = 1","")[non_stock_item] + """ - group by bom_item.item_code""", {"bom": bom, "parent_qty": parent_qty}, as_dict=1): - if (d.default_material_request_type == "Purchase" and not (d.is_sub_contracted \ - and only_raw and include_sublevel)) or (d.default_material_request_type == \ - "Manufacture" and not only_raw): + items = frappe.db.sql(""" + SELECT + bom_item.item_code, + default_material_request_type, + ifnull(%(parent_qty)s * sum(bom_item.qty/ifnull(bom.quantity, 1)), 0) as qty, + item.is_sub_contracted_item as is_sub_contracted, + item.default_bom as default_bom, + bom_item.description as description, + bom_item.stock_uom as stock_uom, + item.min_order_qty as min_order_qty + FROM + `tabBOM Item` bom_item, + `tabBOM` bom, + tabItem item + where + bom.name = bom_item.parent + and bom.name = %(bom)s + and bom_item.docstatus < 2 + and bom_item.item_code = item.name + """ + ("and item.is_stock_item = 1", "")[non_stock_item] + """ + group by bom_item.item_code""", {"bom": bom, "parent_qty": parent_qty}, as_dict=1) + + for d in items: + if ((d.default_material_request_type == "Purchase" + and not (d.is_sub_contracted and only_raw and include_sublevel)) + or (d.default_material_request_type == "Manufacture" and not only_raw)): + if d.item_code in bom_wise_item_details: - bom_wise_item_details[d.item_code].qty = bom_wise_item_details[d.item_code].qty\ - + d.qty + bom_wise_item_details[d.item_code].qty = bom_wise_item_details[d.item_code].qty + d.qty else: bom_wise_item_details[d.item_code] = d + if include_sublevel: - if (d.default_material_request_type == "Purchase" and d.is_sub_contracted \ - and supply_subs) or (d.default_material_request_type == "Manufacture"): + if ((d.default_material_request_type == "Purchase" and d.is_sub_contracted and supply_subs) + or (d.default_material_request_type == "Manufacture")): + my_qty = 0 + projected_qty = self.get_item_projected_qty(d.item_code) + if self.create_material_requests_for_all_required_qty: my_qty = d.qty - elif (bom_wise_item_details[d.item_code].qty - d.qty) < self.get_item_projected_qty(d.item_code): - my_qty = bom_wise_item_details[d.item_code].qty - self.get_item_projected_qty(d.item_code) + elif (bom_wise_item_details[d.item_code].qty - d.qty) < projected_qty: + my_qty = bom_wise_item_details[d.item_code].qty - projected_qty else: my_qty = d.qty + if my_qty > 0: - self.get_subitems(bom_wise_item_details,d.default_bom, \ - my_qty, include_sublevel, only_raw, supply_subs) + self.get_subitems(bom_wise_item_details, + d.default_bom, my_qty, include_sublevel, only_raw, supply_subs) + return bom_wise_item_details def make_items_dict(self, item_list): @@ -388,9 +408,12 @@ class ProductionPlanningTool(Document): item_list.append([item, self.item_dict[item][0][1], self.item_dict[item][0][2], total_qty]) item_qty = frappe.db.sql("""select warehouse, indented_qty, ordered_qty, actual_qty from `tabBin` where item_code = %s""", item, as_dict=1) + i_qty, o_qty, a_qty = 0, 0, 0 for w in item_qty: - i_qty, o_qty, a_qty = i_qty + flt(w.indented_qty), o_qty + flt(w.ordered_qty), a_qty + flt(w.actual_qty) + i_qty, o_qty, a_qty = i_qty + flt(w.indented_qty), o_qty + \ + flt(w.ordered_qty), a_qty + flt(w.actual_qty) + item_list.append(['', '', '', '', w.warehouse, flt(w.indented_qty), flt(w.ordered_qty), flt(w.actual_qty)]) if item_qty: @@ -460,10 +483,15 @@ class ProductionPlanningTool(Document): return items_to_be_requested def get_item_projected_qty(self,item): - item_projected_qty = frappe.db.sql("""select ifnull(sum(projected_qty),0) as qty from `tabBin` - where item_code = %(item_code)s and warehouse=%(warehouse)s""", \ - {"item_code":item, "warehouse": self.purchase_request_for_warehouse},as_dict=1) - #frappe.msgprint(item_projected_qty) + item_projected_qty = frappe.db.sql(""" + select ifnull(sum(projected_qty),0) as qty + from `tabBin` + where item_code = %(item_code)s and warehouse=%(warehouse)s + """, { + "item_code": item, + "warehouse": self.purchase_request_for_warehouse + }, as_dict=1) + return item_projected_qty[0].qty def get_projected_qty(self):