2015-03-03 09:25:30 +00:00
|
|
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
2012-09-21 14:16:24 +00:00
|
|
|
|
2015-01-29 12:39:11 +00:00
|
|
|
frappe.pages['sales-analytics'].on_page_load = function(wrapper) {
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.ui.make_app_page({
|
2012-09-21 09:49:40 +00:00
|
|
|
parent: wrapper,
|
2014-04-14 10:55:30 +00:00
|
|
|
title: __('Sales Analytics'),
|
2012-09-21 09:49:40 +00:00
|
|
|
single_column: true
|
|
|
|
});
|
|
|
|
new erpnext.SalesAnalytics(wrapper);
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2013-11-13 11:25:38 +00:00
|
|
|
|
2015-03-09 10:17:15 +00:00
|
|
|
frappe.breadcrumbs.add("Selling")
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2015-02-17 14:23:23 +00:00
|
|
|
};
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
erpnext.SalesAnalytics = frappe.views.TreeGridReport.extend({
|
2012-09-21 09:49:40 +00:00
|
|
|
init: function(wrapper) {
|
|
|
|
this._super({
|
2014-04-14 10:55:30 +00:00
|
|
|
title: __("Sales Analytics"),
|
2012-09-21 09:49:40 +00:00
|
|
|
parent: $(wrapper).find('.layout-main'),
|
2014-12-23 12:26:47 +00:00
|
|
|
page: wrapper.page,
|
2014-05-28 09:42:36 +00:00
|
|
|
doctypes: ["Item", "Item Group", "Customer", "Customer Group", "Company", "Territory",
|
|
|
|
"Fiscal Year", "Sales Invoice", "Sales Invoice Item",
|
|
|
|
"Sales Order", "Sales Order Item[Sales Analytics]",
|
2013-01-24 12:54:33 +00:00
|
|
|
"Delivery Note", "Delivery Note Item[Sales Analytics]"],
|
2012-09-21 09:49:40 +00:00
|
|
|
tree_grid: { show: true }
|
|
|
|
});
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2012-09-21 09:49:40 +00:00
|
|
|
this.tree_grids = {
|
|
|
|
"Customer Group": {
|
2014-04-14 10:55:30 +00:00
|
|
|
label: __("Customer Group / Customer"),
|
2014-05-28 09:42:36 +00:00
|
|
|
show: true,
|
2012-09-21 09:49:40 +00:00
|
|
|
item_key: "customer",
|
2014-05-28 09:42:36 +00:00
|
|
|
parent_field: "parent_customer_group",
|
2017-05-06 07:15:37 +00:00
|
|
|
formatter: function(item) {
|
|
|
|
return item.customer_name? item.customer_name + " (" + item.name + ")" : item.name;
|
|
|
|
}
|
2012-09-21 09:49:40 +00:00
|
|
|
},
|
|
|
|
"Customer": {
|
2014-04-14 10:55:30 +00:00
|
|
|
label: __("Customer"),
|
2014-05-28 09:42:36 +00:00
|
|
|
show: false,
|
2012-09-21 09:49:40 +00:00
|
|
|
item_key: "customer",
|
|
|
|
formatter: function(item) {
|
2017-05-06 07:15:37 +00:00
|
|
|
return item.customer_name? item.customer_name + " (" + item.name + ")" : item.name;
|
2012-09-21 09:49:40 +00:00
|
|
|
}
|
2014-05-28 09:42:36 +00:00
|
|
|
},
|
2012-09-21 09:49:40 +00:00
|
|
|
"Item Group": {
|
2014-04-14 10:55:30 +00:00
|
|
|
label: __("Item"),
|
2014-05-28 09:42:36 +00:00
|
|
|
show: true,
|
|
|
|
parent_field: "parent_item_group",
|
2012-09-21 09:49:40 +00:00
|
|
|
item_key: "item_code",
|
|
|
|
formatter: function(item) {
|
|
|
|
return item.name;
|
|
|
|
}
|
2014-05-28 09:42:36 +00:00
|
|
|
},
|
2012-09-21 09:49:40 +00:00
|
|
|
"Item": {
|
2014-04-14 10:55:30 +00:00
|
|
|
label: __("Item"),
|
2014-05-28 09:42:36 +00:00
|
|
|
show: false,
|
2012-09-21 09:49:40 +00:00
|
|
|
item_key: "item_code",
|
|
|
|
formatter: function(item) {
|
|
|
|
return item.name;
|
|
|
|
}
|
2014-05-28 09:42:36 +00:00
|
|
|
},
|
2012-09-21 09:49:40 +00:00
|
|
|
"Territory": {
|
2014-04-14 10:55:30 +00:00
|
|
|
label: __("Territory / Customer"),
|
2014-05-28 09:42:36 +00:00
|
|
|
show: true,
|
2012-09-21 09:49:40 +00:00
|
|
|
item_key: "customer",
|
2014-05-28 09:42:36 +00:00
|
|
|
parent_field: "parent_territory",
|
2012-09-21 09:49:40 +00:00
|
|
|
formatter: function(item) {
|
2017-05-06 07:15:37 +00:00
|
|
|
return item.customer_name? item.customer_name + " (" + item.name + ")" : item.name;
|
2012-09-21 09:49:40 +00:00
|
|
|
}
|
2014-05-28 09:42:36 +00:00
|
|
|
}
|
2012-09-21 09:49:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setup_columns: function() {
|
|
|
|
this.tree_grid = this.tree_grids[this.tree_type];
|
|
|
|
|
|
|
|
var std_columns = [
|
|
|
|
{id: "check", name: "Plot", field: "check", width: 30,
|
|
|
|
formatter: this.check_formatter},
|
|
|
|
{id: "name", name: this.tree_grid.label, field: "name", width: 300,
|
2012-09-21 14:16:24 +00:00
|
|
|
formatter: this.tree_formatter},
|
2012-09-21 09:49:40 +00:00
|
|
|
{id: "total", name: "Total", field: "total", plot: false,
|
|
|
|
formatter: this.currency_formatter}
|
|
|
|
];
|
|
|
|
|
2014-05-28 09:42:36 +00:00
|
|
|
this.make_date_range_columns();
|
2012-09-21 09:49:40 +00:00
|
|
|
this.columns = std_columns.concat(this.columns);
|
|
|
|
},
|
|
|
|
filters: [
|
2014-05-28 09:42:36 +00:00
|
|
|
{fieldtype:"Select", fieldname: "tree_type", label: __("Tree Type"), options:["Customer Group", "Customer",
|
2012-09-21 09:49:40 +00:00
|
|
|
"Item Group", "Item", "Territory"],
|
|
|
|
filter: function(val, item, opts, me) {
|
|
|
|
return me.apply_zero_filter(val, item, opts, me);
|
|
|
|
}},
|
2014-05-28 09:42:36 +00:00
|
|
|
{fieldtype:"Select", fieldname: "based_on", label: __("Based On"), options:["Sales Invoice",
|
2013-01-24 12:54:33 +00:00
|
|
|
"Sales Order", "Delivery Note"]},
|
2014-06-03 07:32:18 +00:00
|
|
|
{fieldtype:"Select", fieldname: "value_or_qty", label: __("Value or Qty"),
|
|
|
|
options:[{label: __("Value"), value: "Value"}, {label: __("Quantity"), value: "Quantity"}]},
|
2014-04-14 10:55:30 +00:00
|
|
|
{fieldtype:"Date", fieldname: "from_date", label: __("From Date")},
|
|
|
|
{fieldtype:"Label", fieldname: "to", label: __("To")},
|
|
|
|
{fieldtype:"Date", fieldname: "to_date", label: __("To Date")},
|
2016-05-26 09:55:52 +00:00
|
|
|
{fieldtype:"Select", fieldname: "company", label: __("Company"), link:"Company",
|
|
|
|
default_value: __("Select Company...")},
|
2014-06-03 07:32:18 +00:00
|
|
|
{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-21 09:49:40 +00:00
|
|
|
],
|
|
|
|
setup_filters: function() {
|
|
|
|
var me = this;
|
|
|
|
this._super();
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2013-07-23 05:31:27 +00:00
|
|
|
this.trigger_refresh_on_change(["value_or_qty", "tree_type", "based_on", "company"]);
|
2012-09-21 09:49:40 +00:00
|
|
|
|
2014-05-28 09:42:36 +00:00
|
|
|
this.show_zero_check()
|
2016-05-26 09:55:52 +00:00
|
|
|
this.setup_chart_check();
|
2012-09-21 09:49:40 +00:00
|
|
|
},
|
|
|
|
init_filter_values: function() {
|
|
|
|
this._super();
|
2012-11-28 11:42:18 +00:00
|
|
|
this.filter_inputs.range.val('Monthly');
|
2012-09-21 09:49:40 +00:00
|
|
|
},
|
|
|
|
prepare_data: function() {
|
|
|
|
var me = this;
|
|
|
|
if (!this.tl) {
|
|
|
|
// add 'Not Set' Customer & Item
|
|
|
|
// (Customer / Item are not mandatory!!)
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.report_dump.data["Customer"].push({
|
2014-05-28 09:42:36 +00:00
|
|
|
name: "Not Set",
|
2012-09-21 09:49:40 +00:00
|
|
|
parent_customer_group: "All Customer Groups",
|
|
|
|
parent_territory: "All Territories",
|
|
|
|
id: "Not Set",
|
|
|
|
});
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.report_dump.data["Item"].push({
|
2014-05-28 09:42:36 +00:00
|
|
|
name: "Not Set",
|
2012-09-21 09:49:40 +00:00
|
|
|
parent_item_group: "All Item Groups",
|
|
|
|
id: "Not Set",
|
|
|
|
});
|
|
|
|
}
|
2013-01-24 12:54:33 +00:00
|
|
|
|
|
|
|
if (!this.tl || !this.tl[this.based_on]) {
|
|
|
|
this.make_transaction_list(this.based_on, this.based_on + " Item");
|
|
|
|
}
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2012-09-21 09:49:40 +00:00
|
|
|
if(!this.data || me.item_type != me.tree_type) {
|
|
|
|
if(me.tree_type=='Customer') {
|
2014-02-14 10:17:51 +00:00
|
|
|
var items = frappe.report_dump.data["Customer"];
|
2012-09-21 09:49:40 +00:00
|
|
|
} if(me.tree_type=='Customer Group') {
|
|
|
|
var items = this.prepare_tree("Customer", "Customer Group");
|
|
|
|
} else if(me.tree_type=="Item Group") {
|
|
|
|
var items = this.prepare_tree("Item", "Item Group");
|
|
|
|
} else if(me.tree_type=="Item") {
|
2014-02-14 10:17:51 +00:00
|
|
|
var items = frappe.report_dump.data["Item"];
|
2012-09-21 09:49:40 +00:00
|
|
|
} else if(me.tree_type=="Territory") {
|
|
|
|
var items = this.prepare_tree("Customer", "Territory");
|
|
|
|
}
|
|
|
|
|
|
|
|
me.item_type = me.tree_type
|
|
|
|
me.parent_map = {};
|
|
|
|
me.item_by_name = {};
|
|
|
|
me.data = [];
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2012-09-21 09:49:40 +00:00
|
|
|
$.each(items, function(i, v) {
|
|
|
|
var d = copy_dict(v);
|
|
|
|
|
|
|
|
me.data.push(d);
|
|
|
|
me.item_by_name[d.name] = d;
|
|
|
|
if(d[me.tree_grid.parent_field]) {
|
|
|
|
me.parent_map[d.name] = d[me.tree_grid.parent_field];
|
|
|
|
}
|
|
|
|
me.reset_item_values(d);
|
2014-05-28 09:42:36 +00:00
|
|
|
});
|
|
|
|
|
2012-09-21 09:49:40 +00:00
|
|
|
this.set_indent();
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2012-09-21 09:49:40 +00:00
|
|
|
} else {
|
|
|
|
// otherwise, only reset values
|
|
|
|
$.each(this.data, function(i, d) {
|
|
|
|
me.reset_item_values(d);
|
|
|
|
});
|
|
|
|
}
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2012-09-21 09:49:40 +00:00
|
|
|
this.prepare_balances();
|
|
|
|
if(me.tree_grid.show) {
|
2014-05-28 09:42:36 +00:00
|
|
|
this.set_totals(false);
|
2012-09-21 09:49:40 +00:00
|
|
|
this.update_groups();
|
|
|
|
} else {
|
|
|
|
this.set_totals(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
prepare_balances: function() {
|
|
|
|
var me = this;
|
2017-05-30 07:24:42 +00:00
|
|
|
var from_date = frappe.datetime.str_to_obj(this.from_date);
|
|
|
|
var to_date = frappe.datetime.str_to_obj(this.to_date);
|
2012-09-21 09:49:40 +00:00
|
|
|
var is_val = this.value_or_qty == 'Value';
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2013-01-24 12:54:33 +00:00
|
|
|
$.each(this.tl[this.based_on], function(i, tl) {
|
2014-05-28 09:42:36 +00:00
|
|
|
if (me.is_default('company') ? true : tl.company === me.company) {
|
2017-05-30 07:24:42 +00:00
|
|
|
var posting_date = frappe.datetime.str_to_obj(tl.posting_date);
|
2012-09-21 09:49:40 +00:00
|
|
|
if (posting_date >= from_date && posting_date <= to_date) {
|
2014-05-28 09:42:36 +00:00
|
|
|
var item = me.item_by_name[tl[me.tree_grid.item_key]] ||
|
2013-01-24 12:54:33 +00:00
|
|
|
me.item_by_name['Not Set'];
|
2017-02-03 07:01:54 +00:00
|
|
|
if(item){
|
|
|
|
item[me.column_map[tl.posting_date].field] += (is_val ? tl.base_net_amount : tl.qty);
|
|
|
|
}
|
2012-09-21 09:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
update_groups: function() {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
$.each(this.data, function(i, item) {
|
|
|
|
var parent = me.parent_map[item.name];
|
|
|
|
while(parent) {
|
2017-05-30 07:24:42 +00:00
|
|
|
var parent_group = me.item_by_name[parent];
|
2014-05-28 09:42:36 +00:00
|
|
|
|
2012-09-21 09:49:40 +00:00
|
|
|
$.each(me.columns, function(c, col) {
|
|
|
|
if (col.formatter == me.currency_formatter) {
|
2014-05-28 09:42:36 +00:00
|
|
|
parent_group[col.field] =
|
2012-09-21 09:49:40 +00:00
|
|
|
flt(parent_group[col.field])
|
|
|
|
+ flt(item[col.field]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
parent = me.parent_map[parent];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
set_totals: function(sort) {
|
|
|
|
var me = this;
|
|
|
|
var checked = false;
|
2014-05-28 09:42:36 +00:00
|
|
|
$.each(this.data, function(i, d) {
|
2012-09-21 09:49:40 +00:00
|
|
|
d.total = 0.0;
|
|
|
|
$.each(me.columns, function(i, col) {
|
2014-05-28 09:42:36 +00:00
|
|
|
if(col.formatter==me.currency_formatter && !col.hidden && col.field!="total")
|
2012-09-21 09:49:40 +00:00
|
|
|
d.total += d[col.field];
|
|
|
|
if(d.checked) checked = true;
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
if(sort)this.data = this.data.sort(function(a, b) { return a.total < b.total; });
|
|
|
|
|
|
|
|
if(!this.checked) {
|
|
|
|
this.data[0].checked = true;
|
|
|
|
}
|
|
|
|
}
|
2013-11-12 17:06:49 +00:00
|
|
|
});
|