fix in get_balance_on for a date which does not fall within any fiscal year

This commit is contained in:
Anand Doshi
2012-10-10 11:44:21 +05:30
parent 037c2570b6
commit d69b32e856
3 changed files with 27 additions and 16 deletions

View File

@@ -19,7 +19,9 @@ from __future__ import unicode_literals
import webnotes
from webnotes.utils import nowdate
def get_fiscal_year(date):
class FiscalYearError(webnotes.ValidationError): pass
def get_fiscal_year(date, verbose=1):
from webnotes.utils import formatdate
# if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate)
fy = webnotes.conn.sql("""select name, year_start_date,
@@ -30,8 +32,9 @@ def get_fiscal_year(date):
(date, date))
if not fy:
webnotes.msgprint("""%s not in any Fiscal Year""" % formatdate(date),
raise_exception=1)
error_msg = """%s not in any Fiscal Year""" % formatdate(date)
if verbose: webnotes.msgprint(error_msg)
raise FiscalYearError, error_msg
return fy[0]
@@ -40,18 +43,34 @@ def get_balance_on(account=None, date=None):
if not account and webnotes.form_dict.get("account"):
account = webnotes.form_dict.get("account")
date = webnotes.form_dict.get("date")
cond = []
if date:
cond.append("posting_date <= '%s'" % date)
else:
# get balance of all entries that exist
date = nowdate()
try:
year_start_date = get_fiscal_year(date, verbose=0)[1]
except FiscalYearError, e:
from webnotes.utils import getdate
if getdate(date) > getdate(nowdate()):
# if fiscal year not found and the date is greater than today
# get fiscal year for today's date and its corresponding year start date
year_start_date = get_fiscal_year(nowdate(), verbose=1)[1]
else:
# this indicates that it is a date older than any existing fiscal year.
# hence, assuming balance as 0.0
return 0.0
acc = webnotes.conn.get_value('Account', account, \
['lft', 'rgt', 'debit_or_credit', 'is_pl_account', 'group_or_ledger'], as_dict=1)
# for pl accounts, get balance within a fiscal year
if acc.is_pl_account == 'Yes':
cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % \
get_fiscal_year(date or nowdate())[1])
cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \
% year_start_date)
# different filter for group and ledger - improved performance
if acc.group_or_ledger=="Group":