blob: 8ad18990e49984ec15d25335d1fc7c1542a3d1a7 [file] [log] [blame]
Nabin Hait23941aa2013-01-29 11:32:38 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18
19import webnotes
20from webnotes.utils import nowdate
21
22class FiscalYearError(webnotes.ValidationError): pass
23
24def get_fiscal_year(date, verbose=1):
25 from webnotes.utils import formatdate
26 # if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate)
27 fy = webnotes.conn.sql("""select name, year_start_date,
28 subdate(adddate(year_start_date, interval 1 year), interval 1 day)
29 as year_end_date
30 from `tabFiscal Year`
31 where %s >= year_start_date and %s < adddate(year_start_date, interval 1 year)
32 order by year_start_date desc""", (date, date))
33
34 if not fy:
35 error_msg = """%s not in any Fiscal Year""" % formatdate(date)
36 if verbose: webnotes.msgprint(error_msg)
37 raise FiscalYearError, error_msg
38
39 return fy[0]
40
41@webnotes.whitelist()
42def get_balance_on(account=None, date=None):
43 if not account and webnotes.form_dict.get("account"):
44 account = webnotes.form_dict.get("account")
45 date = webnotes.form_dict.get("date")
46
47 cond = []
48 if date:
49 cond.append("posting_date <= '%s'" % date)
50 else:
51 # get balance of all entries that exist
52 date = nowdate()
53
54 try:
55 year_start_date = get_fiscal_year(date, verbose=0)[1]
56 except FiscalYearError, e:
57 from webnotes.utils import getdate
58 if getdate(date) > getdate(nowdate()):
59 # if fiscal year not found and the date is greater than today
60 # get fiscal year for today's date and its corresponding year start date
61 year_start_date = get_fiscal_year(nowdate(), verbose=1)[1]
62 else:
63 # this indicates that it is a date older than any existing fiscal year.
64 # hence, assuming balance as 0.0
65 return 0.0
66
67 acc = webnotes.conn.get_value('Account', account, \
68 ['lft', 'rgt', 'debit_or_credit', 'is_pl_account', 'group_or_ledger'], as_dict=1)
69
70 # for pl accounts, get balance within a fiscal year
71 if acc.is_pl_account == 'Yes':
72 cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \
73 % year_start_date)
74
75 # different filter for group and ledger - improved performance
76 if acc.group_or_ledger=="Group":
77 cond.append("""exists (
78 select * from `tabAccount` ac where ac.name = gle.account
79 and ac.lft >= %s and ac.rgt <= %s
80 )""" % (acc.lft, acc.rgt))
81 else:
82 cond.append("""gle.account = "%s" """ % (account, ))
83
84 # join conditional conditions
85 cond = " and ".join(cond)
86 if cond:
87 cond += " and "
88
89 bal = webnotes.conn.sql("""
90 SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
91 FROM `tabGL Entry` gle
92 WHERE %s ifnull(is_cancelled, 'No') = 'No' """ % (cond, ))[0][0]
93
94 # if credit account, it should calculate credit - debit
95 if bal and acc.debit_or_credit == 'Credit':
96 bal = -bal
97
98 # if bal is None, return 0
99 return bal or 0