Test cases
diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index 4ea18ac..b4b8444 100644
--- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -6,7 +6,7 @@
 import unittest
 import frappe
 import frappe.model
-from frappe.utils import cint, flt, today
+from frappe.utils import cint, flt, today, nowdate
 import frappe.defaults
 from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory, \
 	test_records as pr_test_records
@@ -17,6 +17,12 @@
 test_ignore = ["Serial No"]
 
 class TestPurchaseInvoice(unittest.TestCase):
+	def setUp(self):
+		unlink_payment_on_cancel_of_invoice()
+
+	def tearDown(self):
+		unlink_payment_on_cancel_of_invoice(0)
+
 	def test_gl_entries_without_auto_accounting_for_stock(self):
 		set_perpetual_inventory(0)
 		self.assertTrue(not cint(frappe.defaults.get_global_default("auto_accounting_for_stock")))
@@ -55,6 +61,27 @@
 
 		set_perpetual_inventory(0)
 
+	def test_payment_entry_unlink_against_purchase_invoice(self):
+		from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry
+		unlink_payment_on_cancel_of_invoice(0)
+
+		pi_doc = make_purchase_invoice()
+
+		pe = get_payment_entry("Purchase Invoice", pi_doc.name, bank_account="_Test Bank - _TC")
+		pe.reference_no = "1"
+		pe.reference_date = nowdate()
+		pe.paid_from_account_currency = pi_doc.currency
+		pe.paid_to_account_currency = pi_doc.currency
+		pe.source_exchange_rate = 1
+		pe.target_exchange_rate = 1
+		pe.paid_amount = pi_doc.grand_total
+		pe.save(ignore_permissions=True)
+		pe.submit()
+
+		pi_doc = frappe.get_doc('Purchase Invoice', pi_doc.name)
+
+		self.assertRaises(frappe.LinkExistsError, pi_doc.cancel)
+
 	def test_gl_entries_with_auto_accounting_for_stock_against_pr(self):
 		set_perpetual_inventory(1)
 		self.assertEqual(cint(frappe.defaults.get_global_default("auto_accounting_for_stock")), 1)
@@ -411,6 +438,11 @@
 		self.assertEquals(frappe.db.get_value("Serial No", pi.get("items")[0].rejected_serial_no,
 			"warehouse"), pi.get("items")[0].rejected_warehouse)
 
+def unlink_payment_on_cancel_of_invoice(enable=1):
+	accounts_settings = frappe.get_doc("Accounts Settings")
+	accounts_settings.unlink_payment_on_cancellation_of_invoice = enable
+	accounts_settings.save()
+
 def make_purchase_invoice(**args):
 	pi = frappe.new_doc("Purchase Invoice")
 	args = frappe._dict(args)
@@ -455,4 +487,4 @@
 			pi.submit()
 	return pi
 
-test_records = frappe.get_test_records('Purchase Invoice')
+test_records = frappe.get_test_records('Purchase Invoice')
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 1bb7b1c..83fc83c 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -4,8 +4,9 @@
 
 import frappe
 import unittest, copy
-from frappe.utils import nowdate, add_days, flt
+from frappe.utils import nowdate, add_days, flt, nowdate
 from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry, get_qty_after_transaction
+from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import unlink_payment_on_cancel_of_invoice
 from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory
 from erpnext.exceptions import InvalidAccountCurrency, InvalidCurrency
 from erpnext.stock.doctype.serial_no.serial_no import SerialNoWarehouseError
@@ -19,6 +20,12 @@
 		w.submit()
 		return w
 
+	def setUp(self):
+		unlink_payment_on_cancel_of_invoice()
+
+	def tearDown(self):
+		unlink_payment_on_cancel_of_invoice(0)
+
 	def test_timestamp_change(self):
 		w = frappe.copy_doc(test_records[0])
 		w.docstatus = 0
@@ -78,6 +85,28 @@
 		self.assertEquals(si.base_grand_total, 1627.05)
 		self.assertEquals(si.grand_total, 1627.05)
 
+	def test_payment_entry_unlink_against_invoice(self):
+		from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry
+		si = frappe.copy_doc(test_records[0])
+		si.is_pos = 0
+		si.insert()
+		si.submit()
+
+		pe = get_payment_entry("Sales Invoice", si.name, bank_account="_Test Bank - _TC")
+		pe.reference_no = "1"
+		pe.reference_date = nowdate()
+		pe.paid_from_account_currency = si.currency
+		pe.paid_to_account_currency = si.currency
+		pe.source_exchange_rate = 1
+		pe.target_exchange_rate = 1
+		pe.paid_amount = si.grand_total
+		pe.insert()
+		pe.submit()
+
+		unlink_payment_on_cancel_of_invoice(0)
+		si = frappe.get_doc('Sales Invoice', si.name)
+		self.assertRaises(frappe.LinkExistsError, si.cancel)
+
 	def test_sales_invoice_calculation_export_currency(self):
 		si = frappe.copy_doc(test_records[2])
 		si.currency = "USD"