[patch] Fix delivery and billing status of recurring orders
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 43ec83d..efcceb3 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -256,4 +256,5 @@
 erpnext.patches.v6_20x.set_compact_print
 execute:frappe.delete_doc_if_exists("Web Form", "contact") #2016-03-10
 erpnext.patches.v6_20x.remove_fiscal_year_from_holiday_list
-erpnext.patches.v6_24.map_customer_address_to_shipping_address_on_po
\ No newline at end of file
+erpnext.patches.v6_24.map_customer_address_to_shipping_address_on_po
+erpnext.patches.v6_27.fix_recurring_order_status
\ No newline at end of file
diff --git a/erpnext/patches/v6_27/__init__.py b/erpnext/patches/v6_27/__init__.py
new file mode 100644
index 0000000..baffc48
--- /dev/null
+++ b/erpnext/patches/v6_27/__init__.py
@@ -0,0 +1 @@
+from __future__ import unicode_literals
diff --git a/erpnext/patches/v6_27/fix_recurring_order_status.py b/erpnext/patches/v6_27/fix_recurring_order_status.py
new file mode 100644
index 0000000..c67973a
--- /dev/null
+++ b/erpnext/patches/v6_27/fix_recurring_order_status.py
@@ -0,0 +1,51 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	for doc in (
+		{
+			"doctype": "Sales Order",
+			"stock_doctype": "Delivery Note",
+			"invoice_doctype": "Sales Invoice",
+			"stock_doctype_ref_field": "against_sales_order",
+			"invoice_ref_field": "sales_order"
+		},
+		{
+			"doctype": "Purchase Order",
+			"stock_doctype": "Purchase Receipt",
+			"invoice_doctype": "Purchase Invoice",
+			"stock_doctype_ref_field": "prevdoc_docname",
+			"invoice_ref_field": "purchase_order"
+		}):
+		
+		order_list = frappe.db.sql("""select name from `tab{0}` 
+			where docstatus=1 and is_recurring=1 
+			and ifnull(recurring_id, '') != name and creation >= '2016-01-25'"""
+			.format(doc["doctype"]), as_dict=1)
+	
+		for order in order_list:
+			frappe.db.sql("""update `tab{0} Item` 
+				set delivered_qty=0, billed_amt=0 where parent=%s""".format(doc["doctype"]), order.name)
+		
+			# Check against Delivery Note and Purchase Receipt
+			stock_doc_list = frappe.db.sql("""select distinct parent from `tab{0} Item` 
+				where docstatus=1 and ifnull({1}, '')=%s"""
+				.format(doc["stock_doctype"], doc["stock_doctype_ref_field"]), order.name)
+			
+			if stock_doc_list:
+				for dn in stock_doc_list:
+					frappe.get_doc(doc["stock_doctype"], dn[0]).update_qty(update_modified=False)
+		
+			# Check against Invoice
+			invoice_list = frappe.db.sql("""select distinct parent from `tab{0} Item` 
+				where docstatus=1 and ifnull({1}, '')=%s"""
+				.format(doc["invoice_doctype"], doc["invoice_ref_field"]), order.name)
+			
+			if invoice_list:
+				for dn in invoice_list:
+					frappe.get_doc(doc["invoice_doctype"], dn[0]).update_qty(update_modified=False)
+		
+			frappe.get_doc(doc["doctype"], order.name).set_status(update=True, update_modified=False)
\ No newline at end of file