2012-09-12 11:43:36 +00:00
|
|
|
// ERPNext - web based ERP (http://erpnext.com)
|
|
|
|
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/* todo
|
|
|
|
- load / display chart of accounts
|
|
|
|
- settings for company, start date, end data
|
|
|
|
- load balances
|
|
|
|
- open ledger on link
|
|
|
|
*/
|
|
|
|
|
|
|
|
wn.pages['chart-of-accounts'].onload = function(wrapper) {
|
|
|
|
wn.ui.make_app_page({
|
|
|
|
parent: wrapper,
|
|
|
|
title: 'Chart of Accounts',
|
|
|
|
single_column: true
|
|
|
|
});
|
|
|
|
|
2012-09-13 06:29:32 +00:00
|
|
|
erpnext.coa.make_page(wrapper);
|
|
|
|
erpnext.coa.load_companies();
|
2012-09-12 11:43:36 +00:00
|
|
|
}
|
|
|
|
erpnext.coa = {
|
2012-09-13 06:29:32 +00:00
|
|
|
make_page: function(wrapper) {
|
|
|
|
erpnext.coa.company_select = wrapper.appframe
|
|
|
|
.add_select("Company", ["Loading..."])
|
|
|
|
.change(function() {
|
|
|
|
erpnext.coa.chart = new erpnext.ChartOfAccounts();
|
|
|
|
});
|
2012-09-13 14:10:56 +00:00
|
|
|
|
|
|
|
erpnext.coa.fiscal_year_select = wrapper.appframe
|
|
|
|
.add_select("Fiscal Year", ["Loading..."]).css("width", "100px")
|
|
|
|
.change(function() {
|
|
|
|
var selected_year = $(this).val();
|
|
|
|
var fiscal_year = $.map(erpnext.coa.fiscal_years, function(v) {
|
|
|
|
return v[0] === selected_year ? v : null;
|
|
|
|
});
|
|
|
|
erpnext.coa.opening_date.val(dateutil.str_to_user(fiscal_year[1]));
|
|
|
|
erpnext.coa.closing_date.val(dateutil.str_to_user(fiscal_year[2]));
|
|
|
|
erpnext.coa.refresh_btn.click();
|
|
|
|
})
|
2012-09-13 06:29:32 +00:00
|
|
|
|
|
|
|
erpnext.coa.opening_date = wrapper.appframe.add_date("Opening Date")
|
|
|
|
.val(dateutil.str_to_user(sys_defaults.year_start_date));
|
|
|
|
|
|
|
|
var end_date = new Date();
|
|
|
|
if(end_date > dateutil.str_to_obj(sys_defaults.year_end_date))
|
|
|
|
end_date = sys_defaults.year_end_date;
|
|
|
|
|
|
|
|
erpnext.coa.closing_date = wrapper.appframe.add_date("Closing Date")
|
|
|
|
.val(dateutil.obj_to_user(end_date));
|
|
|
|
|
|
|
|
erpnext.coa.refresh_btn = wrapper.appframe.add_button("Refresh", function() {
|
2012-09-13 14:10:56 +00:00
|
|
|
erpnext.coa.chart.refresh();
|
2012-09-13 06:29:32 +00:00
|
|
|
}, "icon-refresh");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
erpnext.coa.waiting = $('<div class="well" style="width: 63%; margin: 30px auto;">\
|
|
|
|
<p style="text-align: center;">Building Trial Balance Report. \
|
|
|
|
Please wait for a few moments</p>\
|
|
|
|
<div class="progress progress-striped active">\
|
|
|
|
<div class="bar" style="width: 100%"></div></div>')
|
|
|
|
.appendTo($(wrapper).find('.layout-main'));
|
|
|
|
|
|
|
|
$('<div id="chart-of-accounts" style="height: 500px; border: 1px solid #aaa;">\
|
|
|
|
</div>').appendTo($(wrapper).find('.layout-main'));
|
|
|
|
|
|
|
|
},
|
|
|
|
load_companies: function() {
|
|
|
|
wn.call({
|
|
|
|
module: "accounts",
|
|
|
|
page: "chart_of_accounts",
|
|
|
|
method: "get_companies",
|
|
|
|
callback: function(r) {
|
|
|
|
erpnext.coa.waiting.toggle();
|
|
|
|
erpnext.coa.company_select.empty().add_options(r.message.companies)
|
|
|
|
.val(sys_defaults.company || r.message.companies[0]).change();
|
2012-09-13 14:10:56 +00:00
|
|
|
erpnext.coa.fiscal_year_select.empty()
|
|
|
|
.add_options($.map(r.message.fiscal_years, function(v) { return v[0]; }))
|
|
|
|
.val(sys_defaults.fiscal_year);
|
2012-09-13 06:29:32 +00:00
|
|
|
erpnext.coa.fiscal_years = r.message.fiscal_years;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
erpnext.ChartOfAccounts = Class.extend({
|
|
|
|
init: function() {
|
|
|
|
this.load_slickgrid();
|
|
|
|
this.load_data($(erpnext.coa.company_select).val());
|
|
|
|
},
|
2012-09-12 11:43:36 +00:00
|
|
|
load_slickgrid: function() {
|
|
|
|
// load tree
|
|
|
|
wn.require('js/lib/slickgrid/slick.grid.css');
|
|
|
|
wn.require('js/lib/slickgrid/slick-default-theme.css');
|
|
|
|
wn.require('js/lib/slickgrid/jquery.event.drag.min.js');
|
|
|
|
wn.require('js/lib/slickgrid/slick.core.js');
|
|
|
|
wn.require('js/lib/slickgrid/slick.grid.js');
|
|
|
|
wn.require('js/lib/slickgrid/slick.dataview.js');
|
2012-09-13 14:10:56 +00:00
|
|
|
wn.dom.set_style('.slick-cell { font-size: 12px; }');
|
2012-09-12 11:43:36 +00:00
|
|
|
},
|
2012-09-12 12:44:12 +00:00
|
|
|
refresh: function() {
|
2012-09-13 06:29:32 +00:00
|
|
|
this.prepare_balances();
|
|
|
|
this.render();
|
2012-09-12 12:44:12 +00:00
|
|
|
},
|
2012-09-13 14:10:56 +00:00
|
|
|
render: function() {
|
|
|
|
var me = this;
|
|
|
|
erpnext.coa.waiting.toggle(false);
|
|
|
|
this.setup_dataview();
|
|
|
|
|
|
|
|
var columns = [
|
|
|
|
{id: "name", name: "Account", field: "name", width: 300, cssClass: "cell-title",
|
|
|
|
formatter: this.account_formatter},
|
|
|
|
{id: "opening_debit", name: "Opening (Dr)", field: "opening_debit", width: 100},
|
|
|
|
{id: "opening_credit", name: "Opening (Cr)", field: "opening_credit", width: 100},
|
|
|
|
{id: "debit", name: "Debit", field: "debit", width: 100},
|
|
|
|
{id: "credit", name: "Credit", field: "credit", width: 100},
|
|
|
|
{id: "closing_debit", name: "Closing (Dr)", field: "closing_debit", width: 100},
|
|
|
|
{id: "closing_credit", name: "Closing (Cr)", field: "closing_credit", width: 100}
|
|
|
|
];
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
editable: false,
|
|
|
|
enableColumnReorder: false
|
|
|
|
};
|
|
|
|
|
|
|
|
// initialize the grid
|
|
|
|
var grid = new Slick.Grid("#chart-of-accounts", this.dataView, columns, options);
|
|
|
|
this.add_events(grid);
|
|
|
|
this.grid = grid;
|
|
|
|
},
|
2012-09-12 11:43:36 +00:00
|
|
|
load_data: function(company) {
|
2012-09-13 06:29:32 +00:00
|
|
|
var me = this;
|
2012-09-12 11:43:36 +00:00
|
|
|
wn.call({
|
|
|
|
module: "accounts",
|
|
|
|
page: "chart_of_accounts",
|
|
|
|
method: "get_chart",
|
|
|
|
args: {company: company},
|
|
|
|
callback: function(r) {
|
2012-09-13 06:29:32 +00:00
|
|
|
me.gl = r.message.gl;
|
|
|
|
me.prepare_chart(r.message.chart);
|
2012-09-13 14:10:56 +00:00
|
|
|
$.each(me.gl, function(i, v) {
|
|
|
|
v[1] = me.accounts[v[1]].name;
|
|
|
|
});
|
2012-09-13 06:29:32 +00:00
|
|
|
me.refresh();
|
2012-09-12 11:43:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2012-09-12 12:44:12 +00:00
|
|
|
prepare_chart: function(indata) {
|
2012-09-12 11:43:36 +00:00
|
|
|
var data = [];
|
|
|
|
var parent_map = {};
|
|
|
|
var data_by_name = {};
|
|
|
|
$.each(indata, function(i, v) {
|
|
|
|
if(v[0]) {
|
|
|
|
var d = {
|
|
|
|
"id": v[0],
|
|
|
|
"name": v[0],
|
|
|
|
"parent": v[1],
|
2012-09-13 06:29:32 +00:00
|
|
|
"debit_or_credit": v[2],
|
|
|
|
"opening_debit": 0,
|
|
|
|
"opening_credit": 0,
|
2012-09-12 12:44:12 +00:00
|
|
|
"debit": 0,
|
|
|
|
"credit": 0,
|
2012-09-13 06:29:32 +00:00
|
|
|
"closing_debit": 0,
|
|
|
|
"closing_credit": 0,
|
2012-09-12 13:30:14 +00:00
|
|
|
"is_pl": v[3]
|
2012-09-12 11:43:36 +00:00
|
|
|
};
|
2012-09-13 06:29:32 +00:00
|
|
|
|
2012-09-12 11:43:36 +00:00
|
|
|
data.push(d);
|
|
|
|
data_by_name[d.name] = d;
|
|
|
|
if(d.parent) {
|
|
|
|
parent_map[d.name] = d.parent;
|
2012-09-13 06:29:32 +00:00
|
|
|
}
|
2012-09-12 11:43:36 +00:00
|
|
|
}
|
|
|
|
});
|
2012-09-13 06:29:32 +00:00
|
|
|
this.set_indent(data, parent_map);
|
2012-09-13 14:10:56 +00:00
|
|
|
this.accounts = data;
|
2012-09-13 06:29:32 +00:00
|
|
|
this.parent_map = parent_map;
|
2012-09-13 14:10:56 +00:00
|
|
|
this.accounts_by_name = data_by_name;
|
2012-09-12 11:43:36 +00:00
|
|
|
},
|
2012-09-12 12:44:12 +00:00
|
|
|
prepare_balances: function() {
|
2012-09-13 06:29:32 +00:00
|
|
|
var gl = this.gl;
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
this.opening_date = dateutil.user_to_obj(erpnext.coa.opening_date.val());
|
|
|
|
this.closing_date = dateutil.user_to_obj(erpnext.coa.closing_date.val());
|
|
|
|
this.set_fiscal_year();
|
|
|
|
if (!this.fiscal_year) return;
|
2012-09-12 13:30:14 +00:00
|
|
|
|
2012-09-13 14:10:56 +00:00
|
|
|
$.each(this.accounts, function(i, v) {
|
2012-09-13 06:29:32 +00:00
|
|
|
v.opening_debit = v.opening_credit = v.debit
|
|
|
|
= v.credit = v.closing_debit = v.closing_credit = 0;
|
2012-09-12 12:44:12 +00:00
|
|
|
});
|
2012-09-12 13:30:14 +00:00
|
|
|
|
2012-09-12 12:44:12 +00:00
|
|
|
$.each(gl, function(i, v) {
|
|
|
|
var posting_date = dateutil.str_to_obj(v[0]);
|
2012-09-13 14:10:56 +00:00
|
|
|
var account = me.accounts_by_name[v[1]];
|
2012-09-13 06:29:32 +00:00
|
|
|
me.update_balances(account, posting_date, v)
|
|
|
|
});
|
|
|
|
|
|
|
|
this.update_groups();
|
|
|
|
this.format_balances();
|
|
|
|
},
|
|
|
|
update_balances: function(account, posting_date, v) {
|
|
|
|
// opening
|
|
|
|
if (posting_date < this.opening_date || v[4] === "Y") {
|
|
|
|
if (account.is_pl === "Yes" && posting_date <= dateutil.str_to_obj(this.fiscal_year[1])) {
|
|
|
|
// balance of previous fiscal_year should
|
|
|
|
// not be part of opening of pl account balance
|
|
|
|
} else {
|
|
|
|
if(account.debit_or_credit=='D') {
|
|
|
|
account.opening_debit += (v[2] - v[3]);
|
2012-09-12 12:44:12 +00:00
|
|
|
} else {
|
2012-09-13 06:29:32 +00:00
|
|
|
account.opening_credit += (v[3] - v[2]);
|
2012-09-12 12:44:12 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-13 06:29:32 +00:00
|
|
|
} else if (this.opening_date <= posting_date && posting_date <= this.closing_date) {
|
|
|
|
// in between
|
|
|
|
account.debit += v[2];
|
|
|
|
account.credit += v[3];
|
|
|
|
}
|
|
|
|
// closing
|
|
|
|
if(account.debit_or_credit=='D') {
|
|
|
|
account.closing_debit = account.opening_debit + account.debit - account.credit;
|
|
|
|
} else {
|
|
|
|
account.closing_credit = account.opening_credit - account.debit + account.credit;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update_groups: function() {
|
|
|
|
// update groups
|
|
|
|
var me= this;
|
2012-09-13 14:10:56 +00:00
|
|
|
$.each(this.accounts, function(i, account) {
|
2012-09-13 06:29:32 +00:00
|
|
|
// update groups
|
|
|
|
var parent = me.parent_map[account.name];
|
|
|
|
while(parent) {
|
2012-09-13 14:10:56 +00:00
|
|
|
parent_account = me.accounts_by_name[parent];
|
2012-09-13 06:29:32 +00:00
|
|
|
parent_account.opening_debit += account.opening_debit;
|
|
|
|
parent_account.opening_credit += account.opening_credit;
|
|
|
|
parent_account.debit += account.debit;
|
|
|
|
parent_account.credit += account.credit;
|
|
|
|
parent_account.closing_debit += account.closing_debit;
|
|
|
|
parent_account.closing_credit += account.closing_credit;
|
|
|
|
parent = me.parent_map[parent];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
format_balances: function() {
|
2012-09-12 12:44:12 +00:00
|
|
|
// format amount
|
2012-09-13 14:10:56 +00:00
|
|
|
$.each(this.accounts, function(i, v) {
|
2012-09-13 06:29:32 +00:00
|
|
|
v.opening_debit = fmt_money(v.opening_debit);
|
|
|
|
v.opening_credit = fmt_money(v.opening_credit);
|
2012-09-12 12:44:12 +00:00
|
|
|
v.debit = fmt_money(v.debit);
|
|
|
|
v.credit = fmt_money(v.credit);
|
2012-09-13 06:29:32 +00:00
|
|
|
v.closing_debit = fmt_money(v.closing_debit);
|
|
|
|
v.closing_credit = fmt_money(v.closing_credit);
|
|
|
|
});
|
2012-09-12 12:44:12 +00:00
|
|
|
},
|
2012-09-13 06:29:32 +00:00
|
|
|
set_fiscal_year: function() {
|
|
|
|
if (this.opening_date > this.closing_date) {
|
2012-09-12 13:30:14 +00:00
|
|
|
msgprint("Opening Date should be before Closing Date");
|
|
|
|
return;
|
|
|
|
}
|
2012-09-13 06:29:32 +00:00
|
|
|
|
|
|
|
this.fiscal_year = null;
|
|
|
|
var me = this;
|
2012-09-12 13:30:14 +00:00
|
|
|
$.each(erpnext.coa.fiscal_years, function(i, v) {
|
2012-09-13 06:29:32 +00:00
|
|
|
if (me.opening_date >= dateutil.str_to_obj(v[1]) &&
|
|
|
|
me.closing_date <= dateutil.str_to_obj(v[2])) {
|
|
|
|
me.fiscal_year = v;
|
2012-09-12 13:30:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-09-13 06:29:32 +00:00
|
|
|
if (!this.fiscal_year) {
|
2012-09-12 13:30:14 +00:00
|
|
|
msgprint("Opening Date and Closing Date should be within same Fiscal Year");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2012-09-12 11:43:36 +00:00
|
|
|
set_indent: function(data, parent_map) {
|
|
|
|
$.each(data, function(i, d) {
|
|
|
|
var indent = 0;
|
|
|
|
var parent = parent_map[d.name];
|
|
|
|
if(parent) {
|
|
|
|
while(parent) {
|
|
|
|
indent++;
|
|
|
|
parent = parent_map[parent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.indent = indent;
|
|
|
|
});
|
|
|
|
},
|
2012-09-13 06:29:32 +00:00
|
|
|
setup_dataview: function() {
|
|
|
|
var me = this;
|
|
|
|
// initialize the model
|
|
|
|
this.dataView = new Slick.Data.DataView({ inlineFilters: true });
|
|
|
|
this.dataView.beginUpdate();
|
2012-09-13 14:10:56 +00:00
|
|
|
this.dataView.setItems(this.accounts);
|
2012-09-13 06:29:32 +00:00
|
|
|
this.dataView.setFilter(this.dataview_filter);
|
|
|
|
this.dataView.endUpdate();
|
|
|
|
},
|
|
|
|
dataview_filter: function(item) {
|
|
|
|
if (item.parent) {
|
|
|
|
var parent = item.parent;
|
|
|
|
while (parent) {
|
2012-09-13 14:10:56 +00:00
|
|
|
if (erpnext.coa.chart.accounts_by_name[parent]._collapsed) {
|
2012-09-13 06:29:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
parent = erpnext.coa.chart.parent_map[parent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-09-12 11:43:36 +00:00
|
|
|
},
|
|
|
|
add_events: function(grid) {
|
2012-09-13 06:29:32 +00:00
|
|
|
var me = this;
|
2012-09-12 11:43:36 +00:00
|
|
|
grid.onClick.subscribe(function (e, args) {
|
|
|
|
if ($(e.target).hasClass("toggle")) {
|
2012-09-13 06:29:32 +00:00
|
|
|
var item = me.dataView.getItem(args.row);
|
2012-09-12 11:43:36 +00:00
|
|
|
if (item) {
|
|
|
|
if (!item._collapsed) {
|
|
|
|
item._collapsed = true;
|
|
|
|
} else {
|
|
|
|
item._collapsed = false;
|
|
|
|
}
|
|
|
|
|
2012-09-13 06:29:32 +00:00
|
|
|
me.dataView.updateItem(item.id, item);
|
2012-09-12 11:43:36 +00:00
|
|
|
}
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-09-13 06:29:32 +00:00
|
|
|
this.dataView.onRowsChanged.subscribe(function (e, args) {
|
2012-09-12 11:43:36 +00:00
|
|
|
grid.invalidateRows(args.rows);
|
|
|
|
grid.render();
|
|
|
|
});
|
|
|
|
|
2012-09-13 06:29:32 +00:00
|
|
|
this.dataView.onRowCountChanged.subscribe(function (e, args) {
|
2012-09-12 11:43:36 +00:00
|
|
|
grid.updateRowCount();
|
|
|
|
grid.render();
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
account_formatter: function (row, cell, value, columnDef, dataContext) {
|
|
|
|
value = value.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
|
2012-09-13 14:10:56 +00:00
|
|
|
var data = erpnext.coa.chart.accounts;
|
2012-09-12 11:43:36 +00:00
|
|
|
var spacer = "<span style='display:inline-block;height:1px;width:" +
|
|
|
|
(15 * dataContext["indent"]) + "px'></span>";
|
2012-09-13 06:29:32 +00:00
|
|
|
var idx = erpnext.coa.chart.dataView.getIdxById(dataContext.id);
|
2012-09-12 11:43:36 +00:00
|
|
|
if (data[idx + 1] && data[idx + 1].indent > data[idx].indent) {
|
|
|
|
if (dataContext._collapsed) {
|
|
|
|
return spacer + " <span class='toggle expand'></span> " + value;
|
|
|
|
} else {
|
|
|
|
return spacer + " <span class='toggle collapse'></span> " + value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return spacer + " <span class='toggle'></span> " + value;
|
|
|
|
}
|
|
|
|
}
|
2012-09-13 06:29:32 +00:00
|
|
|
});
|