remove updating territory for customers on change (#13162)

This commit is contained in:
Zarrar 2018-03-05 12:32:41 +05:30 committed by Nabin Hait
parent 5fd7b3bb80
commit 2550180a05
2 changed files with 28 additions and 34 deletions

View File

@ -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
))

View File

@ -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)