on_trash modified for item group, territory, customer group, and sales person
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index f11ec0c..2ba86e6 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -136,7 +136,7 @@
 	# check if child exists
 	# ==================================================================
 	def check_if_child_exists(self):
-		return sql("select name from `tabAccount` where parent_account = %s and docstatus != 2", self.doc.name, debug=0)
+		return sql("select name from `tabAccount` where parent_account = %s and docstatus != 2", self.doc.name)
 	
 	# Update balance
 	# ==================================================================
diff --git a/erpnext/setup/doctype/customer_group/customer_group.py b/erpnext/setup/doctype/customer_group/customer_group.py
index cc4a9e0..edae5df 100644
--- a/erpnext/setup/doctype/customer_group/customer_group.py
+++ b/erpnext/setup/doctype/customer_group/customer_group.py
@@ -60,6 +60,15 @@
 
 	def on_trash(self):
 		cust = sql("select name from `tabCustomer` where ifnull(customer_group, '') = %s", self.doc.name)
+		cust = [d[0] for d in cust]
+		
 		if cust:
 			msgprint("""Customer Group: %s can not be trashed/deleted because it is used in customer: %s. 
-				To trash/delete this, remove/change customer group in customer master""" % (self.doc.name, cust[0][0] or ''), raise_exception=1)
+				To trash/delete this, remove/change customer group in customer master""" % (self.doc.name, cust or ''), raise_exception=1)
+
+		if sql("select name from `tabCustomer Group` where parent_customer_group = %s and docstatus != 2", self.doc.name):
+			msgprint("Child customer group exists for this customer group. You can not trash/cancel/delete this customer group.", raise_exception=1)
+
+		# rebuild tree
+		webnotes.conn.set(self.doc,'old_parent', '')
+		self.update_nsm_model()
\ No newline at end of file
diff --git a/erpnext/setup/doctype/item_group/item_group.py b/erpnext/setup/doctype/item_group/item_group.py
index b43523d..c96edcf 100644
--- a/erpnext/setup/doctype/item_group/item_group.py
+++ b/erpnext/setup/doctype/item_group/item_group.py
@@ -63,7 +63,17 @@
 			raise Exception
 	
 	def on_trash(self):
-		ig = sql("select name from `tabItem` where ifnull(item_group, '') = %s", self.doc.name)
+		item = sql("select name from `tabItem` where ifnull(item_group, '') = %s", self.doc.name)
+		item = [d[0] for d in item]
+		
 		if ig:
 			msgprint("""Item Group: %s can not be trashed/deleted because it is used in item: %s. 
-				To trash/delete this, remove/change item group in item master""" % (self.doc.name, ig[0][0] or ''), raise_exception=1)
+				To trash/delete this, remove/change item group in item master""" % (self.doc.name, item or ''), raise_exception=1)
+				
+		if sql("select name from `tabItem Group` where parent_item_group = %s and docstatus != 2", self.doc.name):
+			msgprint("Child item group exists for this item group. You can not trash/cancel/delete this item group.", raise_exception=1)
+		
+		
+		# rebuild tree
+		set(self.doc,'old_parent', '')
+		self.update_nsm_model()
\ No newline at end of file
diff --git a/erpnext/setup/doctype/sales_person/sales_person.py b/erpnext/setup/doctype/sales_person/sales_person.py
index 651cfb4..d8c6e43 100644
--- a/erpnext/setup/doctype/sales_person/sales_person.py
+++ b/erpnext/setup/doctype/sales_person/sales_person.py
@@ -21,7 +21,7 @@
 from webnotes.model.doclist import getlist
 from webnotes.model.code import get_obj
 from webnotes import session, form, is_testing, msgprint, errprint
-from webnotes.utils import flt
+from webnotes.utils import flt, cstr
 
 sql = webnotes.conn.sql
 convert_to_lists = webnotes.conn.convert_to_lists
@@ -77,4 +77,19 @@
 		d.sales_person_id = self.doc.name
 		d.is_sales_person = 1
 		
-		d.save(new = (not d.name))		
+		d.save(new = (not d.name))
+		
+
+	def on_trash(self):
+		st = sql("select parent, parenttype from `tabSales Team` where ifnull(sales_person, '') = %s and docstatus != 2", self.doc.name)
+		st = [(d[1] + ' : ' + d[0]) for d in st]
+		if st:
+			msgprint("""Sales Person: %s can not be trashed/deleted because it is used in  %s. 
+				To trash/delete this, remove/change sales person in %s""" % (self.doc.name, st or '', st or ''), raise_exception=1)
+
+		if sql("select name from `tabSales Person` where parent_sales_person = %s and docstatus != 2", self.doc.name):
+			msgprint("Child sales person exists for this sales person. You can not trash/cancel this sales person.", raise_exception=1)
+
+		# rebuild tree
+		webnotes.conn.set(self.doc,'old_parent', '')
+		self.update_nsm_model()
diff --git a/erpnext/setup/doctype/territory/territory.py b/erpnext/setup/doctype/territory/territory.py
index 8017ad5..21c3392 100644
--- a/erpnext/setup/doctype/territory/territory.py
+++ b/erpnext/setup/doctype/territory/territory.py
@@ -78,7 +78,17 @@
 
 
 	def on_trash(self):
-		terr = sql("select name from `tabCustomer` where ifnull(territory, '') = %s", self.doc.name)
-		if terr:
-			msgprint("""Territory: %s can not be trashed/deleted because it is used in territory: %s. 
-				To trash/delete this, remove/change territory in customer master""" % (self.doc.name, terr[0][0] or ''), raise_exception=1)
+		cust = sql("select name from `tabCustomer` where ifnull(territory, '') = %s", self.doc.name)
+		cust = [d[0] for d in cust]		
+		
+		if cust:
+			msgprint("""Territory: %s can not be trashed/deleted because it is used in customer: %s. 
+				To trash/delete this, remove/change territory in customer master""" % (self.doc.name, cust or ''), raise_exception=1)
+				
+				
+		if sql("select name from `tabTerritory` where parent_territory = %s and docstatus != 2", self.doc.name):
+			msgprint("Child territory exists for this territory. You can not trash/cancel/delete this territory.", raise_exception=1)
+
+		# rebuild tree
+		set(self.doc,'old_parent', '')
+		self.update_nsm_model()
\ No newline at end of file