From 706baf77aef6194f4a2f40058c26c15771535460 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 23 Jan 2012 18:35:28 +0530 Subject: [PATCH] customer address contact patch --- .../customer_address_contact_patch.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 erpnext/patches/jan_mar_2012/customer_address_contact_patch.py diff --git a/erpnext/patches/jan_mar_2012/customer_address_contact_patch.py b/erpnext/patches/jan_mar_2012/customer_address_contact_patch.py new file mode 100644 index 0000000000..6a7d40568c --- /dev/null +++ b/erpnext/patches/jan_mar_2012/customer_address_contact_patch.py @@ -0,0 +1,58 @@ +import webnotes + +def execute(): + """ + * Reload Search Criteria "Customer Address Contact" + * SET is_primary_contact=1, is_primary_address=1 WHERE not specified + """ + reload_sc() + patch_primary_contact() + patch_primary_address() + +def reload_sc(): + from webnotes.modules.module_manager import reload_doc + reload_doc('selling', 'search_criteria', 'customer_address_contact') + reload_doc('selling', 'Module Def', 'Selling') + +def patch_primary_contact(): + res = webnotes.conn.sql(""" + SELECT name FROM `tabContact` + WHERE customer IN ( + SELECT customer FROM `tabContact` + WHERE IFNULL(customer, '')!='' + GROUP BY customer HAVING SUM(IFNULL(is_primary_contact, 0))=0 + ) OR supplier IN ( + SELECT supplier FROM `tabContact` + WHERE IFNULL(supplier, '')!='' + GROUP BY supplier HAVING SUM(IFNULL(is_primary_contact, 0))=0 + ) OR sales_partner IN ( + SELECT sales_partner FROM `tabContact` + WHERE IFNULL(sales_partner, '')!='' + GROUP BY sales_partner HAVING SUM(IFNULL(is_primary_contact, 0))=0 + ) + """, as_list=1) + names = ", ".join(["'" + str(r[0]) + "'" for r in res if r]) + if names: webnotes.conn.sql("UPDATE `tabContact` SET is_primary_contact=1 WHERE name IN (%s)" % names) + +def patch_primary_address(): + res = webnotes.conn.sql(""" + SELECT name FROM `tabAddress` + WHERE customer IN ( + SELECT customer FROM `tabAddress` + WHERE IFNULL(customer, '')!='' + GROUP BY customer HAVING SUM(IFNULL(is_primary_address, 0))=0 + AND SUM(IFNULL(is_shipping_address, 0))=0 + ) OR supplier IN ( + SELECT supplier FROM `tabAddress` + WHERE IFNULL(supplier, '')!='' + GROUP BY supplier HAVING SUM(IFNULL(is_primary_address, 0))=0 + AND SUM(IFNULL(is_shipping_address, 0))=0 + ) OR sales_partner IN ( + SELECT sales_partner FROM `tabAddress` + WHERE IFNULL(sales_partner, '')!='' + GROUP BY sales_partner HAVING SUM(IFNULL(is_primary_address, 0))=0 + AND SUM(IFNULL(is_shipping_address, 0))=0 + ) + """, as_list=1) + names = ", ".join(["'" + str(r[0]) + "'" for r in res if r]) + if names: webnotes.conn.sql("UPDATE `tabAddress` SET is_primary_address=1 WHERE name IN (%s)" % names)