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