Merge branch 'cms'
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index 8272fb2..abea87c 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -119,7 +119,6 @@
 
 	// Show / Hide button
 	cur_frm.clear_custom_buttons();
-
 	if (!cur_frm.cscript.is_onload)	cur_frm.cscript.hide_price_list_currency(doc, dt, dn); 
 
 	if(doc.docstatus==1) {
diff --git a/erpnext/selling/doctype/installation_note/installation_note.py b/erpnext/selling/doctype/installation_note/installation_note.py
index 846d143..0dedf54 100644
--- a/erpnext/selling/doctype/installation_note/installation_note.py
+++ b/erpnext/selling/doctype/installation_note/installation_note.py
@@ -138,7 +138,7 @@
       
       if d.serial_no:
 
-        sr_list = get_sr_no_list(d.serial_no, d.qty)
+        sr_list = get_sr_no_list(d.serial_no, d.qty, d.item_code)
         self.is_serial_no_exist(d.item_code, sr_list)
         
         prevdoc_s_no = self.get_prevdoc_serial_no(d.prevdoc_detail_docname, d.prevdoc_docname)
diff --git a/erpnext/selling/doctype/sales_common/sales_common.py b/erpnext/selling/doctype/sales_common/sales_common.py
index f9f8adb..1a60a06 100644
--- a/erpnext/selling/doctype/sales_common/sales_common.py
+++ b/erpnext/selling/doctype/sales_common/sales_common.py
@@ -311,16 +311,6 @@
 			}
 		return ret
 		
-
-	# Make Packing List from Sales BOM
-	# =======================================================================
-	def has_sales_bom(self, item_code):
-		return webnotes.conn.sql("select name from `tabSales BOM` where name=%s and docstatus != 2", item_code)
-	
-	def get_sales_bom_items(self, item_code):
-		return webnotes.conn.sql("select item_code, qty, uom from `tabSales BOM Item` where parent=%s", item_code)
-
-
 	# --------------
 	# get item list
 	# --------------
@@ -345,8 +335,9 @@
 			warehouse = (obj.fname == "sales_order_details") and d.reserved_warehouse or d.warehouse
 			
 			if self.has_sales_bom(d.item_code):
-				for i in self.get_sales_bom_items(d.item_code):
-					il.append([warehouse, i[0], flt(flt(i[1])* qty), flt(flt(i[1])*reserved_qty), i[2], d.batch_no, d.serial_no])
+				for p in getlist(obj.doclist, 'packing_details'):
+					if p.parent_item == d.item_code:
+						il.append([warehouse, p.item_code, flt(p.qty)*qty, flt(p.qty)* reserved_qty, p.uom, p.batch_no, p.serial_no])
 			else:
 				il.append([warehouse, d.item_code, qty, reserved_qty, d.stock_uom, d.batch_no, d.serial_no])
 		return il
@@ -371,12 +362,16 @@
 		return qty, max_qty, amt, max_amt
 
 
+	# Make Packing List from Sales BOM
+	# =======================================================================
+	def has_sales_bom(self, item_code):
+		return webnotes.conn.sql("select name from `tabSales BOM` where new_item_code=%s and docstatus != 2", item_code)
+	
+	def get_sales_bom_items(self, item_code):
+		return webnotes.conn.sql("""select t1.item_code, t1.qty, t1.uom 
+			from `tabSales BOM Item` t1, `tabSales BOM` t2 
+			where t2.new_item_code=%s and t1.parent = t2.name""", item_code, as_dict=1)
 
-
-
-	# -----------------------
-	# add packing list items
-	# -----------------------
 	def get_packing_item_details(self, item):
 		return webnotes.conn.sql("select item_name, description, stock_uom from `tabItem` where name = %s", item, as_dict = 1)[0]
 
@@ -384,12 +379,22 @@
 		det = webnotes.conn.sql("select actual_qty, projected_qty from `tabBin` where item_code = '%s' and warehouse = '%s'" % (item, warehouse), as_dict = 1)
 		return det and det[0] or ''
 
-	def add_packing_list_item(self,obj, item_code, qty, warehouse, line):
-		bin = self.get_bin_qty(item_code, warehouse)
-		item = self.get_packing_item_details(item_code)
-		pi = addchild(obj.doc, 'packing_details', 'Delivery Note Packing Item', 1, obj.doclist)
-		pi.parent_item = item_code
-		pi.item_code = item_code
+	def update_packing_list_item(self,obj, packing_item_code, qty, warehouse, line):
+		bin = self.get_bin_qty(packing_item_code, warehouse)
+		item = self.get_packing_item_details(packing_item_code)
+
+		# check if exists
+		exists = 0
+		for d in getlist(obj.doclist, 'packing_details'):
+			if d.parent_item == line.item_code and d.item_code == packing_item_code:
+				pi, exists = d, 1
+				break
+
+		if not exists:
+			pi = addchild(obj.doc, 'packing_details', 'Delivery Note Packing Item', 1, obj.doclist)
+
+		pi.parent_item = line.item_code
+		pi.item_code = packing_item_code
 		pi.item_name = item['item_name']
 		pi.parent_detail_docname = line.name
 		pi.description = item['description']
@@ -399,7 +404,9 @@
 		pi.projected_qty = bin and flt(bin['projected_qty']) or 0
 		pi.warehouse = warehouse
 		pi.prevdoc_doctype = line.prevdoc_doctype
-		pi.serial_no = cstr(line.serial_no)
+		if packing_item_code == line.item_code:
+			pi.serial_no = cstr(line.serial_no)
+			pi.batch_no = cstr(line.batch_no)
 		pi.idx = self.packing_list_idx
 		self.packing_list_idx += 1
 
@@ -408,15 +415,14 @@
 	# make packing list from sales bom if exists or directly copy item with balance
 	# ------------------ 
 	def make_packing_list(self, obj, fname):
-		obj.doc.clear_table(obj.doclist, 'packing_details')
 		self.packing_list_idx = 0
 		for d in getlist(obj.doclist, fname):
 			warehouse = fname == "sales_order_details" and d.reserved_warehouse or d.warehouse
 			if self.has_sales_bom(d.item_code):
 				for i in self.get_sales_bom_items(d.item_code):
-					self.add_packing_list_item(obj, i[0], flt(i[1])*flt(d.qty), warehouse, d)
+					self.update_packing_list_item(obj, i['item_code'], flt(i['qty'])*flt(d.qty), warehouse, d)
 			else:
-				self.add_packing_list_item(obj, d.item_code, d.qty, warehouse, d)
+				self.update_packing_list_item(obj, d.item_code, d.qty, warehouse, d)
 
 
 	# Get total in words
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 445e93b..e2d2ca3 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -416,7 +416,6 @@
 				if not d[0]:
 					msgprint("Message: Please enter Warehouse for item %s as it is stock item."% d[1])
 					raise Exception
-				# if prevdoc_doctype = "Sales Order"
 				if d[3] < 0 :
 					# Reduce Reserved Qty from warehouse
 					bin = get_obj('Warehouse', d[0]).update_bin(0, flt(update_stock) * flt(d[3]), 0, 0, 0, d[1], self.doc.transaction_date,doc_type=self.doc.doctype,doc_name=self.doc.name, is_amended = (self.doc.amended_from and 'Yes' or 'No'))
diff --git a/erpnext/stock/doctype/delivery_note_packing_item/delivery_note_packing_item.txt b/erpnext/stock/doctype/delivery_note_packing_item/delivery_note_packing_item.txt
index 4299fcc..51001fc 100644
--- a/erpnext/stock/doctype/delivery_note_packing_item/delivery_note_packing_item.txt
+++ b/erpnext/stock/doctype/delivery_note_packing_item/delivery_note_packing_item.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-03-27 14:36:30',
+		'creation': '2012-04-13 11:56:35',
 		'docstatus': 0,
-		'modified': '2012-03-27 14:36:30',
+		'modified': '2012-05-09 12:55:23',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -20,7 +20,7 @@
 		'section_style': u'Tray',
 		'server_code_error': u' ',
 		'show_in_menu': 0,
-		'version': 17
+		'version': 1
 	},
 
 	# These values are common for all DocField
@@ -137,6 +137,15 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'batch_no',
+		'fieldtype': u'Data',
+		'label': u'Batch No',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'actual_qty',
 		'fieldtype': u'Currency',
 		'label': u'Actual Qty',
@@ -173,34 +182,6 @@
 
 	# DocField
 	{
-		'doctype': u'DocField',
-		'fieldname': u'planned_qty',
-		'fieldtype': u'Currency',
-		'hidden': 1,
-		'label': u'Planned Qty ',
-		'no_copy': 1,
-		'oldfieldname': u'planned_qty',
-		'oldfieldtype': u'Currency',
-		'permlevel': 1,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'produced_qty',
-		'fieldtype': u'Currency',
-		'hidden': 1,
-		'label': u'Produced Qty',
-		'no_copy': 1,
-		'oldfieldname': u'produced_qty',
-		'oldfieldtype': u'Currency',
-		'permlevel': 1,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
 		'colour': u'White:FFF',
 		'doctype': u'DocField',
 		'fieldname': u'prevdoc_doctype',
@@ -215,47 +196,6 @@
 
 	# DocField
 	{
-		'description': u'The date at which current entry is made in system.',
-		'doctype': u'DocField',
-		'fieldname': u'transaction_date',
-		'fieldtype': u'Date',
-		'hidden': 0,
-		'label': u'Sales Order Date',
-		'oldfieldname': u'transaction_date',
-		'oldfieldtype': u'Date',
-		'permlevel': 1,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'delivery_date',
-		'fieldtype': u'Date',
-		'hidden': 0,
-		'label': u'Expected Delivery Date',
-		'oldfieldname': u'delivery_date',
-		'oldfieldtype': u'Date',
-		'permlevel': 1,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'confirmation_date',
-		'fieldtype': u'Date',
-		'hidden': 1,
-		'label': u'Confirmed delivery date',
-		'no_copy': 1,
-		'oldfieldname': u'confirmation_date',
-		'oldfieldtype': u'Date',
-		'permlevel': 1,
-		'print_hide': 1
-	},
-
-	# DocField
-	{
 		'allow_on_submit': 1,
 		'doctype': u'DocField',
 		'fieldname': u'page_break',
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index 798a2c5..37bc8a5 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -114,7 +114,7 @@
 
 			# If Reject Qty than Rejected warehouse is mandatory
 			if flt(d.rejected_qty) and (not self.doc.rejected_warehouse):
-				msgprint("Rejected Warehouse is necessary if there are rejections. See 'Receipt Items'")
+				msgprint("Rejected Warehouse is necessary if there are rejections.")
 				raise Exception
 
 			# Check Received Qty = Accepted Qty + Rejected Qty
@@ -198,6 +198,15 @@
 
 		self.update_rw_material_detail()
 		get_obj('Stock Ledger').scrub_serial_nos(self)
+		self.scrub_rejected_serial_nos()
+
+
+	def scrub_rejected_serial_nos(self):
+		for d in getlist(self.doclist, 'purchase_receipt_details'):
+			if d.rejected_serial_no:
+				d.rejected_serial_no = d.rejected_serial_no.replace(',', '\n')
+				d.save()
+
 
 
 	# On Submit
@@ -232,7 +241,7 @@
 				self.make_sl_entry(d, d.warehouse, flt(pr_qty), d.valuation_rate, is_submit)
 				# UPDATE actual to rejected warehouse by rejected qty
 				if flt(d.rejected_qty) > 0:
-					self.make_sl_entry(d, self.doc.rejected_warehouse, flt(d.rejected_qty) * flt(d.conversion_factor), d.valuation_rate, is_submit)
+					self.make_sl_entry(d, self.doc.rejected_warehouse, flt(d.rejected_qty) * flt(d.conversion_factor), d.valuation_rate, is_submit, rejected = 1)
 
 		self.bk_flush_supp_wh(is_submit)
 
@@ -241,24 +250,29 @@
 
 
 	# make Stock Entry
-	def make_sl_entry(self, d, wh, qty, in_value, is_submit):
+	def make_sl_entry(self, d, wh, qty, in_value, is_submit, rejected = 0):
+		if rejected:
+			serial_no = d.rejected_serial_no
+		else:
+			serial_no = d.serial_no
+
 		self.values.append({
-			'item_code'					 : d.fields.has_key('item_code') and d.item_code or d.rm_item_code,
-			'warehouse'					 : wh,
+			'item_code'					: d.fields.has_key('item_code') and d.item_code or d.rm_item_code,
+			'warehouse'					: wh,
 			'transaction_date'			: getdate(self.doc.modified).strftime('%Y-%m-%d'),
 			'posting_date'				: self.doc.posting_date,
 			'posting_time'				: self.doc.posting_time,
 			'voucher_type'				: 'Purchase Receipt',
-			'voucher_no'					: self.doc.name,
-			'voucher_detail_no'	 : d.name,
-			'actual_qty'					: qty,
-			'stock_uom'					 : d.stock_uom,
-			'incoming_rate'			 : in_value,
-			'company'						 : self.doc.company,
-			'fiscal_year'				 : self.doc.fiscal_year,
+			'voucher_no'				: self.doc.name,
+			'voucher_detail_no'			: d.name,
+			'actual_qty'				: qty,
+			'stock_uom'					: d.stock_uom,
+			'incoming_rate'				: in_value,
+			'company'					: self.doc.company,
+			'fiscal_year'				: self.doc.fiscal_year,
 			'is_cancelled'				: (is_submit==1) and 'No' or 'Yes',
-			'batch_no'						: d.batch_no,
-			'serial_no'					 : d.serial_no
+			'batch_no'					: d.batch_no,
+			'serial_no'					: serial_no
 			})
 
 
diff --git a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.txt b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.txt
index 1a2c383..b316bd8 100755
--- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.txt
+++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-03-27 14:36:35',
+		'creation': '2012-04-13 11:56:36',
 		'docstatus': 0,
-		'modified': '2012-03-27 14:36:35',
+		'modified': '2012-05-09 14:25:12',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -22,7 +22,7 @@
 		'section_style': u'Tray',
 		'server_code_error': u' ',
 		'show_in_menu': 0,
-		'version': 78
+		'version': 1
 	},
 
 	# These values are common for all DocField
@@ -301,6 +301,16 @@
 
 	# DocField
 	{
+		'doctype': u'DocField',
+		'fieldname': u'rejected_serial_no',
+		'fieldtype': u'Text',
+		'label': u'Rejected Serial No',
+		'permlevel': 0,
+		'print_hide': 1
+	},
+
+	# DocField
+	{
 		'colour': u'White:FFF',
 		'doctype': u'DocField',
 		'fieldname': u'batch_no',
@@ -579,4 +589,4 @@
 		'permlevel': 0,
 		'print_hide': 1
 	}
-]
+]
\ No newline at end of file
diff --git a/erpnext/stock/doctype/sales_bom/sales_bom.js b/erpnext/stock/doctype/sales_bom/sales_bom.js
index 7d857a2..275af6a 100644
--- a/erpnext/stock/doctype/sales_bom/sales_bom.js
+++ b/erpnext/stock/doctype/sales_bom/sales_bom.js
@@ -23,7 +23,7 @@
 
 cur_frm.cscript.refresh = function(doc, cdt, cdn) {
 	if(!doc.__islocal) {
-		hide_field('new_item_code');
+		get_field(doc.doctype, 'new_item_code', doc.name).permlevel = 1;
 	}
 }
 
@@ -45,4 +45,4 @@
 
 cur_frm.cscript.find_sales_bom = function(doc, dt, dn) {
   $c_obj(make_doclist(dt,dn), 'check_duplicate', 1, '');
-}
\ No newline at end of file
+}
diff --git a/erpnext/stock/doctype/sales_bom/sales_bom.py b/erpnext/stock/doctype/sales_bom/sales_bom.py
index 7dea692..437c91e 100644
--- a/erpnext/stock/doctype/sales_bom/sales_bom.py
+++ b/erpnext/stock/doctype/sales_bom/sales_bom.py
@@ -62,13 +62,9 @@
 			if d.is_main_item == 'Yes':
 				is_main_item.append(d.item_code)
 			# Check that Sales Bom Item cannot be child of Sales Bom.
-			if sql("select name from `tabSales BOM` where name = '%s' " % d.item_code):
-				msgprint("Sales Bom Item " + d.item_code +" cannot be child item.")
+			if d.item_code == self.doc.new_item_code:
+				msgprint("Sales Bom Item " + d.new_item_code +" cannot be child item.")
 				raise Exception
-			# Check if is_main_item is modified once saved
-			#if not self.doc.fields.get('__islocal') and d.is_main_item == "Yes" and cstr(d.item_code) != cstr(self.doc.new_item_code)[:-3] :
-			#	msgprint("Modifying the main item is not allowed.")
-			#	raise Exception
 		if len(is_main_item) > 1:
 			msgprint('Main item cannot be more than one.')
 			raise Exception , " Validation Error."
@@ -81,8 +77,7 @@
 	# Make Item
 	# ---------
 	def create_new_item(self):
-		i = Document("Item")
-		
+		i = Document("Item")		
 		i.item_code = self.doc.new_item_code
 		i.item_name = self.doc.new_item_name
 		i.name = i.item_code
@@ -106,7 +101,7 @@
 				sql("delete from `tabItem Price` where parent=%s and price_list_name = %s", (i.name, self.doc.price_list))
 				
 				pld = addchild(i,"ref_rate_details", "Item Price")
-				pld.price_list_name = self.doc.price_List
+				pld.price_list_name = self.doc.price_list
 				pld.ref_rate = flt(ref_rate)
 				pld.ref_currency = self.doc.currency
 				pld.save()
@@ -121,14 +116,11 @@
 		i.stock_uom = self.doc.stock_uom 
 		i.item_group = self.doc.item_group
 		
-		# update rates
-		new_rates = {}
-		self.update_ref_rate(i)
 
 		i.item_name = self.doc.new_item_name
 		i.description = self.doc.description
 
-		# set default as 'No' or 0 in Item Master	as per TIC/3456
+		# set default as 'No' or 0
 		i.is_sample_item = 'No'
 		i.is_asset_item = 'No'
 		i.is_purchase_item = 'No'
@@ -138,8 +130,10 @@
 		i.inspection_required = 'No'
 		i.has_serial_no = 'No'
 		i.lead_time_days = flt(0)
+		# update rates
+		self.update_ref_rate(i)
 		i.save()
-		msgprint("Items updated successfully.")
+		msgprint("Items: %s updated successfully. To update more details open and edit item master" % self.doc.new_item_code)
 
 
 	def validate(self):
diff --git a/erpnext/stock/doctype/stock_ledger/stock_ledger.py b/erpnext/stock/doctype/stock_ledger/stock_ledger.py
index 1d3aabe..9ee1f62 100644
--- a/erpnext/stock/doctype/stock_ledger/stock_ledger.py
+++ b/erpnext/stock/doctype/stock_ledger/stock_ledger.py
@@ -32,7 +32,7 @@
 
 # -----------------------------------------------------------------------------------------
 
-def get_sr_no_list(sr_nos, qty = 0):
+def get_sr_no_list(sr_nos, qty = 0, item_code = ''):
 	serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n')
 	valid_serial_nos = []
 	for val in serial_nos:
@@ -41,9 +41,8 @@
 				msgprint("You have entered duplicate serial no: %s" % val, raise_exception=1)
 			else:
 				valid_serial_nos.append(val.strip())
-
-	if qty > 0 and cstr(sr_nos).strip() and len(valid_serial_nos) != flt(qty):
-		msgprint("Please enter serial nos for all "+ cstr(qty) + " quantity", raise_exception = 1)
+	if qty and cstr(sr_nos).strip() and len(valid_serial_nos) != abs(qty):
+		msgprint("Please enter serial nos for "+ cstr(abs(qty)) + " quantity against item code: " + item_code , raise_exception = 1)
 	return valid_serial_nos
 
 class DocType:
@@ -92,36 +91,46 @@
 				if is_stock_item != 'Yes':
 					msgprint("Serial No is not required for non-stock item: %s" % d.item_code, raise_exception=1)
 				elif ar_required != 'Yes':
-					msgprint("If serial no required, please select 'Yes' in 'Has Serial No' in Item :" + d.item_code + ', otherwise please remove serial no', raise_exception=1)
+					msgprint("If serial no required, please select 'Yes' in 'Has Serial No' in Item :" + d.item_code + \
+						', otherwise please remove serial no', raise_exception=1)
 			elif ar_required == 'Yes' and not d.serial_no:
 				msgprint("Serial no is mandatory for item: "+ d.item_code, raise_exception = 1)
 
+			# validate rejected serial nos
+			if fname == 'purchase_receipt_details' and d.rejected_qty and ar_required == 'Yes' and not d.rejected_serial_no:
+				msgprint("Rejected serial no is mandatory for rejected qty of item: "+ d.item_code, raise_exception = 1)
+				
+				
+
 
 
 	# -------------------
 	# get serial no list
 	# -------------------
-	def get_sr_no_list(self, sr_nos, qty = 0):
-		return get_sr_no_list(sr_nos, qty)
+	def get_sr_no_list(self, sr_nos, qty = 0, item_code = ''):
+		return get_sr_no_list(sr_nos, qty, item_code)
 
 	# ---------------------
 	# set serial no values
 	# ---------------------
 	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)
+		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.valuation_rate or d.incoming_rate
 		s.item_code					=	d.item_code
+		s.item_name					=	d.item_name
 		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.address_display			=	obj.doc.address_display or obj.doc.supplier_address
 		s.warehouse					=	d.warehouse or d.t_warehouse
 		s.docstatus					=	0
 		s.status					=	'In Store'
@@ -184,7 +193,7 @@
 		s.delivery_time					=	 obj.doc.posting_time
 		s.customer						=	 obj.doc.customer
 		s.customer_name					=	 obj.doc.customer_name
-		s.delivery_address			 	=	 obj.doc.delivery_address
+		s.delivery_address			 	=	 obj.doc.address_display
 		s.territory						=	 obj.doc.territory
 		s.warranty_expiry_date	 		=	 s.warranty_period and add_days(cstr(obj.doc.posting_date), s.warranty_period) or ''
 		s.docstatus						=	 1
@@ -220,6 +229,13 @@
 					else:
 						self.update_serial_delivery_details(obj, d, serial_no, is_submit)
 
+			if fname == 'purchase_receipt_details' and d.rejected_qty and d.rejected_serial_no:
+				serial_nos = self.get_sr_no_list(d.rejected_serial_no)
+				for a in serial_nos:
+					self.update_serial_purchase_details(obj, d, a, is_submit)
+				
+				
+
 
 	# -------------
 	# update stock
@@ -227,21 +243,22 @@
 	def update_stock(self, values, is_amended = 'No'):
 		for v in values:
 			sle_id, serial_nos = '', ''
-
 			# get serial nos
 			if v["serial_no"]:
-				serial_nos = self.get_sr_no_list(v["serial_no"], v['actual_qty'])
+				serial_nos = self.get_sr_no_list(v["serial_no"], v['actual_qty'], v['item_code'])
 
 			# reverse quantities for cancel
 			if v['is_cancelled'] == 'Yes':
 				v['actual_qty'] = -flt(v['actual_qty'])
 				# cancel matching entry
-				sql("update `tabStock Ledger Entry` set is_cancelled='Yes' where voucher_no=%s and voucher_type=%s", (v['voucher_no'], v['voucher_type']))
+				sql("update `tabStock Ledger Entry` set is_cancelled='Yes' where voucher_no=%s \
+					and voucher_type=%s", (v['voucher_no'], v['voucher_type']))
 
 			if v["actual_qty"]:
 				sle_id = self.make_entry(v)
 
-			get_obj('Warehouse', v["warehouse"]).update_bin(flt(v["actual_qty"]), 0, 0, 0, 0, v["item_code"], v["posting_date"], sle_id, v["posting_time"], '', v["is_cancelled"],v["voucher_type"],v["voucher_no"], is_amended)
+			get_obj('Warehouse', v["warehouse"]).update_bin(flt(v["actual_qty"]), 0, 0, 0, 0, v["item_code"], \
+				v["posting_date"], sle_id, v["posting_time"], '', v["is_cancelled"],v["voucher_type"],v["voucher_no"], is_amended)
 
 
 	# -----------