Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 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 | |
Rushabh Mehta | 09d84b6 | 2012-09-21 19:46:24 +0530 | [diff] [blame] | 17 | erpnext.AccountTreeGrid = wn.views.TreeGridReport.extend({ |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 18 | init: function(wrapper, title) { |
| 19 | this._super({ |
| 20 | title: title, |
| 21 | page: wrapper, |
| 22 | parent: $(wrapper).find('.layout-main'), |
| 23 | appframe: wrapper.appframe, |
Rushabh Mehta | 09d84b6 | 2012-09-21 19:46:24 +0530 | [diff] [blame] | 24 | doctypes: ["Company", "Fiscal Year", "Account", "GL Entry", "Cost Center"], |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 25 | tree_grid: { |
| 26 | show: true, |
| 27 | parent_field: "parent_account", |
| 28 | formatter: function(item) { |
| 29 | return repl('<a href="#general-ledger/account=%(enc_value)s">%(value)s</a>', { |
| 30 | value: item.name, |
| 31 | enc_value: encodeURIComponent(item.name) |
| 32 | }); |
| 33 | } |
| 34 | }, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 35 | }); |
| 36 | }, |
| 37 | setup_columns: function() { |
| 38 | this.columns = [ |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 39 | {id: "name", name: wn._("Account"), field: "name", width: 300, cssClass: "cell-title", |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 40 | formatter: this.tree_formatter}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 41 | {id: "opening_debit", name: wn._("Opening (Dr)"), field: "opening_debit", width: 100, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 42 | formatter: this.currency_formatter}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 43 | {id: "opening_credit", name: wn._("Opening (Cr)"), field: "opening_credit", width: 100, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 44 | formatter: this.currency_formatter}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 45 | {id: "debit", name: wn._("Debit"), field: "debit", width: 100, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 46 | formatter: this.currency_formatter}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 47 | {id: "credit", name: wn._("Credit"), field: "credit", width: 100, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 48 | formatter: this.currency_formatter}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 49 | {id: "closing_debit", name: wn._("Closing (Dr)"), field: "closing_debit", width: 100, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 50 | formatter: this.currency_formatter}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 51 | {id: "closing_credit", name: wn._("Closing (Cr)"), field: "closing_credit", width: 100, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 52 | formatter: this.currency_formatter} |
| 53 | ]; |
| 54 | |
| 55 | }, |
| 56 | filters: [ |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 57 | {fieldtype: "Select", label: wn._("Company"), link:"Company", default_value: "Select Company...", |
Rushabh Mehta | 4573515 | 2012-09-19 16:41:19 +0530 | [diff] [blame] | 58 | filter: function(val, item, opts, me) { |
| 59 | if (item.company == val || val == opts.default_value) { |
| 60 | return me.apply_zero_filter(val, item, opts, me); |
| 61 | } |
| 62 | return false; |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 63 | }}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 64 | {fieldtype: "Select", label: wn._("Fiscal Year"), link:"Fiscal Year", |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 65 | default_value: "Select Fiscal Year..."}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 66 | {fieldtype: "Date", label: wn._("From Date")}, |
| 67 | {fieldtype: "Label", label: wn._("To")}, |
| 68 | {fieldtype: "Date", label: wn._("To Date")}, |
| 69 | {fieldtype: "Button", label: wn._("Refresh"), icon:"icon-refresh icon-white", |
Nabin Hait | 3882db0 | 2013-01-01 13:48:40 +0530 | [diff] [blame] | 70 | cssClass:"btn-info"}, |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 71 | {fieldtype: "Button", label: wn._("Reset Filters")}, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 72 | ], |
| 73 | setup_filters: function() { |
| 74 | this._super(); |
| 75 | var me = this; |
| 76 | // default filters |
| 77 | this.filter_inputs.fiscal_year.change(function() { |
| 78 | var fy = $(this).val(); |
| 79 | $.each(wn.report_dump.data["Fiscal Year"], function(i, v) { |
| 80 | if (v.name==fy) { |
| 81 | me.filter_inputs.from_date.val(dateutil.str_to_user(v.year_start_date)); |
| 82 | me.filter_inputs.to_date.val(dateutil.str_to_user(v.year_end_date)); |
| 83 | } |
| 84 | }); |
| 85 | me.set_route(); |
| 86 | }); |
Rushabh Mehta | 4573515 | 2012-09-19 16:41:19 +0530 | [diff] [blame] | 87 | me.show_zero_check() |
Nabin Hait | 3882db0 | 2013-01-01 13:48:40 +0530 | [diff] [blame] | 88 | if(me.ignore_closing_entry) me.ignore_closing_entry(); |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 89 | }, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 90 | prepare_data: function() { |
Rushabh Mehta | 4573515 | 2012-09-19 16:41:19 +0530 | [diff] [blame] | 91 | var me = this; |
Anand Doshi | 42d37bf6 | 2012-11-28 16:06:37 +0530 | [diff] [blame] | 92 | if(!this.primary_data) { |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 93 | // make accounts list |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 94 | me.data = []; |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 95 | me.parent_map = {}; |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 96 | me.item_by_name = {}; |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 97 | |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 98 | $.each(wn.report_dump.data["Account"], function(i, v) { |
| 99 | var d = copy_dict(v); |
| 100 | |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 101 | me.data.push(d); |
| 102 | me.item_by_name[d.name] = d; |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 103 | if(d.parent_account) { |
| 104 | me.parent_map[d.name] = d.parent_account; |
| 105 | } |
Anand Doshi | 93a35ca | 2012-11-21 18:02:22 +0530 | [diff] [blame] | 106 | }); |
Anand Doshi | 42d37bf6 | 2012-11-28 16:06:37 +0530 | [diff] [blame] | 107 | |
| 108 | me.primary_data = [].concat(me.data); |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 109 | } |
Anand Doshi | 42d37bf6 | 2012-11-28 16:06:37 +0530 | [diff] [blame] | 110 | |
| 111 | me.data = [].concat(me.primary_data); |
| 112 | $.each(me.data, function(i, d) { |
| 113 | me.init_account(d); |
| 114 | }); |
| 115 | |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 116 | this.set_indent(); |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 117 | this.prepare_balances(); |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 118 | |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 119 | }, |
Rushabh Mehta | 09d84b6 | 2012-09-21 19:46:24 +0530 | [diff] [blame] | 120 | init_account: function(d) { |
| 121 | this.reset_item_values(d); |
| 122 | }, |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 123 | |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 124 | prepare_balances: function() { |
| 125 | var gl = wn.report_dump.data['GL Entry']; |
| 126 | var me = this; |
| 127 | |
| 128 | this.opening_date = dateutil.user_to_obj(this.filter_inputs.from_date.val()); |
| 129 | this.closing_date = dateutil.user_to_obj(this.filter_inputs.to_date.val()); |
| 130 | this.set_fiscal_year(); |
| 131 | if (!this.fiscal_year) return; |
| 132 | |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 133 | $.each(this.data, function(i, v) { |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 134 | v.opening_debit = v.opening_credit = v.debit |
| 135 | = v.credit = v.closing_debit = v.closing_credit = 0; |
| 136 | }); |
| 137 | |
| 138 | $.each(gl, function(i, v) { |
| 139 | var posting_date = dateutil.str_to_obj(v.posting_date); |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 140 | var account = me.item_by_name[v.account]; |
Anand Doshi | 5476318 | 2013-08-12 17:32:47 +0530 | [diff] [blame] | 141 | me.update_balances(account, posting_date, v); |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 142 | }); |
| 143 | |
| 144 | this.update_groups(); |
| 145 | }, |
| 146 | update_balances: function(account, posting_date, v) { |
| 147 | // opening |
| 148 | if (posting_date < this.opening_date || v.is_opening === "Yes") { |
| 149 | if (account.is_pl_account === "Yes" && |
| 150 | posting_date <= dateutil.str_to_obj(this.fiscal_year[1])) { |
| 151 | // balance of previous fiscal_year should |
| 152 | // not be part of opening of pl account balance |
| 153 | } else { |
| 154 | if(account.debit_or_credit=='Debit') { |
| 155 | account.opening_debit += (v.debit - v.credit); |
| 156 | } else { |
| 157 | account.opening_credit += (v.credit - v.debit); |
| 158 | } |
| 159 | } |
| 160 | } else if (this.opening_date <= posting_date && posting_date <= this.closing_date) { |
| 161 | // in between |
| 162 | account.debit += v.debit; |
| 163 | account.credit += v.credit; |
| 164 | } |
| 165 | // closing |
| 166 | if(account.debit_or_credit=='Debit') { |
| 167 | account.closing_debit = account.opening_debit + account.debit - account.credit; |
| 168 | } else { |
| 169 | account.closing_credit = account.opening_credit - account.debit + account.credit; |
| 170 | } |
| 171 | }, |
| 172 | update_groups: function() { |
| 173 | // update groups |
| 174 | var me= this; |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 175 | $.each(this.data, function(i, account) { |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 176 | // update groups |
Nabin Hait | 392596c | 2013-10-27 16:20:51 +0530 | [diff] [blame] | 177 | if((account.group_or_ledger == "Ledger") || (account.rgt - account.lft == 1)) { |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 178 | var parent = me.parent_map[account.name]; |
| 179 | while(parent) { |
Anand Doshi | 73519e1 | 2012-10-11 14:04:27 +0530 | [diff] [blame] | 180 | var parent_account = me.item_by_name[parent]; |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 181 | $.each(me.columns, function(c, col) { |
| 182 | if (col.formatter == me.currency_formatter) { |
| 183 | parent_account[col.field] = |
| 184 | flt(parent_account[col.field]) |
| 185 | + flt(account[col.field]); |
| 186 | } |
| 187 | }); |
| 188 | parent = me.parent_map[parent]; |
| 189 | } |
| 190 | } |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 191 | }); |
| 192 | }, |
| 193 | |
| 194 | set_fiscal_year: function() { |
| 195 | if (this.opening_date > this.closing_date) { |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 196 | msgprint(wn._("Opening Date should be before Closing Date")); |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
| 200 | this.fiscal_year = null; |
| 201 | var me = this; |
| 202 | $.each(wn.report_dump.data["Fiscal Year"], function(i, v) { |
| 203 | if (me.opening_date >= dateutil.str_to_obj(v.year_start_date) && |
| 204 | me.closing_date <= dateutil.str_to_obj(v.year_end_date)) { |
| 205 | me.fiscal_year = v; |
| 206 | } |
| 207 | }); |
| 208 | |
| 209 | if (!this.fiscal_year) { |
Bárbara Perretti | 4de55b4 | 2013-10-07 17:10:04 -0300 | [diff] [blame] | 210 | msgprint(wn._("Opening Date and Closing Date should be within same Fiscal Year")); |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | }, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 214 | }); |