Financial analytics graph fix #1542

This commit is contained in:
Nabin Hait 2014-05-04 16:02:26 +05:30
parent 232954d4b9
commit a096b2004f

View File

@ -3,7 +3,7 @@
frappe.require("assets/erpnext/js/account_tree_grid.js");
frappe.pages['financial-analytics'].onload = function(wrapper) {
frappe.pages['financial-analytics'].onload = function(wrapper) {
frappe.ui.make_app_page({
parent: wrapper,
title: __('Financial Analytics'),
@ -11,38 +11,38 @@ frappe.pages['financial-analytics'].onload = function(wrapper) {
});
erpnext.trial_balance = new erpnext.FinancialAnalytics(wrapper, 'Financial Analytics');
wrapper.appframe.add_module_icon("Accounts")
}
erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
filters: [
{
fieldtype:"Select", label: __("PL or BS"),
fieldtype:"Select", label: __("PL or BS"),
options:["Profit and Loss", "Balance Sheet"],
filter: function(val, item, opts, me) {
if(item._show) return true;
// pl or bs
var out = (val=='Balance Sheet') ?
var out = (val=='Balance Sheet') ?
item.report_type=='Balance Sheet' : item.report_type=='Profit and Loss';
if(!out) return false;
return me.apply_zero_filter(val, item, opts, me);
}
},
{
fieldtype:"Select", label: __("Company"),
fieldtype:"Select", label: __("Company"),
link:"Company", default_value: "Select Company...",
filter: function(val, item, opts) {
return item.company == val || val == opts.default_value || item._show;
}
},
{fieldtype:"Select", label: __("Fiscal Year"), link:"Fiscal Year",
{fieldtype:"Select", label: __("Fiscal Year"), link:"Fiscal Year",
default_value: "Select Fiscal Year..."},
{fieldtype:"Date", label: __("From Date")},
{fieldtype:"Label", label: __("To")},
{fieldtype:"Date", label: __("To Date")},
{fieldtype:"Select", label: __("Range"),
{fieldtype:"Select", label: __("Range"),
options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]},
{fieldtype:"Button", label: __("Refresh"), icon:"icon-refresh icon-white"},
{fieldtype:"Button", label: __("Reset Filters")}
@ -53,27 +53,27 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
formatter: this.check_formatter},
{id: "name", name: __("Account"), field: "name", width: 300,
formatter: this.tree_formatter},
{id: "opening_dr", name: __("Opening (Dr)"), field: "opening_dr",
{id: "opening_dr", name: __("Opening (Dr)"), field: "opening_dr",
hidden: true, formatter: this.currency_formatter, balance_type: "Dr"},
{id: "opening_cr", name: __("Opening (Cr)"), field: "opening_cr",
{id: "opening_cr", name: __("Opening (Cr)"), field: "opening_cr",
hidden: true, formatter: this.currency_formatter, balance_type: "Cr"},
];
this.make_date_range_columns(true);
this.make_date_range_columns(true);
this.columns = std_columns.concat(this.columns);
},
make_date_range_columns: function() {
this.columns = [];
var me = this;
var range = this.filter_inputs.range.val();
this.from_date = dateutil.user_to_str(this.filter_inputs.from_date.val());
this.to_date = dateutil.user_to_str(this.filter_inputs.to_date.val());
var date_diff = dateutil.get_diff(this.to_date, this.from_date);
me.column_map = {};
me.last_date = null;
var add_column = function(date, balance_type) {
me.columns.push({
id: date + "_" + balance_type.toLowerCase(),
@ -82,29 +82,29 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
date: date,
balance_type: balance_type,
formatter: me.currency_formatter,
width: 100
width: 110
});
}
var build_columns = function(condition) {
// add column for each date range
for(var i=0; i <= date_diff; i++) {
var date = dateutil.add_days(me.from_date, i);
if(!condition) condition = function() { return true; }
if(condition(date)) {
$.each(["Dr", "Cr"], function(i, v) {
add_column(date, v)
});
}
me.last_date = date;
if(me.columns.length) {
me.column_map[date] = me.columns[me.columns.length-1];
}
}
}
// make columns for all date ranges
if(range=='Daily') {
build_columns();
@ -112,7 +112,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
build_columns(function(date) {
if(!me.last_date) return true;
return !(dateutil.get_diff(date, me.from_date) % 7)
});
});
} else if(range=='Monthly') {
build_columns(function(date) {
if(!me.last_date) return true;
@ -122,7 +122,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
build_columns(function(date) {
if(!me.last_date) return true;
return dateutil.str_to_obj(date).getDate()==1 && in_list([0,3,6,9], dateutil.str_to_obj(date).getMonth())
});
});
} else if(range=='Yearly') {
build_columns(function(date) {
if(!me.last_date) return true;
@ -130,21 +130,21 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
return date==v.year_start_date ? true : null;
}).length;
});
}
// set label as last date of period
$.each(this.columns, function(i, col) {
col.name = me.columns[i+2]
? dateutil.str_to_user(dateutil.add_days(me.columns[i+2].date, -1)) + " (" + me.columns[i].balance_type + ")"
: dateutil.str_to_user(me.to_date) + " (" + me.columns[i].balance_type + ")";
});
});
},
setup_filters: function() {
var me = this;
this._super();
this.trigger_refresh_on_change(["pl_or_bs"]);
this.filter_inputs.pl_or_bs
.add_options($.map(frappe.report_dump.data["Cost Center"], function(v) {return v.name;}));
@ -160,10 +160,10 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
if(!this.cost_center_by_name) {
this.cost_center_by_name = this.make_name_map(frappe.report_dump.data["Cost Center"]);
}
var cost_center = inList(["Balance Sheet", "Profit and Loss"], this.pl_or_bs)
var cost_center = inList(["Balance Sheet", "Profit and Loss"], this.pl_or_bs)
? null : this.cost_center_by_name[this.pl_or_bs];
$.each(frappe.report_dump.data['GL Entry'], function(i, gl) {
var filter_by_cost_center = (function() {
if(cost_center) {
@ -192,13 +192,13 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
me.add_balance(col.date, account, gl);
}
} else if(account.report_type=='Balance Sheet'
} else if(account.report_type=='Balance Sheet'
&& (posting_date < dateutil.str_to_obj(me.from_date))) {
me.add_balance('opening', account, gl);
}
}
});
// make balances as cumulative
if(me.pl_or_bs=='Balance Sheet') {
$.each(me.data, function(i, ac) {
@ -208,18 +208,18 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter) {
if(col.balance_type=="Dr") {
opening = opening + flt(ac[col.date + "_dr"]) -
opening = opening + flt(ac[col.date + "_dr"]) -
flt(ac[col.date + "_cr"]);
me.set_debit_or_credit(ac, col.date, opening);
}
}
});
});
}
})
}
this.update_groups();
this.accounts_initialized = true;
if(!me.is_default("company")) {
// show Net Profit / Loss
var net_profit = {
@ -232,7 +232,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
report_type: me.pl_or_bs,
};
me.item_by_name[net_profit.name] = net_profit;
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter) {
if(!net_profit[col.id]) net_profit[col.id] = 0;
@ -240,12 +240,12 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
});
$.each(me.data, function(i, ac) {
if(!ac.parent_account && me.apply_filter(ac, "company") &&
if(!ac.parent_account && me.apply_filter(ac, "company") &&
ac.report_type==me.pl_or_bs) {
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter && col.balance_type=="Dr") {
var bal = net_profit[col.date+"_dr"] -
net_profit[col.date+"_cr"] +
var bal = net_profit[col.date+"_dr"] -
net_profit[col.date+"_cr"] +
ac[col.date+"_dr"] - ac[col.date+"_cr"];
me.set_debit_or_credit(net_profit, col.date, bal);
}
@ -259,7 +259,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
}
},
add_balance: function(field, account, gl) {
var bal = flt(account[field+"_dr"]) - flt(account[field+"_cr"]) +
var bal = flt(account[field+"_dr"]) - flt(account[field+"_cr"]) +
flt(gl.debit) - flt(gl.credit);
this.set_debit_or_credit(account, field, bal);
},
@ -274,63 +274,62 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
var parent_account = me.item_by_name[parent];
$.each(me.columns, function(c, col) {
if (col.formatter == me.currency_formatter && col.balance_type=="Dr") {
var bal = flt(parent_account[col.date+"_dr"]) -
flt(parent_account[col.date+"_cr"]) +
flt(account[col.date+"_dr"]) -
var bal = flt(parent_account[col.date+"_dr"]) -
flt(parent_account[col.date+"_cr"]) +
flt(account[col.date+"_dr"]) -
flt(account[col.date+"_cr"]);
me.set_debit_or_credit(parent_account, col.date, bal);
}
});
parent = me.parent_map[parent];
}
}
}
});
});
},
init_account: function(d) {
// set 0 values for all columns
this.reset_item_values(d);
// check for default graphs
if(!this.accounts_initialized && !d.parent_account) {
d.checked = true;
}
},
get_plot_data: function() {
var data = [];
var me = this;
var pl_or_bs = this.pl_or_bs;
$.each(this.data, function(i, account) {
var show = pl_or_bs == "Balance Sheet" ?
var show = pl_or_bs == "Balance Sheet" ?
account.report_type=="Balance Sheet" : account.report_type=="Profit and Loss";
if (show && account.checked && me.apply_filter(account, "company")) {
data.push({
label: account.name,
data: $.map(me.columns, function(col, idx) {
if(col.formatter==me.currency_formatter && !col.hidden &&
if(col.formatter==me.currency_formatter && !col.hidden &&
col.balance_type=="Dr") {
var bal = account[col.date+"_dr"]||account[col.date+"_cr"];
if (pl_or_bs != "Balance Sheet") {
return [[dateutil.str_to_obj(col.date).getTime(), bal],
[dateutil.user_to_obj(col.date).getTime(), bal]];
return [[dateutil.str_to_obj(col.date).getTime(), bal],
[dateutil.str_to_obj(col.date).getTime(), bal]];
} else {
return [[dateutil.user_to_obj(col.date).getTime(), bal]];
}
return [[dateutil.str_to_obj(col.date).getTime(), bal]];
}
}
}),
points: {show: true},
lines: {show: true, fill: true},
});
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(),
data[data.length-1].data = [[dateutil.str_to_obj(me.from_date).getTime(),
account.opening]].concat(data[data.length-1].data);
}
}
});
return data;
}
})
})