Merge pull request #2120 from nabinhait/hotfix

Fixes in BOM and maintenance schedule
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 8bc0c9d..5f418c4 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -197,7 +197,7 @@
 
 				landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \
 					if self.doctype == "Purchase Receipt" else 0.0
-				
+
 				item.valuation_rate = ((item.base_amount + item.item_tax_amount + rm_supp_cost
 					 + landed_cost_voucher_amount) / qty_in_stock_uom)
 			else:
@@ -289,7 +289,8 @@
 					self.append(raw_material_table, d)
 
 	def get_items_from_default_bom(self, item_code):
-		bom_items = frappe.db.sql("""select t2.item_code, t2.qty_consumed_per_unit,
+		bom_items = frappe.db.sql("""select t2.item_code,
+			ifnull(t2.qty, 0) / ifnull(t1.quantity, 1) as qty_consumed_per_unit,
 			t2.rate, t2.stock_uom, t2.name, t2.description
 			from `tabBOM` t1, `tabBOM Item` t2
 			where t2.parent = t1.name and t1.item = %s and t1.is_default = 1
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index ffcbbd9..5fa2cc7 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -288,8 +288,8 @@
 		for d in self.get('bom_materials'):
 			if d.bom_no:
 				d.rate = self.get_bom_unitcost(d.bom_no)
-			d.amount = flt(d.rate) * flt(d.qty)
-			d.qty_consumed_per_unit = flt(d.qty) / flt(self.quantity)
+			d.amount = flt(d.rate, self.precision("rate", d)) * flt(d.qty, self.precision("qty", d))
+			d.qty_consumed_per_unit = flt(d.qty, self.precision("qty", d)) / flt(self.quantity, self.precision("quantity"))
 			total_rm_cost += d.amount
 
 		self.raw_material_cost = total_rm_cost
@@ -322,17 +322,19 @@
 
 	def get_child_exploded_items(self, bom_no, qty):
 		""" Add all items from Flat BOM of child BOM"""
-
-		child_fb_items = frappe.db.sql("""select item_code, description, stock_uom, qty, rate,
-			qty_consumed_per_unit from `tabBOM Explosion Item`
-			where parent = %s and docstatus = 1""", bom_no, as_dict = 1)
+		# Did not use qty_consumed_per_unit in the query, as it leads to rounding loss
+		child_fb_items = frappe.db.sql("""select bom_item.item_code, bom_item.description,
+			bom_item.stock_uom, bom_item.qty, bom_item.rate,
+			ifnull(bom_item.qty, 0 ) / ifnull(bom.quantity, 1) as qty_consumed_per_unit
+			from `tabBOM Explosion Item` bom_item, tabBOM bom
+			where bom_item.parent = bom.name and bom.name = %s and bom.docstatus = 1""", bom_no, as_dict = 1)
 
 		for d in child_fb_items:
 			self.add_to_cur_exploded_items(frappe._dict({
 				'item_code'				: d['item_code'],
 				'description'			: d['description'],
 				'stock_uom'				: d['stock_uom'],
-				'qty'					: flt(d['qty_consumed_per_unit'])*qty,
+				'qty'					: d['qty_consumed_per_unit']*qty,
 				'rate'					: flt(d['rate']),
 			}))
 
@@ -362,19 +364,21 @@
 def get_bom_items_as_dict(bom, 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_item.item_code,
 				item.item_name,
-				ifnull(sum(bom_item.qty_consumed_per_unit),0) * %(qty)s as qty,
+				sum(ifnull(bom_item.qty, 0)/ifnull(bom.quantity, 1)) * %(qty)s as qty,
 				item.description,
 				item.stock_uom,
 				item.default_warehouse,
 				item.expense_account as expense_account,
 				item.buying_cost_center as cost_center
 			from
-				`tab%(table)s` bom_item, `tabItem` item
+				`tab%(table)s` bom_item, `tabBOM` bom, `tabItem` item
 			where
-				bom_item.docstatus < 2
+				bom_item.parent = bom.name
+				and bom_item.docstatus < 2
 				and bom_item.parent = "%(bom)s"
 				and item.name = bom_item.item_code
 				%(conditions)s
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 945c77e..547ca8b 100644
--- a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
+++ b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
@@ -250,23 +250,24 @@
 			bom_wise_item_details = {}
 			if self.use_multi_level_bom:
 				# get all raw materials with sub assembly childs
+				# Did not use qty_consumed_per_unit in the query, as it leads to rounding loss
 				for d in frappe.db.sql("""select fb.item_code,
-					ifnull(sum(fb.qty_consumed_per_unit), 0) as qty,
+					ifnull(sum(ifnull(fb.qty, 0)/ifnull(bom.quantity, 1)), 0) as qty,
 					fb.description, fb.stock_uom, it.min_order_qty
-					from `tabBOM Explosion Item` fb,`tabItem` it
-					where it.name = fb.item_code and ifnull(it.is_pro_applicable, 'No') = 'No'
+					from `tabBOM Explosion Item` fb, `tabBOM` bom, `tabItem` it
+					where bom.name = fb.parent and it.name = fb.item_code and ifnull(it.is_pro_applicable, 'No') = 'No'
 					and ifnull(it.is_sub_contracted_item, 'No') = 'No'
-					and fb.docstatus<2 and fb.parent=%s
+					and fb.docstatus<2 and bom.name=%s
 					group by item_code, stock_uom""", bom, as_dict=1):
 						bom_wise_item_details.setdefault(d.item_code, d)
 			else:
 				# Get all raw materials considering SA items as raw materials,
 				# so no childs of SA items
 				for d in frappe.db.sql("""select bom_item.item_code,
-					ifnull(sum(bom_item.qty_consumed_per_unit), 0) as qty,
+					ifnull(sum(ifnull(bom_item.qty, 0)/ifnull(bom.quantity, 1)), 0) as qty,
 					bom_item.description, bom_item.stock_uom, item.min_order_qty
-					from `tabBOM Item` bom_item, tabItem item
-					where bom_item.parent = %s and bom_item.docstatus < 2
+					from `tabBOM Item` bom_item, `tabBOM` bom, tabItem item
+					where bom.name = bom_item.parent and bom.name = %s and bom_item.docstatus < 2
 					and bom_item.item_code = item.name
 					group by item_code""", bom, as_dict=1):
 						bom_wise_item_details.setdefault(d.item_code, d)
diff --git a/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.json b/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.json
index 7035f43..56344b1 100644
--- a/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.json
+++ b/erpnext/support/doctype/maintenance_schedule/maintenance_schedule.json
@@ -1,6 +1,6 @@
 {
  "autoname": "MS.#####", 
- "creation": "2013-01-10 16:34:30.000000", 
+ "creation": "2013-01-10 16:34:30", 
  "docstatus": 0, 
  "doctype": "DocType", 
  "fields": [
@@ -213,12 +213,21 @@
    "permlevel": 0, 
    "reqd": 1, 
    "search_index": 0
+  }, 
+  {
+   "fieldname": "amended_from", 
+   "fieldtype": "Link", 
+   "label": "Amended From", 
+   "no_copy": 1, 
+   "options": "Maintenance Schedule", 
+   "print_hide": 1, 
+   "read_only": 1
   }
  ], 
  "icon": "icon-calendar", 
  "idx": 1, 
  "is_submittable": 1, 
- "modified": "2014-01-20 17:48:56.000000", 
+ "modified": "2014-08-28 11:39:17.152817", 
  "modified_by": "Administrator", 
  "module": "Support", 
  "name": "Maintenance Schedule", 
@@ -239,5 +248,5 @@
    "write": 1
   }
  ], 
- "search_fields": "status,customer,customer_name, sales_order_no"
+ "search_fields": "status,customer,customer_name"
 }
\ No newline at end of file