diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 73831bf9af..74977cd8bc 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -308,6 +308,9 @@ doc_events = { }, ('Quotation', 'Sales Order', 'Sales Invoice'): { 'validate': ["erpnext.erpnext_integrations.taxjar_integration.set_sales_tax"] + }, + "Company": { + "on_trash": "erpnext.regional.india.utils.delete_gst_settings_for_company" } } diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py index 949733e0ad..4e4dcf8585 100644 --- a/erpnext/regional/india/utils.py +++ b/erpnext/regional/india/utils.py @@ -871,3 +871,20 @@ def set_item_tax_from_hsn_code(item): 'tax_category': tax.tax_category, 'valid_from': tax.valid_from }) + +def delete_gst_settings_for_company(doc, method): + if doc.country != 'India': + return + + gst_settings = frappe.get_doc("GST Settings") + records_to_delete = [] + + for d in reversed(gst_settings.get('gst_accounts')): + if d.company == doc.name: + records_to_delete.append(d) + + for d in records_to_delete: + gst_settings.remove(d) + + gst_settings.save() + diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index 54c67538ae..45d5ce0c1c 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -393,6 +393,10 @@ class Company(NestedSet): frappe.db.sql("delete from `tabPurchase Taxes and Charges Template` where company=%s", self.name) frappe.db.sql("delete from `tabItem Tax Template` where company=%s", self.name) + # delete Process Deferred Accounts if no GL Entry found + if not frappe.db.get_value('GL Entry', {'company': self.name}): + frappe.db.sql("delete from `tabProcess Deferred Accounting` where company=%s", self.name) + @frappe.whitelist() def enqueue_replace_abbr(company, old, new): kwargs = dict(queue="long", company=company, old=old, new=new)