merged from master
diff --git a/erpnext/accounts/DocType Mapper/Delivery Note-Receivable Voucher/Delivery Note-Receivable Voucher.txt b/erpnext/accounts/DocType Mapper/Delivery Note-Receivable Voucher/Delivery Note-Receivable Voucher.txt
index c06c3b8..40cdad3 100644
--- a/erpnext/accounts/DocType Mapper/Delivery Note-Receivable Voucher/Delivery Note-Receivable Voucher.txt
+++ b/erpnext/accounts/DocType Mapper/Delivery Note-Receivable Voucher/Delivery Note-Receivable Voucher.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:34',
 		'docstatus': 0,
-		'modified': '2011-09-14 12:36:24',
+		'modified': '2011-09-13 17:35:54',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -47,7 +47,7 @@
 	# Field Mapper Detail
 	{
 		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: (flt(obj.amount) - flt(obj.billed_amt)) / flt(obj.basic_rate)',
+		'from_field': 'eval: obj.basic_rate and (flt(obj.amount) - flt(obj.billed_amt)) / flt(obj.basic_rate) or obj.qty',
 		'map': 'Yes',
 		'match_id': 1,
 		'to_field': 'qty'
@@ -128,28 +128,10 @@
 	# Field Mapper Detail
 	{
 		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: flt(obj.qty) - flt(obj.billed_qty)',
+		'from_field': 'serial_no',
 		'map': 'Yes',
 		'match_id': 1,
-		'to_field': 'qty'
-	},
-
-	# Field Mapper Detail
-	{
-		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: (flt(obj.qty) - flt(obj.billed_qty)) * flt(obj.basic_rate)',
-		'map': 'Yes',
-		'match_id': 1,
-		'to_field': 'amount'
-	},
-
-	# Field Mapper Detail
-	{
-		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: (flt(obj.qty) - flt(obj.billed_qty)) * flt(obj.export_rate)',
-		'map': 'Yes',
-		'match_id': 1,
-		'to_field': 'export_amount'
+		'to_field': 'serial_no'
 	},
 
 	# Table Mapper Detail
@@ -169,7 +151,7 @@
 		'match_id': 1,
 		'to_field': 'entries',
 		'to_table': 'RV Detail',
-		'validation_logic': 'amount > ifnull(billed_amt, 0) and docstatus = 1'
+		'validation_logic': '(ifnull(amount, 0) = 0 or amount > ifnull(billed_amt, 0)) and docstatus = 1'
 	},
 
 	# Table Mapper Detail
diff --git a/erpnext/accounts/DocType Mapper/Purchase Order-Payable Voucher/Purchase Order-Payable Voucher.txt b/erpnext/accounts/DocType Mapper/Purchase Order-Payable Voucher/Purchase Order-Payable Voucher.txt
index 9527347..61ee9b6 100644
--- a/erpnext/accounts/DocType Mapper/Purchase Order-Payable Voucher/Purchase Order-Payable Voucher.txt
+++ b/erpnext/accounts/DocType Mapper/Purchase Order-Payable Voucher/Purchase Order-Payable Voucher.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:35',
 		'docstatus': 0,
-		'modified': '2011-09-14 12:36:24',
+		'modified': '2011-09-13 17:37:09',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -124,7 +124,7 @@
 		'match_id': 1,
 		'to_field': 'entries',
 		'to_table': 'PV Detail',
-		'validation_logic': 'ifnull(billed_qty,0)  < qty'
+		'validation_logic': 'ifnull(billed_qty,0)  < qty and docstatus = 1'
 	},
 
 	# Table Mapper Detail
diff --git a/erpnext/accounts/DocType Mapper/Purchase Receipt-Payable Voucher/Purchase Receipt-Payable Voucher.txt b/erpnext/accounts/DocType Mapper/Purchase Receipt-Payable Voucher/Purchase Receipt-Payable Voucher.txt
index fae1006..b2baa57 100644
--- a/erpnext/accounts/DocType Mapper/Purchase Receipt-Payable Voucher/Purchase Receipt-Payable Voucher.txt
+++ b/erpnext/accounts/DocType Mapper/Purchase Receipt-Payable Voucher/Purchase Receipt-Payable Voucher.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:35',
 		'docstatus': 0,
-		'modified': '2011-09-14 12:36:24',
+		'modified': '2011-09-13 17:27:32',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
diff --git a/erpnext/accounts/DocType Mapper/Sales Order-Receivable Voucher/Sales Order-Receivable Voucher.txt b/erpnext/accounts/DocType Mapper/Sales Order-Receivable Voucher/Sales Order-Receivable Voucher.txt
index ae0aced..ebbdbcd 100644
--- a/erpnext/accounts/DocType Mapper/Sales Order-Receivable Voucher/Sales Order-Receivable Voucher.txt
+++ b/erpnext/accounts/DocType Mapper/Sales Order-Receivable Voucher/Sales Order-Receivable Voucher.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2010-08-08 17:09:36',
 		'docstatus': 0,
-		'modified': '2011-09-14 12:36:25',
+		'modified': '2011-09-13 17:36:31',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -57,7 +57,7 @@
 	# Field Mapper Detail
 	{
 		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: (flt(obj.amount) - flt(obj.billed_amt))/flt(obj.basic_rate)',
+		'from_field': 'eval: obj.basic_rate and (flt(obj.amount) - flt(obj.billed_amt))/flt(obj.basic_rate) or obj.qty',
 		'map': 'Yes',
 		'match_id': 1,
 		'to_field': 'qty'
@@ -136,33 +136,6 @@
 		'to_field': 'incentives'
 	},
 
-	# Field Mapper Detail
-	{
-		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: flt(obj.qty) - flt(obj.billed_qty)',
-		'map': 'Yes',
-		'match_id': 1,
-		'to_field': 'qty'
-	},
-
-	# Field Mapper Detail
-	{
-		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: (flt(obj.qty) - flt(obj.billed_qty)) * flt(obj.basic_rate)',
-		'map': 'Yes',
-		'match_id': 1,
-		'to_field': 'amount'
-	},
-
-	# Field Mapper Detail
-	{
-		'doctype': 'Field Mapper Detail',
-		'from_field': 'eval: (flt(obj.qty) - flt(obj.billed_qty)) * flt(obj.export_rate)',
-		'map': 'Yes',
-		'match_id': 1,
-		'to_field': 'export_amount'
-	},
-
 	# Table Mapper Detail
 	{
 		'doctype': 'Table Mapper Detail',
@@ -180,7 +153,7 @@
 		'match_id': 1,
 		'to_field': 'entries',
 		'to_table': 'RV Detail',
-		'validation_logic': 'docstatus = 1'
+		'validation_logic': '(ifnull(amount, 0) = 0 or amount > ifnull(billed_amt, 0)) and docstatus = 1'
 	},
 
 	# Table Mapper Detail
diff --git a/erpnext/patches/delivery_billing_status_patch.py b/erpnext/patches/delivery_billing_status_patch.py
index 2f95013..1fcd8bf 100644
--- a/erpnext/patches/delivery_billing_status_patch.py
+++ b/erpnext/patches/delivery_billing_status_patch.py
@@ -1,32 +1,43 @@
 import webnotes
 sql = webnotes.conn.sql
 
+test=1
+
+# Update SO and DN Detail 
+#--------------------------
+def update_delivered_billed_qty():
+	# update billed amt in item table in so and dn
+	sql("""	update `tabSales Order Detail` so
+		set billed_amt = (select sum(amount) from `tabRV Detail` where `so_detail`= so.name and docstatus=1 and parent not like 'old%%'),
+		delivered_qty = (select sum(qty) from `tabDelivery Note Detail` where `prevdoc_detail_docname`= so.name and docstatus=1 and parent not like 'old%%'), 
+		modified = now()
+		where docstatus = 1
+	""")
+
+	sql(""" update `tabDelivery Note Detail` dn
+		set billed_amt = (select sum(amount) from `tabRV Detail` where `dn_detail`= dn.name and docstatus=1 and parent not like 'old%%'), 
+		modified = now()
+		where docstatus = 1
+	""")
+
 # update SO
 #---------------
 def update_percent():
-	so = sql("select name from 	`tabSales Order` where docstatus = 1")
-	for d in so:
-		sql("""
-			update 
-				`tabSales Order` 
-			set 
-				per_delivered = (select sum(if(qty > ifnull(delivered_qty, 0), delivered_qty, qty))/sum(qty)*100 from `tabSales Order Detail` where parent='%s'), 
-				per_billed = (select sum(if(qty > ifnull(billed_qty, 0), billed_qty, qty))/sum(qty)*100 from `tabSales Order Detail` where parent='%s') 
-			where 
-				name='%s'""" % (d[0], d[0], d[0]))
-
+	# calculate % billed based on item table
+	sql("""	update `tabSales Order` so
+		set per_delivered = (select sum(if(qty > ifnull(delivered_qty, 0), delivered_qty, qty))/sum(qty)*100 from `tabSales Order Detail` where parent=so.name), 
+		per_billed = (select sum(if(amount > ifnull(billed_amt, 0), billed_amt, amount))/sum(amount)*100 from `tabSales Order Detail` where parent = so.name), 
+		modified = now()
+		where docstatus = 1
+	""")
+		
 	# update DN	
-	# ---------
-	dn = sql("select name from 	`tabDelivery Note` where docstatus = 1")
-	for d in dn:
-		sql("""
-			update 
-				`tabDelivery Note` 
-			set 
-				per_billed = (select sum(if(qty > ifnull(billed_qty, 0), billed_qty, qty))/sum(qty)*100 from `tabDelivery Note Detail` where parent='%s') 
-			where 
-				name='%s'""" % (d[0], d[0]))
-	
+	# ---------	
+	sql("""	update `tabDelivery Note` dn
+		set per_billed = (select sum(if(amount > ifnull(billed_amt, 0), billed_amt, amount))/sum(amount)*100 from `tabDelivery Note Detail` where parent = dn.name), 
+		modified = now()
+		where docstatus=1
+	""")
 
 # update delivery/billing status 
 #-------------------------------
@@ -39,5 +50,6 @@
 			if(per_billed >= 99.99, 'Fully Billed', 'Partly Billed'))""")
 			
 def run_patch():
+	update_delivered_billed_qty()
 	update_percent()
 	update_status()
diff --git a/erpnext/patches/patch.py b/erpnext/patches/patch.py
index a98143d..8ced1a3 100644
--- a/erpnext/patches/patch.py
+++ b/erpnext/patches/patch.py
@@ -1,7 +1,7 @@
 # REMEMBER to update this
 # ========================
 
-last_patch = 363
+last_patch = 365
 
 #-------------------------------------------
 
@@ -328,3 +328,15 @@
 		for d in mappers:
 			if d[0] and d[1]:
 				reload_doc(d[1].lower(), 'DocType Mapper', d[0])
+	elif patch_no == 364:
+		sql("""delete from `tabField Mapper Detail` 
+			where to_field in ('qty', 'amount', 'export_amount') 
+			and parent in ('Sales Order-Receivable Voucher', 'Delivery Note-Receivable Voucher')
+		""")
+		mappers = sql("select name, module from `tabDocType Mapper`")
+		for d in mappers:
+			if d[0] and d[1]:
+				reload_doc(d[1].lower(), 'DocType Mapper', d[0])
+	elif patch_no == 365:
+		from patches.delivery_billing_status_patch import run_patch
+		run_patch()
diff --git a/erpnext/selling/DocType Mapper/Delivery Note-Installation Note/Delivery Note-Installation Note.txt b/erpnext/selling/DocType Mapper/Delivery Note-Installation Note/Delivery Note-Installation Note.txt
index 67abbdc..e45c487 100644
--- a/erpnext/selling/DocType Mapper/Delivery Note-Installation Note/Delivery Note-Installation Note.txt
+++ b/erpnext/selling/DocType Mapper/Delivery Note-Installation Note/Delivery Note-Installation Note.txt
@@ -5,8 +5,8 @@
 	{
 		'creation': '2010-08-08 17:09:34',
 		'docstatus': 0,
-		'modified': '2011-05-17 11:42:57',
-		'modified_by': 'umair@iwebnotes.com',
+		'modified': '2011-09-13 17:28:37',
+		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
 
@@ -31,7 +31,7 @@
 
 	# These values are common for all DocType Mapper
 	{
-		'doctype': 'DocType Mapper',
+		'doctype': u'DocType Mapper',
 		'from_doctype': 'Delivery Note',
 		'module': 'Selling',
 		'name': '__common__',
@@ -41,7 +41,7 @@
 
 	# DocType Mapper, Delivery Note-Installation Note
 	{
-		'doctype': 'DocType Mapper',
+		'doctype': u'DocType Mapper',
 		'name': 'Delivery Note-Installation Note'
 	},
 
@@ -111,6 +111,14 @@
 		'to_field': 'prevdoc_date'
 	},
 
+	# Field Mapper Detail
+	{
+		'doctype': 'Field Mapper Detail',
+		'from_field': 'serial_no',
+		'match_id': 1,
+		'to_field': 'serial_no'
+	},
+
 	# Table Mapper Detail
 	{
 		'doctype': 'Table Mapper Detail',
diff --git a/erpnext/stock/DocType Mapper/Purchase Order-Purchase Receipt/Purchase Order-Purchase Receipt.txt b/erpnext/stock/DocType Mapper/Purchase Order-Purchase Receipt/Purchase Order-Purchase Receipt.txt
index 54e272a..f561c15 100644
--- a/erpnext/stock/DocType Mapper/Purchase Order-Purchase Receipt/Purchase Order-Purchase Receipt.txt
+++ b/erpnext/stock/DocType Mapper/Purchase Order-Purchase Receipt/Purchase Order-Purchase Receipt.txt
@@ -5,8 +5,8 @@
 	{
 		'creation': '2010-08-08 17:09:35',
 		'docstatus': 0,
-		'modified': '2011-05-13 12:42:57',
-		'modified_by': 'umair@iwebnotes.com',
+		'modified': '2011-09-13 17:26:55',
+		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
 
@@ -31,7 +31,7 @@
 
 	# These values are common for all DocType Mapper
 	{
-		'doctype': 'DocType Mapper',
+		'doctype': u'DocType Mapper',
 		'from_doctype': 'Purchase Order',
 		'module': 'Stock',
 		'name': '__common__',
@@ -41,7 +41,7 @@
 
 	# DocType Mapper, Purchase Order-Purchase Receipt
 	{
-		'doctype': 'DocType Mapper',
+		'doctype': u'DocType Mapper',
 		'name': 'Purchase Order-Purchase Receipt'
 	},
 
diff --git a/erpnext/stock/DocType Mapper/Receivable Voucher-Delivery Note/Receivable Voucher-Delivery Note.txt b/erpnext/stock/DocType Mapper/Receivable Voucher-Delivery Note/Receivable Voucher-Delivery Note.txt
index d0d4c17..7443731 100755
--- a/erpnext/stock/DocType Mapper/Receivable Voucher-Delivery Note/Receivable Voucher-Delivery Note.txt
+++ b/erpnext/stock/DocType Mapper/Receivable Voucher-Delivery Note/Receivable Voucher-Delivery Note.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2010-12-14 17:56:41',
+		'creation': '2010-12-15 08:39:22',
 		'docstatus': 0,
-		'modified': '2011-09-14 12:36:24',
+		'modified': '2011-09-13 17:26:01',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -110,10 +110,10 @@
 	# Field Mapper Detail
 	{
 		'doctype': 'Field Mapper Detail',
-		'from_field': 'customer_address',
+		'from_field': 'serial_no',
 		'map': 'Yes',
-		'match_id': 0,
-		'to_field': 'delivery_address'
+		'match_id': 1,
+		'to_field': 'serial_no'
 	},
 
 	# Table Mapper Detail
diff --git a/erpnext/stock/doctype/stock_ledger/stock_ledger.py b/erpnext/stock/doctype/stock_ledger/stock_ledger.py
index dca9754..ff6de70 100644
--- a/erpnext/stock/doctype/stock_ledger/stock_ledger.py
+++ b/erpnext/stock/doctype/stock_ledger/stock_ledger.py
@@ -51,7 +51,8 @@
 	# -----------------------------
 	def validate_serial_no_warehouse(self, obj, fname):
 		for d in getlist(obj.doclist, fname):
-			if d.serial_no:
+			wh = d.warehouse or d.s_warehouse
+			if d.serial_no and wh:
 				serial_nos = self.get_sr_no_list(d.serial_no)
 				for s in serial_nos:
 					s = s.strip()
@@ -60,8 +61,8 @@
 						msgprint("Serial No %s does not exists"%s, raise_exception = 1)
 					elif not sr_war[0][0]:
 						msgprint("Warehouse not mentioned in the Serial No <b>%s</b>" % s, raise_exception = 1)
-					elif (d.warehouse and sr_war[0][0] != d.warehouse) or (d.s_warehouse and sr_war[0][0] != d.s_warehouse):
-						msgprint("Serial No : %s for Item : %s doesn't exists in Warehouse : %s" % (s, d.item_code, d.warehouse or d.s_warehouse), raise_exception = 1)
+					elif sr_war[0][0] != wh:
+						msgprint("Serial No : %s for Item : %s doesn't exists in Warehouse : %s" % (s, d.item_code, wh), raise_exception = 1)
 
 
 	# ------------------------------------
@@ -133,8 +134,10 @@
 		else:
 			if exists and exists[0][1] == 'Delivered' and exists[0][2] != 2:
 				msgprint("Serial No: %s is already delivered, you can not cancel the document." % serial_no, raise_exception=1)
-			elif purpose in ['Material Transfer', 'Sales Return']:
-				sql("update `tabSerial No` set status = '%s', purchase_document_type = '', purchase_document_no = '', warehouse = '%s' where name = '%s'" % (purpose == 'Material Transfer' and 'In Store' or 'Delivered', d.s_warehouse, serial_no))				
+			elif purpose == 'Material Transfer':
+				sql("update `tabSerial No` set status = 'In Store', purchase_document_type = '', purchase_document_no = '', warehouse = '%s' where name = '%s'" % (d.s_warehouse, serial_no))				
+			elif purpose == 'Sales Return':
+				sql("update `tabSerial No` set status = 'Delivered', purchase_document_type = '', purchase_document_no = '' where name = '%s'" % serial_no)
 			else:
 				sql("update `tabSerial No` set docstatus = 2, status = 'Not in Use', purchase_document_type = '', purchase_document_no = '', purchase_date = '', purchase_rate = '', supplier = null, supplier_name = '', supplier_address = '', warehouse = '' where name = '%s'" % serial_no)