blob: 1cd9aa60834798e847d2f72bccdb259084aa9e44 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// 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) {
Nabin Haitc38527e2013-12-23 17:07:57 +053029 return repl("<a \
30 onclick='wn.cur_grid_report.show_general_ledger(\"%(value)s\")'>\
31 %(value)s</a>", {
Rushabh Mehta6ca80542012-09-20 19:03:14 +053032 value: item.name,
Rushabh Mehta6ca80542012-09-20 19:03:14 +053033 });
34 }
35 },
Rushabh Mehta41565232012-09-17 19:10:36 +053036 });
37 },
38 setup_columns: function() {
39 this.columns = [
Bárbara Perretti4de55b42013-10-07 17:10:04 -030040 {id: "name", name: wn._("Account"), field: "name", width: 300, cssClass: "cell-title",
Rushabh Mehta6ca80542012-09-20 19:03:14 +053041 formatter: this.tree_formatter},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030042 {id: "opening_debit", name: wn._("Opening (Dr)"), field: "opening_debit", width: 100,
Rushabh Mehta41565232012-09-17 19:10:36 +053043 formatter: this.currency_formatter},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030044 {id: "opening_credit", name: wn._("Opening (Cr)"), field: "opening_credit", width: 100,
Rushabh Mehta41565232012-09-17 19:10:36 +053045 formatter: this.currency_formatter},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030046 {id: "debit", name: wn._("Debit"), field: "debit", width: 100,
Rushabh Mehta41565232012-09-17 19:10:36 +053047 formatter: this.currency_formatter},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030048 {id: "credit", name: wn._("Credit"), field: "credit", width: 100,
Rushabh Mehta41565232012-09-17 19:10:36 +053049 formatter: this.currency_formatter},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030050 {id: "closing_debit", name: wn._("Closing (Dr)"), field: "closing_debit", width: 100,
Rushabh Mehta41565232012-09-17 19:10:36 +053051 formatter: this.currency_formatter},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030052 {id: "closing_credit", name: wn._("Closing (Cr)"), field: "closing_credit", width: 100,
Rushabh Mehta41565232012-09-17 19:10:36 +053053 formatter: this.currency_formatter}
54 ];
55
56 },
57 filters: [
Bárbara Perretti4de55b42013-10-07 17:10:04 -030058 {fieldtype: "Select", label: wn._("Company"), link:"Company", default_value: "Select Company...",
Rushabh Mehta45735152012-09-19 16:41:19 +053059 filter: function(val, item, opts, me) {
60 if (item.company == val || val == opts.default_value) {
61 return me.apply_zero_filter(val, item, opts, me);
62 }
63 return false;
Rushabh Mehta41565232012-09-17 19:10:36 +053064 }},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030065 {fieldtype: "Select", label: wn._("Fiscal Year"), link:"Fiscal Year",
Rushabh Mehta41565232012-09-17 19:10:36 +053066 default_value: "Select Fiscal Year..."},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030067 {fieldtype: "Date", label: wn._("From Date")},
68 {fieldtype: "Label", label: wn._("To")},
69 {fieldtype: "Date", label: wn._("To Date")},
70 {fieldtype: "Button", label: wn._("Refresh"), icon:"icon-refresh icon-white",
Nabin Hait3882db02013-01-01 13:48:40 +053071 cssClass:"btn-info"},
Bárbara Perretti4de55b42013-10-07 17:10:04 -030072 {fieldtype: "Button", label: wn._("Reset Filters")},
Rushabh Mehta41565232012-09-17 19:10:36 +053073 ],
74 setup_filters: function() {
75 this._super();
76 var me = this;
77 // default filters
78 this.filter_inputs.fiscal_year.change(function() {
79 var fy = $(this).val();
80 $.each(wn.report_dump.data["Fiscal Year"], function(i, v) {
81 if (v.name==fy) {
82 me.filter_inputs.from_date.val(dateutil.str_to_user(v.year_start_date));
83 me.filter_inputs.to_date.val(dateutil.str_to_user(v.year_end_date));
84 }
85 });
86 me.set_route();
87 });
Rushabh Mehta45735152012-09-19 16:41:19 +053088 me.show_zero_check()
Nabin Hait3882db02013-01-01 13:48:40 +053089 if(me.ignore_closing_entry) me.ignore_closing_entry();
Rushabh Mehta41565232012-09-17 19:10:36 +053090 },
Rushabh Mehta41565232012-09-17 19:10:36 +053091 prepare_data: function() {
Rushabh Mehta45735152012-09-19 16:41:19 +053092 var me = this;
Anand Doshi42d37bf62012-11-28 16:06:37 +053093 if(!this.primary_data) {
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053094 // make accounts list
Rushabh Mehta6ca80542012-09-20 19:03:14 +053095 me.data = [];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053096 me.parent_map = {};
Rushabh Mehta6ca80542012-09-20 19:03:14 +053097 me.item_by_name = {};
Rushabh Mehta41565232012-09-17 19:10:36 +053098
Rushabh Mehta2ad0d422012-09-18 18:52:05 +053099 $.each(wn.report_dump.data["Account"], function(i, v) {
100 var d = copy_dict(v);
101
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530102 me.data.push(d);
103 me.item_by_name[d.name] = d;
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530104 if(d.parent_account) {
105 me.parent_map[d.name] = d.parent_account;
106 }
Anand Doshi93a35ca2012-11-21 18:02:22 +0530107 });
Anand Doshi42d37bf62012-11-28 16:06:37 +0530108
109 me.primary_data = [].concat(me.data);
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530110 }
Anand Doshi42d37bf62012-11-28 16:06:37 +0530111
112 me.data = [].concat(me.primary_data);
113 $.each(me.data, function(i, d) {
114 me.init_account(d);
115 });
116
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530117 this.set_indent();
Rushabh Mehta41565232012-09-17 19:10:36 +0530118 this.prepare_balances();
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530119
Rushabh Mehta41565232012-09-17 19:10:36 +0530120 },
Rushabh Mehta09d84b62012-09-21 19:46:24 +0530121 init_account: function(d) {
122 this.reset_item_values(d);
123 },
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530124
Rushabh Mehta41565232012-09-17 19:10:36 +0530125 prepare_balances: function() {
126 var gl = wn.report_dump.data['GL Entry'];
127 var me = this;
128
129 this.opening_date = dateutil.user_to_obj(this.filter_inputs.from_date.val());
130 this.closing_date = dateutil.user_to_obj(this.filter_inputs.to_date.val());
131 this.set_fiscal_year();
132 if (!this.fiscal_year) return;
133
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530134 $.each(this.data, function(i, v) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530135 v.opening_debit = v.opening_credit = v.debit
136 = v.credit = v.closing_debit = v.closing_credit = 0;
137 });
138
139 $.each(gl, function(i, v) {
140 var posting_date = dateutil.str_to_obj(v.posting_date);
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530141 var account = me.item_by_name[v.account];
Anand Doshi54763182013-08-12 17:32:47 +0530142 me.update_balances(account, posting_date, v);
Rushabh Mehta41565232012-09-17 19:10:36 +0530143 });
144
145 this.update_groups();
146 },
147 update_balances: function(account, posting_date, v) {
148 // opening
149 if (posting_date < this.opening_date || v.is_opening === "Yes") {
150 if (account.is_pl_account === "Yes" &&
151 posting_date <= dateutil.str_to_obj(this.fiscal_year[1])) {
152 // balance of previous fiscal_year should
153 // not be part of opening of pl account balance
154 } else {
155 if(account.debit_or_credit=='Debit') {
156 account.opening_debit += (v.debit - v.credit);
157 } else {
158 account.opening_credit += (v.credit - v.debit);
159 }
160 }
161 } else if (this.opening_date <= posting_date && posting_date <= this.closing_date) {
162 // in between
163 account.debit += v.debit;
164 account.credit += v.credit;
165 }
166 // closing
167 if(account.debit_or_credit=='Debit') {
168 account.closing_debit = account.opening_debit + account.debit - account.credit;
169 } else {
170 account.closing_credit = account.opening_credit - account.debit + account.credit;
171 }
172 },
173 update_groups: function() {
174 // update groups
175 var me= this;
Rushabh Mehta6ca80542012-09-20 19:03:14 +0530176 $.each(this.data, function(i, account) {
Rushabh Mehta41565232012-09-17 19:10:36 +0530177 // update groups
Nabin Hait392596c2013-10-27 16:20:51 +0530178 if((account.group_or_ledger == "Ledger") || (account.rgt - account.lft == 1)) {
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530179 var parent = me.parent_map[account.name];
180 while(parent) {
Anand Doshi73519e12012-10-11 14:04:27 +0530181 var parent_account = me.item_by_name[parent];
Rushabh Mehta2ad0d422012-09-18 18:52:05 +0530182 $.each(me.columns, function(c, col) {
183 if (col.formatter == me.currency_formatter) {
184 parent_account[col.field] =
185 flt(parent_account[col.field])
186 + flt(account[col.field]);
187 }
188 });
189 parent = me.parent_map[parent];
190 }
191 }
Rushabh Mehta41565232012-09-17 19:10:36 +0530192 });
193 },
194
195 set_fiscal_year: function() {
196 if (this.opening_date > this.closing_date) {
Bárbara Perretti4de55b42013-10-07 17:10:04 -0300197 msgprint(wn._("Opening Date should be before Closing Date"));
Rushabh Mehta41565232012-09-17 19:10:36 +0530198 return;
199 }
200
201 this.fiscal_year = null;
202 var me = this;
203 $.each(wn.report_dump.data["Fiscal Year"], function(i, v) {
204 if (me.opening_date >= dateutil.str_to_obj(v.year_start_date) &&
205 me.closing_date <= dateutil.str_to_obj(v.year_end_date)) {
206 me.fiscal_year = v;
207 }
208 });
209
210 if (!this.fiscal_year) {
Bárbara Perretti4de55b42013-10-07 17:10:04 -0300211 msgprint(wn._("Opening Date and Closing Date should be within same Fiscal Year"));
Rushabh Mehta41565232012-09-17 19:10:36 +0530212 return;
213 }
214 },
Nabin Haitc38527e2013-12-23 17:07:57 +0530215
216 show_general_ledger: function(account) {
217 wn.route_options = {
218 account: account,
219 company: this.company,
220 from_date: this.from_date,
221 to_date: this.to_date
222 };
223 wn.set_route("query-report", "General Ledger");
224 }
Rushabh Mehta41565232012-09-17 19:10:36 +0530225});