[fix] recurring document logic
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
index 757ae12..c2628c9 100755
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json
@@ -2472,7 +2472,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring && doc.recurring_id === doc.name", 
@@ -2692,7 +2692,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2016-03-18 02:03:39.309675", 
+ "modified": "2016-03-18 13:13:43.694604", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Purchase Invoice", 
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index bbb0cf7..180831e 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -234,8 +234,6 @@
 			reconcile_against_document(lst)
 
 	def on_submit(self):
-		super(PurchaseInvoice, self).on_submit()
-
 		self.check_prev_docstatus()
 
 		frappe.get_doc('Authorization Control').validate_approving_authority(self.doctype,
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
index a9cf9cb..598cfb8 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
@@ -3171,7 +3171,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring && doc.recurring_id === doc.name", 
@@ -3418,7 +3418,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2016-03-18 02:03:34.511447", 
+ "modified": "2016-03-18 13:12:12.430038", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Sales Invoice", 
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 5a29424..2d45abe 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -81,8 +81,6 @@
 		self.update_packing_list()
 
 	def on_submit(self):
-		super(SalesInvoice, self).on_submit()
-
 		if cint(self.update_stock) == 1:
 			self.update_stock_ledger()
 		else:
@@ -473,7 +471,7 @@
 			frappe.db.set(self,'paid_amount',0)
 
 		frappe.db.set(self, 'base_paid_amount',
-			flt(self.paid_amount*self.conversion_rate, self.precision("base_paid_amount")))
+			flt(self.paid_amount*self.conversion_rate, self.precision("base_paid_amount")))		
 
 	def check_prev_docstatus(self):
 		for d in self.get('items'):
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json
index 0b83e62..055f059 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.json
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.json
@@ -2474,7 +2474,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring && doc.recurring_id === doc.name", 
@@ -2693,7 +2693,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-03-18 02:03:28.445402", 
+ "modified": "2016-03-18 13:13:07.334625", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order", 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.py b/erpnext/buying/doctype/purchase_order/purchase_order.py
index 47b5922..5dd59e8 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.py
@@ -171,8 +171,6 @@
 		if self.is_against_so():
 			self.update_status_updater()
 
-		super(PurchaseOrder, self).on_submit()
-
 		purchase_controller = frappe.get_doc("Purchase Common")
 
 		self.update_prevdoc_status()
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 7b6f943..bcaa9fb 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import frappe
 from frappe import _, throw
-from frappe.utils import today, flt, cint, fmt_money, getdate
+from frappe.utils import today, flt, cint, fmt_money
 from erpnext.setup.utils import get_company_currency, get_exchange_rate
 from erpnext.accounts.utils import get_fiscal_year, validate_fiscal_year, get_account_currency
 from erpnext.utilities.transaction_base import TransactionBase
@@ -31,9 +31,6 @@
 			self.set_missing_values(for_validate=True)
 		self.validate_date_with_fiscal_year()
 
-		if self.meta.get_field('next_date') and self.next_date:
-			self.validate_recurring_next_date()
-
 		if self.meta.get_field("currency"):
 			self.calculate_taxes_and_totals()
 			if not self.meta.get_field("is_return") or not self.is_return:
@@ -45,17 +42,14 @@
 		if self.doctype in ("Sales Invoice", "Purchase Invoice") and not self.is_return:
 			self.validate_due_date()
 
-		if self.meta.get_field("is_recurring"):
-			validate_recurring_document(self)
-
 		if self.meta.get_field("taxes_and_charges"):
 			self.validate_enabled_taxes_and_charges()
 
 		self.validate_party()
 		self.validate_currency()
-
-	def on_submit(self):
-		if self.meta.get_field("is_recurring"):
+		
+		if self.meta.get_field("is_recurring") and not self.get("__islocal"):
+			validate_recurring_document(self)
 			convert_to_recurring(self, self.get("posting_date") or self.get("transaction_date"))
 
 	def on_update_after_submit(self):
@@ -91,14 +85,6 @@
 				validate_fiscal_year(self.get(date_field), self.fiscal_year,
 					self.meta.get_label(date_field), self)
 
-	def validate_recurring_next_date(self):
-		posting_date = self.get("posting_date") or self.get("transaction_date")
-		if getdate(posting_date) > getdate(self.next_date):
-			frappe.throw(_("Next Date must be greater than Posting Date"))
-
-		if getdate(self.next_date).day != self.repeat_on_day_of_month:
-			frappe.throw(_("Next Date's day and Repeat on Day of Month must be equal"))
-
 	def validate_due_date(self):
 		from erpnext.accounts.party import validate_due_date
 		if self.doctype == "Sales Invoice":
diff --git a/erpnext/controllers/recurring_document.py b/erpnext/controllers/recurring_document.py
index c3e98e8..2df7352 100644
--- a/erpnext/controllers/recurring_document.py
+++ b/erpnext/controllers/recurring_document.py
@@ -108,6 +108,7 @@
 	new_document.run_method("on_recurring", reference_doc=reference_doc)
 
 	if reference_doc.submit_on_creation:
+		new_document.insert()
 		new_document.submit()
 	else:
 		new_document.docstatus=0
@@ -160,26 +161,33 @@
 def validate_recurring_document(doc):
 	if doc.is_recurring:
 		validate_notification_email_id(doc)
-
-		if not doc.recurring_id and not doc.is_new():
-			doc.recurring_id = doc.name
-
 		if not doc.recurring_type:
-			msgprint(_("Please select {0}").format(doc.meta.get_label("recurring_type")),
-			raise_exception=1)
+			frappe.throw(_("Please select {0}").format(doc.meta.get_label("recurring_type")))
 
 		elif not (doc.from_date and doc.to_date):
-			throw(_("Period From and Period To dates mandatory for recurring {0}").format(doc.doctype))
+			frappe.throw(_("Period From and Period To dates mandatory for recurring {0}").format(doc.doctype))
+			
+def validate_recurring_next_date(doc):
+	posting_date = doc.get("posting_date") or doc.get("transaction_date")
+	if getdate(posting_date) > getdate(doc.next_date):
+		frappe.throw(_("Next Date must be greater than Posting Date"))
 
-#
+	if getdate(doc.next_date).day != doc.repeat_on_day_of_month:
+		frappe.throw(_("Next Date's day and Repeat on Day of Month must be equal"))
+
 def convert_to_recurring(doc, posting_date):
-    if doc.is_recurring:
-        set_next_date(doc, posting_date)
+	if doc.is_recurring:
+		if not doc.recurring_id:
+			doc.db_set("recurring_id", doc.name)
 
-    elif doc.recurring_id:
-        frappe.db.sql("""update `tab%s` set is_recurring = 0
-            where recurring_id = %s""" % (doc.doctype, '%s'), (doc.recurring_id))
-#
+		set_next_date(doc, posting_date)
+
+	elif doc.recurring_id:
+		frappe.db.sql("""update `tab%s` set is_recurring = 0
+			where recurring_id = %s""" % (doc.doctype, '%s'), (doc.recurring_id))
+			
+	if doc.next_date:
+		validate_recurring_next_date(doc)
 
 def validate_notification_email_id(doc):
 	if doc.notify_by_email:
@@ -198,13 +206,12 @@
 
 def set_next_date(doc, posting_date):
 	""" Set next date on which recurring document will be created"""
-
 	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],
 		cint(doc.repeat_on_day_of_month))
 
-	frappe.db.set(doc, 'next_date', next_date)
+	doc.db_set('next_date', next_date)
 
 	msgprint(_("Next Recurring {0} will be created on {1}").format(doc.doctype, next_date))
diff --git a/erpnext/selling/doctype/sales_order/sales_order.json b/erpnext/selling/doctype/sales_order/sales_order.json
index 072ade8..a9d4e29 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.json
+++ b/erpnext/selling/doctype/sales_order/sales_order.json
@@ -2799,7 +2799,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "depends_on": "eval:doc.is_recurring && doc.recurring_id === doc.name", 
@@ -3018,7 +3018,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-03-18 02:03:43.706326", 
+ "modified": "2016-03-18 13:11:32.654873", 
  "modified_by": "Administrator", 
  "module": "Selling", 
  "name": "Sales Order", 
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 2aff7c5..529c17c 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -150,8 +150,6 @@
 				frappe.throw(_("Row #{0}: Set Supplier for item {1}").format(d.idx, d.item_code))
 
 	def on_submit(self):
-		super(SalesOrder, self).on_submit()
-
 		self.check_credit_limit()
 		self.update_reserved_qty()