fixes in cform
diff --git a/accounts/doctype/c_form/c_form.py b/accounts/doctype/c_form/c_form.py
index e574708..9f89ad5 100644
--- a/accounts/doctype/c_form/c_form.py
+++ b/accounts/doctype/c_form/c_form.py
@@ -34,11 +34,11 @@
 		for d in getlist(self.doclist, 'invoice_details'):
 			if d.invoice_no:
 				inv = webnotes.conn.sql("""select c_form_applicable, c_form_no from
-					`tabSales Invoice` where name = %s""", d.invoice_no)
+					`tabSales Invoice` where name = %s and docstatus = 1""", d.invoice_no)
 				
 				if not inv:
-					webnotes.msgprint("Invoice: %s is not exists in the system, please check." % 
-						d.invoice_no, raise_exception=1)
+					webnotes.msgprint("""Invoice: %s is not exists in the system or 
+						is not submitted, please check.""" % d.invoice_no, raise_exception=1)
 					
 				elif inv[0][0] != 'Yes':
 					webnotes.msgprint("C-form is not applicable for Invoice: %s" % 
@@ -52,21 +52,30 @@
 
 	def on_update(self):
 		"""	Update C-Form No on invoices"""
+		self.set_total_invoiced_amount()
+	
+	def on_submit(self):
+		self.set_cform_in_sales_invoices()
+		
+	def before_cancel(self):
+		# remove cform reference
+		webnotes.conn.sql("""update `tabSales Invoice` set c_form_no=null
+			where c_form_no=%s""", self.doc.name)
+		
+	def set_cform_in_sales_invoices(self):
 		inv = [d.invoice_no for d in getlist(self.doclist, 'invoice_details')]
 		if inv:
 			webnotes.conn.sql("""update `tabSales Invoice` set c_form_no=%s, modified=%s 
 				where name in (%s)""" % ('%s', '%s', ', '.join(['%s'] * len(inv))), 
 				tuple([self.doc.name, self.doc.modified] + inv))
 				
-			webnotes.conn.sql("""update `tabSales Invoice` set c_form_no = '', modified = %s 
+			webnotes.conn.sql("""update `tabSales Invoice` set c_form_no = null, modified = %s 
 				where name not in (%s) and ifnull(c_form_no, '') = %s""" % 
 				('%s', ', '.join(['%s']*len(inv)), '%s'),
 				tuple([self.doc.modified] + inv + [self.doc.name]))
 		else:
 			webnotes.msgprint("Please enter atleast 1 invoice in the table", raise_exception=1)
 
-		self.set_total_invoiced_amount()
-
 	def set_total_invoiced_amount(self):
 		total = sum([flt(d.grand_total) for d in getlist(self.doclist, 'invoice_details')])
 		webnotes.conn.set(self.doc, 'total_invoiced_amount', total)
diff --git a/patches/march_2013/p01_c_form.py b/patches/march_2013/p01_c_form.py
new file mode 100644
index 0000000..5261d2b
--- /dev/null
+++ b/patches/march_2013/p01_c_form.py
@@ -0,0 +1,6 @@
+import webnotes
+
+def execute():
+	for cform in webnotes.conn.sql("""select name from `tabC-Form` where docstatus=2"""):
+		webnotes.conn.sql("""update `tabSales Invoice` set c_form_no=null
+			where c_form_no=%s""", cform[0])
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 2bd4ad8..dd7ff06 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -204,4 +204,5 @@
 	"patches.march_2013.update_po_prevdoc_doctype",
 	"patches.february_2013.p09_timesheets",
 	"execute:(not webnotes.conn.exists('UOM', 'Hour')) and webnotes.doc({'uom_name': 'Hour', 'doctype': 'UOM', 'name': 'Hour'}).insert()",
+	"patches.march_2013.p01_c_form"
 ]
\ No newline at end of file