perpetual inventory fix related to sub-contracting

Cherry-picked and modified by @anandpdoshi

Conflicts:
	accounts/doctype/purchase_invoice/purchase_invoice.py
	controllers/buying_controller.py
	controllers/stock_controller.py
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index d2bf427..36514ff 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -52,7 +52,6 @@
 		self.set_aging_date()
 		self.set_against_expense_account()
 		self.validate_write_off_account()
-		self.update_raw_material_cost()
 		self.update_valuation_rate("entries")
 		self.validate_multiple_billing("Purchase Receipt", "pr_detail", "amount",
 			"purchase_receipt_details")
@@ -321,7 +320,7 @@
 					# expense will be booked in sales invoice
 					stock_item_and_auto_accounting_for_stock = True
 
-					valuation_amt = flt(item.base_amount + item.item_tax_amount + item.rm_supp_cost,
+					valuation_amt = flt(item.base_amount + item.item_tax_amount,
 						self.precision("base_amount", item))
 
 					gl_entries.append(
@@ -390,20 +389,6 @@
 	def on_update(self):
 		pass
 
-	def update_raw_material_cost(self):
-		if self.sub_contracted_items:
-			for d in self.get("entries"):
-				rm_cost = frappe.db.sql("""select raw_material_cost / quantity
-					from `tabBOM` where item = %s and is_default = 1 and docstatus = 1
-					and is_active = 1 """, (d.item_code,))
-				rm_cost = rm_cost and flt(rm_cost[0][0]) or 0
-
-				d.conversion_factor = d.conversion_factor or flt(frappe.db.get_value(
-					"UOM Conversion Detail", {"parent": d.item_code, "uom": d.uom},
-					"conversion_factor")) or 1
-
-				d.rm_supp_cost = rm_cost * flt(d.qty) * flt(d.conversion_factor)
-
 @frappe.whitelist()
 def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
 	from erpnext.controllers.queries import get_match_cond
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 6a6da74..db3d851 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -190,7 +190,8 @@
 					"UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
 					"conversion_factor")) or 1
 				qty_in_stock_uom = flt(item.qty * item.conversion_factor)
-				item.valuation_rate = ((item.base_amount + item.item_tax_amount + item.rm_supp_cost)
+				rm_supp_cost = item.rm_supp_cost if self.doctype=="Purchase Receipt" else 0.0
+				item.valuation_rate = ((item.base_amount + item.item_tax_amount + rm_supp_cost)
 					/ qty_in_stock_uom)
 			else:
 				item.valuation_rate = 0.0
@@ -207,9 +208,15 @@
 		self.set(raw_material_table, [])
 		if self.is_subcontracted=="Yes":
 			for item in self.get(self.fname):
+				if self.doctype == "Purchase Receipt":
+					item.rm_supp_cost = 0.0
 				if item.item_code in self.sub_contracted_items:
 					self.add_bom_items(item, raw_material_table)
 
+		elif self.doctype == "Purchase Receipt":
+			for item in self.get(self.fname):
+				item.rm_supp_cost = 0.0
+
 	def add_bom_items(self, d, raw_material_table):
 		bom_items = self.get_items_from_default_bom(d.item_code)
 		raw_materials_cost = 0