Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index 50d5d43..c53b6d9 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -464,15 +464,17 @@
 					# if auto inventory accounting enabled and stock item, 
 					# then do stock related gl entries
 					# expense will be booked in sales invoice
-					
 					stock_item_and_auto_inventory_accounting = True
 					
+					valuation_amt = (flt(item.amount, self.precision.item.amount) + 
+						flt(item.item_tax_amount, self.precision.item.item_tax_amount) + 
+						flt(item.rm_supp_cost, self.precision.item.rm_supp_cost))
+					
 					gl_entries.append(
 						self.get_gl_dict({
 							"account": stock_account,
 							"against": self.doc.credit_to,
-							"debit": flt(item.valuation_rate) * flt(item.conversion_factor) \
-								*  flt(item.qty),
+							"debit": valuation_amt,
 							"remarks": self.doc.remarks or "Accounting Entry for Stock"
 						})
 					)
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index 9e181bc..28d2db6 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -367,9 +367,13 @@
 			if d.item_code and d.qty:
 				# if no item code, which is sometimes the case in purchase invoice, 
 				# then it is not possible to track valuation against it
-				d.valuation_rate = (flt(d.purchase_rate or d.rate)
-					+ (flt(d.item_tax_amount) + flt(d.rm_supp_cost)) / flt(d.qty)
-					) / flt(d.conversion_factor)
+				d.valuation_rate = flt((flt(d.purchase_rate, self.precision.item.purchase_rate) or 
+					flt(d.rate, self.precision.item.rate) + 
+					(flt(d.item_tax_amount, self.precision.item.item_tax_amount) + 
+					flt(d.rm_supp_cost, self.precision.item.rm_supp_cost)) / 
+					flt(d.qty, self.precision.item.qty)) / 
+					flt(d.conversion_factor, self.precision.item.conversion_factor), 
+					self.precision.item.valuation_rate)					
 			else:
 				d.valuation_rate = 0.0