added description to sales_invoice.txt
diff --git a/accounts/doctype/fiscal_year/test_fiscal_year.py b/accounts/doctype/fiscal_year/test_fiscal_year.py
index e031a19..b209b39 100644
--- a/accounts/doctype/fiscal_year/test_fiscal_year.py
+++ b/accounts/doctype/fiscal_year/test_fiscal_year.py
@@ -10,5 +10,15 @@
 		"doctype": "Fiscal Year",
 		"year": "_Test Fiscal Year 2014", 
 		"year_start_date": "2014-01-01"
-	}]
+	}],
+	[{
+		"doctype": "Fiscal Year",
+		"year": "_Test Fiscal Year 2015", 
+		"year_start_date": "2015-01-01"
+	}],
+	[{
+		"doctype": "Fiscal Year",
+		"year": "_Test Fiscal Year 2016", 
+		"year_start_date": "2016-01-01"
+	}],
 ]
\ No newline at end of file
diff --git a/accounts/doctype/journal_voucher/journal_voucher.py b/accounts/doctype/journal_voucher/journal_voucher.py
index 2acf08e..f4bd55c 100644
--- a/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/accounts/doctype/journal_voucher/journal_voucher.py
@@ -63,6 +63,9 @@
 		self.make_gl_entries()
 
 	def on_cancel(self):
+		from accounts.utils import remove_against_link_from_jv
+		remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_jv")
+		
 		self.make_gl_entries(cancel=1)
 
 	def validate_debit_credit(self):
diff --git a/accounts/doctype/journal_voucher/test_journal_voucher.py b/accounts/doctype/journal_voucher/test_journal_voucher.py
index bb846d1..7cfeb59 100644
--- a/accounts/doctype/journal_voucher/test_journal_voucher.py
+++ b/accounts/doctype/journal_voucher/test_journal_voucher.py
@@ -19,8 +19,34 @@
 import unittest
 import webnotes
 
-test_records = [[
-	{
+class TestJournalVoucher(unittest.TestCase):
+	def test_journal_voucher_with_against_jv(self):
+		jv_invoice = webnotes.bean(copy=test_records[2])
+		jv_invoice.insert()
+		jv_invoice.submit()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s""", jv_invoice.doc.name))
+		
+		jv_payment = webnotes.bean(copy=test_records[0])
+		jv_payment.doclist[1].against_jv = jv_invoice.doc.name
+		jv_payment.insert()
+		jv_payment.submit()
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s""", jv_invoice.doc.name))
+			
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s and credit=400""", jv_invoice.doc.name))
+		
+		# cancel jv_invoice
+		jv_invoice.cancel()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s""", jv_invoice.doc.name))
+
+test_records = [
+	[{
 		"company": "_Test Company", 
 		"doctype": "Journal Voucher", 
 		"fiscal_year": "_Test Fiscal Year 2013", 
@@ -44,8 +70,59 @@
 		"debit": 400.0,
 		"credit": 0.0,
 		"parentfield": "entries"
-	}
-]]
+	}],
+	[{
+		"company": "_Test Company", 
+		"doctype": "Journal Voucher", 
+		"fiscal_year": "_Test Fiscal Year 2013", 
+		"naming_series": "_T-Journal Voucher-",
+		"posting_date": "2013-02-14", 
+		"user_remark": "test",
+		"voucher_type": "Bank Voucher",
+		"cheque_no": "33",
+		"cheque_date": "2013-02-14"
+	}, 
+	{
+		"account": "_Test Supplier - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"credit": 0.0,
+		"debit": 400.0,
+		"parentfield": "entries"
+	}, 
+	{
+		"account": "_Test Account Bank Account - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"debit": 0.0,
+		"credit": 400.0,
+		"parentfield": "entries"
+	}],
+	[{
+		"company": "_Test Company", 
+		"doctype": "Journal Voucher", 
+		"fiscal_year": "_Test Fiscal Year 2013", 
+		"naming_series": "_T-Journal Voucher-",
+		"posting_date": "2013-02-14", 
+		"user_remark": "test",
+		"voucher_type": "Bank Voucher",
+		"cheque_no": "33",
+		"cheque_date": "2013-02-14"
+	}, 
+	{
+		"account": "_Test Customer - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"credit": 0.0,
+		"debit": 400.0,
+		"parentfield": "entries"
+	}, 
+	{
+		"account": "Sales - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"credit": 400.0,
+		"debit": 0.0,
+		"parentfield": "entries",
+		"cost_center": "_Test Cost Center - _TC"
+	}],
+]
 
 
 
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index b56e2ac..f535d56 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -507,18 +507,13 @@
 		if gl_entries:
 			make_gl_entries(gl_entries, cancel=is_cancel)
 
-	def check_next_docstatus(self):
-		submit_jv = sql("select t1.name from `tabJournal Voucher` t1,`tabJournal Voucher Detail` t2 where t1.name = t2.parent and t2.against_voucher = '%s' and t1.docstatus = 1" % (self.doc.name))
-		if submit_jv:
-			msgprint("Journal Voucher : " + cstr(submit_jv[0][0]) + " has been created against " + cstr(self.doc.doctype) + ". So " + cstr(self.doc.doctype) + " cannot be Cancelled.")
-			raise Exception, "Validation Error."
-	
 	def on_cancel(self):
-		self.check_next_docstatus()
+		from accounts.utils import remove_against_link_from_jv
+		remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_voucher")
 		
 		self.make_gl_entries(is_cancel=1)
 		get_obj(dt = 'Purchase Common').update_prevdoc_detail(self, is_submit = 0)
-
+		
 	def on_update(self):
 		pass
 		
diff --git a/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index b9f7ec9..6d9cfca 100644
--- a/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -115,6 +115,42 @@
 		for i, item in enumerate(wrapper.doclist.get({"parentfield": "entries"})):
 			self.assertEqual(item.item_code, expected_values[i][0])
 			self.assertEqual(item.item_tax_amount, expected_values[i][1])
+			
+	def test_purchase_invoice_with_advance(self):
+		from accounts.doctype.journal_voucher.test_journal_voucher \
+			import test_records as jv_test_records
+			
+		jv = webnotes.bean(copy=jv_test_records[1])
+		jv.insert()
+		jv.submit()
+		
+		pi = webnotes.bean(copy=test_records[0])
+		pi.doclist.append({
+			"doctype": "Purchase Invoice Advance",
+			"parentfield": "advance_allocation_details",
+			"journal_voucher": jv.doc.name,
+			"jv_detail_no": jv.doclist[1].name,
+			"advance_amount": 400,
+			"allocated_amount": 300,
+			"remarks": jv.doc.remark
+		})
+		pi.run_method("calculate_taxes_and_totals")
+		pi.insert()
+		pi.submit()
+		pi.load_from_db()
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_voucher=%s""", pi.doc.name))
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_voucher=%s and debit=300""", pi.doc.name))
+			
+		self.assertEqual(pi.doc.outstanding_amount, 1212.30)
+		
+		pi.cancel()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_voucher=%s""", pi.doc.name))
 	
 test_records = [
 	[
diff --git a/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt b/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt
index 201bb53..6b31684 100644
--- a/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt
+++ b/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt
@@ -1,8 +1,8 @@
 [
  {
-  "creation": "2013-02-22 01:27:40", 
+  "creation": "2013-03-08 15:36:46", 
   "docstatus": 0, 
-  "modified": "2013-03-07 07:03:26", 
+  "modified": "2013-03-20 16:52:12", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -40,7 +40,7 @@
  {
   "doctype": "DocField", 
   "fieldname": "jv_detail_no", 
-  "fieldtype": "Date", 
+  "fieldtype": "Data", 
   "hidden": 1, 
   "label": "Journal Voucher Detail No", 
   "oldfieldname": "jv_detail_no", 
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index 3f32a47..22ac845 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -17,7 +17,9 @@
 from __future__ import unicode_literals
 import webnotes
 
-from webnotes.utils import add_days, cint, cstr, date_diff, flt, getdate, nowdate
+from webnotes.utils import add_days, cint, cstr, date_diff, flt, getdate, nowdate, \
+	get_first_day, get_last_day
+
 from webnotes.utils.email_lib import sendmail
 from webnotes.utils import comma_and
 from webnotes.model.doc import make_autoname
@@ -127,11 +129,14 @@
 		
 		sales_com_obj = get_obj(dt = 'Sales Common')
 		sales_com_obj.check_stop_sales_order(self)
-		self.check_next_docstatus()
+		
+		from accounts.utils import remove_against_link_from_jv
+		remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_invoice")
+
 		sales_com_obj.update_prevdoc_detail(0, self)
 		
 		self.make_gl_entries()
-
+		
 	def on_update_after_submit(self):
 		self.validate_recurring_invoice()
 		self.convert_to_recurring()
@@ -399,8 +404,7 @@
 		if lst:
 			from accounts.utils import reconcile_against_document
 			reconcile_against_document(lst)
-	
-	
+			
 	def validate_customer(self):
 		"""	Validate customer name with SO and DN"""
 		for d in getlist(self.doclist,'entries'):
@@ -830,12 +834,6 @@
 				grand_total = %s where invoice_no = %s and parent = %s""", 
 				(self.doc.name, self.doc.amended_from, self.doc.c_form_no))
 
-	def check_next_docstatus(self):
-		submit_jv = webnotes.conn.sql("select t1.name from `tabJournal Voucher` t1,`tabJournal Voucher Detail` t2 where t1.name = t2.parent and t2.against_invoice = '%s' and t1.docstatus = 1" % (self.doc.name))
-		if submit_jv:
-			msgprint("Journal Voucher : " + cstr(submit_jv[0][0]) + " has been created against " + cstr(self.doc.doctype) + ". So " + cstr(self.doc.doctype) + " cannot be Cancelled.")
-			raise Exception, "Validation Error."
-	
 	@property
 	def meta(self):
 		if not hasattr(self, "_meta"):
@@ -895,25 +893,18 @@
 		
 		next_date = get_next_date(self.doc.posting_date,
 			month_map[self.doc.recurring_type], cint(self.doc.repeat_on_day_of_month))
+		
 		webnotes.conn.set(self.doc, 'next_date', next_date)
 	
 def get_next_date(dt, mcount, day=None):
-	import datetime
-	month = getdate(dt).month + mcount
-	year = getdate(dt).year
-	if not day:
-		day = getdate(dt).day
-	if month > 12:
-		month, year = month-12, year+1
-	try:
-		next_month_date = datetime.date(year, month, day)
-	except:
-		import calendar
-		last_day = calendar.monthrange(year, month)[1]
-		next_month_date = datetime.date(year, month, last_day)
-	return next_month_date.strftime("%Y-%m-%d")
-
-def manage_recurring_invoices(next_date=None):
+	dt = getdate(dt)
+	
+	from dateutil.relativedelta import relativedelta
+	dt += relativedelta(months=mcount, day=day)
+	
+	return dt
+	
+def manage_recurring_invoices(next_date=None, commit=True):
 	""" 
 		Create recurring invoices on specific date by copying the original one
 		and notify the concerned people
@@ -933,19 +924,22 @@
 				ref_wrapper = webnotes.bean('Sales Invoice', ref_invoice)
 				new_invoice_wrapper = make_new_invoice(ref_wrapper, next_date)
 				send_notification(new_invoice_wrapper)
-				webnotes.conn.commit()
+				if commit:
+					webnotes.conn.commit()
 			except:
-				webnotes.conn.rollback()
+				if commit:
+					webnotes.conn.rollback()
 
-				webnotes.conn.begin()
-				webnotes.conn.sql("update `tabSales Invoice` set \
-					convert_into_recurring_invoice = 0 where name = %s", ref_invoice)
-				notify_errors(ref_invoice, ref_wrapper.doc.owner)
-				webnotes.conn.commit()
+					webnotes.conn.begin()
+					webnotes.conn.sql("update `tabSales Invoice` set \
+						convert_into_recurring_invoice = 0 where name = %s", ref_invoice)
+					notify_errors(ref_invoice, ref_wrapper.doc.owner)
+					webnotes.conn.commit()
 
 				exception_list.append(webnotes.getTraceback())
 			finally:
-				webnotes.conn.begin()
+				if commit:
+					webnotes.conn.begin()
 			
 	if exception_list:
 		exception_message = "\n\n".join([cstr(d) for d in exception_list])
@@ -957,19 +951,27 @@
 	new_invoice = clone(ref_wrapper)
 	
 	mcount = month_map[ref_wrapper.doc.recurring_type]
-		
+	
+	invoice_period_from_date = get_next_date(ref_wrapper.doc.invoice_period_from_date, mcount)
+	
+	# get last day of the month to maintain period if the from date is first day of its own month 
+	# and to date is the last day of its own month
+	if (cstr(get_first_day(ref_wrapper.doc.invoice_period_from_date)) == \
+			cstr(ref_wrapper.doc.invoice_period_from_date)) and \
+		(cstr(get_last_day(ref_wrapper.doc.invoice_period_to_date)) == \
+			cstr(ref_wrapper.doc.invoice_period_to_date)):
+		invoice_period_to_date = get_last_day(get_next_date(ref_wrapper.doc.invoice_period_to_date,
+			mcount))
+	else:
+		invoice_period_to_date = get_next_date(ref_wrapper.doc.invoice_period_to_date, mcount)
+	
 	new_invoice.doc.fields.update({
 		"posting_date": posting_date,
 		"aging_date": posting_date,
-		
 		"due_date": add_days(posting_date, cint(date_diff(ref_wrapper.doc.due_date,
 			ref_wrapper.doc.posting_date))),
-			
-		"invoice_period_from_date": \
-			get_next_date(ref_wrapper.doc.invoice_period_from_date, mcount),
-			
-		"invoice_period_to_date": \
-			get_next_date(ref_wrapper.doc.invoice_period_to_date, mcount),
+		"invoice_period_from_date": invoice_period_from_date,
+		"invoice_period_to_date": invoice_period_to_date,
 		"fiscal_year": get_fiscal_year(posting_date)[0],
 		"owner": ref_wrapper.doc.owner,
 	})
diff --git a/accounts/doctype/sales_invoice/sales_invoice.txt b/accounts/doctype/sales_invoice/sales_invoice.txt
index 41a66b8..fe05d84 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.txt
+++ b/accounts/doctype/sales_invoice/sales_invoice.txt
@@ -1094,7 +1094,7 @@
   "description": "The day of the month on which auto invoice will be generated e.g. 05, 28 etc ", 
   "doctype": "DocField", 
   "fieldname": "repeat_on_day_of_month", 
-  "fieldtype": "Data", 
+  "fieldtype": "Int", 
   "label": "Repeat on Day of Month", 
   "no_copy": 1, 
   "print_hide": 1
diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py
index 91c0622..92feae8 100644
--- a/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -281,6 +281,7 @@
 		return dn
 		
 	def _insert_pos_settings(self):
+		webnotes.conn.sql("""delete from `tabPOS Setting`""")
 		ps = webnotes.bean([
 			{
 				"cash_bank_account": "_Test Account Bank Account - _TC", 
@@ -297,6 +298,177 @@
 		])
 		ps.insert()
 		
+	def test_sales_invoice_with_advance(self):
+		from accounts.doctype.journal_voucher.test_journal_voucher \
+			import test_records as jv_test_records
+			
+		jv = webnotes.bean(copy=jv_test_records[0])
+		jv.insert()
+		jv.submit()
+		
+		si = webnotes.bean(copy=test_records[0])
+		si.doclist.append({
+			"doctype": "Sales Invoice Advance",
+			"parentfield": "advance_adjustment_details",
+			"journal_voucher": jv.doc.name,
+			"jv_detail_no": jv.doclist[1].name,
+			"advance_amount": 400,
+			"allocated_amount": 300,
+			"remarks": jv.doc.remark
+		})
+		si.insert()
+		si.submit()
+		si.load_from_db()
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_invoice=%s""", si.doc.name))
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_invoice=%s and credit=300""", si.doc.name))
+			
+		self.assertEqual(si.doc.outstanding_amount, 261.8)
+		
+		si.cancel()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_invoice=%s""", si.doc.name))
+			
+	def test_recurring_invoice(self):
+		from webnotes.utils import now_datetime, get_first_day, get_last_day, add_to_date
+		today = now_datetime().date()
+		
+		base_si = webnotes.bean(copy=test_records[0])
+		base_si.doc.fields.update({
+			"convert_into_recurring_invoice": 1,
+			"recurring_type": "Monthly",
+			"notification_email_address": "test@example.com, test1@example.com, test2@example.com",
+			"repeat_on_day_of_month": today.day,
+			"posting_date": today,
+			"invoice_period_from_date": get_first_day(today),
+			"invoice_period_to_date": get_last_day(today)
+		})
+		
+		# monthly
+		si1 = webnotes.bean(copy=base_si.doclist)
+		si1.insert()
+		si1.submit()
+		self._test_recurring_invoice(si1, True)
+		
+		# monthly without a first and last day period
+		si2 = webnotes.bean(copy=base_si.doclist)
+		si2.doc.fields.update({
+			"invoice_period_from_date": today,
+			"invoice_period_to_date": add_to_date(today, days=30)
+		})
+		si2.insert()
+		si2.submit()
+		self._test_recurring_invoice(si2, False)
+		
+		# quarterly
+		si3 = webnotes.bean(copy=base_si.doclist)
+		si3.doc.fields.update({
+			"recurring_type": "Quarterly",
+			"invoice_period_from_date": get_first_day(today),
+			"invoice_period_to_date": get_last_day(add_to_date(today, months=3))
+		})
+		si3.insert()
+		si3.submit()
+		self._test_recurring_invoice(si3, True)
+		
+		# quarterly without a first and last day period
+		si4 = webnotes.bean(copy=base_si.doclist)
+		si4.doc.fields.update({
+			"recurring_type": "Quarterly",
+			"invoice_period_from_date": today,
+			"invoice_period_to_date": add_to_date(today, months=3)
+		})
+		si4.insert()
+		si4.submit()
+		self._test_recurring_invoice(si4, False)
+		
+		# yearly
+		si5 = webnotes.bean(copy=base_si.doclist)
+		si5.doc.fields.update({
+			"recurring_type": "Yearly",
+			"invoice_period_from_date": get_first_day(today),
+			"invoice_period_to_date": get_last_day(add_to_date(today, years=1))
+		})
+		si5.insert()
+		si5.submit()
+		self._test_recurring_invoice(si5, True)
+		
+		# yearly without a first and last day period
+		si6 = webnotes.bean(copy=base_si.doclist)
+		si6.doc.fields.update({
+			"recurring_type": "Yearly",
+			"invoice_period_from_date": today,
+			"invoice_period_to_date": add_to_date(today, years=1)
+		})
+		si6.insert()
+		si6.submit()
+		self._test_recurring_invoice(si6, False)
+		
+		# change posting date but keep recuring day to be today
+		si7 = webnotes.bean(copy=base_si.doclist)
+		si7.doc.fields.update({
+			"posting_date": add_to_date(today, days=-3)
+		})
+		si7.insert()
+		si7.submit()
+		
+		# setting so that _test function works
+		si7.doc.posting_date = today
+		self._test_recurring_invoice(si7, True)
+			
+	def _test_recurring_invoice(self, base_si, first_and_last_day):
+		from webnotes.utils import add_months, get_last_day, getdate
+		from accounts.doctype.sales_invoice.sales_invoice import manage_recurring_invoices
+		
+		no_of_months = ({"Monthly": 1, "Quarterly": 3, "Yearly": 12})[base_si.doc.recurring_type]
+		
+		def _test(i):
+			self.assertEquals(i+1, webnotes.conn.sql("""select count(*) from `tabSales Invoice`
+				where recurring_id=%s and docstatus=1""", base_si.doc.recurring_id)[0][0])
+				
+			next_date = add_months(base_si.doc.posting_date, no_of_months)
+			
+			manage_recurring_invoices(next_date=next_date, commit=False)
+			
+			recurred_invoices = webnotes.conn.sql("""select name from `tabSales Invoice`
+				where recurring_id=%s and docstatus=1 order by name desc""", base_si.doc.recurring_id)
+			
+			self.assertEquals(i+2, len(recurred_invoices))
+			
+			new_si = webnotes.bean("Sales Invoice", recurred_invoices[0][0])
+			
+			for fieldname in ["convert_into_recurring_invoice", "recurring_type",
+				"repeat_on_day_of_month", "notification_email_address"]:
+					self.assertEquals(base_si.doc.fields.get(fieldname),
+						new_si.doc.fields.get(fieldname))
+
+			self.assertEquals(new_si.doc.posting_date, unicode(next_date))
+			
+			self.assertEquals(new_si.doc.invoice_period_from_date,
+				unicode(add_months(base_si.doc.invoice_period_from_date, no_of_months)))
+			
+			if first_and_last_day:
+				self.assertEquals(new_si.doc.invoice_period_to_date, 
+					unicode(get_last_day(add_months(base_si.doc.invoice_period_to_date,
+						no_of_months))))
+			else:
+				self.assertEquals(new_si.doc.invoice_period_to_date, 
+					unicode(add_months(base_si.doc.invoice_period_to_date, no_of_months)))
+					
+			self.assertEquals(getdate(new_si.doc.posting_date).day, 
+				base_si.doc.repeat_on_day_of_month)
+			
+			return new_si
+		
+		# if yearly, test 3 repetitions, else test 13 repetitions
+		count = no_of_months == 12 and 3 or 13
+		for i in xrange(count):
+			base_si = _test(i)
+		
 test_dependencies = ["Journal Voucher", "POS Setting"]
 
 test_records = [
diff --git a/accounts/utils.py b/accounts/utils.py
index 14ceb4e..051cdd1 100644
--- a/accounts/utils.py
+++ b/accounts/utils.py
@@ -17,7 +17,7 @@
 from __future__ import unicode_literals
 
 import webnotes
-from webnotes.utils import nowdate, cstr, flt
+from webnotes.utils import nowdate, cstr, flt, now
 from webnotes.model.doc import addchild
 from webnotes import msgprint, _
 from webnotes.utils import formatdate
@@ -233,4 +233,18 @@
 	return webnotes.conn.sql("""select name, parent_cost_center from `tabCost Center` 
 		where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % 
 		(conditions, searchfield, "%s", "%s", "%s"), 
-		tuple(filter_values + ["%%%s%%" % txt, start, page_len]))
\ No newline at end of file
+		tuple(filter_values + ["%%%s%%" % txt, start, page_len]))
+		
+def remove_against_link_from_jv(ref_type, ref_no, against_field):
+	webnotes.conn.sql("""update `tabJournal Voucher Detail` set `%s`=null,
+		modified=%s, modified_by=%s
+		where `%s`=%s and docstatus < 2""" % (against_field, "%s", "%s", against_field, "%s"), 
+		(now(), webnotes.session.user, ref_no))
+	
+	webnotes.conn.sql("""update `tabGL Entry`
+		set against_voucher_type=null, against_voucher=null,
+		modified=%s, modified_by=%s
+		where against_voucher_type=%s and against_voucher=%s
+		and voucher_no != ifnull(against_voucher, "")
+		and ifnull(is_cancelled, "No")="No" """,
+		(now(), webnotes.session.user, ref_type, ref_no))
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index df78212..ac6481c 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -22,6 +22,10 @@
 from utilities.transaction_base import TransactionBase
 
 class AccountsController(TransactionBase):
+	def validate(self):
+		if self.meta.get_field("grand_total"):
+			self.validate_value("grand_total", ">=", 0)
+
 	def get_gl_dict(self, args, cancel=None):
 		"""this method populates the common properties of a gl entry record"""
 		if cancel is None:
@@ -101,4 +105,4 @@
 
 @webnotes.whitelist()
 def get_default_account(account_for, company):
-	return webnotes.conn.get_value("Company", company, account_for)
\ No newline at end of file
+	return webnotes.conn.get_value("Company", company, account_for)
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index 03d2a92..2c2bb44 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -27,7 +27,8 @@
 from controllers.stock_controller import StockController
 
 class BuyingController(StockController):
-	def validate(self):		
+	def validate(self):
+		super(BuyingController, self).validate()
 		if self.meta.get_field("currency"):
 			self.company_currency = get_company_currency(self.doc.company)
 			self.validate_conversion_rate("currency", "conversion_rate")
@@ -37,7 +38,7 @@
 			
 			# IMPORTANT: enable this only when client side code is similar to this one
 			# self.calculate_taxes_and_totals()
-			
+						
 			# set total in words
 			self.set_total_in_words()
 		
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index f9498cb..cd81e18 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -23,6 +23,7 @@
 
 class SellingController(StockController):
 	def validate(self):
+		super(SellingController, self).validate()
 		self.set_total_in_words()
 		
 	def set_total_in_words(self):
@@ -37,4 +38,4 @@
 				self.doc.grand_total or self.doc.rounded_total, company_currency)
 		if self.meta.get_field("in_words_export"):
 			self.doc.in_words_export = money_in_words(disable_rounded_total and 
-				self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)
\ No newline at end of file
+				self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)