Merge pull request #4871 from ShashaQin/patch-27

Improvements about recurring documents
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
index 4a22de9..3bbf082 100755
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
@@ -2472,7 +2472,7 @@
    "width": "50%"
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
@@ -2490,7 +2490,7 @@
    "permlevel": 0, 
    "print_hide": 1, 
    "print_hide_if_no_value": 0, 
-   "read_only": 1, 
+   "read_only": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -2528,6 +2528,32 @@
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
+   "description": "Check to send it via Email, uncheck to generate a draft document only.", 
+   "fieldname": "notify_by_email", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Notify by Email", 
+   "length": 0, 
+   "no_copy": 1, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  },
+  {
+   "allow_on_submit": 1, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "depends_on": "eval:doc.notify_by_email==1", 
    "description": "Enter email id separated by commas, invoice will be mailed automatically on particular date", 
    "fieldname": "notification_email_address", 
    "fieldtype": "Code", 
@@ -2589,7 +2615,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2016-02-22 09:32:04.196291", 
+ "modified": "2016-02-24 16:03:07.975604", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Purchase Invoice", 
@@ -2743,4 +2769,4 @@
  "sort_order": "DESC", 
  "timeline_field": "supplier", 
  "title_field": "title"
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
index ab486aa..7ac8601 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
@@ -3221,7 +3221,7 @@
    "width": "50%"
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
@@ -3239,7 +3239,7 @@
    "permlevel": 0, 
    "print_hide": 1, 
    "print_hide_if_no_value": 0, 
-   "read_only": 1, 
+   "read_only": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -3272,11 +3272,37 @@
    "set_only_once": 0, 
    "unique": 0
   }, 
-  {
+    {
    "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
+   "description": "Check to send it via Email, uncheck to generate a draft document only.", 
+   "fieldname": "notify_by_email", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Notify by Email", 
+   "length": 0, 
+   "no_copy": 1, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 1, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "depends_on": "eval:doc.notify_by_email==1", 
    "description": "Enter email id separated by commas, invoice will be mailed automatically on particular date", 
    "fieldname": "notification_email_address", 
    "fieldtype": "Code", 
@@ -3364,7 +3390,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2016-02-22 09:34:35.695558", 
+ "modified": "2016-02-24 15:56:43.416423", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Sales Invoice", 
@@ -3459,4 +3485,4 @@
  "sort_order": "DESC", 
  "timeline_field": "customer", 
  "title_field": "title"
-}
\ No newline at end of file
+}
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index fb1781a..9c518d1 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -2523,7 +2523,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
@@ -2541,7 +2541,7 @@
    "permlevel": 0, 
    "print_hide": 1, 
    "print_hide_if_no_value": 0, 
-   "read_only": 1, 
+   "read_only": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -2578,6 +2578,32 @@
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
+   "description": "Check to send it via Email, uncheck to generate a draft document only.", 
+   "fieldname": "notify_by_email", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Notify by Email", 
+   "length": 0, 
+   "no_copy": 1, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 1, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "depends_on": "eval:doc.notify_by_email==1", 
    "description": "Enter email id separated by commas, order will be mailed automatically on particular date", 
    "fieldname": "notification_email_address", 
    "fieldtype": "Code", 
@@ -2638,7 +2664,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-02-22 09:32:53.191526", 
+ "modified": "2016-02-25 09:51:38.479762", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order", 
@@ -2772,4 +2798,4 @@
  "sort_order": "DESC", 
  "timeline_field": "supplier", 
  "title_field": "title"
-}
\ No newline at end of file
+}
diff --git a/erpnext/controllers/recurring_document.py b/erpnext/controllers/recurring_document.py
index 3ecbe5f..d029132 100644
--- a/erpnext/controllers/recurring_document.py
+++ b/erpnext/controllers/recurring_document.py
@@ -38,18 +38,19 @@
 
 	recurring_documents = frappe.db.sql("""select name, recurring_id
 		from `tab{0}` where is_recurring=1
-		and docstatus=1 and next_date=%s
+		and (docstatus=1 or docstatus=0) and next_date=%s
 		and next_date <= ifnull(end_date, '2199-12-31') {1}""".format(doctype, condition), next_date)
 
 	exception_list = []
 	for ref_document, recurring_id in recurring_documents:
 		if not frappe.db.sql("""select name from `tab%s`
-				where %s=%s and recurring_id=%s and docstatus=1"""
+				where %s=%s and recurring_id=%s and (docstatus=1 or docstatus=0)"""
 				% (doctype, date_field, '%s', '%s'), (next_date, recurring_id)):
 			try:
 				reference_doc = frappe.get_doc(doctype, ref_document)
 				new_doc = make_new_document(reference_doc, date_field, next_date)
-				send_notification(new_doc)
+				if reference_doc.notify_by_email:
+					send_notification(new_doc)
 				if commit:
 					frappe.db.commit()
 			except:
@@ -92,7 +93,8 @@
 		date_field: posting_date,
 		"from_date": from_date,
 		"to_date": to_date,
-		"fiscal_year": get_fiscal_year(posting_date)[0]
+		"fiscal_year": get_fiscal_year(posting_date)[0],
+        	"next_date": get_next_date(from_date, mcount,cint(reference_doc.repeat_on_day_of_month))
 	})
 
 	# copy document fields
@@ -110,7 +112,11 @@
 
 	new_document.run_method("on_recurring", reference_doc=reference_doc)
 
-	new_document.submit()
+	if not reference_doc.notify_by_email:
+		new_document.docstatus=0
+		new_document.insert()
+	else:
+		new_document.submit()
 
 	return new_document
 
@@ -181,18 +187,19 @@
 #
 
 def validate_notification_email_id(doc):
-	if doc.notification_email_address:
-		email_list = split_emails(doc.notification_email_address.replace("\n", ""))
+	if doc.notify_by_email:
+		if doc.notification_email_address:
+			email_list = split_emails(doc.notification_email_address.replace("\n", ""))
+	
+			from frappe.utils import validate_email_add
+			for email in email_list:
+				if not validate_email_add(email):
+					throw(_("{0} is an invalid email address in 'Notification \
+						Email Address'").format(email))
 
-		from frappe.utils import validate_email_add
-		for email in email_list:
-			if not validate_email_add(email):
-				throw(_("{0} is an invalid email address in 'Notification \
-					Email Address'").format(email))
-
-	else:
-		frappe.throw(_("'Notification Email Addresses' not specified for recurring %s") \
-			% doc.doctype)
+		else:
+			frappe.throw(_("'Notification Email Addresses' not specified for recurring %s") \
+				% doc.doctype)
 
 def set_next_date(doc, posting_date):
 	""" Set next date on which recurring document will be created"""
@@ -200,7 +207,7 @@
 	if not doc.repeat_on_day_of_month:
 		msgprint(_("Please enter 'Repeat on Day of Month' field value"), raise_exception=1)
 
-	next_date = get_next_date(posting_date, month_map[doc.recurring_type],
+	next_date = doc.next_date or get_next_date(doc.from_date, month_map[doc.recurring_type],
 		cint(doc.repeat_on_day_of_month))
 
 	frappe.db.set(doc, 'next_date', next_date)
diff --git a/erpnext/controllers/tests/test_recurring_document.py b/erpnext/controllers/tests/test_recurring_document.py
index ef8d5fb..0723f3d 100644
--- a/erpnext/controllers/tests/test_recurring_document.py
+++ b/erpnext/controllers/tests/test_recurring_document.py
@@ -112,7 +112,7 @@
 
 	def _test(i):
 		obj.assertEquals(i+1, frappe.db.sql("""select count(*) from `tab%s`
-			where recurring_id=%s and docstatus=1""" % (base_doc.doctype, '%s'),
+			where recurring_id=%s and (docstatus=1 or docstatus=0)""" % (base_doc.doctype, '%s'),
 			(base_doc.recurring_id))[0][0])
 
 		next_date = get_next_date(base_doc.get(date_field), no_of_months,
@@ -121,7 +121,7 @@
 		manage_recurring_documents(base_doc.doctype, next_date=next_date, commit=False)
 
 		recurred_documents = frappe.db.sql("""select name from `tab%s`
-			where recurring_id=%s and docstatus=1 order by name desc"""
+			where recurring_id=%s and (docstatus=1 or docstatus=0) order by name desc"""
 			% (base_doc.doctype, '%s'), (base_doc.recurring_id))
 
 		obj.assertEquals(i+2, len(recurred_documents))
diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json
index 73c5d20..1be2031 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.json
+++ b/erpnext/selling/doctype/sales_order/sales_order.json
@@ -2826,7 +2826,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
@@ -2844,7 +2844,7 @@
    "permlevel": 0, 
    "print_hide": 1, 
    "print_hide_if_no_value": 0, 
-   "read_only": 1, 
+   "read_only": 0, 
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
@@ -2881,6 +2881,32 @@
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring==1", 
+   "description": "Check to send it via Email, uncheck to generate a draft document only.", 
+   "fieldname": "notify_by_email", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Notify by Email", 
+   "length": 0, 
+   "no_copy": 1, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  },
+  {
+   "allow_on_submit": 1, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "depends_on": "eval:doc.notify_by_email==1", 
    "description": "Enter email id separated by commas, order will be mailed automatically on particular date", 
    "fieldname": "notification_email_address", 
    "fieldtype": "Code", 
@@ -2941,7 +2967,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-02-22 09:35:08.094329", 
+ "modified": "2016-02-24 16:10:47.732147", 
  "modified_by": "Administrator", 
  "module": "Selling", 
  "name": "Sales Order", 
@@ -3116,4 +3142,4 @@
  "sort_order": "DESC", 
  "timeline_field": "customer", 
  "title_field": "title"
-}
\ No newline at end of file
+}