blob: c779cfa6d9236e6c77f7197fe5a0451f574eeeae [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2// License: GNU General Public License v3. See license.txt
3
Rushabh Mehta41565232012-09-17 19:10:36 +05304// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Rushabh Mehta09d84b62012-09-21 19:46:24 +053017erpnext.AccountTreeGrid = wn.views.TreeGridReport.extend({
Rushabh Mehta41565232012-09-17 19:10:36 +053018 init: function(wrapper, title) {
19 this._super({
20 title: title,
21 page: wrapper,
22 parent: $(wrapper).find('.layout-main'),
23 appframe: wrapper.appframe,
Rushabh Mehta09d84b62012-09-21 19:46:24 +053024 doctypes: ["Company", "Fiscal Year", "Account", "GL Entry", "Cost Center"],
Rushabh Mehta6ca80542012-09-20 19:03:14 +053025 tree_grid: {
26 show: true,
27 parent_field: "parent_account",
28 formatter: function(item) {
29 return repl('<a href="#general-ledger/account=%(enc_value)s">%(value)s</a>', {
30 value: item.name,
31 enc_value: encodeURIComponent(item.name)
32 });
33 }
34 },
Rushabh Mehta41565232012-09-17 19:10:36 +053035 });
36 },
37 setup_columns: function() {
38 this.columns = [
39 {id: "name", name: "Account", field: "name", width: 300, cssClass: "cell-title",
Rushabh Mehta6ca80542012-09-20 19:03:14 +053040 formatter: this.tree_formatter},
Rushabh Mehta41565232012-09-17 19:10:36 +053041 {id: "opening_debit", name: "Opening (Dr)", field: "opening_debit", width: 100,
42 formatter: this.currency_formatter},
43 {id: "opening_credit", name: "Opening (Cr)", field: "opening_credit", width: 100,
44 formatter: this.currency_formatter},
45 {id: "debit", name: "Debit", field: "debit", width: 100,
46 formatter: this.currency_formatter},
47 {id: "credit", name: "Credit", field: "credit", width: 100,
48 formatter: this.currency_formatter},
49 {id: "closing_debit", name: "Closing (Dr)", field: "closing_debit", width: 100,
50 formatter: this.currency_formatter},
51 {id: "closing_credit", name: "Closing (Cr)", field: "closing_credit", width: 100,
52 formatter: this.currency_formatter}
53 ];
54
55 },
56 filters: [
Nabin Hait3882db02013-01-01 13:48:40 +053057 {fieldtype: "Select", label: "Company", link:"Company", default_value: "Select Company...",
Rushabh Mehta45735152012-09-19 16:41:19 +053058 filter: function(val, item, opts, me) {
59 if (item.company == val || val == opts.default_value) {
60 return me.apply_zero_filter(val, item, opts, me);
61 }
62 return false;
Rushabh Mehta41565232012-09-17 19:10:36 +053063 }},
Nabin Hait3882db02013-01-01 13:48:40 +053064 {fieldtype: "Select", label: "Fiscal Year", link:"Fiscal Year",
Rushabh Mehta41565232012-09-17 19:10:36 +053065 default_value: "Select Fiscal Year..."},
Nabin Hait3882db02013-01-01 13:48:40 +053066 {fieldtype: "Date", label: "From Date"},
67 {fieldtype: "Label", label: "To"},
68 {fieldtype: "Date", label: "To Date"},
69 {fieldtype: "Button", label: "Refresh", icon:"icon-refresh icon-white",
70 cssClass:"btn-info"},
71 {fieldtype: "Button", label: "Reset Filters"},
Rushabh Mehta41565232012-09-17 19:10:36 +053072 ],
73 setup_filters: function() {
74 this._super();
75 var me = this;
76 // default filters
77 this.filter_inputs.fiscal_year.change(function() {
78 var fy = $(this).val();
79 $.each(wn.report_dump.data["Fiscal Year"], function(i, v) {
80 if (v.name==fy) {
81 me.filter_inputs.from_date.val(dateutil.str_to_user(v.year_start_date));
82 me.filter_inputs.to_date.val(dateutil.str_to_user(v.year_end_date));
83 }
84 });
85 me.set_route();
86 });
Rushabh Mehta45735152012-09-19 16:41:19 +053087 me.show_zero_check()
Nabin Hait3882db02013-01-01 13:48:40 +053088 if(me.ignore_closing_entry) me.ignore_closing_entry();
Rushabh Mehta41565232012-09-17 19:10:36 +053089 },
Rushabh Mehta41565232012-09-17 19:10:36 +053090 prepare_data: function() {
Rushabh Mehta45735152012-09-19 16:41:19 +053091 var me = this;
Anand Doshi42d37bf62012-11-28 16:06:37 +053092 if(!this.primary_data) {
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053093 // 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 }
Anand Doshi93a35ca2012-11-21 18:02:22 +0530106 });
Anand Doshi42d37bf62012-11-28 16:06:37 +0530107
108 me.primary_data = [].concat(me.data);
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530109 }
Anand Doshi42d37bf62012-11-28 16:06:37 +0530110
111 me.data = [].concat(me.primary_data);
112 $.each(me.data, function(i, d) {
113 me.init_account(d);
114 });
115
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530116 this.set_indent();
Rushabh Mehta41565232012-09-17 19:10:36 +0530117 this.prepare_balances();
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530118
Rushabh Mehta41565232012-09-17 19:10:36 +0530119 },
Rushabh Mehta09d84b62012-09-21 19:46:24 +0530120 init_account: function(d) {
121 this.reset_item_values(d);
122 },
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530123
Rushabh Mehta41565232012-09-17 19:10:36 +0530124 prepare_balances: function() {
125 var gl = wn.report_dump.data['GL Entry'];
126 var me = this;
127
128 this.opening_date = dateutil.user_to_obj(this.filter_inputs.from_date.val());
129 this.closing_date = dateutil.user_to_obj(this.filter_inputs.to_date.val());
130 this.set_fiscal_year();
131 if (!this.fiscal_year) return;
132
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530133 $.each(this.data, function(i, v) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530134 v.opening_debit = v.opening_credit = v.debit
135 = v.credit = v.closing_debit = v.closing_credit = 0;
136 });
137
138 $.each(gl, function(i, v) {
139 var posting_date = dateutil.str_to_obj(v.posting_date);
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530140 var account = me.item_by_name[v.account];
Anand Doshi54763182013-08-12 17:32:47 +0530141 me.update_balances(account, posting_date, v);
Rushabh Mehta41565232012-09-17 19:10:36 +0530142 });
143
144 this.update_groups();
145 },
146 update_balances: function(account, posting_date, v) {
147 // opening
148 if (posting_date < this.opening_date || v.is_opening === "Yes") {
149 if (account.is_pl_account === "Yes" &&
150 posting_date <= dateutil.str_to_obj(this.fiscal_year[1])) {
151 // balance of previous fiscal_year should
152 // not be part of opening of pl account balance
153 } else {
154 if(account.debit_or_credit=='Debit') {
155 account.opening_debit += (v.debit - v.credit);
156 } else {
157 account.opening_credit += (v.credit - v.debit);
158 }
159 }
160 } else if (this.opening_date <= posting_date && posting_date <= this.closing_date) {
161 // in between
162 account.debit += v.debit;
163 account.credit += v.credit;
164 }
165 // closing
166 if(account.debit_or_credit=='Debit') {
167 account.closing_debit = account.opening_debit + account.debit - account.credit;
168 } else {
169 account.closing_credit = account.opening_credit - account.debit + account.credit;
170 }
171 },
172 update_groups: function() {
173 // update groups
174 var me= this;
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530175 $.each(this.data, function(i, account) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530176 // update groups
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530177 if(account.rgt - account.lft == 1) {
178 var parent = me.parent_map[account.name];
179 while(parent) {
Anand Doshi73519e12012-10-11 14:04:27 +0530180 var parent_account = me.item_by_name[parent];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530181 $.each(me.columns, function(c, col) {
182 if (col.formatter == me.currency_formatter) {
183 parent_account[col.field] =
184 flt(parent_account[col.field])
185 + flt(account[col.field]);
186 }
187 });
188 parent = me.parent_map[parent];
189 }
190 }
Rushabh Mehta41565232012-09-17 19:10:36 +0530191 });
192 },
193
194 set_fiscal_year: function() {
195 if (this.opening_date > this.closing_date) {
196 msgprint("Opening Date should be before Closing Date");
197 return;
198 }
199
200 this.fiscal_year = null;
201 var me = this;
202 $.each(wn.report_dump.data["Fiscal Year"], function(i, v) {
203 if (me.opening_date >= dateutil.str_to_obj(v.year_start_date) &&
204 me.closing_date <= dateutil.str_to_obj(v.year_end_date)) {
205 me.fiscal_year = v;
206 }
207 });
208
209 if (!this.fiscal_year) {
210 msgprint("Opening Date and Closing Date should be within same Fiscal Year");
211 return;
212 }
213 },
Rushabh Mehta41565232012-09-17 19:10:36 +0530214});