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:
parent
4a0d27078e
commit
2572b05b35
@ -1,13 +1,36 @@
|
||||
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():
|
||||
ignore_doctypes = ["Lead", "Opportunity", "POS Profile", "Tax Rule", "Pricing Rule"]
|
||||
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
|
||||
update_linked_doctypes(territory_fetch, customer.name, customer.territory_value)
|
||||
# Update Territory across all transaction
|
||||
update_linked_doctypes(customer_group_fetch, customer.name, customer.customer_group_value)
|
||||
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(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
|
||||
))
|
||||
|
@ -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, get_fetch_fields
|
||||
from frappe.model.rename_doc import update_linked_doctypes
|
||||
|
||||
class Customer(TransactionBase):
|
||||
def get_feed(self):
|
||||
@ -76,12 +76,15 @@ class Customer(TransactionBase):
|
||||
if self.flags.is_new_doc:
|
||||
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:
|
||||
territory_fetch = get_fetch_fields('Customer', 'Territory')
|
||||
update_linked_doctypes(territory_fetch, self.name, self.territory)
|
||||
update_linked_doctypes('Customer', self.name, 'Territory', self.territory, ignore_doctypes)
|
||||
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)
|
||||
update_linked_doctypes('Customer', self.name, 'Customer Group',
|
||||
self.customer_group, ignore_doctypes)
|
||||
|
||||
def create_primary_contact(self):
|
||||
if not self.customer_primary_contact and not self.lead_name:
|
||||
|
Loading…
x
Reference in New Issue
Block a user