[#2164] New field names in DN, fix field name refs, Patch added
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index a9586b6..5201e5e 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -131,7 +131,7 @@
 				'keyword':'Delivered',
 				'second_source_dt': 'Delivery Note Item',
 				'second_source_field': 'qty',
-				'second_join_field': 'prevdoc_detail_docname',
+				'second_join_field': 'so_detail',
 				'overflow_type': 'delivery',
 				'extra_cond': """ and exists(select name from `tabSales Invoice` 
 					where name=`tabSales Invoice Item`.parent and ifnull(update_stock, 0) = 1)"""
@@ -633,7 +633,7 @@
 				"parent": "against_sales_invoice",
 				"serial_no": "serial_no",
 				"sales_order": "against_sales_order",
-				"so_detail": "prevdoc_detail_docname"
+				"so_detail": "so_detail"
 			},
 			"postprocess": update_item
 		},
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index d173abe..5d77814 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -334,8 +334,8 @@
 				# But in this case reserved qty should only be reduced by 10 and not 12
 
 				already_delivered_qty = self.get_already_delivered_qty(self.name,
-					d.against_sales_order, d.prevdoc_detail_docname)
-				so_qty, reserved_warehouse = self.get_so_qty_and_warehouse(d.prevdoc_detail_docname)
+					d.against_sales_order, d.so_detail)
+				so_qty, reserved_warehouse = self.get_so_qty_and_warehouse(d.so_detail)
 
 				if already_delivered_qty + d.qty > so_qty:
 					reserved_qty_for_main_item = -(so_qty - already_delivered_qty)
@@ -377,7 +377,7 @@
 
 	def get_already_delivered_qty(self, dn, so, so_detail):
 		qty = frappe.db.sql("""select sum(qty) from `tabDelivery Note Item`
-			where prevdoc_detail_docname = %s and docstatus = 1
+			where so_detail = %s and docstatus = 1
 			and against_sales_order = %s
 			and parent != %s""", (so_detail, so, dn))
 		return qty and flt(qty[0][0]) or 0.0
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 52dd66c..1a45c59 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -84,3 +84,4 @@
 erpnext.patches.v4_2.seprate_manufacture_and_repack
 erpnext.patches.v4_2.party_model
 erpnext.patches.v5_0.update_frozen_accounts_permission_role
+erpnext.patches.v5_0.update_dn_against_doc_fields
diff --git a/erpnext/patches/v5_0/update_dn_against_doc_fields.py b/erpnext/patches/v5_0/update_dn_against_doc_fields.py
new file mode 100644
index 0000000..e8780a7
--- /dev/null
+++ b/erpnext/patches/v5_0/update_dn_against_doc_fields.py
@@ -0,0 +1,13 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+import frappe
+
+def execute():
+	frappe.reload_doc('stock', 'doctype', 'delivery_note_item')
+
+	frappe.db.sql("""update `tabDelivery Note Item` set so_detail = prevdoc_detail_docname
+		where ifnull(against_sales_order, '') != ''""")
+
+	frappe.db.sql("""update `tabDelivery Note Item` set si_detail = prevdoc_detail_docname
+		where ifnull(against_sales_invoice, '') != ''""")
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index b24d297..e59d209 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -294,7 +294,7 @@
 			"doctype": "Delivery Note Item",
 			"field_map": {
 				"rate": "rate",
-				"name": "prevdoc_detail_docname",
+				"name": "so_detail",
 				"parent": "against_sales_order",
 			},
 			"postprocess": update_item,
diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py
index c7ec893..8195a1b 100644
--- a/erpnext/selling/doctype/sales_order/test_sales_order.py
+++ b/erpnext/selling/doctype/sales_order/test_sales_order.py
@@ -85,7 +85,7 @@
 		dn = frappe.get_doc(frappe.copy_doc(dn_test_records[0]))
 		dn.get("delivery_note_details")[0].item_code = so.get("sales_order_details")[0].item_code
 		dn.get("delivery_note_details")[0].against_sales_order = so.name
-		dn.get("delivery_note_details")[0].prevdoc_detail_docname = so.get("sales_order_details")[0].name
+		dn.get("delivery_note_details")[0].so_detail = so.get("sales_order_details")[0].name
 		if delivered_qty:
 			dn.get("delivery_note_details")[0].qty = delivered_qty
 		dn.insert()
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 51413c5..67a33c9 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -25,7 +25,7 @@
 		self.status_updater = [{
 			'source_dt': 'Delivery Note Item',
 			'target_dt': 'Sales Order Item',
-			'join_field': 'prevdoc_detail_docname',
+			'join_field': 'so_detail',
 			'target_field': 'delivered_qty',
 			'target_parent_dt': 'Sales Order',
 			'target_parent_field': 'per_delivered',
@@ -129,7 +129,7 @@
 				if cint(frappe.defaults.get_global_default('maintain_same_sales_rate')):
 					super(DeliveryNote, self).validate_with_previous_doc(self.tname, {
 						fn[0] + " Item": {
-							"ref_dn_field": "prevdoc_detail_docname",
+							"ref_dn_field": "so_detail",
 							"compare_fields": [["rate", "="]],
 							"is_child_table": True
 						}
@@ -328,7 +328,7 @@
 			"field_map": {
 				"name": "dn_detail",
 				"parent": "delivery_note",
-				"prevdoc_detail_docname": "so_detail",
+				"so_detail": "so_detail",
 				"against_sales_order": "sales_order",
 				"serial_no": "serial_no"
 			},
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
index 9fc9aae..57adf3f 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -373,11 +373,11 @@
    "read_only": 1
   }, 
   {
-   "fieldname": "prevdoc_detail_docname", 
+   "fieldname": "so_detail", 
    "fieldtype": "Data", 
    "hidden": 1, 
    "in_filter": 1, 
-   "label": "Against Document Detail No", 
+   "label": "Against Sales Order Item", 
    "no_copy": 1, 
    "oldfieldname": "prevdoc_detail_docname", 
    "oldfieldtype": "Data", 
@@ -393,7 +393,7 @@
    "fieldtype": "Data", 
    "hidden": 1, 
    "in_filter": 1, 
-   "label": "SI Detail", 
+   "label": "Against Sales Invoice Item", 
    "no_copy": 1, 
    "permlevel": 0, 
    "precision": "", 
@@ -438,7 +438,7 @@
  ], 
  "idx": 1, 
  "istable": 1, 
- "modified": "2014-10-07 18:18:04.375810", 
+ "modified": "2014-10-08 11:11:59.411329", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Delivery Note Item",