Merge pull request #5399 from nabinhait/report-graph

Charts on reports / activity page, deprecated financial analytics report
This commit is contained in:
Nabin Hait 2016-05-26 17:42:36 +05:30
commit 37b5e0c99f
16 changed files with 107 additions and 403 deletions

View File

@ -90,13 +90,26 @@ frappe.ui.form.on('Asset', {
last_depreciation_date = frm.doc.disposal_date;
}
frm.dashboard.reset();
frm.dashboard.add_graph({
frm.dashboard.setup_chart({
data: {
x: 'x',
columns: [x_intervals, asset_values],
regions: {
'Asset Value': [{'start': last_depreciation_date, 'style':'dashed'}]
}
},
legend: {
show: false
},
axis: {
x: {
type: 'category'
},
y: {
min: 0,
padding: {bottom: 10}
}
}
});
},

View File

@ -1 +0,0 @@
Trends (multi-year) for account balances including.

View File

@ -1 +0,0 @@
from __future__ import unicode_literals

View File

@ -1,332 +0,0 @@
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
frappe.pages['financial-analytics'].on_page_load = function(wrapper) {
frappe.ui.make_app_page({
parent: wrapper,
title: __('Financial Analytics'),
single_column: true
});
erpnext.financial_analytics = new erpnext.FinancialAnalytics(wrapper, 'Financial Analytics');
frappe.breadcrumbs.add("Accounts");
};
{% include "erpnext/public/js/account_tree_grid.js" %}
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"}],
filter: function(val, item, opts, me) {
if(item._show) return true;
// pl or bs
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"), fieldname: "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", 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"},
{label: __("Yearly"), value: "Yearly"}]}
],
setup_columns: function() {
var std_columns = [
{id: "_check", name: __("Plot"), field: "_check", width: 30,
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",
hidden: true, formatter: this.currency_formatter, balance_type: "Dr"},
{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.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(),
name: dateutil.str_to_user(date),
field: date + "_" + balance_type.toLowerCase(),
date: date,
balance_type: balance_type,
formatter: me.currency_formatter,
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();
} else if(range=='Weekly') {
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;
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())
});
} 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;
});
}
// 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;}));
this.setup_plot_check();
},
init_filter_values: function() {
this._super();
this.filter_inputs.range.val('Monthly');
},
prepare_balances: function() {
var me = this;
// setup cost center map
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)
? 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) {
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;
}
} else {
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);
} else {
me.add_balance(col.date, account, gl);
}
} 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) {
if((ac.rgt - ac.lft)==1 && ac.report_type=='Balance Sheet') {
var opening = flt(ac["opening_dr"]) - flt(ac["opening_cr"]);
//if(opening) throw opening;
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter) {
if(col.balance_type=="Dr" && !in_list(["opening_dr", "opening_cr"], col.field)) {
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 = {
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;
$.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) {
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") {
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);
}
});
}
});
this.data.push(net_profit);
}
},
add_balance: function(field, account, gl) {
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
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") {
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" ?
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 &&
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.str_to_obj(col.date).getTime(), bal]];
} else {
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(),
account.opening]].concat(data[data.length-1].data);
}
}
});
return data;
}
})

View File

@ -1,23 +0,0 @@
{
"creation": "2013-01-27 16:30:52.000000",
"docstatus": 0,
"doctype": "Page",
"icon": "icon-bar-chart",
"idx": 1,
"modified": "2013-07-11 14:42:16.000000",
"modified_by": "Administrator",
"module": "Accounts",
"name": "financial-analytics",
"owner": "Administrator",
"page_name": "financial-analytics",
"roles": [
{
"role": "Analytics"
},
{
"role": "Accounts Manager"
}
],
"standard": "Yes",
"title": "Financial Analytics"
}

View File

@ -16,7 +16,10 @@ class ReceivablePayableReport(object):
def run(self, args):
party_naming_by = frappe.db.get_value(args.get("naming_by")[0], None, args.get("naming_by")[1])
return self.get_columns(party_naming_by, args), self.get_data(party_naming_by, args)
columns = self.get_columns(party_naming_by, args)
data = self.get_data(party_naming_by, args)
chart = self.get_chart_data(columns, data)
return columns, data, None, chart
def get_columns(self, party_naming_by, args):
columns = [_("Posting Date") + ":Date:80", _(args.get("party_type")) + ":Link/" + args.get("party_type") + ":200"]
@ -40,6 +43,8 @@ class ReceivablePayableReport(object):
columns += [_("Age (Days)") + ":Int:80"]
self.ageing_col_idx_start = len(columns)
if not "range1" in self.filters:
self.filters["range1"] = "30"
if not "range2" in self.filters:
@ -251,6 +256,23 @@ class ReceivablePayableReport(object):
.get(against_voucher_type, {})\
.get(against_voucher, [])
def get_chart_data(self, columns, data):
ageing_columns = columns[self.ageing_col_idx_start : self.ageing_col_idx_start+4]
range_totals = [[d.get("label")] for d in ageing_columns]
for d in data:
for i in xrange(4):
range_totals[i].append(d[self.ageing_col_idx_start + i])
return {
"data": {
'columns': range_totals
},
"chart_type": 'pie'
}
def execute(filters=None):
args = {
"party_type": "Customer",

View File

@ -28,7 +28,9 @@ def execute(filters=None):
columns = get_columns(filters.periodicity, period_list, company=filters.company)
return columns, data, message
chart = get_chart_data(columns, asset, liability, equity)
return columns, data, message, chart
def get_provisional_profit_loss(asset, liability, equity, period_list, company):
if asset and (liability or equity):
@ -70,3 +72,20 @@ def check_opening_balance(asset, liability, equity):
if opening_balance:
return _("Previous Financial Year is not closed")
def get_chart_data(columns, asset, liability, equity):
x_intervals = ['x'] + [d.get("label") for d in columns[2:]]
asset_data, liability_data, equity_data = ["Assets"], ["Liabilities"], ["Equity"]
for p in columns[2:]:
asset_data.append(asset[-2].get(p.get("fieldname")))
liability_data.append(liability[-2].get(p.get("fieldname")))
equity_data.append(equity[-2].get(p.get("fieldname")))
return {
"data": {
'x': 'x',
'columns': [x_intervals, asset_data, liability_data, equity_data]
}
}

View File

@ -112,7 +112,7 @@ def get_data(company, root_type, balance_must_be, period_list,
out = filter_out_zero_value_rows(out, parent_children_map)
if out:
add_total_row(out, balance_must_be, period_list, company_currency)
add_total_row(out, root_type, balance_must_be, period_list, company_currency)
return out
@ -193,9 +193,9 @@ def filter_out_zero_value_rows(data, parent_children_map, show_zero_values=False
return data_with_value
def add_total_row(out, balance_must_be, period_list, company_currency):
def add_total_row(out, root_type, balance_must_be, period_list, company_currency):
total_row = {
"account_name": "'" + _("Total ({0})").format(balance_must_be) + "'",
"account_name": "'" + _("Total {0} ({1})").format(root_type, balance_must_be) + "'",
"account": None,
"currency": company_currency
}

View File

@ -25,7 +25,9 @@ def execute(filters=None):
columns = get_columns(filters.periodicity, period_list, filters.accumulated_values, filters.company)
return columns, data
chart = get_chart_data(filters, columns, income, expense, net_profit_loss)
return columns, data, None, chart
def get_net_profit_loss(income, expense, period_list, company):
if income and expense:
@ -50,3 +52,25 @@ def get_net_profit_loss(income, expense, period_list, company):
if has_value:
return net_profit_loss
def get_chart_data(filters, columns, income, expense, net_profit_loss):
x_intervals = ['x'] + [d.get("label") for d in columns[2:-1]]
income_data, expense_data, net_profit = ["Income"], ["Expense"], ["Net Profit/Loss"]
for p in columns[2:]:
income_data.append(income[-2].get(p.get("fieldname")))
expense_data.append(expense[-2].get(p.get("fieldname")))
net_profit.append(net_profit_loss.get(p.get("fieldname")))
chart = {
"data": {
'x': 'x',
'columns': [x_intervals, income_data, expense_data, net_profit]
}
}
if not filters.accumulated_values:
chart["chart_type"] = "bar"
return chart

View File

@ -110,7 +110,7 @@ erpnext.PurchaseAnalytics = frappe.views.TreeGridReport.extend({
this.trigger_refresh_on_change(["value_or_qty", "tree_type", "based_on", "company"]);
this.show_zero_check()
this.setup_plot_check();
this.setup_chart_check();
},
init_filter_values: function() {
this._super();
@ -248,9 +248,5 @@ erpnext.PurchaseAnalytics = frappe.views.TreeGridReport.extend({
if(!this.checked) {
this.data[0].checked = true;
}
},
get_plot_points: function(item, col, idx) {
return [[dateutil.str_to_obj(col.id).getTime(), item[col.field]],
[dateutil.user_to_obj(col.name).getTime(), item[col.field]]];
}
});

View File

@ -320,12 +320,6 @@ def get_data():
{
"label": _("Analytics"),
"items": [
{
"type": "page",
"name": "financial-analytics",
"label": _("Financial Analytics"),
"icon": "icon-bar-chart",
},
{
"type": "report",
"name": "Gross Profit",

View File

@ -266,3 +266,4 @@ erpnext.patches.v7_0.fix_duplicate_icons
erpnext.patches.v7_0.remove_features_setup
erpnext.patches.v7_0.update_home_page
erpnext.patches.v7_0.create_budget_record
execute:frappe.delete_doc_if_exists("Page", "financial-analytics")

View File

@ -28,7 +28,7 @@ erpnext.financial_statements = {
{ "value": "Half-Yearly", "label": __("Half-Yearly") },
{ "value": "Yearly", "label": __("Yearly") }
],
"default": "Yearly",
"default": "Monthly",
"reqd": 1
}
],
@ -83,5 +83,5 @@ erpnext.financial_statements = {
var filters = report.get_values();
frappe.set_route('query-report', 'Cash Flow', {company: filters.company});
}, 'Financial Statements');
},
}
};

View File

@ -78,7 +78,7 @@ frappe.require("assets/erpnext/js/stock_grid_report.js", function() {
this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
this.show_zero_check();
this.setup_plot_check();
this.setup_chart_check();
},
init_filter_values: function() {
this._super();
@ -198,9 +198,6 @@ frappe.require("assets/erpnext/js/stock_grid_report.js", function() {
}
});
},
get_plot_points: function(item, col, idx) {
return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
},
show_stock_ledger: function(item_code) {
frappe.route_options = {
item_code: item_code,

View File

@ -97,11 +97,11 @@ erpnext.SalesAnalytics = frappe.views.TreeGridReport.extend({
"Sales Order", "Delivery Note"]},
{fieldtype:"Select", fieldname: "value_or_qty", label: __("Value or Qty"),
options:[{label: __("Value"), value: "Value"}, {label: __("Quantity"), value: "Quantity"}]},
{fieldtype:"Select", fieldname: "company", label: __("Company"), link:"Company",
default_value: __("Select Company...")},
{fieldtype:"Date", fieldname: "from_date", label: __("From Date")},
{fieldtype:"Label", fieldname: "to", label: __("To")},
{fieldtype:"Date", fieldname: "to_date", label: __("To Date")},
{fieldtype:"Select", fieldname: "company", label: __("Company"), link:"Company",
default_value: __("Select Company...")},
{fieldtype:"Select", label: __("Range"), fieldname: "range",
options:[{label: __("Daily"), value: "Daily"}, {label: __("Weekly"), value: "Weekly"},
{label: __("Monthly"), value: "Monthly"}, {label: __("Quarterly"), value: "Quarterly"},
@ -114,7 +114,7 @@ erpnext.SalesAnalytics = frappe.views.TreeGridReport.extend({
this.trigger_refresh_on_change(["value_or_qty", "tree_type", "based_on", "company"]);
this.show_zero_check()
this.setup_plot_check();
this.setup_chart_check();
},
init_filter_values: function() {
this._super();
@ -243,9 +243,5 @@ erpnext.SalesAnalytics = frappe.views.TreeGridReport.extend({
if(!this.checked) {
this.data[0].checked = true;
}
},
get_plot_points: function(item, col, idx) {
return [[dateutil.str_to_obj(col.id).getTime(), item[col.field]],
[dateutil.user_to_obj(col.name).getTime(), item[col.field]]];
}
});

View File

@ -32,9 +32,14 @@ erpnext.SupportAnalytics = frappe.views.GridReportWithPlot.extend({
{fieldtype:"Date", label: __("From Date")},
{fieldtype:"Date", label: __("To Date")},
{fieldtype:"Select", label: __("Range"),
options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]}
options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"], default_value: "Monthly"}
],
init_filter_values: function() {
this._super();
this.filter_inputs.range.val('Monthly');
},
setup_columns: function() {
var std_columns = [
{id: "_check", name: __("Plot"), field: "_check", width: 30,
@ -100,11 +105,5 @@ erpnext.SupportAnalytics = frappe.views.GridReportWithPlot.extend({
})
this.data = [total_tickets, days_to_close, hours_to_close, hours_to_respond];
},
get_plot_points: function(item, col, idx) {
return [[dateutil.str_to_obj(col.id).getTime(), item[col.field]],
[dateutil.user_to_obj(col.name).getTime(), item[col.field]]];
}
});