[#2164] Add Delivered Qty to Sales Inv from Delivery Note, fix issues
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 352dfab..a9586b6 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -132,7 +132,9 @@
 				'second_source_dt': 'Delivery Note Item',
 				'second_source_field': 'qty',
 				'second_join_field': 'prevdoc_detail_docname',
-				'overflow_type': 'delivery'
+				'overflow_type': 'delivery',
+				'extra_cond': """ and exists(select name from `tabSales Invoice` 
+					where name=`tabSales Invoice Item`.parent and ifnull(update_stock, 0) = 1)"""
 			})
 
 	def get_portal_page(self):
@@ -627,9 +629,11 @@
 		"Sales Invoice Item": {
 			"doctype": "Delivery Note Item",
 			"field_map": {
-				"name": "prevdoc_detail_docname",
+				"name": "si_detail",
 				"parent": "against_sales_invoice",
-				"serial_no": "serial_no"
+				"serial_no": "serial_no",
+				"sales_order": "against_sales_order",
+				"so_detail": "prevdoc_detail_docname"
 			},
 			"postprocess": update_item
 		},
diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py
index 9825b04..247d4ff 100644
--- a/erpnext/controllers/status_updater.py
+++ b/erpnext/controllers/status_updater.py
@@ -153,16 +153,21 @@
 					args['second_source_condition'] = ""
 					if args.get('second_source_dt') and args.get('second_source_field') \
 							and args.get('second_join_field'):
+						if not args.get("second_source_extra_cond"): 
+							args["second_source_extra_cond"] = ""
+
 						args['second_source_condition'] = """ + ifnull((select sum(%(second_source_field)s)
 							from `tab%(second_source_dt)s`
 							where `%(second_join_field)s`="%(detail_id)s"
-							and (docstatus=1)), 0)""" % args
+							and (`tab%(second_source_dt)s`.docstatus=1) %(second_source_extra_cond)s), 0) """ % args
 
 					if args['detail_id']:
+						if not args.get("extra_cond"): args["extra_cond"] = ""
+
 						frappe.db.sql("""update `tab%(target_dt)s`
 							set %(target_field)s = (select sum(%(source_field)s)
 								from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s"
-								and (docstatus=1 %(cond)s)) %(second_source_condition)s
+								and (docstatus=1 %(cond)s) %(extra_cond)s) %(second_source_condition)s
 							where name='%(detail_id)s'""" % args)
 
 			# get unique transactions to update
@@ -171,12 +176,13 @@
 					args['name'] = name
 
 					# update percent complete in the parent table
-					frappe.db.sql("""update `tab%(target_parent_dt)s`
-						set %(target_parent_field)s = (select sum(if(%(target_ref_field)s >
-							ifnull(%(target_field)s, 0), %(target_field)s,
-							%(target_ref_field)s))/sum(%(target_ref_field)s)*100
-							from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s
-						where name='%(name)s'""" % args)
+					if args.get('target_parent_field'):
+						frappe.db.sql("""update `tab%(target_parent_dt)s`
+							set %(target_parent_field)s = (select sum(if(%(target_ref_field)s >
+								ifnull(%(target_field)s, 0), %(target_field)s,
+								%(target_ref_field)s))/sum(%(target_ref_field)s)*100
+								from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s
+							where name='%(name)s'""" % args)
 
 					# update field
 					if args.get('status_field'):
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index ee8dc0c..51413c5 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -34,6 +34,22 @@
 			'percent_join_field': 'against_sales_order',
 			'status_field': 'delivery_status',
 			'keyword': 'Delivered',
+			'second_source_dt': 'Sales Invoice Item',
+			'second_source_field': 'qty',
+			'second_join_field': 'so_detail',
+			'overflow_type': 'delivery',
+			'second_source_extra_cond': """ and exists(select name from `tabSales Invoice` 
+				where name=`tabSales Invoice Item`.parent and ifnull(update_stock, 0) = 1)"""
+		},
+		{
+			'source_dt': 'Delivery Note Item',
+			'target_dt': 'Sales Invoice Item',
+			'join_field': 'si_detail',
+			'target_field': 'delivered_qty',
+			'target_parent_dt': 'Sales Invoice',
+			'target_ref_field': 'qty',
+			'source_field': 'qty',
+			'percent_join_field': 'against_sales_invoice',
 			'overflow_type': 'delivery'
 		}]
 
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 a5fe469..9fc9aae 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -389,6 +389,18 @@
    "width": "150px"
   }, 
   {
+   "fieldname": "si_detail", 
+   "fieldtype": "Data", 
+   "hidden": 1, 
+   "in_filter": 1, 
+   "label": "SI Detail", 
+   "no_copy": 1, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "read_only": 1
+  }, 
+  {
    "fieldname": "installed_qty", 
    "fieldtype": "Float", 
    "label": "Installed Qty", 
@@ -426,7 +438,7 @@
  ], 
  "idx": 1, 
  "istable": 1, 
- "modified": "2014-09-09 05:35:37.460939", 
+ "modified": "2014-10-07 18:18:04.375810", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Delivery Note Item",