blob: 4c669a9d9ec4bc4668b205971b909fbd510f0d3f [file] [log] [blame]
Rushabh Mehta41565232012-09-17 19:10:36 +05301// 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 Mehta09d84b62012-09-21 19:46:24 +053014erpnext.AccountTreeGrid = wn.views.TreeGridReport.extend({
Rushabh Mehta41565232012-09-17 19:10:36 +053015 init: function(wrapper, title) {
16 this._super({
17 title: title,
18 page: wrapper,
19 parent: $(wrapper).find('.layout-main'),
20 appframe: wrapper.appframe,
Rushabh Mehta09d84b62012-09-21 19:46:24 +053021 doctypes: ["Company", "Fiscal Year", "Account", "GL Entry", "Cost Center"],
Rushabh Mehta6ca80542012-09-20 19:03:14 +053022 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 Mehta41565232012-09-17 19:10:36 +053032 });
33 },
34 setup_columns: function() {
35 this.columns = [
36 {id: "name", name: "Account", field: "name", width: 300, cssClass: "cell-title",
Rushabh Mehta6ca80542012-09-20 19:03:14 +053037 formatter: this.tree_formatter},
Rushabh Mehta41565232012-09-17 19:10:36 +053038 {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 Mehta45735152012-09-19 16:41:19 +053055 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 Mehta41565232012-09-17 19:10:36 +053060 }},
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 Mehta45735152012-09-19 16:41:19 +053083 me.show_zero_check()
Rushabh Mehta41565232012-09-17 19:10:36 +053084 },
Rushabh Mehta41565232012-09-17 19:10:36 +053085 prepare_data: function() {
Rushabh Mehta45735152012-09-19 16:41:19 +053086 var me = this;
Anand Doshi42d37bf62012-11-28 16:06:37 +053087 if(!this.primary_data) {
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053088 // make accounts list
Rushabh Mehta6ca80542012-09-20 19:03:14 +053089 me.data = [];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053090 me.parent_map = {};
Rushabh Mehta6ca80542012-09-20 19:03:14 +053091 me.item_by_name = {};
Rushabh Mehta41565232012-09-17 19:10:36 +053092
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053093 $.each(wn.report_dump.data["Account"], function(i, v) {
94 var d = copy_dict(v);
95
Rushabh Mehta6ca80542012-09-20 19:03:14 +053096 me.data.push(d);
97 me.item_by_name[d.name] = d;
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053098 if(d.parent_account) {
99 me.parent_map[d.name] = d.parent_account;
100 }
Anand Doshi93a35ca2012-11-21 18:02:22 +0530101 });
Anand Doshi42d37bf62012-11-28 16:06:37 +0530102
103 me.primary_data = [].concat(me.data);
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530104 }
Anand Doshi42d37bf62012-11-28 16:06:37 +0530105
106 me.data = [].concat(me.primary_data);
107 $.each(me.data, function(i, d) {
108 me.init_account(d);
109 });
110
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530111 this.set_indent();
Rushabh Mehta41565232012-09-17 19:10:36 +0530112 this.prepare_balances();
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530113
Rushabh Mehta41565232012-09-17 19:10:36 +0530114 },
Rushabh Mehta09d84b62012-09-21 19:46:24 +0530115 init_account: function(d) {
116 this.reset_item_values(d);
117 },
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530118
Rushabh Mehta41565232012-09-17 19:10:36 +0530119 prepare_balances: function() {
120 var gl = wn.report_dump.data['GL Entry'];
121 var me = this;
122
123 this.opening_date = dateutil.user_to_obj(this.filter_inputs.from_date.val());
124 this.closing_date = dateutil.user_to_obj(this.filter_inputs.to_date.val());
125 this.set_fiscal_year();
126 if (!this.fiscal_year) return;
127
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530128 $.each(this.data, function(i, v) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530129 v.opening_debit = v.opening_credit = v.debit
130 = v.credit = v.closing_debit = v.closing_credit = 0;
131 });
132
133 $.each(gl, function(i, v) {
134 var posting_date = dateutil.str_to_obj(v.posting_date);
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530135 var account = me.item_by_name[v.account];
Rushabh Mehta41565232012-09-17 19:10:36 +0530136 me.update_balances(account, posting_date, v)
137 });
138
139 this.update_groups();
140 },
141 update_balances: function(account, posting_date, v) {
142 // opening
143 if (posting_date < this.opening_date || v.is_opening === "Yes") {
144 if (account.is_pl_account === "Yes" &&
145 posting_date <= dateutil.str_to_obj(this.fiscal_year[1])) {
146 // balance of previous fiscal_year should
147 // not be part of opening of pl account balance
148 } else {
149 if(account.debit_or_credit=='Debit') {
150 account.opening_debit += (v.debit - v.credit);
151 } else {
152 account.opening_credit += (v.credit - v.debit);
153 }
154 }
155 } else if (this.opening_date <= posting_date && posting_date <= this.closing_date) {
156 // in between
157 account.debit += v.debit;
158 account.credit += v.credit;
159 }
160 // closing
161 if(account.debit_or_credit=='Debit') {
162 account.closing_debit = account.opening_debit + account.debit - account.credit;
163 } else {
164 account.closing_credit = account.opening_credit - account.debit + account.credit;
165 }
166 },
167 update_groups: function() {
168 // update groups
169 var me= this;
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530170 $.each(this.data, function(i, account) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530171 // update groups
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530172 if(account.rgt - account.lft == 1) {
173 var parent = me.parent_map[account.name];
174 while(parent) {
Anand Doshi73519e12012-10-11 14:04:27 +0530175 var parent_account = me.item_by_name[parent];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530176 $.each(me.columns, function(c, col) {
177 if (col.formatter == me.currency_formatter) {
178 parent_account[col.field] =
179 flt(parent_account[col.field])
180 + flt(account[col.field]);
181 }
182 });
183 parent = me.parent_map[parent];
184 }
185 }
Rushabh Mehta41565232012-09-17 19:10:36 +0530186 });
187 },
188
189 set_fiscal_year: function() {
190 if (this.opening_date > this.closing_date) {
191 msgprint("Opening Date should be before Closing Date");
192 return;
193 }
194
195 this.fiscal_year = null;
196 var me = this;
197 $.each(wn.report_dump.data["Fiscal Year"], function(i, v) {
198 if (me.opening_date >= dateutil.str_to_obj(v.year_start_date) &&
199 me.closing_date <= dateutil.str_to_obj(v.year_end_date)) {
200 me.fiscal_year = v;
201 }
202 });
203
204 if (!this.fiscal_year) {
205 msgprint("Opening Date and Closing Date should be within same Fiscal Year");
206 return;
207 }
208 },
Rushabh Mehta41565232012-09-17 19:10:36 +0530209});