unlink against voucher in jv when Purchase Invoice, Sales Invoice or Journal Voucher are cancelled
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..f29c2e9 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -127,11 +127,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 +402,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 +832,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"):
diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py
index 91c0622..b63642c 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,42 @@
 		])
 		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))
+		
+		
 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))