From 79756c44c9ecc3e9d175eeaf1ffc1d39c30ab34e Mon Sep 17 00:00:00 2001 From: Zarrar Date: Thu, 22 Feb 2018 10:57:49 +0530 Subject: [PATCH] Update Territory & Customer Group across all transaction (#13004) * added method for update query based on changes * patch added * updated function, moved util function --- erpnext/patches.txt | 1 + .../update_territory_and_customer_group.py | 13 +++++++++++++ erpnext/selling/doctype/customer/customer.py | 18 +++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 erpnext/patches/v10_0/update_territory_and_customer_group.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 331cd1e10ad..65c391eda21 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -502,3 +502,4 @@ erpnext.patches.v10_0.update_translatable_fields erpnext.patches.v10_0.rename_offer_letter_to_job_offer execute:frappe.delete_doc('DocType', 'Production Planning Tool', ignore_missing=True) erpnext.patches.v10_0.migrate_daily_work_summary_settings_to_daily_work_summary_group +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 00000000000..52a6db2b73d --- /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 6f25bf7fd6f..9b44bc9dd0a 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -11,6 +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 class Customer(TransactionBase): def get_feed(self): @@ -53,6 +54,16 @@ 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() + + 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 @@ 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) + def create_primary_contact(self): if not self.customer_primary_contact and not self.lead_name: if self.mobile_no or self.email_id: @@ -320,4 +336,4 @@ def get_customer_primary_address(doctype, txt, searchfield, start, page_len, fil """, { 'customer': customer, 'txt': '%%%s%%' % txt - }) \ No newline at end of file + })