Update Territory & Customer Group across all transaction (#13004)

* added method for update query based on changes

* patch added

* updated function, moved util function
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index e21da22..df795fc 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -490,3 +490,4 @@
 erpnext.patches.v10_0.update_sales_order_link_to_purchase_order
 erpnext.patches.v10_0.added_extra_gst_custom_field_in_gstr2 #2018-02-13
 erpnext.patches.v10_0.set_b2c_limit
+erpnext.patches.v10_0.update_territory_and_customer_group
diff --git a/erpnext/patches/v10_0/update_territory_and_customer_group.py b/erpnext/patches/v10_0/update_territory_and_customer_group.py
new file mode 100644
index 0000000..52a6db2
--- /dev/null
+++ b/erpnext/patches/v10_0/update_territory_and_customer_group.py
@@ -0,0 +1,13 @@
+import frappe
+from frappe.model.rename_doc import update_linked_doctypes
+
+def execute():
+	customers = frappe.get_all('Customer')
+	for customer in customers:
+		# Update Territory across all transaction
+		terr = frappe.get_value('Customer', customer, 'territory')
+		update_linked_doctypes("Customer", "Territory", customer.name, terr)
+
+		# Update Territory across all transaction
+		cust_group = frappe.get_value('Customer', customer, 'customer_group')
+		update_linked_doctypes("Customer", "Customer Group", customer.name, cust_group)
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index 2284f85..c550d06 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -11,6 +11,7 @@
 from erpnext.utilities.transaction_base import TransactionBase
 from erpnext.accounts.party import validate_party_accounts, get_dashboard_info, get_timeline_data # keep this
 from frappe.contacts.address_and_contact import load_address_and_contact, delete_contact_and_address
+from frappe.model.rename_doc import update_linked_doctypes
 
 class Customer(TransactionBase):
 	def get_feed(self):
@@ -53,6 +54,16 @@
 		self.flags.old_lead = self.lead_name
 		validate_party_accounts(self)
 		self.validate_credit_limit_on_change()
+		self.check_customer_group_or_territory_change()
+
+	def check_customer_group_or_territory_change(self):
+		frappe.flags.customer_group, frappe.flags.territory = False, False
+
+		if not self.get('__islocal'):
+			if self.customer_group != frappe.db.get_value('Customer', self.name, 'customer_group'):
+				frappe.flags.customer_group = True
+			if self.territory != frappe.db.get_value('Customer', self.name, 'territory'):
+				frappe.flags.territory = True
 
 	def on_update(self):
 		self.validate_name_with_customer_group()
@@ -65,6 +76,11 @@
 		if self.flags.is_new_doc:
 			self.create_lead_address_contact()
 
+		if frappe.flags.territory:
+			update_linked_doctypes("Customer", "Territory", self.name, self.territory)
+		if frappe.flags.customer_group:
+			update_linked_doctypes("Customer", "Customer Group", self.name, self.customer_group)
+
 	def create_primary_contact(self):
 		if not self.customer_primary_contact and not self.lead_name:
 			if self.mobile_no or self.email_id: