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: [ |
| 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 | }}, |
| 61 | {fieldtype:"Select", label: "Fiscal Year", link:"Fiscal Year", |
| 62 | default_value: "Select Fiscal Year..."}, |
| 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", cssClass:"btn-info"}, |
| 67 | {fieldtype:"Button", label: "Reset Filters"} |
| 68 | ], |
| 69 | setup_filters: function() { |
| 70 | this._super(); |
| 71 | var me = this; |
| 72 | // default filters |
| 73 | this.filter_inputs.fiscal_year.change(function() { |
| 74 | var fy = $(this).val(); |
| 75 | $.each(wn.report_dump.data["Fiscal Year"], function(i, v) { |
| 76 | if (v.name==fy) { |
| 77 | me.filter_inputs.from_date.val(dateutil.str_to_user(v.year_start_date)); |
| 78 | me.filter_inputs.to_date.val(dateutil.str_to_user(v.year_end_date)); |
| 79 | } |
| 80 | }); |
| 81 | me.set_route(); |
| 82 | }); |
Rushabh Mehta | 4573515 | 2012-09-19 16:41:19 +0530 | [diff] [blame] | 83 | me.show_zero_check() |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 84 | }, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 85 | prepare_data: function() { |
Rushabh Mehta | 4573515 | 2012-09-19 16:41:19 +0530 | [diff] [blame] | 86 | var me = this; |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 87 | if(this.data) { |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 88 | // refresh -- only initialize |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 89 | $.each(this.data, function(i, d) { |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 90 | me.init_account(d); |
| 91 | }) |
| 92 | } else { |
| 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 | } |
| 106 | |
| 107 | me.init_account(d); |
| 108 | }); |
| 109 | } |
| 110 | this.set_indent(); |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 111 | this.prepare_balances(); |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 112 | |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 113 | }, |
Rushabh Mehta | 09d84b6 | 2012-09-21 19:46:24 +0530 | [diff] [blame] | 114 | init_account: function(d) { |
| 115 | this.reset_item_values(d); |
| 116 | }, |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 117 | |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 118 | prepare_balances: function() { |
| 119 | var gl = wn.report_dump.data['GL Entry']; |
| 120 | var me = this; |
| 121 | |
| 122 | this.opening_date = dateutil.user_to_obj(this.filter_inputs.from_date.val()); |
| 123 | this.closing_date = dateutil.user_to_obj(this.filter_inputs.to_date.val()); |
| 124 | this.set_fiscal_year(); |
| 125 | if (!this.fiscal_year) return; |
| 126 | |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 127 | $.each(this.data, function(i, v) { |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 128 | v.opening_debit = v.opening_credit = v.debit |
| 129 | = v.credit = v.closing_debit = v.closing_credit = 0; |
| 130 | }); |
| 131 | |
| 132 | $.each(gl, function(i, v) { |
| 133 | var posting_date = dateutil.str_to_obj(v.posting_date); |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 134 | var account = me.item_by_name[v.account]; |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 135 | me.update_balances(account, posting_date, v) |
| 136 | }); |
| 137 | |
| 138 | this.update_groups(); |
| 139 | }, |
| 140 | update_balances: function(account, posting_date, v) { |
| 141 | // opening |
| 142 | if (posting_date < this.opening_date || v.is_opening === "Yes") { |
| 143 | if (account.is_pl_account === "Yes" && |
| 144 | posting_date <= dateutil.str_to_obj(this.fiscal_year[1])) { |
| 145 | // balance of previous fiscal_year should |
| 146 | // not be part of opening of pl account balance |
| 147 | } else { |
| 148 | if(account.debit_or_credit=='Debit') { |
| 149 | account.opening_debit += (v.debit - v.credit); |
| 150 | } else { |
| 151 | account.opening_credit += (v.credit - v.debit); |
| 152 | } |
| 153 | } |
| 154 | } else if (this.opening_date <= posting_date && posting_date <= this.closing_date) { |
| 155 | // in between |
| 156 | account.debit += v.debit; |
| 157 | account.credit += v.credit; |
| 158 | } |
| 159 | // closing |
| 160 | if(account.debit_or_credit=='Debit') { |
| 161 | account.closing_debit = account.opening_debit + account.debit - account.credit; |
| 162 | } else { |
| 163 | account.closing_credit = account.opening_credit - account.debit + account.credit; |
| 164 | } |
| 165 | }, |
| 166 | update_groups: function() { |
| 167 | // update groups |
| 168 | var me= this; |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 169 | $.each(this.data, function(i, account) { |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 170 | // update groups |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 171 | if(account.rgt - account.lft == 1) { |
| 172 | var parent = me.parent_map[account.name]; |
| 173 | while(parent) { |
Rushabh Mehta | 6ca8054 | 2012-09-20 19:03:14 +0530 | [diff] [blame] | 174 | parent_account = me.item_by_name[parent]; |
Rushabh Mehta | 2ad0d42 | 2012-09-18 18:52:05 +0530 | [diff] [blame] | 175 | $.each(me.columns, function(c, col) { |
| 176 | if (col.formatter == me.currency_formatter) { |
| 177 | parent_account[col.field] = |
| 178 | flt(parent_account[col.field]) |
| 179 | + flt(account[col.field]); |
| 180 | } |
| 181 | }); |
| 182 | parent = me.parent_map[parent]; |
| 183 | } |
| 184 | } |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 185 | }); |
| 186 | }, |
| 187 | |
| 188 | set_fiscal_year: function() { |
| 189 | if (this.opening_date > this.closing_date) { |
| 190 | msgprint("Opening Date should be before Closing Date"); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | this.fiscal_year = null; |
| 195 | var me = this; |
| 196 | $.each(wn.report_dump.data["Fiscal Year"], function(i, v) { |
| 197 | if (me.opening_date >= dateutil.str_to_obj(v.year_start_date) && |
| 198 | me.closing_date <= dateutil.str_to_obj(v.year_end_date)) { |
| 199 | me.fiscal_year = v; |
| 200 | } |
| 201 | }); |
| 202 | |
| 203 | if (!this.fiscal_year) { |
| 204 | msgprint("Opening Date and Closing Date should be within same Fiscal Year"); |
| 205 | return; |
| 206 | } |
| 207 | }, |
Rushabh Mehta | 4156523 | 2012-09-17 19:10:36 +0530 | [diff] [blame] | 208 | }); |