brotherton-erpnext/erpnext/accounts/page/financial_analytics/financial_analytics.js

333 lines
11 KiB
JavaScript
Raw Normal View History

// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
2012-09-17 13:40:36 +00:00
2015-01-29 12:39:11 +00:00
frappe.pages['financial-analytics'].on_page_load = function(wrapper) {
2014-02-14 10:17:51 +00:00
frappe.ui.make_app_page({
2012-09-17 13:40:36 +00:00
parent: wrapper,
2014-04-14 10:55:30 +00:00
title: __('Financial Analytics'),
2012-09-17 13:40:36 +00:00
single_column: true
});
2015-01-01 10:55:43 +00:00
erpnext.financial_analytics = new erpnext.FinancialAnalytics(wrapper, 'Financial Analytics');
2015-03-09 10:17:15 +00:00
frappe.breadcrumbs.add("Accounts");
2014-05-04 10:32:26 +00:00
2015-02-17 14:23:23 +00:00
};
{% include "erpnext/public/js/account_tree_grid.js" %}
2012-09-17 13:40:36 +00:00
erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
filters: [
{
fieldtype:"Select", label: __("PL or BS"), fieldname: "pl_or_bs",
options:[{"label": __("Profit and Loss"), "value": "Profit and Loss"},
{"label": __("Balance Sheet"), "value": "Balance Sheet"}],
2012-09-18 13:22:05 +00:00
filter: function(val, item, opts, me) {
if(item._show) return true;
2014-05-04 10:32:26 +00:00
2012-09-18 13:22:05 +00:00
// pl or bs
2014-05-04 10:32:26 +00:00
var out = (val=='Balance Sheet') ?
item.report_type=='Balance Sheet' : item.report_type=='Profit and Loss';
2012-09-18 13:22:05 +00:00
if(!out) return false;
2014-05-04 10:32:26 +00:00
return me.apply_zero_filter(val, item, opts, me);
}
},
{
fieldtype:"Select", label: __("Company"), fieldname: "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;
}
},
{fieldtype:"Select", label: __("Fiscal Year"), link:"Fiscal Year", fieldname: "fiscal_year",
default_value: __("Select Fiscal Year...")},
{fieldtype:"Date", label: __("From Date"), fieldname: "from_date"},
{fieldtype:"Date", label: __("To Date"), fieldname: "to_date"},
{fieldtype:"Select", label: __("Range"), fieldname: "range",
options:[{label: __("Daily"), value: "Daily"}, {label: __("Weekly"), value: "Weekly"},
{label: __("Monthly"), value: "Monthly"}, {label: __("Quarterly"), value: "Quarterly"},
2015-01-01 10:55:43 +00:00
{label: __("Yearly"), value: "Yearly"}]}
2012-09-17 13:40:36 +00:00
],
setup_columns: function() {
2012-09-18 13:22:05 +00:00
var std_columns = [
{id: "_check", name: __("Plot"), field: "_check", width: 30,
2012-09-18 13:22:05 +00:00
formatter: this.check_formatter},
2014-04-14 10:55:30 +00:00
{id: "name", name: __("Account"), field: "name", width: 300,
2012-09-20 13:33:14 +00:00
formatter: this.tree_formatter},
2014-05-04 10:32:26 +00:00
{id: "opening_dr", name: __("Opening (Dr)"), field: "opening_dr",
hidden: true, formatter: this.currency_formatter, balance_type: "Dr"},
2014-05-04 10:32:26 +00:00
{id: "opening_cr", name: __("Opening (Cr)"), field: "opening_cr",
hidden: true, formatter: this.currency_formatter, balance_type: "Cr"},
2012-09-17 13:40:36 +00:00
];
2014-05-04 10:32:26 +00:00
this.make_date_range_columns(true);
2012-09-20 13:33:14 +00:00
this.columns = std_columns.concat(this.columns);
2012-09-18 13:22:05 +00:00
},
make_date_range_columns: function() {
this.columns = [];
2014-05-04 10:32:26 +00:00
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);
2014-05-04 10:32:26 +00:00
me.column_map = {};
me.last_date = null;
2014-05-04 10:32:26 +00:00
var add_column = function(date, balance_type) {
me.columns.push({
id: date + "_" + balance_type.toLowerCase(),
name: dateutil.str_to_user(date),
field: date + "_" + balance_type.toLowerCase(),
date: date,
balance_type: balance_type,
formatter: me.currency_formatter,
2014-05-04 10:32:26 +00:00
width: 110
});
}
2014-05-04 10:32:26 +00:00
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; }
2014-05-04 10:32:26 +00:00
if(condition(date)) {
$.each(["Dr", "Cr"], function(i, v) {
add_column(date, v)
});
}
me.last_date = date;
2014-05-04 10:32:26 +00:00
if(me.columns.length) {
me.column_map[date] = me.columns[me.columns.length-1];
}
}
}
2014-05-04 10:32:26 +00:00
// make columns for all date ranges
if(range=='Daily') {
build_columns();
} else if(range=='Weekly') {
build_columns(function(date) {
if(!me.last_date) return true;
return !(dateutil.get_diff(date, me.from_date) % 7)
2014-05-04 10:32:26 +00:00
});
} else if(range=='Monthly') {
build_columns(function(date) {
if(!me.last_date) return true;
return dateutil.str_to_obj(me.last_date).getMonth() != dateutil.str_to_obj(date).getMonth()
});
} else if(range=='Quarterly') {
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())
2014-05-04 10:32:26 +00:00
});
} else if(range=='Yearly') {
build_columns(function(date) {
if(!me.last_date) return true;
return $.map(frappe.report_dump.data['Fiscal Year'], function(v) {
return date==v.year_start_date ? true : null;
}).length;
});
2014-05-04 10:32:26 +00:00
}
2014-05-04 10:32:26 +00:00
// 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 + ")";
2014-05-04 10:32:26 +00:00
});
},
2012-09-18 13:22:05 +00:00
setup_filters: function() {
var me = this;
this._super();
this.trigger_refresh_on_change(["pl_or_bs"]);
2014-05-04 10:32:26 +00:00
this.filter_inputs.pl_or_bs
2014-02-14 10:17:51 +00:00
.add_options($.map(frappe.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();
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) {
2014-02-14 10:17:51 +00:00
this.cost_center_by_name = this.make_name_map(frappe.report_dump.data["Cost Center"]);
2012-09-21 14:16:24 +00:00
}
2014-05-04 10:32:26 +00:00
var cost_center = inList(["Balance Sheet", "Profit and Loss"], this.pl_or_bs)
2012-09-21 14:16:24 +00:00
? null : this.cost_center_by_name[this.pl_or_bs];
2014-05-04 10:32:26 +00:00
2014-02-14 10:17:51 +00:00
$.each(frappe.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.report_type=='Balance Sheet')
me.add_balance(col.date, account, gl);
2012-09-21 14:16:24 +00:00
} else {
me.add_balance(col.date, account, gl);
2012-09-21 14:16:24 +00:00
}
2014-05-04 10:32:26 +00:00
} else if(account.report_type=='Balance Sheet'
2012-09-21 14:16:24 +00:00
&& (posting_date < dateutil.str_to_obj(me.from_date))) {
me.add_balance('opening', account, gl);
2012-09-17 13:40:36 +00:00
}
}
});
2014-05-04 10:32:26 +00:00
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) {
if((ac.rgt - ac.lft)==1 && ac.report_type=='Balance Sheet') {
2014-06-17 08:22:03 +00:00
var opening = flt(ac["opening_dr"]) - flt(ac["opening_cr"]);
2012-09-17 13:40:36 +00:00
//if(opening) throw opening;
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter) {
2014-06-17 08:22:03 +00:00
if(col.balance_type=="Dr" && !in_list(["opening_dr", "opening_cr"], col.field)) {
2014-05-04 10:32:26 +00:00
opening = opening + flt(ac[col.date + "_dr"]) -
flt(ac[col.date + "_cr"]);
me.set_debit_or_credit(ac, col.date, opening);
}
2012-09-17 13:40:36 +00:00
}
2014-05-04 10:32:26 +00:00
});
2012-09-17 13:40:36 +00:00
}
})
}
this.update_groups();
2012-09-18 13:22:05 +00:00
this.accounts_initialized = true;
2014-05-04 10:32:26 +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,
report_type: me.pl_or_bs=="Balance Sheet"? "Balance Sheet" : "Profit and Loss",
};
me.item_by_name[net_profit.name] = net_profit;
2014-05-04 10:32:26 +00:00
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter) {
if(!net_profit[col.id]) net_profit[col.id] = 0;
}
});
$.each(me.data, function(i, ac) {
2014-05-04 10:32:26 +00:00
if(!ac.parent_account && me.apply_filter(ac, "company") &&
ac.report_type==net_profit.report_type) {
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter && col.balance_type=="Dr") {
2014-05-04 10:32:26 +00:00
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);
}
});
2012-11-28 10:36:37 +00:00
}
});
this.data.push(net_profit);
}
2012-09-17 13:40:36 +00:00
},
add_balance: function(field, account, gl) {
2014-05-04 10:32:26 +00:00
var bal = flt(account[field+"_dr"]) - flt(account[field+"_cr"]) +
flt(gl.debit) - flt(gl.credit);
this.set_debit_or_credit(account, field, bal);
},
update_groups: function() {
// update groups
var me= this;
$.each(this.data, function(i, account) {
// update groups
2015-04-23 07:44:17 +00:00
if((account.is_group == 0) || (account.rgt - account.lft == 1)) {
var parent = me.parent_map[account.name];
while(parent) {
var parent_account = me.item_by_name[parent];
$.each(me.columns, function(c, col) {
if (col.formatter == me.currency_formatter && col.balance_type=="Dr") {
2014-05-04 10:32:26 +00:00
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];
2014-05-04 10:32:26 +00:00
}
}
2014-05-04 10:32:26 +00:00
});
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);
2014-05-04 10:32:26 +00:00
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
}
2014-05-04 10:32:26 +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) {
2014-05-04 10:32:26 +00:00
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")) {
2012-09-17 13:40:36 +00:00
data.push({
label: account.name,
data: $.map(me.columns, function(col, idx) {
2014-05-04 10:32:26 +00:00
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") {
2014-05-04 10:32:26 +00:00
return [[dateutil.str_to_obj(col.date).getTime(), bal],
[dateutil.str_to_obj(col.date).getTime(), bal]];
} else {
2014-05-04 10:32:26 +00:00
return [[dateutil.str_to_obj(col.date).getTime(), bal]];
}
2012-09-18 13:22:05 +00:00
}
}),
points: {show: true},
lines: {show: true, fill: true},
2012-09-17 13:40:36 +00:00
});
2014-05-04 10:32:26 +00:00
2012-09-18 13:22:05 +00:00
if(pl_or_bs == "Balance Sheet") {
// prepend opening for balance sheet accounts
2014-05-04 10:32:26 +00:00
data[data.length-1].data = [[dateutil.str_to_obj(me.from_date).getTime(),
2012-09-18 13:22:05 +00:00
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
}
2014-05-04 10:32:26 +00:00
})