blob: 10e1daccdedc7795427ea3bedce6b31bd28d47e1 [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;
Rushabh Mehta6ca80542012-09-20 19:03:14 +053087 if(this.data) {
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053088 // refresh -- only initialize
Rushabh Mehta6ca80542012-09-20 19:03:14 +053089 $.each(this.data, function(i, d) {
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053090 me.init_account(d);
91 })
92 } else {
93 // make accounts list
Rushabh Mehta6ca80542012-09-20 19:03:14 +053094 me.data = [];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053095 me.parent_map = {};
Rushabh Mehta6ca80542012-09-20 19:03:14 +053096 me.item_by_name = {};
Rushabh Mehta41565232012-09-17 19:10:36 +053097
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053098 $.each(wn.report_dump.data["Account"], function(i, v) {
99 var d = copy_dict(v);
100
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530101 me.data.push(d);
102 me.item_by_name[d.name] = d;
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530103 if(d.parent_account) {
104 me.parent_map[d.name] = d.parent_account;
105 }
106
107 me.init_account(d);
Anand Doshi93a35ca2012-11-21 18:02:22 +0530108 });
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530109 }
110 this.set_indent();
Rushabh Mehta41565232012-09-17 19:10:36 +0530111 this.prepare_balances();
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530112
Rushabh Mehta41565232012-09-17 19:10:36 +0530113 },
Rushabh Mehta09d84b62012-09-21 19:46:24 +0530114 init_account: function(d) {
115 this.reset_item_values(d);
116 },
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530117
Rushabh Mehta41565232012-09-17 19:10:36 +0530118 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 Mehta6ca80542012-09-20 19:03:14 +0530127 $.each(this.data, function(i, v) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530128 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 Mehta6ca80542012-09-20 19:03:14 +0530134 var account = me.item_by_name[v.account];
Rushabh Mehta41565232012-09-17 19:10:36 +0530135 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 Mehta6ca80542012-09-20 19:03:14 +0530169 $.each(this.data, function(i, account) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530170 // update groups
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530171 if(account.rgt - account.lft == 1) {
172 var parent = me.parent_map[account.name];
173 while(parent) {
Anand Doshi73519e12012-10-11 14:04:27 +0530174 var parent_account = me.item_by_name[parent];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530175 $.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 Mehta41565232012-09-17 19:10:36 +0530185 });
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 Mehta41565232012-09-17 19:10:36 +0530208});