blob: ac13e47ad5fac0fb54692f1c9e51f20c8746fa24 [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
Anand Doshibbe24032012-10-05 19:29:11 +053024 # if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate)
Anand Doshi440c1eb2012-09-28 16:03:11 +053025 fy = webnotes.conn.sql("""select name, year_start_date,
Anand Doshibbe24032012-10-05 19:29:11 +053026 subdate(adddate(year_start_date, interval 1 year), interval 1 day)
27 as year_end_date
Anand Doshi440c1eb2012-09-28 16:03:11 +053028 from `tabFiscal Year`
Anand Doshibbe24032012-10-05 19:29:11 +053029 where %s >= year_start_date and %s < adddate(year_start_date, interval 1 year)""",
30 (date, date))
Anand Doshi440c1eb2012-09-28 16:03:11 +053031
32 if not fy:
Anand Doshibbe24032012-10-05 19:29:11 +053033 webnotes.msgprint("""%s not in any Fiscal Year""" % formatdate(date),
34 raise_exception=1)
Anand Doshi440c1eb2012-09-28 16:03:11 +053035
36 return fy[0]
Brahma Kd7db2ea2011-07-26 19:04:13 +053037
Anand Doshi440c1eb2012-09-28 16:03:11 +053038@webnotes.whitelist()
39def get_balance_on(account=None, date=None):
40 if not account and webnotes.form_dict.get("account"):
41 account = webnotes.form_dict.get("account")
42 date = webnotes.form_dict.get("date")
43
44 cond = []
45 if date:
46 cond.append("posting_date <= '%s'" % date)
47
48 acc = webnotes.conn.get_value('Account', account, \
49 ['lft', 'rgt', 'debit_or_credit', 'is_pl_account', 'group_or_ledger'], as_dict=1)
50
51 # for pl accounts, get balance within a fiscal year
52 if acc.is_pl_account == 'Yes':
53 cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % \
54 get_fiscal_year(date or nowdate())[1])
55
56 # different filter for group and ledger - improved performance
57 if acc.group_or_ledger=="Group":
58 cond.append("""exists (
59 select * from `tabAccount` ac where ac.name = gle.account
60 and ac.lft >= %s and ac.rgt <= %s
61 )""" % (acc.lft, acc.rgt))
62 else:
63 cond.append("""gle.account = "%s" """ % (account, ))
64
65 # join conditional conditions
66 cond = " and ".join(cond)
67 if cond:
68 cond += " and "
69
70 bal = webnotes.conn.sql("""
71 SELECT sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
72 FROM `tabGL Entry` gle
73 WHERE %s ifnull(is_cancelled, 'No') = 'No' """ % (cond, ))[0][0]
Brahma Kd7db2ea2011-07-26 19:04:13 +053074
Anand Doshi440c1eb2012-09-28 16:03:11 +053075 # if credit account, it should calculate credit - debit
76 if bal and acc.debit_or_credit == 'Credit':
77 bal = -bal
Brahma Kd7db2ea2011-07-26 19:04:13 +053078
Anand Doshi440c1eb2012-09-28 16:03:11 +053079 # if bal is None, return 0
80 return bal or 0