Merge pull request #3898 from nabinhait/reserved_qty

[fix][patch] Reserved Qty logic fixed and reposted for existing items
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index a1468e1..9e2bf1d 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -188,7 +188,7 @@
 					reserved_qty_for_main_item = -(so_qty - already_delivered_qty)
 				else:
 					reserved_qty_for_main_item = -flt(d.qty)
-
+					
 			if self.has_product_bundle(d.item_code):
 				for p in self.get("packed_items"):
 					if p.parent_detail_docname == d.name and p.parent_item == d.item_code:
@@ -229,10 +229,12 @@
 			and against_sales_order = %s
 			and parent != %s""", (so_detail, so, current_docname))
 			
-		delivered_via_si = frappe.db.sql("""select sum(qty) from `tabSales Invoice Item`
-			where so_detail = %s and docstatus = 1
-			and sales_order = %s
-			and parent != %s""", (so_detail, so, current_docname))
+		delivered_via_si = frappe.db.sql("""select sum(si_item.qty) 
+			from `tabSales Invoice Item` si_item, `tabSales Invoice` si
+			where si_item.parent = si.name and ifnull(si.update_stock, 0) = 1
+			and si_item.so_detail = %s and si.docstatus = 1 
+			and si_item.sales_order = %s
+			and si.name != %s""", (so_detail, so, current_docname))
 			
 		total_delivered_qty = (flt(delivered_via_dn[0][0]) if delivered_via_dn else 0) \
 			+ (flt(delivered_via_si[0][0]) if delivered_via_si else 0)
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 1a7b209..87a5551 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -137,7 +137,6 @@
 erpnext.patches.v5_0.update_sms_sender
 erpnext.patches.v5_0.set_appraisal_remarks
 erpnext.patches.v5_0.update_time_log_title
-erpnext.patches.v4_2.repost_reserved_qty
 erpnext.patches.v4_2.repost_sle_for_si_with_no_warehouse
 erpnext.patches.v5_0.newsletter
 execute:frappe.delete_doc("DocType", "Chart of Accounts")
@@ -192,3 +191,4 @@
 erpnext.patches.v5_4.cleanup_journal_entry #2015-08-14
 execute:frappe.db.sql("update `tabProduction Order` pro set description = (select description from tabItem where name=pro.production_item) where ifnull(description, '') = ''")
 erpnext.patches.v5_7.item_template_attributes
+erpnext.patches.v4_2.repost_reserved_qty #2015-08-17
diff --git a/erpnext/patches/v4_2/repost_reserved_qty.py b/erpnext/patches/v4_2/repost_reserved_qty.py
index 04dfb77..f91968c 100644
--- a/erpnext/patches/v4_2/repost_reserved_qty.py
+++ b/erpnext/patches/v4_2/repost_reserved_qty.py
@@ -6,7 +6,22 @@
 from erpnext.utilities.repost_stock import update_bin_qty, get_reserved_qty
 
 def execute():
-	for item_code, warehouse in frappe.db.sql("select item_code, warehouse from tabBin where ifnull(reserved_qty, 0) < 0"):
-		update_bin_qty(item_code, warehouse, {
-			"reserved_qty": get_reserved_qty(item_code, warehouse)
-		})
\ No newline at end of file
+	repost_for = frappe.db.sql("""
+		select 
+			distinct item_code, warehouse 
+		from 
+			(
+				(
+					select distinct item_code, warehouse 
+								from `tabSales Order Item` where docstatus=1
+				) UNION (
+					select distinct item_code, warehouse 
+					from `tabPacked Item` where docstatus=1 and parenttype='Sales Order'
+				)
+			) items
+	""")
+	
+	for item_code, warehouse in repost_for:
+			update_bin_qty(item_code, warehouse, {
+				"reserved_qty": get_reserved_qty(item_code, warehouse)
+			})
\ No newline at end of file
diff --git a/erpnext/patches/v5_4/fix_reserved_qty_and_sle_for_packed_items.py b/erpnext/patches/v5_4/fix_reserved_qty_and_sle_for_packed_items.py
index f1632ab..251dd56 100644
--- a/erpnext/patches/v5_4/fix_reserved_qty_and_sle_for_packed_items.py
+++ b/erpnext/patches/v5_4/fix_reserved_qty_and_sle_for_packed_items.py
@@ -3,7 +3,7 @@
 
 from __future__ import unicode_literals
 import frappe
-from erpnext.utilities.repost_stock import update_bin_qty, get_reserved_qty, repost_actual_qty
+from erpnext.utilities.repost_stock import repost_actual_qty
 
 def execute():
 	cancelled_invoices = frappe.db.sql_list("""select name from `tabSales Invoice` 
@@ -19,10 +19,4 @@
 			% (', '.join(['%s']*len(cancelled_invoices))), tuple(cancelled_invoices))
 			
 		for item_code, warehouse in repost_for:
-			repost_actual_qty(item_code, warehouse)
-			
-	for item_code, warehouse in frappe.db.sql("""select distinct item_code, warehouse 
-		from `tabPacked Item` where parenttype = 'Sales Invoice' and docstatus = 1"""):
-			update_bin_qty(item_code, warehouse, {
-				"reserved_qty": get_reserved_qty(item_code, warehouse)
-			})
\ No newline at end of file
+			repost_actual_qty(item_code, warehouse)
\ No newline at end of file