validation while created through company saving and root deleted

This commit is contained in:
Nabin Hait 2012-12-11 14:35:45 +05:30
parent c50f088a22
commit cddbe6dd80
2 changed files with 45 additions and 40 deletions

View File

@ -34,22 +34,21 @@ class DocType:
company_abbr = sql("select abbr from tabCompany where name=%s", self.doc.company)[0][0] 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 self.doc.name = self.doc.account_name.strip() + ' - ' + company_abbr
# Get customer/supplier address
def get_address(self): def get_address(self):
add=sql("Select address from `tab%s` where name='%s'"%(self.doc.master_type,self.doc.master_name)) add=sql("Select address from `tab%s` where name='%s'" %
ret={'address':add[0][0]} (self.doc.master_type, self.doc.master_name))
return ret return {'address': add[0][0]}
# check whether master name entered for supplier/customer
def validate_master_name(self): 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.") 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): def validate_parent(self):
"""Fetch Parent Details and validation for account not to be created under ledger"""
if self.doc.parent_account: 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: if not par:
msgprint("Parent account does not exists", raise_exception=1) msgprint("Parent account does not exists", raise_exception=1)
elif par and par[0][0] == self.doc.name: elif par and par[0][0] == self.doc.name:
@ -57,7 +56,8 @@ class DocType:
elif par and par[0][1] != 'Group': elif par and par[0][1] != 'Group':
msgprint("Parent account can not be a ledger", raise_exception=1) 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: 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: elif par and not self.doc.is_pl_account:
self.doc.is_pl_account = par[0][2] self.doc.is_pl_account = par[0][2]
self.doc.debit_or_credit = par[0][3] 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", webnotes.msgprint("One company cannot have more than 4 root Accounts",
raise_exception=1) raise_exception=1)
# Account name must be unique
def validate_duplicate_account(self): 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" msgprint("Account Name: %s already exists, please rename"
% self.doc.account_name, raise_exception=1) % self.doc.account_name, raise_exception=1)
@ -83,9 +84,11 @@ class DocType:
def convert_group_to_ledger(self): def convert_group_to_ledger(self):
if self.check_if_child_exists(): 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(): 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: else:
self.doc.group_or_ledger = 'Ledger' self.doc.group_or_ledger = 'Ledger'
self.doc.save() self.doc.save()
@ -105,11 +108,13 @@ class DocType:
# Check if any previous balance exists # Check if any previous balance exists
def check_gle_exists(self): 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 '' return exists and exists[0][0] or ''
def check_if_child_exists(self): 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): def validate_mandatory(self):
if not self.doc.debit_or_credit: if not self.doc.debit_or_credit:
@ -124,61 +129,63 @@ class DocType:
self.validate_root_details() self.validate_root_details()
self.validate_mandatory() self.validate_mandatory()
# Defaults
if not self.doc.parent_account: if not self.doc.parent_account:
self.doc.parent_account = '' self.doc.parent_account = ''
# Update Node Set Model
def update_nsm_model(self): def update_nsm_model(self):
import webnotes import webnotes
import webnotes.utils.nestedset import webnotes.utils.nestedset
webnotes.utils.nestedset.update_nsm(self) webnotes.utils.nestedset.update_nsm(self)
def on_update(self): def on_update(self):
# update nsm
self.validate_max_root_accounts() self.validate_max_root_accounts()
self.update_nsm_model() self.update_nsm_model()
# Check user role for approval process
def get_authorized_user(self): def get_authorized_user(self):
# Check logged-in user is authorized # 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 return 1
# Check Credit limit for customer
def check_credit_limit(self, account, company, tot_outstanding): def check_credit_limit(self, account, company, tot_outstanding):
# Get credit limit # Get credit limit
credit_limit_from = 'Customer' 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 credit_limit = cr_limit and flt(cr_limit[0][0]) or 0
if not credit_limit: if not credit_limit:
credit_limit = webnotes.conn.get_value('Company', company, 'credit_limit') credit_limit = webnotes.conn.get_value('Company', company, 'credit_limit')
credit_limit_from = 'global settings in the Company' credit_limit_from = 'global settings in the Company'
# If outstanding greater than credit limit and not authorized person raise exception # 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(): if credit_limit > 0 and flt(tot_outstanding) > credit_limit \
msgprint("Total Outstanding amount (%s) for <b>%s</b> can not be greater than credit limit (%s). To change your credit limit settings, please update the <b>%s</b>" \ and not self.get_authorized_user():
% (fmt_money(tot_outstanding), account, fmt_money(credit_limit), credit_limit_from), raise_exception=1) msgprint("""Total Outstanding amount (%s) for <b>%s</b> can not be \
greater than credit limit (%s). To change your credit limit settings, \
please update the <b>%s</b>""" % (fmt_money(tot_outstanding),
account, fmt_money(credit_limit), credit_limit_from), raise_exception=1)
def validate_trash(self): def validate_trash(self):
"""checks gl entries and if child exists""" """checks gl entries and if child exists"""
if self.check_gle_exists(): if not self.doc.parent_account:
msgprint("Account with existing transaction (Sales Invoice / Purchase Invoice / Journal Voucher) can not be trashed", raise_exception=1) msgprint("Root account can not be deleted", 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) if self.check_gle_exists():
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)
# On Trash
def on_trash(self): def on_trash(self):
self.validate_trash() self.validate_trash()
# rebuild tree
self.update_nsm_model() self.update_nsm_model()
# delete all cancelled gl entry of this account # 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): def on_rename(self, new, old):
company_abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr") company_abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr")
parts = new.split(" - ") parts = new.split(" - ")

View File

@ -204,11 +204,10 @@ class DocType:
glc.add_cc(str(c)) glc.add_cc(str(c))
# On update
# ---------------------------------------------------
def on_update(self): def on_update(self):
self.set_letter_head() 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: if not ac:
self.create_default_accounts() self.create_default_accounts()
self.set_default_groups() self.set_default_groups()
@ -216,8 +215,7 @@ class DocType:
if not cc: if not cc:
self.create_default_cost_center() self.create_default_cost_center()
#
# ---------------------------------------------------
def on_trash(self): def on_trash(self):
""" """
Trash accounts and cost centers for this company if no gl entry exists Trash accounts and cost centers for this company if no gl entry exists