blob: a893a52f003b2951e6c00d59eacf991a75fcaa31 [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: [
Nabin Hait3882db02013-01-01 13:48:40 +053054 {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 }},
Nabin Hait3882db02013-01-01 13:48:40 +053061 {fieldtype: "Select", label: "Fiscal Year", link:"Fiscal Year",
Rushabh Mehta41565232012-09-17 19:10:36 +053062 default_value: "Select Fiscal Year..."},
Nabin Hait3882db02013-01-01 13:48:40 +053063 {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 Mehta41565232012-09-17 19:10:36 +053069 ],
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 Mehta45735152012-09-19 16:41:19 +053084 me.show_zero_check()
Nabin Hait3882db02013-01-01 13:48:40 +053085 if(me.ignore_closing_entry) me.ignore_closing_entry();
Rushabh Mehta41565232012-09-17 19:10:36 +053086 },
Rushabh Mehta41565232012-09-17 19:10:36 +053087 prepare_data: function() {
Rushabh Mehta45735152012-09-19 16:41:19 +053088 var me = this;
Anand Doshi42d37bf62012-11-28 16:06:37 +053089 if(!this.primary_data) {
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053090 // make accounts list
Rushabh Mehta6ca80542012-09-20 19:03:14 +053091 me.data = [];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053092 me.parent_map = {};
Rushabh Mehta6ca80542012-09-20 19:03:14 +053093 me.item_by_name = {};
Rushabh Mehta41565232012-09-17 19:10:36 +053094
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053095 $.each(wn.report_dump.data["Account"], function(i, v) {
96 var d = copy_dict(v);
97
Rushabh Mehta6ca80542012-09-20 19:03:14 +053098 me.data.push(d);
99 me.item_by_name[d.name] = d;
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530100 if(d.parent_account) {
101 me.parent_map[d.name] = d.parent_account;
102 }
Anand Doshi93a35ca2012-11-21 18:02:22 +0530103 });
Anand Doshi42d37bf62012-11-28 16:06:37 +0530104
105 me.primary_data = [].concat(me.data);
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530106 }
Anand Doshi42d37bf62012-11-28 16:06:37 +0530107
108 me.data = [].concat(me.primary_data);
109 $.each(me.data, function(i, d) {
110 me.init_account(d);
111 });
112
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530113 this.set_indent();
Rushabh Mehta41565232012-09-17 19:10:36 +0530114 this.prepare_balances();
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530115
Rushabh Mehta41565232012-09-17 19:10:36 +0530116 },
Rushabh Mehta09d84b62012-09-21 19:46:24 +0530117 init_account: function(d) {
118 this.reset_item_values(d);
119 },
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530120
Rushabh Mehta41565232012-09-17 19:10:36 +0530121 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 Mehta6ca80542012-09-20 19:03:14 +0530130 $.each(this.data, function(i, v) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530131 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 Mehta6ca80542012-09-20 19:03:14 +0530137 var account = me.item_by_name[v.account];
Rushabh Mehta41565232012-09-17 19:10:36 +0530138 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 Mehta6ca80542012-09-20 19:03:14 +0530172 $.each(this.data, function(i, account) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530173 // update groups
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530174 if(account.rgt - account.lft == 1) {
175 var parent = me.parent_map[account.name];
176 while(parent) {
Anand Doshi73519e12012-10-11 14:04:27 +0530177 var parent_account = me.item_by_name[parent];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530178 $.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 Mehta41565232012-09-17 19:10:36 +0530188 });
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 Mehta41565232012-09-17 19:10:36 +0530211});