From 8d76d14614d342209282eaf10959de8052a1fe06 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 25 May 2015 12:22:39 +0530 Subject: [PATCH] reset item's default accounts, warehouses on deletion of company --- erpnext/setup/doctype/company/company.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index 50f2865bb3..227512d017 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -178,6 +178,10 @@ class Company(Document): """ Trash accounts and cost centers for this company if no gl entry exists """ + accounts = frappe.db.sql_list("select name from tabAccount where company=%s", self.name) + cost_centers = frappe.db.sql_list("select name from `tabCost Center` where company=%s", self.name) + warehouses = frappe.db.sql_list("select name from tabWarehouse where company=%s", self.name) + rec = frappe.db.sql("SELECT name from `tabGL Entry` where company = %s", self.name) if not rec: # delete Account @@ -196,7 +200,24 @@ class Company(Document): frappe.db.sql("""delete from `tabWarehouse` where company=%s""", self.name) frappe.defaults.clear_default("company", value=self.name) + + # clear default accounts, warehouses from item + for f in ["default_warehouse", "website_warehouse"]: + frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)""" + % (f, f, ', '.join(['%s']*len(warehouses))), tuple(warehouses)) + + frappe.db.sql("""update `tabItem Reorder` set warehouse=NULL where warehouse in (%s)""" + % ', '.join(['%s']*len(warehouses)), tuple(warehouses)) + + for f in ["income_account", "expense_account"]: + frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)""" + % (f, f, ', '.join(['%s']*len(accounts))), tuple(accounts)) + + for f in ["selling_cost_center", "buying_cost_center"]: + frappe.db.sql("""update tabItem set %s=NULL where %s in (%s)""" + % (f, f, ', '.join(['%s']*len(cost_centers))), tuple(cost_centers)) + # reset default company frappe.db.sql("""update `tabSingles` set value="" where doctype='Global Defaults' and field='default_company' and value=%s""", self.name)