diff --git a/accounts/doctype/journal_voucher/journal_voucher.js b/accounts/doctype/journal_voucher/journal_voucher.js index fe407f52e6..d60dba6d1e 100644 --- a/accounts/doctype/journal_voucher/journal_voucher.js +++ b/accounts/doctype/journal_voucher/journal_voucher.js @@ -150,7 +150,7 @@ cur_frm.cscript.account = function(doc,dt,dn) { args: {account: d.account, date: doc.posting_date}, callback: function(r) { d.balance = fmt_money(r.message); - refresh_field('balance',d.name,'entries'); + refresh_field('balance', d.name, 'entries'); } }); } diff --git a/accounts/search_criteria/general_ledger/general_ledger.py b/accounts/search_criteria/general_ledger/general_ledger.py index 2533238b20..11ab7c336f 100644 --- a/accounts/search_criteria/general_ledger/general_ledger.py +++ b/accounts/search_criteria/general_ledger/general_ledger.py @@ -30,14 +30,6 @@ else: from_date = filter_values['posting_date'] to_date = filter_values['posting_date1'] -from_date_year = sql("select name from `tabFiscal Year` where %s between year_start_date and date_sub(date_add(year_start_date,interval 1 year), interval 1 day)",from_date) -if not from_date_year: - msgprint("From Date is out of range. Please check.", raise_exception=1) -else: - from_date_year = from_date_year[0][0] -#to_date_year = sql("select name from `tabFiscal Year` where %s between year_start_date and date_sub(date_add(year_start_date,interval 1 year), interval 1 day)",to_date)[0][0] - - # define columns #--------------------------------------------------------------------- col = [] diff --git a/accounts/utils/__init__.py b/accounts/utils/__init__.py index ac13e47ad5..c4a0042b15 100644 --- a/accounts/utils/__init__.py +++ b/accounts/utils/__init__.py @@ -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":