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 | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 22 | def get_fiscal_year(date): |
| 23 | from webnotes.utils import formatdate |
Anand Doshi | bbe2403 | 2012-10-05 19:29:11 +0530 | [diff] [blame] | 24 | # 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] | 25 | fy = webnotes.conn.sql("""select name, year_start_date, |
Anand Doshi | bbe2403 | 2012-10-05 19:29:11 +0530 | [diff] [blame] | 26 | subdate(adddate(year_start_date, interval 1 year), interval 1 day) |
| 27 | as year_end_date |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 28 | from `tabFiscal Year` |
Anand Doshi | bbe2403 | 2012-10-05 19:29:11 +0530 | [diff] [blame] | 29 | where %s >= year_start_date and %s < adddate(year_start_date, interval 1 year)""", |
| 30 | (date, date)) |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 31 | |
| 32 | if not fy: |
Anand Doshi | bbe2403 | 2012-10-05 19:29:11 +0530 | [diff] [blame] | 33 | webnotes.msgprint("""%s not in any Fiscal Year""" % formatdate(date), |
| 34 | raise_exception=1) |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 35 | |
| 36 | return fy[0] |
Brahma K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 37 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 38 | @webnotes.whitelist() |
| 39 | def 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 K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 74 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 75 | # if credit account, it should calculate credit - debit |
| 76 | if bal and acc.debit_or_credit == 'Credit': |
| 77 | bal = -bal |
Brahma K | d7db2ea | 2011-07-26 19:04:13 +0530 | [diff] [blame] | 78 | |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 79 | # if bal is None, return 0 |
| 80 | return bal or 0 |