From 4a0d27078e386c6b2eb0fc315d5c32e89a2ab750 Mon Sep 17 00:00:00 2001 From: Zarrar Date: Fri, 23 Feb 2018 17:41:32 +0530 Subject: [PATCH] optimize patch for faster execution (#13068) --- .../update_territory_and_customer_group.py | 14 +++++++------- erpnext/selling/doctype/customer/customer.py | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 15 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 52a6db2b73..56152f928c 100644 --- a/erpnext/patches/v10_0/update_territory_and_customer_group.py +++ b/erpnext/patches/v10_0/update_territory_and_customer_group.py @@ -1,13 +1,13 @@ import frappe -from frappe.model.rename_doc import update_linked_doctypes +from frappe.model.rename_doc import update_linked_doctypes, get_fetch_fields def execute(): - customers = frappe.get_all('Customer') + customers = frappe.get_all('Customer', fields=["name", "territory", "customer_group"]) + territory_fetch = get_fetch_fields('Customer', 'Territory') + customer_group_fetch = get_fetch_fields('Customer', 'Customer Group') + 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_linked_doctypes(territory_fetch, customer.name, customer.territory_value) # Update Territory across all transaction - cust_group = frappe.get_value('Customer', customer, 'customer_group') - update_linked_doctypes("Customer", "Customer Group", customer.name, cust_group) + update_linked_doctypes(customer_group_fetch, customer.name, customer.customer_group_value) diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 9b44bc9dd0..47496ebdca 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -11,7 +11,7 @@ from frappe.desk.reportview import build_match_conditions 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 +from frappe.model.rename_doc import update_linked_doctypes, get_fetch_fields class Customer(TransactionBase): def get_feed(self): @@ -57,13 +57,13 @@ class Customer(TransactionBase): self.check_customer_group_or_territory_change() def check_customer_group_or_territory_change(self): - frappe.flags.customer_group, frappe.flags.territory = False, False + frappe.flags.customer_group_changed, frappe.flags.territory_changed = 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 + frappe.flags.customer_group_changed = True if self.territory != frappe.db.get_value('Customer', self.name, 'territory'): - frappe.flags.territory = True + frappe.flags.territory_changed = True def on_update(self): self.validate_name_with_customer_group() @@ -76,10 +76,12 @@ class Customer(TransactionBase): 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) + if frappe.flags.territory_changed: + territory_fetch = get_fetch_fields('Customer', 'Territory') + update_linked_doctypes(territory_fetch, self.name, self.territory) + if frappe.flags.customer_group_changed: + customer_group_fetch = get_fetch_fields('Customer', 'Customer Group') + update_linked_doctypes(customer_group_fetch, self.name, self.customer_group) def create_primary_contact(self): if not self.customer_primary_contact and not self.lead_name: