blob: 60f70fe0b6c902bfcab9ab3f627cadafe2b95642 [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +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
Anand Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Brahma Kd7db2ea2011-07-26 19:04:13 +053018
Anand Doshi440c1eb2012-09-28 16:03:11 +053019import webnotes
20from webnotes.utils import nowdate
Brahma Kd7db2ea2011-07-26 19:04:13 +053021
Anand Doshi440c1eb2012-09-28 16:03:11 +053022def get_fiscal_year(date):
23 from webnotes.utils import formatdate
24 fy = webnotes.conn.sql("""select name, year_start_date,
25 adddate(year_start_date, interval 1 year)
26 from `tabFiscal Year`
27 where %s between year_start_date and adddate(year_start_date,
28 interval 1 year)""", date)
29
30 if not fy:
31 webnotes.msgprint("""%s not in any Fiscal Year""" % formatdate(date), raise_exception=1)
32
33 return fy[0]
Brahma Kd7db2ea2011-07-26 19:04:13 +053034
Anand Doshi440c1eb2012-09-28 16:03:11 +053035@webnotes.whitelist()
36def get_balance_on(account=None, date=None):
37 if not account and webnotes.form_dict.get("account"):
38 account = webnotes.form_dict.get("account")
39 date = webnotes.form_dict.get("date")
40
41 cond = []
42 if date:
43 cond.append("posting_date <= '%s'" % date)
44
45 acc = webnotes.conn.get_value('Account', account, \
46 ['lft', 'rgt', 'debit_or_credit', 'is_pl_account', 'group_or_ledger'], as_dict=1)
47
48 # for pl accounts, get balance within a fiscal year
49 if acc.is_pl_account == 'Yes':
50 cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % \
51 get_fiscal_year(date or nowdate())[1])
52
53 # different filter for group and ledger - improved performance
54 if acc.group_or_ledger=="Group":
55 cond.append("""exists (
56 select * from `tabAccount` ac where ac.name = gle.account
57 and ac.lft >= %s and ac.rgt <= %s
58 )""" % (acc.lft, acc.rgt))
59 else:
60 cond.append("""gle.account = "%s" """ % (account, ))
61
62 # join conditional conditions
63 cond = " and ".join(cond)
64 if cond:
65 cond += " and "
66
67 bal = webnotes.conn.sql("""
68 SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
69 FROM `tabGL Entry` gle
70 WHERE %s ifnull(is_cancelled, 'No') = 'No' """ % (cond, ))[0][0]
Brahma Kd7db2ea2011-07-26 19:04:13 +053071
Anand Doshi440c1eb2012-09-28 16:03:11 +053072 # if credit account, it should calculate credit - debit
73 if bal and acc.debit_or_credit == 'Credit':
74 bal = -bal
Brahma Kd7db2ea2011-07-26 19:04:13 +053075
Anand Doshi440c1eb2012-09-28 16:03:11 +053076 # if bal is None, return 0
77 return bal or 0