From cddbe6dd80fdb1aae8f9c06e7b43e27d55055e56 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 11 Dec 2012 14:35:45 +0530 Subject: [PATCH] validation while created through company saving and root deleted --- accounts/doctype/account/account.py | 77 ++++++++++++++++------------- setup/doctype/company/company.py | 8 ++- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py index 3f7a18128e3..042cd5a259f 100644 --- a/accounts/doctype/account/account.py +++ b/accounts/doctype/account/account.py @@ -34,22 +34,21 @@ class DocType: company_abbr = sql("select abbr from tabCompany where name=%s", self.doc.company)[0][0] self.doc.name = self.doc.account_name.strip() + ' - ' + company_abbr - # Get customer/supplier address def get_address(self): - add=sql("Select address from `tab%s` where name='%s'"%(self.doc.master_type,self.doc.master_name)) - ret={'address':add[0][0]} - return ret - - # check whether master name entered for supplier/customer + add=sql("Select address from `tab%s` where name='%s'" % + (self.doc.master_type, self.doc.master_name)) + return {'address': add[0][0]} + def validate_master_name(self): - if (self.doc.master_type == 'Customer' or self.doc.master_type == 'Supplier') and not self.doc.master_name: + if (self.doc.master_type == 'Customer' or self.doc.master_type == 'Supplier') \ + and not self.doc.master_name: msgprint("Message: Please enter Master Name once the account is created.") - - # Fetch Parent Details and validation for account not to be created under ledger def validate_parent(self): + """Fetch Parent Details and validation for account not to be created under ledger""" if self.doc.parent_account: - par = sql("select name, group_or_ledger, is_pl_account, debit_or_credit from tabAccount where name =%s",self.doc.parent_account) + par = sql("""select name, group_or_ledger, is_pl_account, debit_or_credit + from tabAccount where name =%s""",self.doc.parent_account) if not par: msgprint("Parent account does not exists", raise_exception=1) elif par and par[0][0] == self.doc.name: @@ -57,7 +56,8 @@ class DocType: elif par and par[0][1] != 'Group': msgprint("Parent account can not be a ledger", raise_exception=1) elif par and self.doc.debit_or_credit and par[0][3] != self.doc.debit_or_credit: - msgprint("You can not move a %s account under %s account" % (self.doc.debit_or_credit, par[0][3]), raise_exception=1) + msgprint("You can not move a %s account under %s account" % + (self.doc.debit_or_credit, par[0][3]), raise_exception=1) elif par and not self.doc.is_pl_account: self.doc.is_pl_account = par[0][2] self.doc.debit_or_credit = par[0][3] @@ -69,9 +69,10 @@ class DocType: webnotes.msgprint("One company cannot have more than 4 root Accounts", raise_exception=1) - # Account name must be unique def validate_duplicate_account(self): - if (self.doc.fields.get('__islocal') or (not self.doc.name)) and sql("select name from tabAccount where account_name=%s and company=%s", (self.doc.account_name, self.doc.company)): + if (self.doc.fields.get('__islocal') or not self.doc.name) and \ + sql("""select name from tabAccount where account_name=%s and company=%s""", + (self.doc.account_name, self.doc.company)): msgprint("Account Name: %s already exists, please rename" % self.doc.account_name, raise_exception=1) @@ -83,9 +84,11 @@ class DocType: def convert_group_to_ledger(self): if self.check_if_child_exists(): - msgprint("Account: %s has existing child. You can not convert this account to ledger" % (self.doc.name), raise_exception=1) + msgprint("Account: %s has existing child. You can not convert this account to ledger" % + (self.doc.name), raise_exception=1) elif self.check_gle_exists(): - msgprint("Account with existing transaction can not be converted to ledger.", raise_exception=1) + msgprint("Account with existing transaction can not be converted to ledger.", + raise_exception=1) else: self.doc.group_or_ledger = 'Ledger' self.doc.save() @@ -105,11 +108,13 @@ class DocType: # Check if any previous balance exists def check_gle_exists(self): - exists = sql("select name from `tabGL Entry` where account = '%s' and ifnull(is_cancelled, 'No') = 'No'" % (self.doc.name)) + exists = sql("""select name from `tabGL Entry` where account = '%s' + and ifnull(is_cancelled, 'No') = 'No'""" % (self.doc.name)) return exists and exists[0][0] or '' 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) def validate_mandatory(self): if not self.doc.debit_or_credit: @@ -124,61 +129,63 @@ class DocType: self.validate_root_details() self.validate_mandatory() - # Defaults if not self.doc.parent_account: self.doc.parent_account = '' - # Update Node Set Model def update_nsm_model(self): import webnotes import webnotes.utils.nestedset webnotes.utils.nestedset.update_nsm(self) def on_update(self): - # update nsm self.validate_max_root_accounts() self.update_nsm_model() - # Check user role for approval process def get_authorized_user(self): # Check logged-in user is authorized - if webnotes.conn.get_value('Global Defaults', None, 'credit_controller') in webnotes.user.get_roles(): + if webnotes.conn.get_value('Global Defaults', None, 'credit_controller') \ + in webnotes.user.get_roles(): return 1 - # Check Credit limit for customer def check_credit_limit(self, account, company, tot_outstanding): # Get credit limit credit_limit_from = 'Customer' - cr_limit = sql("select t1.credit_limit from tabCustomer t1, `tabAccount` t2 where t2.name='%s' and t1.name = t2.master_name" % account) + cr_limit = sql("""select t1.credit_limit from tabCustomer t1, `tabAccount` t2 + where t2.name='%s' and t1.name = t2.master_name""" % account) credit_limit = cr_limit and flt(cr_limit[0][0]) or 0 if not credit_limit: credit_limit = webnotes.conn.get_value('Company', company, 'credit_limit') credit_limit_from = 'global settings in the Company' # If outstanding greater than credit limit and not authorized person raise exception - if credit_limit > 0 and flt(tot_outstanding) > credit_limit and not self.get_authorized_user(): - msgprint("Total Outstanding amount (%s) for %s can not be greater than credit limit (%s). To change your credit limit settings, please update the %s" \ - % (fmt_money(tot_outstanding), account, fmt_money(credit_limit), credit_limit_from), raise_exception=1) + if credit_limit > 0 and flt(tot_outstanding) > credit_limit \ + and not self.get_authorized_user(): + msgprint("""Total Outstanding amount (%s) for %s can not be \ + greater than credit limit (%s). To change your credit limit settings, \ + please update the %s""" % (fmt_money(tot_outstanding), + account, fmt_money(credit_limit), credit_limit_from), raise_exception=1) def validate_trash(self): """checks gl entries and if child exists""" + if not self.doc.parent_account: + msgprint("Root account can not be deleted", raise_exception=1) + if self.check_gle_exists(): - msgprint("Account with existing transaction (Sales Invoice / Purchase Invoice / Journal Voucher) can not be trashed", raise_exception=1) + msgprint("""Account with existing transaction (Sales Invoice / Purchase Invoice / \ + Journal Voucher) can not be trashed""", raise_exception=1) if self.check_if_child_exists(): - msgprint("Child account exists for this account. You can not trash this account.", raise_exception=1) + msgprint("Child account exists for this account. You can not trash this account.", + raise_exception=1) - # On Trash def on_trash(self): self.validate_trash() - - # rebuild tree self.update_nsm_model() # delete all cancelled gl entry of this account - sql("delete from `tabGL Entry` where account = %s and ifnull(is_cancelled, 'No') = 'Yes'", self.doc.name) + sql("""delete from `tabGL Entry` where account = %s and + ifnull(is_cancelled, 'No') = 'Yes'""", self.doc.name) - # on rename def on_rename(self, new, old): company_abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr") parts = new.split(" - ") @@ -191,4 +198,4 @@ class DocType: sql("update `tabAccount` set account_name = '%s' where name = '%s'" % \ (account_name, old)) - return " - ".join(parts) + return " - ".join(parts) \ No newline at end of file diff --git a/setup/doctype/company/company.py b/setup/doctype/company/company.py index 8bed425ab6e..e570b72814f 100644 --- a/setup/doctype/company/company.py +++ b/setup/doctype/company/company.py @@ -204,11 +204,10 @@ class DocType: glc.add_cc(str(c)) - # On update - # --------------------------------------------------- def on_update(self): self.set_letter_head() - ac = sql("select name from tabAccount where account_name='Income' and company=%s", self.doc.name) + ac = sql("select name from tabAccount where company=%s and docstatus<2 limit 1", + self.doc.name) if not ac: self.create_default_accounts() self.set_default_groups() @@ -216,8 +215,7 @@ class DocType: if not cc: self.create_default_cost_center() - # - # --------------------------------------------------- + def on_trash(self): """ Trash accounts and cost centers for this company if no gl entry exists