Update territory and customer_group patch optimization (#13076)

* Update territory and customer_group patch optimization

* Update update_territory_and_customer_group.py
This commit is contained in:
Nabin Hait 2018-02-25 10:45:24 +05:30 committed by GitHub
parent 4a0d27078e
commit 2572b05b35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 13 deletions

View File

@ -1,13 +1,36 @@
import frappe import frappe
from frappe.model.rename_doc import update_linked_doctypes, get_fetch_fields from frappe.model.rename_doc import get_fetch_fields
def execute(): 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", "territory", "customer_group"])
territory_fetch = get_fetch_fields('Customer', 'Territory')
customer_group_fetch = get_fetch_fields('Customer', 'Customer Group')
for customer in customers: territory_fetch = get_fetch_fields('Customer', 'Territory', ignore_doctypes)
# Update Territory across all transaction customer_group_fetch = get_fetch_fields('Customer', 'Customer Group', ignore_doctypes)
update_linked_doctypes(territory_fetch, customer.name, customer.territory_value)
# Update Territory across all transaction batch_size = 1000
update_linked_doctypes(customer_group_fetch, customer.name, customer.customer_group_value) 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(customer.name).encode("utf-8"),
value=frappe.db.escape(customer.get(source_fieldname)).encode("utf-8")))
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
))

View File

@ -11,7 +11,7 @@ from frappe.desk.reportview import build_match_conditions
from erpnext.utilities.transaction_base import TransactionBase from erpnext.utilities.transaction_base import TransactionBase
from erpnext.accounts.party import validate_party_accounts, get_dashboard_info, get_timeline_data # keep this 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.contacts.address_and_contact import load_address_and_contact, delete_contact_and_address
from frappe.model.rename_doc import update_linked_doctypes, get_fetch_fields from frappe.model.rename_doc import update_linked_doctypes
class Customer(TransactionBase): class Customer(TransactionBase):
def get_feed(self): def get_feed(self):
@ -76,12 +76,15 @@ class Customer(TransactionBase):
if self.flags.is_new_doc: if self.flags.is_new_doc:
self.create_lead_address_contact() self.create_lead_address_contact()
self.update_territory_and_customer_groups()
def update_territory_and_customer_groups(self):
ignore_doctypes = ["Lead", "Opportunity", "POS Profile", "Tax Rule", "Pricing Rule"]
if frappe.flags.territory_changed: if frappe.flags.territory_changed:
territory_fetch = get_fetch_fields('Customer', 'Territory') update_linked_doctypes('Customer', self.name, 'Territory', self.territory, ignore_doctypes)
update_linked_doctypes(territory_fetch, self.name, self.territory)
if frappe.flags.customer_group_changed: if frappe.flags.customer_group_changed:
customer_group_fetch = get_fetch_fields('Customer', 'Customer Group') update_linked_doctypes('Customer', self.name, 'Customer Group',
update_linked_doctypes(customer_group_fetch, self.name, self.customer_group) self.customer_group, ignore_doctypes)
def create_primary_contact(self): def create_primary_contact(self):
if not self.customer_primary_contact and not self.lead_name: if not self.customer_primary_contact and not self.lead_name: