From 2550180a0524c1c5ec495197d05f5dc99e4928dc Mon Sep 17 00:00:00 2001 From: Zarrar Date: Mon, 5 Mar 2018 12:32:41 +0530 Subject: [PATCH] remove updating territory for customers on change (#13162) --- .../update_territory_and_customer_group.py | 48 +++++++++---------- erpnext/selling/doctype/customer/customer.py | 14 ++---- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/erpnext/patches/v10_0/update_territory_and_customer_group.py b/erpnext/patches/v10_0/update_territory_and_customer_group.py index f08ac68679..a4a562c0d5 100644 --- a/erpnext/patches/v10_0/update_territory_and_customer_group.py +++ b/erpnext/patches/v10_0/update_territory_and_customer_group.py @@ -3,34 +3,32 @@ from frappe.model.rename_doc import get_fetch_fields def execute(): ignore_doctypes = ["Lead", "Opportunity", "POS Profile", "Tax Rule", "Pricing Rule"] - customers = frappe.get_all('Customer', fields=["name", "territory", "customer_group"]) + customers = frappe.get_all('Customer', fields=["name", "customer_group"]) - territory_fetch = get_fetch_fields('Customer', 'Territory', ignore_doctypes) customer_group_fetch = get_fetch_fields('Customer', 'Customer Group', ignore_doctypes) batch_size = 1000 for i in range(0, len(customers), batch_size): batch_customers = customers[i:i + batch_size] - for source_fieldname, linked_doctypes_info in [["customer_group", customer_group_fetch], ["territory", territory_fetch]]: - for d in linked_doctypes_info: - when_then = [] - for customer in batch_customers: - when_then.append(''' - WHEN `{master_fieldname}` = "{docname}" and {linked_to_fieldname} != "{value}" - THEN "{value}" - '''.format( - master_fieldname=d["master_fieldname"], - linked_to_fieldname=d["linked_to_fieldname"], - docname=frappe.db.escape(frappe.as_unicode(customer.name)), - value=frappe.db.escape(frappe.as_unicode(customer.get(source_fieldname))))) - - frappe.db.sql(""" - update - `tab{doctype}` - set - {linked_to_fieldname} = CASE {when_then_cond} ELSE `{linked_to_fieldname}` END - """.format( - doctype = d['doctype'], - when_then_cond=" ".join(when_then), - linked_to_fieldname=d.linked_to_fieldname - )) + for d in customer_group_fetch: + when_then = [] + for customer in batch_customers: + when_then.append(''' + WHEN `{master_fieldname}` = "{docname}" and {linked_to_fieldname} != "{value}" + THEN "{value}" + '''.format( + master_fieldname=d["master_fieldname"], + linked_to_fieldname=d["linked_to_fieldname"], + docname=frappe.db.escape(frappe.as_unicode(customer.name)), + value=frappe.db.escape(frappe.as_unicode(customer.get("customer_group"))))) + + frappe.db.sql(""" + update + `tab{doctype}` + set + {linked_to_fieldname} = CASE {when_then_cond} ELSE `{linked_to_fieldname}` END + """.format( + doctype = d['doctype'], + when_then_cond=" ".join(when_then), + linked_to_fieldname=d.linked_to_fieldname + )) diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index b84a952ece..0ea1119c76 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -54,16 +54,14 @@ class Customer(TransactionBase): self.flags.old_lead = self.lead_name validate_party_accounts(self) self.validate_credit_limit_on_change() - self.check_customer_group_or_territory_change() + self.check_customer_group_change() - def check_customer_group_or_territory_change(self): - frappe.flags.customer_group_changed, frappe.flags.territory_changed = False, False + def check_customer_group_change(self): + frappe.flags.customer_group_changed = False if not self.get('__islocal'): if self.customer_group != frappe.db.get_value('Customer', self.name, 'customer_group'): frappe.flags.customer_group_changed = True - if self.territory != frappe.db.get_value('Customer', self.name, 'territory'): - frappe.flags.territory_changed = True def on_update(self): self.validate_name_with_customer_group() @@ -76,12 +74,10 @@ class Customer(TransactionBase): if self.flags.is_new_doc: self.create_lead_address_contact() - self.update_territory_and_customer_groups() + self.update_customer_groups() - def update_territory_and_customer_groups(self): + def update_customer_groups(self): ignore_doctypes = ["Lead", "Opportunity", "POS Profile", "Tax Rule", "Pricing Rule"] - if frappe.flags.territory_changed: - update_linked_doctypes('Customer', self.name, 'Territory', self.territory, ignore_doctypes) if frappe.flags.customer_group_changed: update_linked_doctypes('Customer', self.name, 'Customer Group', self.customer_group, ignore_doctypes)