From 8ee5498fe0ebc2c3b25d1e9b208e727742118098 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Tue, 15 Dec 2015 13:14:38 +0530 Subject: [PATCH] [fix] delete lead addresses in delete company transaction --- .../company/delete_company_transactions.py | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py index ba2a5b83f52..ff6810d6967 100644 --- a/erpnext/setup/doctype/company/delete_company_transactions.py +++ b/erpnext/setup/doctype/company/delete_company_transactions.py @@ -17,16 +17,16 @@ def delete_company_transactions(company_name): frappe.throw(_("Transactions can only be deleted by the creator of the Company"), frappe.PermissionError) delete_bins(company_name) - delete_time_logs(company_name) + delete_lead_addresses(company_name) for doctype in frappe.db.sql_list("""select parent from tabDocField where fieldtype='Link' and options='Company'"""): - if doctype not in ("Account", "Cost Center", "Warehouse", "Budget Detail", - "Party Account", "Employee", "Sales Taxes and Charges Template", + if doctype not in ("Account", "Cost Center", "Warehouse", "Budget Detail", + "Party Account", "Employee", "Sales Taxes and Charges Template", "Purchase Taxes and Charges Template", "POS Profile"): delete_for_doctype(doctype, company_name) - + # Clear notification counts clear_notifications() @@ -73,15 +73,22 @@ def delete_time_logs(company_name): # Delete Time Logs as it is linked to Production Order / Project / Task, which are linked to company frappe.db.sql(""" delete from `tabTime Log` - where - (ifnull(project, '') != '' + where + (ifnull(project, '') != '' and exists(select name from `tabProject` where name=`tabTime Log`.project and company=%(company)s)) - or (ifnull(task, '') != '' + or (ifnull(task, '') != '' and exists(select name from `tabTask` where name=`tabTime Log`.task and company=%(company)s)) - or (ifnull(production_order, '') != '' - and exists(select name from `tabProduction Order` + or (ifnull(production_order, '') != '' + and exists(select name from `tabProduction Order` where name=`tabTime Log`.production_order and company=%(company)s)) - or (ifnull(sales_invoice, '') != '' - and exists(select name from `tabSales Invoice` + or (ifnull(sales_invoice, '') != '' + and exists(select name from `tabSales Invoice` where name=`tabTime Log`.sales_invoice and company=%(company)s)) - """, {"company": company_name}) \ No newline at end of file + """, {"company": company_name}) + +def delete_lead_addresses(company_name): + """Delete addresses to which leads are linked""" + frappe.db.sql("""delete from `tabAddress` + where (customer='' or customer is null) and (supplier='' or supplier is null) and (lead != '' and lead is not null)""") + + frappe.db.sql("""update `tabAddress` set lead=null, lead_name=null""")