[fix] delete lead addresses in delete company transaction
diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py
index ba2a5b8..ff6810d 100644
--- a/erpnext/setup/doctype/company/delete_company_transactions.py
+++ b/erpnext/setup/doctype/company/delete_company_transactions.py
@@ -17,16 +17,16 @@
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 @@
# 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""")