2013-08-05 09:29:54 +00:00
|
|
|
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
|
|
|
|
// License: GNU General Public License v3. See license.txt
|
2012-09-17 13:40:36 +00:00
|
|
|
|
2012-09-24 13:43:42 +00:00
|
|
|
wn.require("app/js/account_tree_grid.js");
|
2012-09-17 13:40:36 +00:00
|
|
|
|
|
|
|
wn.pages['financial-analytics'].onload = function(wrapper) {
|
|
|
|
wn.ui.make_app_page({
|
|
|
|
parent: wrapper,
|
2013-09-30 19:57:52 +00:00
|
|
|
title: wn._('Financial Analytics'),
|
2012-09-17 13:40:36 +00:00
|
|
|
single_column: true
|
|
|
|
});
|
|
|
|
erpnext.trial_balance = new erpnext.FinancialAnalytics(wrapper, 'Financial Analytics');
|
2012-11-26 13:17:54 +00:00
|
|
|
|
2012-12-22 08:20:35 +00:00
|
|
|
wrapper.appframe.add_home_breadcrumb()
|
2013-04-19 07:31:32 +00:00
|
|
|
wrapper.appframe.add_module_icon("Accounts")
|
2012-12-22 08:20:35 +00:00
|
|
|
wrapper.appframe.add_breadcrumb("icon-bar-chart")
|
2012-09-17 13:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
|
|
|
|
filters: [
|
2013-09-30 19:57:52 +00:00
|
|
|
{fieldtype:"Select", label: wn._("PL or BS"), options:["Profit and Loss", "Balance Sheet"],
|
2012-09-18 13:22:05 +00:00
|
|
|
filter: function(val, item, opts, me) {
|
|
|
|
if(item._show) return true;
|
|
|
|
|
|
|
|
// pl or bs
|
2012-09-21 14:16:24 +00:00
|
|
|
var out = (val!='Balance Sheet') ? item.is_pl_account=='Yes' : item.is_pl_account!='Yes';
|
2012-09-18 13:22:05 +00:00
|
|
|
if(!out) return false;
|
|
|
|
|
2012-09-19 11:11:19 +00:00
|
|
|
return me.apply_zero_filter(val, item, opts, me);
|
2012-09-17 13:40:36 +00:00
|
|
|
}},
|
2013-09-30 19:57:52 +00:00
|
|
|
{fieldtype:"Select", label: wn._("Company"), link:"Company", default_value: "Select Company...",
|
2012-09-17 13:40:36 +00:00
|
|
|
filter: function(val, item, opts) {
|
|
|
|
return item.company == val || val == opts.default_value || item._show;
|
|
|
|
}},
|
2013-09-30 19:57:52 +00:00
|
|
|
{fieldtype:"Select", label: wn._("Fiscal Year"), link:"Fiscal Year",
|
2012-09-17 13:40:36 +00:00
|
|
|
default_value: "Select Fiscal Year..."},
|
2013-09-30 19:57:52 +00:00
|
|
|
{fieldtype:"Date", label: wn._("From Date")},
|
|
|
|
{fieldtype:"Label", label: wn._("To")},
|
|
|
|
{fieldtype:"Date", label: wn._("To Date")},
|
|
|
|
{fieldtype:"Select", label: wn._("Range"),
|
2012-09-17 13:40:36 +00:00
|
|
|
options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]},
|
2013-09-30 19:57:52 +00:00
|
|
|
{fieldtype:"Button", label: wn._("Refresh"), icon:"icon-refresh icon-white", cssClass:"btn-info"},
|
|
|
|
{fieldtype:"Button", label: wn._("Reset Filters")}
|
2012-09-17 13:40:36 +00:00
|
|
|
],
|
|
|
|
setup_columns: function() {
|
2012-09-18 13:22:05 +00:00
|
|
|
var std_columns = [
|
2013-10-15 18:32:13 +00:00
|
|
|
{id: "check", name: wn._("Plot"), field: "check", width: 30,
|
2012-09-18 13:22:05 +00:00
|
|
|
formatter: this.check_formatter},
|
2013-10-15 18:32:13 +00:00
|
|
|
{id: "name", name: wn._("Account"), field: "name", width: 300,
|
2012-09-20 13:33:14 +00:00
|
|
|
formatter: this.tree_formatter},
|
2013-10-15 18:32:13 +00:00
|
|
|
{id: "opening", name: wn._("Opening"), field: "opening", hidden: true,
|
2012-09-18 13:22:05 +00:00
|
|
|
formatter: this.currency_formatter}
|
2012-09-17 13:40:36 +00:00
|
|
|
];
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
this.make_date_range_columns();
|
|
|
|
this.columns = std_columns.concat(this.columns);
|
2012-09-18 13:22:05 +00:00
|
|
|
},
|
|
|
|
setup_filters: function() {
|
|
|
|
var me = this;
|
|
|
|
this._super();
|
2013-03-07 10:05:36 +00:00
|
|
|
this.trigger_refresh_on_change(["pl_or_bs"]);
|
|
|
|
|
|
|
|
this.filter_inputs.pl_or_bs
|
|
|
|
.add_options($.map(wn.report_dump.data["Cost Center"], function(v) {return v.name;}));
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
this.setup_plot_check();
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
|
|
|
init_filter_values: function() {
|
|
|
|
this._super();
|
2012-11-28 11:42:18 +00:00
|
|
|
this.filter_inputs.range.val('Monthly');
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
|
|
|
prepare_balances: function() {
|
|
|
|
var me = this;
|
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
// setup cost center map
|
|
|
|
if(!this.cost_center_by_name) {
|
|
|
|
this.cost_center_by_name = this.make_name_map(wn.report_dump.data["Cost Center"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var cost_center = inList(["Balance Sheet", "Profit and Loss"], this.pl_or_bs)
|
|
|
|
? null : this.cost_center_by_name[this.pl_or_bs];
|
|
|
|
|
2012-09-17 13:40:36 +00:00
|
|
|
$.each(wn.report_dump.data['GL Entry'], function(i, gl) {
|
2012-09-21 14:16:24 +00:00
|
|
|
var filter_by_cost_center = (function() {
|
|
|
|
if(cost_center) {
|
|
|
|
if(gl.cost_center) {
|
|
|
|
var gl_cost_center = me.cost_center_by_name[gl.cost_center];
|
|
|
|
return gl_cost_center.lft >= cost_center.lft && gl_cost_center.rgt <= cost_center.rgt;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-17 13:40:36 +00:00
|
|
|
} else {
|
2012-09-21 14:16:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
if(filter_by_cost_center) {
|
|
|
|
var posting_date = dateutil.str_to_obj(gl.posting_date);
|
|
|
|
var account = me.item_by_name[gl.account];
|
|
|
|
var col = me.column_map[gl.posting_date];
|
|
|
|
if(col) {
|
|
|
|
if(gl.voucher_type=='Period Closing Voucher') {
|
|
|
|
// period closing voucher not to be added
|
|
|
|
// to profit and loss accounts (else will become zero!!)
|
|
|
|
if(account.is_pl_account!='Yes')
|
|
|
|
me.add_balance(col.field, account, gl);
|
|
|
|
} else {
|
|
|
|
me.add_balance(col.field, account, gl);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(account.is_pl_account!='Yes'
|
|
|
|
&& (posting_date < dateutil.str_to_obj(me.from_date))) {
|
|
|
|
me.add_balance('opening', account, gl);
|
2012-09-17 13:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// make balances as cumulative
|
2012-09-21 14:16:24 +00:00
|
|
|
if(me.pl_or_bs=='Balance Sheet') {
|
2012-09-20 13:33:14 +00:00
|
|
|
$.each(me.data, function(i, ac) {
|
2012-09-17 13:40:36 +00:00
|
|
|
if((ac.rgt - ac.lft)==1 && ac.is_pl_account!='Yes') {
|
2012-10-05 13:03:22 +00:00
|
|
|
var opening = 0;
|
2012-09-17 13:40:36 +00:00
|
|
|
//if(opening) throw opening;
|
|
|
|
$.each(me.columns, function(i, col) {
|
|
|
|
if(col.formatter==me.currency_formatter) {
|
|
|
|
ac[col.field] = opening + flt(ac[col.field]);
|
|
|
|
opening = ac[col.field];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
this.update_groups();
|
2012-09-18 13:22:05 +00:00
|
|
|
this.accounts_initialized = true;
|
2012-11-28 10:36:37 +00:00
|
|
|
|
2012-11-28 11:42:30 +00:00
|
|
|
if(!me.is_default("company")) {
|
|
|
|
// show Net Profit / Loss
|
|
|
|
var net_profit = {
|
|
|
|
company: me.company,
|
|
|
|
id: "Net Profit / Loss",
|
|
|
|
name: "Net Profit / Loss",
|
|
|
|
indent: 0,
|
|
|
|
opening: 0,
|
|
|
|
checked: false,
|
|
|
|
is_pl_account: me.pl_or_bs=="Balance Sheet" ? "No" : "Yes",
|
|
|
|
};
|
|
|
|
me.item_by_name[net_profit.name] = net_profit;
|
|
|
|
|
|
|
|
$.each(me.data, function(i, ac) {
|
|
|
|
if(!ac.parent_account && me.apply_filter(ac, "company")) {
|
|
|
|
if(me.pl_or_bs == "Balance Sheet") {
|
|
|
|
var valid_account = ac.is_pl_account!="Yes";
|
|
|
|
var do_addition_for = "Debit";
|
|
|
|
} else {
|
|
|
|
var valid_account = ac.is_pl_account=="Yes";
|
|
|
|
var do_addition_for = "Credit";
|
|
|
|
}
|
|
|
|
if(valid_account) {
|
|
|
|
$.each(me.columns, function(i, col) {
|
|
|
|
if(col.formatter==me.currency_formatter) {
|
|
|
|
if(!net_profit[col.field]) net_profit[col.field] = 0;
|
|
|
|
if(ac.debit_or_credit==do_addition_for) {
|
|
|
|
net_profit[col.field] += ac[col.field];
|
|
|
|
} else {
|
|
|
|
net_profit[col.field] -= ac[col.field];
|
|
|
|
}
|
2012-11-28 10:36:37 +00:00
|
|
|
}
|
2012-11-28 11:42:30 +00:00
|
|
|
});
|
|
|
|
}
|
2012-11-28 10:36:37 +00:00
|
|
|
}
|
2012-11-28 11:42:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.data.push(net_profit);
|
|
|
|
}
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
|
|
|
add_balance: function(field, account, gl) {
|
|
|
|
account[field] = flt(account[field]) +
|
2012-09-18 13:22:05 +00:00
|
|
|
((account.debit_or_credit == "Debit" ? 1 : -1) * (flt(gl.debit) - flt(gl.credit)))
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
|
|
|
init_account: function(d) {
|
2012-09-18 13:22:05 +00:00
|
|
|
// set 0 values for all columns
|
2012-09-20 13:33:14 +00:00
|
|
|
this.reset_item_values(d);
|
2012-09-18 13:22:05 +00:00
|
|
|
|
|
|
|
// check for default graphs
|
|
|
|
if(!this.accounts_initialized && !d.parent_account) {
|
|
|
|
d.checked = true;
|
2012-09-17 13:40:36 +00:00
|
|
|
}
|
2012-09-18 13:22:05 +00:00
|
|
|
|
2012-09-17 13:40:36 +00:00
|
|
|
},
|
|
|
|
get_plot_data: function() {
|
|
|
|
var data = [];
|
|
|
|
var me = this;
|
2012-09-21 14:16:24 +00:00
|
|
|
var pl_or_bs = this.pl_or_bs;
|
2012-09-20 13:33:14 +00:00
|
|
|
$.each(this.data, function(i, account) {
|
2012-09-21 14:16:24 +00:00
|
|
|
|
|
|
|
var show = pl_or_bs != "Balance Sheet" ? account.is_pl_account=="Yes" : account.is_pl_account!="Yes";
|
2012-09-19 11:11:19 +00:00
|
|
|
if (show && account.checked && me.apply_filter(account, "company")) {
|
2012-09-17 13:40:36 +00:00
|
|
|
data.push({
|
|
|
|
label: account.name,
|
|
|
|
data: $.map(me.columns, function(col, idx) {
|
2012-09-18 13:22:05 +00:00
|
|
|
if(col.formatter==me.currency_formatter && !col.hidden) {
|
2012-09-21 14:16:24 +00:00
|
|
|
if (pl_or_bs != "Balance Sheet") {
|
2012-09-18 13:22:05 +00:00
|
|
|
return [[dateutil.str_to_obj(col.id).getTime(), account[col.field]],
|
|
|
|
[dateutil.user_to_obj(col.name).getTime(), account[col.field]]];
|
|
|
|
} else {
|
|
|
|
return [[dateutil.user_to_obj(col.name).getTime(), account[col.field]]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
points: {show: true},
|
|
|
|
lines: {show: true, fill: true},
|
2012-09-17 13:40:36 +00:00
|
|
|
});
|
2012-09-18 13:22:05 +00:00
|
|
|
|
|
|
|
if(pl_or_bs == "Balance Sheet") {
|
|
|
|
// prepend opening for balance sheet accounts
|
|
|
|
data[data.length-1].data = [[dateutil.str_to_obj(me.from_date).getTime(),
|
|
|
|
account.opening]].concat(data[data.length-1].data);
|
|
|
|
}
|
2012-09-17 13:40:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
2012-09-21 09:49:40 +00:00
|
|
|
}
|
2012-09-17 13:40:36 +00:00
|
|
|
})
|