diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py index ff85666aecc..ed49c2d791e 100644 --- a/accounts/doctype/account/account.py +++ b/accounts/doctype/account/account.py @@ -120,7 +120,7 @@ class DocType: # check if child exists # ================================================================== def check_if_child_exists(self): - return sql("select name from `tabAccount` where parent_account = %s and docstatus<2", self.doc.name) + return sql("select name from `tabAccount` where parent_account = %s and docstatus != 2", self.doc.name, debug=0) # Update balance # ================================================================== @@ -234,24 +234,27 @@ class DocType: if flt(self.get_curr_bal()) != 0: msgprint("Account with existing balance can not be trashed", raise_exception=1) if self.check_if_child_exists(): - msgprint("Child account exists for this account. You can not make trash this account.", raise_exception=1) + msgprint("Child account exists for this account. You can not trash this account.", raise_exception=1) # get current year balance # ================================================================== def get_curr_bal(self): - bal = sql("select balance from `tabAccount Balance` where period = '%s' and parent = '%s'" % (get_defaults()['fiscal_year'], self.doc.name)) + bal = sql("select balance from `tabAccount Balance` where period = '%s' and parent = '%s'" % (get_defaults()['fiscal_year'], self.doc.name),debug=0) return bal and flt(bal[0][0]) or 0 # On Trash # ================================================================== def on_trash(self): # Check balance before trash - self.check_balance_before_trash() + self.check_balance_before_trash() # rebuild tree set(self.doc,'old_parent', '') self.update_nsm_model() + #delete Account Balance + sql("delete from `tabAccount Balance` where account = %s", self.doc.name) + # On restore # ================================================================== def on_restore(self): diff --git a/accounts/doctype/cost_center/cost_center.py b/accounts/doctype/cost_center/cost_center.py index dea00ecd5bc..3b374c5e26c 100644 --- a/accounts/doctype/cost_center/cost_center.py +++ b/accounts/doctype/cost_center/cost_center.py @@ -24,8 +24,7 @@ class DocType: def autoname(self): #company_abbr = sql("select abbr from tabCompany where name=%s", self.doc.company)[0][0] - self.doc.name = self.doc.cost_center_name + ' - ' + self.doc.company_abbr - + self.doc.name = self.doc.cost_center_name + ' - ' + self.doc.company_abbr def get_abbr(self): abbr = sql("select abbr from tabCompany where company_name='%s'"%(self.doc.company_name))[0][0] or '' @@ -35,10 +34,8 @@ class DocType: return cstr(ret) def validate(self): - # Cost Center name must be unique # --------------------------- - if (self.doc.__islocal or (not self.doc.name)) and sql("select name from `tabCost Center` where cost_center_name = %s and company_name=%s", (self.doc.cost_center_name, self.doc.company_name)): msgprint("Cost Center Name already exists, please rename") raise Exception @@ -55,4 +52,17 @@ class DocType: import webnotes import webnotes.utils.nestedset # update Node Set Model - webnotes.utils.nestedset.update_nsm(self) + webnotes.utils.nestedset.update_nsm(self) + + def check_if_child_exists(self): + return sql("select name from `tabCost Center` where parent_cost_center = %s and docstatus != 2", self.doc.name, debug=1) + + # On Trash + # -------- + def on_trash(self): + if self.check_if_child_exists(): + msgprint("Child exists for this cost center. You can not trash this account.", raise_exception=1) + + # rebuild tree + set(self.doc,'old_parent', '') + self.update_nsm_model() diff --git a/setup/doctype/company/company.py b/setup/doctype/company/company.py index fe60a2b862e..ac5d0d63d37 100644 --- a/setup/doctype/company/company.py +++ b/setup/doctype/company/company.py @@ -202,17 +202,35 @@ class DocType: # Trash accounts and cost centers for this company # --------------------------------------------------- - def on_trash(self): - acc = sql("select name from tabAccount where company = '%s' and docstatus = 1" % self.doc.name) - for each in acc: - get_obj('Account', each[0]).on_trash() + #def on_trash1(self): + # acc = sql("select name from tabAccount where company = '%s' and docstatus != 2 order by lft desc, rgt desc limit 2" % self.doc.name, debug=1) + # for each in acc: + # get_obj('Account', each[0]).on_trash() - cc = sql("select name from `tabCost Center` where company_name = '%s' and docstatus = 1" % self.doc.name) - for each in cc: - get_obj('Cost Center', each[0]).on_trash() + # cc = sql("select name from `tabCost Center` where company_name = '%s' and docstatus != 2" % self.doc.name) + # for each in cc: + # get_obj('Cost Center', each[0]).on_trash() - msgprint("Company trashed. All the accounts and cost centers related to this company also trashed. You can restore it anytime from Setup -> Manage Trash") - + # msgprint("Company trashed. All the accounts and cost centers related to this company also trashed. You can restore it anytime from Setup -> Manage Trash") + + def on_trash(self): + rec = sql("SELECT sum(ab.opening), sum(ab.balance), sum(ab.debit), sum(ab.credit) FROM `tabAccount Balance` ab, `tabAccount` a WHERE ab.account = a.name and a.company = %s", self.doc.name) + if rec[0][0] == 0 and rec[0][1] == 0 and rec[0][2] == 0 and rec[0][3] == 0: + #delete tabAccount Balance + sql("delete ab.* from `tabAccount Balance` ab, `tabAccount` a where ab.account = a.name and a.company = %s", self.doc.name) + #delete tabAccount + sql("delete from `tabAccount` where company = %s order by lft desc, rgt desc", self.doc.name) + + #delete cost center child table - budget detail + sql("delete bd.* from `tabBudget Detail` bd, `tabCost Center` cc where bd.parent = cc.name and cc.company_name = %s", self.doc.name) + #delete cost center + sql("delete from `tabCost Center` WHERE company_name = %s order by lft desc, rgt desc", self.doc.name) + + #update value as blank for tabDefaultValue defkey=company + sql("update `tabDefaultValue` set defvalue = '' where defkey='company' and defvalue = %s", self.doc.name) + + #update value as blank for tabSingles Manage Account + sql("update `tabSingles` set value = '' where doctype='Manage Account' and field = 'default_company' and value = %s", self.doc.name) # Restore accounts and cost centers for this company # ---------------------------------------------------