From 54d85900643eef286e1c42d69958c94a6f63b53b Mon Sep 17 00:00:00 2001 From: shreyas Date: Mon, 17 Oct 2016 14:25:20 +0530 Subject: [PATCH 1/3] [Fix] Delete communications when user deletes company transactions --- .../doctype/company/delete_company_transactions.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py index 8f058e86ed..e5196d167b 100644 --- a/erpnext/setup/doctype/company/delete_company_transactions.py +++ b/erpnext/setup/doctype/company/delete_company_transactions.py @@ -14,7 +14,7 @@ def delete_company_transactions(company_name): doc = frappe.get_doc("Company", company_name) if frappe.session.user != doc.owner: - frappe.throw(_("Transactions can only be deleted by the creator of the Company"), + frappe.throw(_("Transactions can only be deleted by the creator of the Company"), frappe.PermissionError) delete_bins(company_name) @@ -64,6 +64,16 @@ def delete_for_doctype(doctype, company_name): frappe.db.sql("""update tabSeries set current = %s where name=%s""", (last, prefix)) + # delete communication + try: + frappe.db.sql(""" + select name, reference_doctype from `tabCommunication` + where reference_doctype = "{0}" and + exists(select name from `tab{1}` where company = %s and + `tabCommunication`.reference_name = name) + """.format(doctype, doctype), company_name) + except Exception, e: + print e def delete_bins(company_name): frappe.db.sql("""delete from tabBin where warehouse in From 0f4cb55c3ca15979d942f0e3faaabedc4f199ae5 Mon Sep 17 00:00:00 2001 From: shreyas Date: Wed, 19 Oct 2016 14:41:48 +0530 Subject: [PATCH 2/3] [Fix] Delete communications before deleting the docs --- .../company/delete_company_transactions.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py index e5196d167b..9242e43792 100644 --- a/erpnext/setup/doctype/company/delete_company_transactions.py +++ b/erpnext/setup/doctype/company/delete_company_transactions.py @@ -37,6 +37,9 @@ def delete_for_doctype(doctype, company_name): if not meta.issingle: if not meta.istable: + # delete communication + delete_communications(meta, doctype, company_name) + # delete children for df in meta.get_table_fields(): frappe.db.sql("""delete from `tab{0}` where parent in @@ -64,17 +67,6 @@ def delete_for_doctype(doctype, company_name): frappe.db.sql("""update tabSeries set current = %s where name=%s""", (last, prefix)) - # delete communication - try: - frappe.db.sql(""" - select name, reference_doctype from `tabCommunication` - where reference_doctype = "{0}" and - exists(select name from `tab{1}` where company = %s and - `tabCommunication`.reference_name = name) - """.format(doctype, doctype), company_name) - except Exception, e: - print e - def delete_bins(company_name): frappe.db.sql("""delete from tabBin where warehouse in (select name from tabWarehouse where company=%s)""", company_name) @@ -86,3 +78,11 @@ def delete_lead_addresses(company_name): where lead=%s and (customer='' or customer is null) and (supplier='' or supplier is null)""", lead.name) frappe.db.sql("""update `tabAddress` set lead=null, lead_name=null where lead=%s""", lead.name) + +def delete_communications(meta, doctype, company_name): + linkfields = [df.fieldname for df in meta.get_link_fields()] + + if 'company' in linkfields: + frappe.db.sql("""DELETE FROM `tabCommunication` WHERE reference_doctype = "{0}" AND + EXISTS (SELECT name FROM `tab{1}` WHERE company = %s AND `tabCommunication`.reference_name = name) + """.format(doctype, doctype), company_name) From c48eb454106bc31b28a996ccf9720912a494f226 Mon Sep 17 00:00:00 2001 From: shreyas Date: Mon, 24 Oct 2016 10:04:13 +0530 Subject: [PATCH 3/3] [Fix] Use company_fieldname as parameter to the query --- .../doctype/company/delete_company_transactions.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/erpnext/setup/doctype/company/delete_company_transactions.py b/erpnext/setup/doctype/company/delete_company_transactions.py index 9242e43792..eb5c043b50 100644 --- a/erpnext/setup/doctype/company/delete_company_transactions.py +++ b/erpnext/setup/doctype/company/delete_company_transactions.py @@ -38,7 +38,7 @@ def delete_for_doctype(doctype, company_name): if not meta.issingle: if not meta.istable: # delete communication - delete_communications(meta, doctype, company_name) + delete_communications(doctype, company_name, company_fieldname) # delete children for df in meta.get_table_fields(): @@ -79,10 +79,8 @@ def delete_lead_addresses(company_name): frappe.db.sql("""update `tabAddress` set lead=null, lead_name=null where lead=%s""", lead.name) -def delete_communications(meta, doctype, company_name): - linkfields = [df.fieldname for df in meta.get_link_fields()] - - if 'company' in linkfields: - frappe.db.sql("""DELETE FROM `tabCommunication` WHERE reference_doctype = "{0}" AND - EXISTS (SELECT name FROM `tab{1}` WHERE company = %s AND `tabCommunication`.reference_name = name) - """.format(doctype, doctype), company_name) +def delete_communications(doctype, company_name, company_fieldname): + frappe.db.sql(""" + DELETE FROM `tabCommunication` WHERE reference_doctype = %s AND + EXISTS (SELECT name FROM `tab{0}` WHERE {1} = %s AND `tabCommunication`.reference_name = name) + """.format(doctype, company_fieldname), (doctype, company_name))