Merge pull request #3973 from nabinhait/fix14
[Fix] Delete Company Transactions
diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py
index 87c1375..f7334b1 100644
--- a/erpnext/setup/doctype/company/delete_company_transactions.py
+++ b/erpnext/setup/doctype/company/delete_company_transactions.py
@@ -6,6 +6,7 @@
from frappe.utils import cint
from frappe import _
+from frappe.desk.notifications import clear_notifications
@frappe.whitelist()
def delete_company_transactions(company_name):
@@ -16,11 +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)
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"):
+ if doctype not in ("Account", "Cost Center", "Warehouse", "Budget Detail", "Party Account", "Employee"):
delete_for_doctype(doctype, company_name)
+
+ # Clear notification counts
+ clear_notifications()
def delete_for_doctype(doctype, company_name):
meta = frappe.get_meta(doctype)
@@ -60,3 +66,20 @@
def delete_bins(company_name):
frappe.db.sql("""delete from tabBin where warehouse in
(select name from tabWarehouse where company=%s)""", company_name)
+
+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, '') != ''
+ and exists(select name from `tabProject` where name=`tabTime Log`.project and company=%(company)s))
+ 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`
+ where name=`tabTime Log`.production_order and company=%(company)s))
+ 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