Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | # 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 Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Brahma K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 18 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 19 | import webnotes |
| 20 | from webnotes.utils import nowdate |
Brahma K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 21 | |
Anand Doshi | d69b32e | 2012-10-10 11:44:21 +0530 | [diff] [blame] | 22 | class FiscalYearError(webnotes.ValidationError): pass |
| 23 | |
| 24 | def get_fiscal_year(date, verbose=1): |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 25 | from webnotes.utils import formatdate |
Anand Doshi | bbe2403 | 2012-10-05 19:29:11 +0530 | [diff] [blame] | 26 | # if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate) |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 27 | fy = webnotes.conn.sql("""select name, year_start_date, |
Anand Doshi | bbe2403 | 2012-10-05 19:29:11 +0530 | [diff] [blame] | 28 | subdate(adddate(year_start_date, interval 1 year), interval 1 day) |
| 29 | as year_end_date |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 30 | from `tabFiscal Year` |
Anand Doshi | bbe2403 | 2012-10-05 19:29:11 +0530 | [diff] [blame] | 31 | where %s >= year_start_date and %s < adddate(year_start_date, interval 1 year)""", |
| 32 | (date, date)) |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 33 | |
| 34 | if not fy: |
Anand Doshi | d69b32e | 2012-10-10 11:44:21 +0530 | [diff] [blame] | 35 | error_msg = """%s not in any Fiscal Year""" % formatdate(date) |
| 36 | if verbose: webnotes.msgprint(error_msg) |
| 37 | raise FiscalYearError, error_msg |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 38 | |
| 39 | return fy[0] |
Brahma K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 40 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 41 | @webnotes.whitelist() |
| 42 | def 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") |
Anand Doshi | d69b32e | 2012-10-10 11:44:21 +0530 | [diff] [blame] | 46 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 47 | cond = [] |
| 48 | if date: |
| 49 | cond.append("posting_date <= '%s'" % date) |
Anand Doshi | d69b32e | 2012-10-10 11:44:21 +0530 | [diff] [blame] | 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 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 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': |
Anand Doshi | d69b32e | 2012-10-10 11:44:21 +0530 | [diff] [blame] | 72 | cond.append("posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" \ |
| 73 | % year_start_date) |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 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] |
Brahma K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 93 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 94 | # if credit account, it should calculate credit - debit |
| 95 | if bal and acc.debit_or_credit == 'Credit': |
| 96 | bal = -bal |
Brahma K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 97 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 98 | # if bal is None, return 0 |
| 99 | return bal or 0 |