Add message on cancel in SI if C-Form exists, Add validation in C-Form, remove update_c_form
diff --git a/erpnext/accounts/doctype/c_form/c_form.py b/erpnext/accounts/doctype/c_form/c_form.py
index 88ced9a..c18d28a 100644
--- a/erpnext/accounts/doctype/c_form/c_form.py
+++ b/erpnext/accounts/doctype/c_form/c_form.py
@@ -17,15 +17,19 @@
 				inv = frappe.db.sql("""select c_form_applicable, c_form_no from
 					`tabSales Invoice` where name = %s and docstatus = 1""", d.invoice_no)
 
-				if inv[0][0] != 'Yes':
+				if inv and inv[0][0] != 'Yes':
 					frappe.throw("C-form is not applicable for Invoice: %s" % d.invoice_no)
 
-				elif inv[0][1] and inv[0][1] != self.name:
+				elif inv and inv[0][1] and inv[0][1] != self.name:
 					frappe.throw("""Invoice %s is tagged in another C-form: %s.
 						If you want to change C-form no for this invoice,
 						please remove invoice no from the previous c-form and then try again""" %
 						(d.invoice_no, inv[0][1]))
 
+				elif not inv:
+					frappe.throw("Row %s: Invoice %s is invalid, it might be cancelled / does not exist. \
+						Please enter a valid Invoice" % d.idx, d.invoice_no)
+
 	def on_update(self):
 		"""	Update C-Form No on invoices"""
 		self.set_total_invoiced_amount()
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index a20d906..4e5ddf0 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -101,7 +101,6 @@
 		if not cint(self.is_pos) == 1:
 			self.update_against_document_in_jv()
 
-		self.update_c_form()
 		self.update_time_log_batch(self.name)
 		convert_to_recurring(self, "RECINV.#####", self.posting_date)
 
@@ -120,6 +119,7 @@
 		self.update_status_updater_args()
 		self.update_prevdoc_status()
 		self.update_billing_status_for_zero_amount_refdoc("Sales Order")
+		self.validate_c_form_on_cancel()
 
 		self.make_gl_entries_on_cancel()
 
@@ -376,6 +376,12 @@
 
 			frappe.db.set(self, 'c_form_no', '')
 
+	def validate_c_form_on_cancel(self):
+		""" Display message if C-Form no exists on cancellation of Sales Invoice"""
+		if self.c_form_applicable == 'Yes' and self.c_form_no:
+			msgprint(_("Please remove this Invoice {0} from C-Form {1}")
+				.format(self.name, self.c_form_no), raise_exception = 1)
+
 	def update_current_stock(self):
 		for d in self.get('entries'):
 			if d.item_code and d.warehouse:
@@ -584,14 +590,6 @@
 					})
 				)
 
-	def update_c_form(self):
-		"""Update amended id in C-form"""
-		if self.c_form_no and self.amended_from:
-			frappe.db.sql("""update `tabC-Form Invoice Detail` set invoice_no = %s,
-				invoice_date = %s, territory = %s, net_total = %s,
-				grand_total = %s where invoice_no = %s and parent = %s""",
-				(self.name, self.amended_from, self.c_form_no))
-
 @frappe.whitelist()
 def get_bank_cash_account(mode_of_payment):
 	val = frappe.db.get_value("Mode of Payment", mode_of_payment, "default_account")