Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/.gitignore b/.gitignore
index 7f90378..0818a02 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@
 patch.log
 lib
 versions-local.db
+*.sql*
diff --git a/erpnext/stock/doctype/bin/bin.py b/erpnext/stock/doctype/bin/bin.py
index e3af0a2..bb1101e 100644
--- a/erpnext/stock/doctype/bin/bin.py
+++ b/erpnext/stock/doctype/bin/bin.py
@@ -146,7 +146,7 @@
 			in_rate = flt(sql("select ifnull(avg(purchase_rate), 0) from `tabSerial No` where name in (%s)" % (serial_nos))[0][0])
 
 		if in_rate and val_rate == 0: # First entry
-			val_rate = in_rate		
+			val_rate = in_rate
 		# val_rate is same as previous entry if val_rate is negative
 		# Otherwise it will be calculated as per moving average
 		elif opening_qty + actual_qty > 0 and ((opening_qty * val_rate) + (actual_qty * in_rate)) > 0:
diff --git a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py
index 2b828f9..c791e86 100644
--- a/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py
+++ b/erpnext/stock/doctype/landed_cost_wizard/landed_cost_wizard.py
@@ -166,7 +166,7 @@
 			  tax_amount = flt(rate) * (flt(ocd[row-1].total_tax_amount)+flt(ocd[row-1].total_amount)) / 100
 			elif ocd[row-1].add_deduct_tax == 'Deduct':
 			  tax_amount = flt(rate) * (flt(ocd[row-1].total_tax_amount)-flt(ocd[row-1].total_amount)) / 100
-			  
+		
 		return tax_amount  
 
 	def add_deduct_taxes(self, ocd, oc, tax_amount, total, prev_total, item_tax):
@@ -202,7 +202,8 @@
 			for d in getlist(pr_obj.doclist, 'purchase_receipt_details'):
 				if flt(d.qty):
 					d.valuation_rate = (flt(d.purchase_rate) + (flt(d.rm_supp_cost)/flt(d.qty)) + (flt(d.item_tax_amount)/flt(d.qty))) / flt(d.conversion_factor)
-					d.save()	
+					d.save()
+					self.update_serial_no(d.serial_no, d.valuation_rate)
 				sql("update `tabStock Ledger Entry` set incoming_rate = '%s' where voucher_detail_no = '%s'"%(flt(d.valuation_rate), d.name))
 				
 				bin = sql("select t1.name, t2.posting_date, t2.posting_time from `tabBin` t1, `tabStock Ledger Entry` t2 where t2.voucher_detail_no = '%s' and t2.item_code = t1.item_code and t2.warehouse = t1.warehouse LIMIT 1" % d.name)
@@ -211,6 +212,13 @@
 				if bin and bin[0][0]:
 					obj = get_obj('Bin', bin[0][0]).update_entries_after(bin[0][1], bin[0][2])
 
+	
+	def update_serial_no(self, sr_no, rate):
+		""" update valuation rate in serial no"""
+		sr_no = sr_no.split('\n')
+		for d in sr_no:
+			sql("update `tabSerial No` set purchase_rate = %s where name = %s", (rate, d))
+
 				
 	def update_landed_cost(self):
 		""" 
diff --git a/erpnext/stock/doctype/stock_ledger/stock_ledger.py b/erpnext/stock/doctype/stock_ledger/stock_ledger.py
index 5165cc9..0970b78 100644
--- a/erpnext/stock/doctype/stock_ledger/stock_ledger.py
+++ b/erpnext/stock/doctype/stock_ledger/stock_ledger.py
@@ -93,27 +93,27 @@
 	# ---------------------
 	def set_pur_serial_no_values(self, obj, serial_no, d, s, new_rec):
 		item_details = sql("select item_group, warranty_period from `tabItem` where name = '%s' and (ifnull(end_of_life,'')='' or end_of_life = '0000-00-00' or end_of_life > now()) " %(d.item_code), as_dict=1)
-		s.purchase_document_type=	 obj.doc.doctype
-		s.purchase_document_no	=	 obj.doc.name
-		s.purchase_date		=	 obj.doc.posting_date
-		s.purchase_time		=	 obj.doc.posting_time
-		s.purchase_rate		=	 d.purchase_rate or d.incoming_rate
-		s.item_code		=	 d.item_code
-		s.brand			=	 d.brand
-		s.description		=	 d.description
-		s.item_group		=	 item_details and item_details[0]['item_group'] or ''
-		s.warranty_period	=	 item_details and item_details[0]['warranty_period'] or 0
-		s.supplier		=	 obj.doc.supplier
-		s.supplier_name		=	 obj.doc.supplier_name
-		s.supplier_address	=	 obj.doc.supplier_address
-		s.warehouse		=	 d.warehouse or d.t_warehouse
-		s.docstatus		=	 0
-		s.status		=	 'In Store'
-		s.modified		=	 nowdate()
-		s.modified_by		=	 session['user']
-		s.serial_no		=	 serial_no
-		s.fiscal_year		=	 obj.doc.fiscal_year
-		s.company		=	 obj.doc.company
+		s.purchase_document_type	=	obj.doc.doctype
+		s.purchase_document_no		=	obj.doc.name
+		s.purchase_date				=	obj.doc.posting_date
+		s.purchase_time				=	obj.doc.posting_time
+		s.purchase_rate				=	d.valuation_rate or d.incoming_rate
+		s.item_code					=	d.item_code
+		s.brand						=	d.brand
+		s.description				=	d.description
+		s.item_group				=	item_details and item_details[0]['item_group'] or ''
+		s.warranty_period			=	item_details and item_details[0]['warranty_period'] or 0
+		s.supplier					=	obj.doc.supplier
+		s.supplier_name				=	obj.doc.supplier_name
+		s.supplier_address			=	obj.doc.supplier_address
+		s.warehouse					=	d.warehouse or d.t_warehouse
+		s.docstatus					=	0
+		s.status					=	'In Store'
+		s.modified					=	nowdate()
+		s.modified_by				=	session['user']
+		s.serial_no					=	serial_no
+		s.fiscal_year				=	obj.doc.fiscal_year
+		s.company					=	obj.doc.company
 		s.save(new_rec)