diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py index e75d82ba99..c6a3f1fcea 100644 --- a/erpnext/manufacturing/doctype/bom/bom.py +++ b/erpnext/manufacturing/doctype/bom/bom.py @@ -376,7 +376,7 @@ class BOM(Document): if not d.description: d.description = frappe.db.get_value('Operation', d.operation, 'description') -def get_bom_items_as_dict(bom, company, qty=1, fetch_exploded=1): +def get_bom_items_as_dict(bom, company, qty=1, fetch_exploded=1, fetch_scrap_items=0): item_dict = {} # Did not use qty_consumed_per_unit in the query, as it leads to rounding loss @@ -405,53 +405,13 @@ def get_bom_items_as_dict(bom, company, qty=1, fetch_exploded=1): query = query.format(table="BOM Explosion Item", conditions="""and item.is_sub_contracted_item = 0""") items = frappe.db.sql(query, { "qty": qty, "bom": bom }, as_dict=True) + elif fetch_scrap_items: + query = query.format(table="BOM Scrap Item", conditions="") + items = frappe.db.sql(query, { "qty": qty, "bom": bom }, as_dict=True) else: query = query.format(table="BOM Item", conditions="") items = frappe.db.sql(query, { "qty": qty, "bom": bom }, as_dict=True) - # make unique - for item in items: - if item_dict.has_key(item.item_code): - item_dict[item.item_code]["qty"] += flt(item.qty) - else: - item_dict[item.item_code] = item - - for item, item_details in item_dict.items(): - for d in [["Account", "expense_account", "default_expense_account"], - ["Cost Center", "cost_center", "cost_center"], ["Warehouse", "default_warehouse", ""]]: - company_in_record = frappe.db.get_value(d[0], item_details.get(d[1]), "company") - if not item_details.get(d[1]) or (company_in_record and company != company_in_record): - item_dict[item][d[1]] = frappe.db.get_value("Company", company, d[2]) if d[2] else None - - return item_dict - -def get_bom_scrap_items_as_dict(bom, company, qty=1, fetch_exploded=1): - item_dict = {} - - # Did not use qty_consumed_per_unit in the query, as it leads to rounding loss - query = """select - bom_scrap_item.item_code, - item.item_name, - sum(bom_scrap_item.qty/ifnull(bom.quantity, 1)) * %(qty)s as qty, - item.description, - item.image, - item.stock_uom, - item.default_warehouse, - item.expense_account as expense_account, - item.buying_cost_center as cost_center - from - `tabBOM Scrap Item` bom_scrap_item, `tabBOM` bom, `tabItem` item - where - bom_scrap_item.parent = bom.name - and bom_scrap_item.docstatus < 2 - and bom_scrap_item.parent = %(bom)s - and item.name = bom_scrap_item.item_code - and is_stock_item = 1 - group by item_code, stock_uom""" - - items = frappe.db.sql(query, { "qty": qty, "bom": bom }, as_dict=True) - - # make unique for item in items: if item_dict.has_key(item.item_code): item_dict[item.item_code]["qty"] += flt(item.qty) diff --git a/erpnext/manufacturing/doctype/production_order/production_order.js b/erpnext/manufacturing/doctype/production_order/production_order.js index d0e0d86d2e..d8eb25b4c3 100644 --- a/erpnext/manufacturing/doctype/production_order/production_order.js +++ b/erpnext/manufacturing/doctype/production_order/production_order.js @@ -225,6 +225,10 @@ $.extend(cur_frm.cscript, { $.each(["description", "stock_uom", "bom_no"], function(i, field) { cur_frm.set_value(field, r.message[field]); }); + + if(r.message["set_scrap_wh_mandatory"]){ + cur_frm.toggle_reqd("scrap_warehouse", true); + } } }); }, @@ -260,7 +264,12 @@ $.extend(cur_frm.cscript, { bom_no: function() { return this.frm.call({ doc: this.frm.doc, - method: "set_production_order_operations" + method: "set_production_order_operations", + callback: function(r) { + if(r.message["set_scrap_wh_mandatory"]){ + cur_frm.toggle_reqd("scrap_warehouse", true); + } + } }); }, diff --git a/erpnext/manufacturing/doctype/production_order/production_order.py b/erpnext/manufacturing/doctype/production_order/production_order.py index a0dc55433d..3934935206 100644 --- a/erpnext/manufacturing/doctype/production_order/production_order.py +++ b/erpnext/manufacturing/doctype/production_order/production_order.py @@ -222,6 +222,8 @@ class ProductionOrder(Document): self.set('operations', operations) self.calculate_time() + return check_if_scrap_warehouse_mandatory(self.bom_no) + def calculate_time(self): bom_qty = frappe.db.get_value("BOM", self.bom_no, "quantity") @@ -457,11 +459,25 @@ def get_item_details(item): return {} res = res[0] + res["bom_no"] = frappe.db.get_value("BOM", filters={"item": item, "is_default": 1}) if not res["bom_no"]: variant_of= frappe.db.get_value("Item", item, "variant_of") if variant_of: res["bom_no"] = frappe.db.get_value("BOM", filters={"item": variant_of, "is_default": 1}) + + res.update(check_if_scrap_warehouse_mandatory(res["bom_no"])) + + return res + +@frappe.whitelist() +def check_if_scrap_warehouse_mandatory(bom_no): + res = {"set_scrap_wh_mandatory": False } + bom = frappe.get_doc("BOM", bom_no) + + if len(bom.scrap_items) > 0: + res["set_scrap_wh_mandatory"] = True + return res @frappe.whitelist() diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 98924dbc60..a6f702d4b8 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -276,6 +276,13 @@ class StockEntry(StockController): if not d.t_warehouse: raw_material_cost += flt(d.basic_amount) + # get scrap items basic rate + if d.bom_no: + if not flt(d.basic_rate) and getattr(self, "pro_doc", frappe._dict()).scrap_warehouse == d.t_warehouse: + basic_rate = flt(get_incoming_rate(args), self.precision("basic_rate", d)) + if basic_rate > 0: + d.basic_rate = basic_rate + number_of_fg_items = len([t.t_warehouse for t in self.get("items") if t.t_warehouse]) if (fg_basic_rate == 0.0 and number_of_fg_items == 1) or update_finished_item_rate: self.set_basic_rate_for_finished_goods(raw_material_cost) @@ -283,7 +290,7 @@ class StockEntry(StockController): def set_basic_rate_for_finished_goods(self, raw_material_cost): if self.purpose in ["Manufacture", "Repack"]: for d in self.get("items"): - if d.bom_no or d.t_warehouse: + if (d.bom_no or d.t_warehouse) and (getattr(self, "pro_doc", frappe._dict()).scrap_warehouse != d.t_warehouse): d.basic_rate = flt(raw_material_cost / flt(d.transfer_qty), d.precision("basic_rate")) d.basic_amount = flt(raw_material_cost, d.precision("basic_amount")) @@ -302,7 +309,7 @@ class StockEntry(StockController): def update_valuation_rate(self): for d in self.get("items"): - d.amount = flt(d.basic_amount + flt(d.additional_cost), d.precision("amount")) + d.amount = flt(flt(d.basic_amount) + flt(d.additional_cost), d.precision("amount")) d.valuation_rate = flt( flt(d.basic_rate) + (flt(d.additional_cost) / flt(d.transfer_qty)), @@ -617,11 +624,11 @@ class StockEntry(StockController): return item_dict def get_bom_scrap_material(self, qty): - from erpnext.manufacturing.doctype.bom.bom import get_bom_scrap_items_as_dict + from erpnext.manufacturing.doctype.bom.bom import get_bom_items_as_dict # item dict = { item_code: {qty, description, stock_uom} } - item_dict = get_bom_scrap_items_as_dict(self.bom_no, self.company, qty=qty, - fetch_exploded = self.use_multi_level_bom) + item_dict = get_bom_items_as_dict(self.bom_no, self.company, qty=qty, + fetch_exploded = 0, fetch_scrap_items = 1) for item in item_dict.values(): item.from_warehouse = ""