brotherton-erpnext/erpnext/selling/page/sales_funnel/sales_funnel.js

313 lines
8.3 KiB
JavaScript
Raw Normal View History

// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2013-10-04 15:49:30 +00:00
// License: GNU General Public License v3. See license.txt
2015-01-29 12:39:11 +00:00
frappe.pages['sales-funnel'].on_page_load = function(wrapper) {
2014-02-14 10:17:51 +00:00
frappe.ui.make_app_page({
2013-10-04 15:49:30 +00:00
parent: wrapper,
2014-09-10 07:36:53 +00:00
title: __('Sales Funnel'),
2013-10-04 15:49:30 +00:00
single_column: true
});
2014-12-19 10:50:32 +00:00
2013-10-31 13:36:11 +00:00
wrapper.sales_funnel = new erpnext.SalesFunnel(wrapper);
2014-12-19 10:50:32 +00:00
2015-03-09 10:17:15 +00:00
frappe.breadcrumbs.add("Selling");
2013-10-04 15:49:30 +00:00
}
erpnext.SalesFunnel = class SalesFunnel {
constructor(wrapper) {
2013-10-04 15:49:30 +00:00
var me = this;
// 0 setTimeout hack - this gives time for canvas to get width and height
2014-12-19 10:50:32 +00:00
setTimeout(function() {
2013-10-04 15:49:30 +00:00
me.setup(wrapper);
me.get_data();
}, 0);
}
2014-12-19 10:50:32 +00:00
setup(wrapper) {
2013-10-04 15:49:30 +00:00
var me = this;
2014-12-19 10:50:32 +00:00
this.company_field = wrapper.page.add_field({"fieldtype": "Link", "fieldname": "company", "options": "Company",
"label": __("Company"), "reqd": 1, "default": frappe.defaults.get_user_default('company'),
change: function() {
me.company = this.value || frappe.defaults.get_user_default('company');
me.get_data();
}
}),
2014-12-19 10:50:32 +00:00
this.elements = {
2013-10-04 15:49:30 +00:00
layout: $(wrapper).find(".layout-main"),
2014-12-23 12:26:47 +00:00
from_date: wrapper.page.add_date(__("From Date")),
to_date: wrapper.page.add_date(__("To Date")),
chart: wrapper.page.add_select(__("Chart"), [{value: 'sales_funnel', label:__("Sales Funnel")},
{value: 'sales_pipeline', label:__("Sales Pipeline")},
{value: 'opp_by_lead_source', label:__("Opportunities by lead source")}]),
2014-12-23 12:26:47 +00:00
refresh_btn: wrapper.page.set_primary_action(__("Refresh"),
function() { me.get_data(); }, "fa fa-refresh"),
2013-10-04 15:49:30 +00:00
};
2014-12-19 10:50:32 +00:00
2014-09-10 07:36:53 +00:00
this.elements.no_data = $('<div class="alert alert-warning">' + __("No Data") + '</div>')
2013-10-04 15:49:30 +00:00
.toggle(false)
.appendTo(this.elements.layout);
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
this.elements.funnel_wrapper = $('<div class="funnel-wrapper text-center"></div>')
.appendTo(this.elements.layout);
2014-12-19 10:50:32 +00:00
this.company = frappe.defaults.get_user_default('company');
2013-10-04 15:49:30 +00:00
this.options = {
2014-02-14 10:17:51 +00:00
from_date: frappe.datetime.add_months(frappe.datetime.get_today(), -1),
to_date: frappe.datetime.get_today(),
chart: 'sales_funnel'
2013-10-04 15:49:30 +00:00
};
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
// set defaults and bind on change
2014-12-19 10:50:32 +00:00
$.each(this.options, function(k, v) {
if (['from_date', 'to_date'].includes(k)) {
me.elements[k].val(frappe.datetime.str_to_user(v));
} else {
me.elements[k].val(v);
}
2013-10-04 15:49:30 +00:00
me.elements[k].on("change", function() {
if (['from_date', 'to_date'].includes(k)) {
me.options[k] = frappe.datetime.user_to_str($(this).val()) != 'Invalid date' ? frappe.datetime.user_to_str($(this).val()) : frappe.datetime.get_today();
} else {
me.options.chart = $(this).val();
}
2013-10-04 15:49:30 +00:00
me.get_data();
});
});
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
// bind refresh
this.elements.refresh_btn.on("click", function() {
me.get_data(this);
});
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
// bind resize
$(window).resize(function() {
me.render();
});
}
2014-12-19 10:50:32 +00:00
get_data(btn) {
2013-10-04 15:49:30 +00:00
var me = this;
if (me.options.chart == 'sales_funnel'){
frappe.call({
method: "erpnext.selling.page.sales_funnel.sales_funnel.get_funnel_data",
args: {
from_date: this.options.from_date,
to_date: this.options.to_date,
company: this.company
},
btn: btn,
callback: function(r) {
if(!r.exc) {
me.options.data = r.message;
if (me.options.data=='empty') {
const $parent = me.elements.funnel_wrapper;
$parent.html(__('No data for this period'));
} else {
me.render_funnel();
}
}
2013-10-04 15:49:30 +00:00
}
});
} else if (me.options.chart == 'opp_by_lead_source'){
frappe.call({
method: "erpnext.selling.page.sales_funnel.sales_funnel.get_opp_by_lead_source",
args: {
from_date: this.options.from_date,
to_date: this.options.to_date,
company: this.company
},
btn: btn,
callback: function(r) {
if(!r.exc) {
me.options.data = r.message;
if (me.options.data=='empty') {
const $parent = me.elements.funnel_wrapper;
$parent.html(__('No data for this period'));
} else {
me.render_opp_by_lead_source();
}
}
}
});
} else if (me.options.chart == 'sales_pipeline'){
frappe.call({
method: "erpnext.selling.page.sales_funnel.sales_funnel.get_pipeline_data",
args: {
from_date: this.options.from_date,
to_date: this.options.to_date,
company: this.company
},
btn: btn,
callback: function(r) {
if(!r.exc) {
me.options.data = r.message;
if (me.options.data=='empty') {
const $parent = me.elements.funnel_wrapper;
$parent.html(__('No data for this period'));
} else {
me.render_pipeline();
}
}
}
});
}
}
2014-12-19 10:50:32 +00:00
render() {
let me = this;
if (me.options.chart == 'sales_funnel'){
me.render_funnel();
} else if (me.options.chart == 'opp_by_lead_source'){
me.render_opp_by_lead_source();
} else if (me.options.chart == 'sales_pipeline'){
me.render_pipeline();
}
}
render_funnel() {
2013-10-04 15:49:30 +00:00
var me = this;
this.prepare_funnel();
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
var context = this.elements.context,
x_start = 0.0,
x_end = this.options.width,
x_mid = (x_end - x_start) / 2.0,
y = 0,
y_old = 0.0;
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
if(this.options.total_value === 0) {
this.elements.no_data.toggle(true);
return;
}
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
this.options.data.forEach(function(d) {
context.fillStyle = d.color;
context.strokeStyle = d.color;
me.draw_triangle(x_start, x_mid, x_end, y, me.options.height);
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
y_old = y;
// new y
y = y + d.height;
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
// new x
var half_side = (me.options.height - y) / Math.sqrt(3);
x_start = x_mid - half_side;
x_end = x_mid + half_side;
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
var y_mid = y_old + (y - y_old) / 2.0;
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
me.draw_legend(x_mid, y_mid, me.options.width, me.options.height, d.value + " - " + d.title);
});
}
2014-12-19 10:50:32 +00:00
prepare_funnel() {
var me = this;
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
this.elements.no_data.toggle(false);
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
// calculate width and height options
this.options.width = $(this.elements.funnel_wrapper).width() * 2.0 / 3.0;
this.options.height = (Math.sqrt(3) * this.options.width) / 2.0;
2014-12-19 10:50:32 +00:00
// calculate total weightage
// as height decreases, area decreases by the square of the reduction
// hence, compensating by squaring the index value
this.options.total_weightage = this.options.data.reduce(
function(prev, curr, i) { return prev + Math.pow(i+1, 2) * curr.value; }, 0.0);
2014-12-19 10:50:32 +00:00
// calculate height for each data
$.each(this.options.data, function(i, d) {
d.height = me.options.height * d.value * Math.pow(i+1, 2) / me.options.total_weightage;
});
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
this.elements.canvas = $('<canvas></canvas>')
.appendTo(this.elements.funnel_wrapper.empty())
.attr("width", $(this.elements.funnel_wrapper).width())
.attr("height", this.options.height);
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
this.elements.context = this.elements.canvas.get(0).getContext("2d");
}
2014-12-19 10:50:32 +00:00
draw_triangle(x_start, x_mid, x_end, y, height) {
2013-10-04 15:49:30 +00:00
var context = this.elements.context;
context.beginPath();
context.moveTo(x_start, y);
context.lineTo(x_end, y);
context.lineTo(x_mid, height);
context.lineTo(x_start, y);
context.closePath();
context.fill();
}
2014-12-19 10:50:32 +00:00
draw_legend(x_mid, y_mid, width, height, title) {
2013-10-04 15:49:30 +00:00
var context = this.elements.context;
2014-12-19 10:50:32 +00:00
if(y_mid == 0) {
y_mid = 7;
}
2013-10-04 15:49:30 +00:00
// draw line
context.beginPath();
context.moveTo(x_mid, y_mid);
context.lineTo(width, y_mid);
context.closePath();
context.stroke();
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
// draw circle
context.beginPath();
context.arc(width, y_mid, 5, 0, Math.PI * 2, false);
context.closePath();
context.fill();
2014-12-19 10:50:32 +00:00
2013-10-04 15:49:30 +00:00
// draw text
context.fillStyle = "black";
context.textBaseline = "middle";
context.font = "1.1em sans-serif";
2016-01-04 10:27:01 +00:00
context.fillText(__(title), width + 20, y_mid);
2013-10-04 15:49:30 +00:00
}
render_opp_by_lead_source() {
let me = this;
let currency = frappe.defaults.get_default("currency");
let chart_data = me.options.data ? me.options.data : null;
const parent = me.elements.funnel_wrapper[0];
this.chart = new Chart(parent, {
title: __("Sales Opportunities by Source"),
height: 400,
data: chart_data,
type: 'bar',
barOptions: {
stacked: 1
},
tooltipOptions: {
formatTooltipY: d => format_currency(d, currency),
}
});
}
render_pipeline() {
let me = this;
let currency = frappe.defaults.get_default("currency");
let chart_data = me.options.data ? me.options.data : null;
const parent = me.elements.funnel_wrapper[0];
this.chart = new Chart(parent, {
title: __("Sales Pipeline by Stage"),
height: 400,
data: chart_data,
type: 'bar',
tooltipOptions: {
formatTooltipY: d => format_currency(d, currency),
},
colors: ['light-green', 'green']
});
}
};