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