Fixed logic for reserved qty for subcontract and production and written a patch (#13396)

* Fixed logic for reserved qty for subcontract and production and written a patch

* repost reserved qty for filtered bins
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 36e68ca..410636f 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -497,4 +497,5 @@
 erpnext.patches.v10_0.update_reserved_qty_for_purchase_order
 erpnext.patches.v10_0.update_hub_connector_domain
 erpnext.patches.v10_0.set_student_party_type
-erpnext.patches.v10_0.update_project_in_sle
\ No newline at end of file
+erpnext.patches.v10_0.update_project_in_sle
+erpnext.patches.v10_0.fix_reserved_qty_for_sub_contract
\ No newline at end of file
diff --git a/erpnext/patches/v10_0/fix_reserved_qty_for_sub_contract.py b/erpnext/patches/v10_0/fix_reserved_qty_for_sub_contract.py
new file mode 100644
index 0000000..8a45638
--- /dev/null
+++ b/erpnext/patches/v10_0/fix_reserved_qty_for_sub_contract.py
@@ -0,0 +1,29 @@
+# Copyright (c) 2017, Frappe and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+from erpnext.stock.utils import get_bin
+
+def execute():
+	for d in frappe.db.sql("""
+		select distinct rm_item_code, reserve_warehouse
+		from `tabPurchase Order Item Supplied`
+		where docstatus=1 and reserve_warehouse is not null and reserve_warehouse != ''"""):
+
+		try:
+			bin_doc = get_bin(d[0], d[1])
+			bin_doc.update_reserved_qty_for_sub_contracting()
+		except:
+			pass
+
+	for d in frappe.db.sql("""select distinct item_code, source_warehouse
+		from `tabProduction Order Item`
+		where docstatus=1 and transferred_qty > required_qty
+			and source_warehouse is not null and source_warehouse != ''""", as_list=1):
+
+		try:
+			bin_doc = get_bin(d[0], d[1])
+			bin_doc.update_reserved_qty_for_production()
+		except:
+			pass
\ No newline at end of file
diff --git a/erpnext/stock/doctype/bin/bin.py b/erpnext/stock/doctype/bin/bin.py
index 430d9fb..eef405d 100644
--- a/erpnext/stock/doctype/bin/bin.py
+++ b/erpnext/stock/doctype/bin/bin.py
@@ -76,14 +76,16 @@
 	def update_reserved_qty_for_production(self):
 		'''Update qty reserved for production from Production Item tables
 			in open production orders'''
-		self.reserved_qty_for_production = frappe.db.sql('''select sum(required_qty - transferred_qty)
+		self.reserved_qty_for_production = frappe.db.sql('''
+			select sum(item.required_qty - item.transferred_qty)
 			from `tabProduction Order` pro, `tabProduction Order Item` item
 			where
 				item.item_code = %s
 				and item.parent = pro.name
 				and pro.docstatus = 1
 				and item.source_warehouse = %s
-				and pro.status not in ("Stopped", "Completed")''', (self.item_code, self.warehouse))[0][0]
+				and pro.status not in ("Stopped", "Completed")
+				and item.required_qty > item.transferred_qty''', (self.item_code, self.warehouse))[0][0]
 
 		self.set_projected_qty()
 
@@ -123,7 +125,12 @@
 				and po.per_received < 100
 		""", (self.item_code))[0][0]
 
-		self.db_set('reserved_qty_for_sub_contract', (reserved_qty_for_sub_contract - materials_transferred))
+		if reserved_qty_for_sub_contract > materials_transferred:
+			reserved_qty_for_sub_contract = reserved_qty_for_sub_contract - materials_transferred
+		else:
+			reserved_qty_for_sub_contract = 0
+
+		self.db_set('reserved_qty_for_sub_contract', reserved_qty_for_sub_contract)
 		self.set_projected_qty()
 		self.db_set('projected_qty', self.projected_qty)