added sales analytics

This commit is contained in:
Rushabh Mehta
2012-09-21 15:19:40 +05:30
parent 6ca8054d55
commit f200c5296d
9 changed files with 500 additions and 98 deletions

View File

@@ -164,13 +164,5 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
});
return data;
},
get_plot_options: function() {
return {
grid: { hoverable: true, clickable: true },
xaxis: { mode: "time",
min: dateutil.str_to_obj(this.from_date).getTime(),
max: dateutil.str_to_obj(this.to_date).getTime() }
}
},
}
})

View File

@@ -584,4 +584,8 @@ patch_list = [
'patch_module': 'patches.september_2012',
'patch_file': 'add_stock_ledger_entry_index',
},
{
'patch_module': 'patches.september_2012',
'patch_file': 'plot_patch',
},
]

View File

@@ -0,0 +1,32 @@
import webnotes
def execute():
set_master_name_in_accounts()
set_customer_in_sales_invoices()
reset_lft_rgt()
def set_master_name_in_accounts():
accounts = webnotes.conn.sql("""select name, account_name, master_type from tabAccount
where ifnull(master_name, '')=''""", as_dict=1)
for acc in accounts:
if acc["master_type"] in ["Customer", "Supplier"]:
master = webnotes.conn.sql("""select name from `tab%s`
where name=%s """ % (acc["master_type"], "%s"), acc["account_name"])
if master:
webnotes.conn.sql("""update `tabAccount`
set master_name=%s where name=%s""", (master[0][0], acc["name"]))
def set_customer_in_sales_invoices():
webnotes.conn.sql("""update `tabSales Invoice` si
set si.customer=(select a.master_name from `tabAccount` a where a.name=si.debit_to)
where ifnull(si.customer, '')=''""")
def reset_lft_rgt():
from webnotes.utils.nestedset import rebuild_tree
rebuild_tree("Item Group", "parent_item_group")
rebuild_tree("Customer Group", "parent_customer_group")
rebuild_tree("Territory", "parent_territory")
rebuild_tree("Account", "parent_account")
rebuild_tree("Cost Center", "parent_cost_center")
rebuild_tree("Sales Person", "parent_sales_person")

View File

@@ -0,0 +1,262 @@
wn.pages['sales-analytics'].onload = function(wrapper) {
wn.ui.make_app_page({
parent: wrapper,
title: 'Sales Analytics',
single_column: true
});
new erpnext.SalesAnalytics(wrapper);
}
erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({
init: function(wrapper) {
this._super({
title: "Sales Analytics",
page: wrapper,
parent: $(wrapper).find('.layout-main'),
appframe: wrapper.appframe,
doctypes: ["Item", "Item Group", "Customer", "Customer Group", "Company",
"Sales Invoice", "Sales Invoice Item", "Territory"],
tree_grid: { show: true }
});
this.tree_grids = {
"Customer Group": {
label: "Customer Group / Customer",
show: true,
item_key: "customer",
parent_field: "parent_customer_group",
formatter: function(item) {
// return repl('<a href="#Report2/stock-invoices/customer=%(enc_value)s">%(value)s</a>', {
// value: item.name,
// enc_value: encodeURIComponent(item.name)
// });
return item.name;
}
},
"Customer": {
label: "Customer",
show: false,
item_key: "customer",
formatter: function(item) {
return item.name;
}
},
"Item Group": {
label: "Item",
show: true,
parent_field: "parent_item_group",
item_key: "item_code",
formatter: function(item) {
return item.name;
}
},
"Item": {
label: "Item",
show: false,
item_key: "item_code",
formatter: function(item) {
return item.name;
}
},
"Territory": {
label: "Territory / Customer",
show: true,
item_key: "customer",
parent_field: "parent_territory",
formatter: function(item) {
return item.name;
}
}
}
},
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,
formatter: this.tree_formatter, doctype: "Customer"},
{id: "total", name: "Total", field: "total", plot: false,
formatter: this.currency_formatter}
];
this.make_date_range_columns();
this.columns = std_columns.concat(this.columns);
},
filters: [
{fieldtype:"Select", label: "Tree Type", options:["Customer Group", "Customer",
"Item Group", "Item", "Territory"],
filter: function(val, item, opts, me) {
return me.apply_zero_filter(val, item, opts, me);
}},
{fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"]},
{fieldtype:"Select", label: "Company", link:"Company",
default_value: "Select Company..."},
{fieldtype:"Date", label: "From Date"},
{fieldtype:"Label", label: "To"},
{fieldtype:"Date", label: "To Date"},
{fieldtype:"Select", label: "Range",
options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]},
{fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"},
{fieldtype:"Button", label: "Reset Filters"}
],
setup_filters: function() {
var me = this;
this._super();
this.filter_inputs.value_or_qty.change(function() {
me.filter_inputs.refresh.click();
});
this.filter_inputs.tree_type.change(function() {
me.filter_inputs.refresh.click();
});
this.show_zero_check()
this.setup_plot_check();
},
init_filter_values: function() {
this._super();
this.filter_inputs.range.val('Weekly');
},
prepare_data: function() {
var me = this;
if (!this.tl) {
this.make_tl("Sales Invoice", "Sales Invoice Item");
// add 'Not Set' Customer & Item
// (Customer / Item are not mandatory!!)
wn.report_dump.data["Customer"].push({
name: "Not Set",
parent_customer_group: "All Customer Groups",
parent_territory: "All Territories",
id: "Not Set",
});
wn.report_dump.data["Item"].push({
name: "Not Set",
parent_item_group: "All Item Groups",
id: "Not Set",
});
}
if(!this.data || me.item_type != me.tree_type) {
if(me.tree_type=='Customer') {
var items = wn.report_dump.data["Customer"];
} 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") {
var items = wn.report_dump.data["Item"];
} 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 = [];
$.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);
});
this.set_indent();
} else {
// otherwise, only reset values
$.each(this.data, function(i, d) {
me.reset_item_values(d);
});
}
this.prepare_balances();
if(me.tree_grid.show) {
this.set_totals(false);
this.update_groups();
} else {
this.set_totals(true);
}
},
make_tl: function(parent_doctype, doctype) {
var me = this;
var tmap = {};
$.each(wn.report_dump.data[doctype], function(i, v) {
if(!tmap[v.parent]) tmap[v.parent] = [];
tmap[v.parent].push(v);
});
this.tl = [];
$.each(wn.report_dump.data[parent_doctype], function(i, parent) {
$.each(tmap[parent.name], function(i, d) {
me.tl.push($.extend(copy_dict(parent), d));
});
});
},
prepare_balances: function() {
var me = this;
var from_date = dateutil.str_to_obj(this.from_date);
var to_date = dateutil.str_to_obj(this.to_date);
var is_val = this.value_or_qty == 'Value';
$.each(this.tl, function(i, tl) {
if (me.is_default('company') ? true : me.apply_filter(tl, "company")) {
var posting_date = dateutil.str_to_obj(tl.posting_date);
if (posting_date >= from_date && posting_date <= to_date) {
var item = me.item_by_name[tl[me.tree_grid.item_key]] || me.item_by_name['Not Set'];
item[me.column_map[tl.posting_date].field] += (is_val ? tl.amount : tl.qty);
}
}
});
},
update_groups: function() {
var me = this;
$.each(this.data, function(i, item) {
var parent = me.parent_map[item.name];
while(parent) {
parent_group = me.item_by_name[parent];
$.each(me.columns, function(c, col) {
if (col.formatter == me.currency_formatter) {
parent_group[col.field] =
flt(parent_group[col.field])
+ flt(item[col.field]);
}
});
parent = me.parent_map[parent];
}
});
},
set_totals: function(sort) {
var me = this;
var checked = false;
$.each(this.data, function(i, d) {
d.total = 0.0;
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter && !col.hidden && col.field!="total")
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;
}
},
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

@@ -0,0 +1,28 @@
# Page, sales-analytics
[
# These values are common in all dictionaries
{
u'creation': '2012-09-21 12:06:14',
u'docstatus': 0,
u'modified': '2012-09-21 12:06:14',
u'modified_by': u'Administrator',
u'owner': u'Administrator'
},
# These values are common for all Page
{
u'doctype': u'Page',
'module': u'Selling',
u'name': u'__common__',
'page_name': u'sales-analytics',
'standard': u'Yes',
'title': u'Sales Analytics'
},
# Page, sales-analytics
{
u'doctype': u'Page',
u'name': u'sales-analytics'
}
]

View File

@@ -20,6 +20,16 @@ from __future__ import unicode_literals
# "remember to add indexes!"
data_map = {
"Company": {
"columns": ["name"],
"conditions": ["docstatus < 2"]
},
"Fiscal Year": {
"columns": ["name", "year_start_date",
"adddate(adddate(year_start_date, interval 1 year), interval -1 day) as year_end_date"]
},
# Accounts
"Account": {
"columns": ["name", "parent_account", "lft", "rgt", "debit_or_credit", "is_pl_account",
"company"],
@@ -40,13 +50,23 @@ data_map = {
"company": ["Company", "name"]
}
},
"Company": {
"columns": ["name"],
"conditions": ["docstatus < 2"]
# Stock
"Item": {
"columns": ["name", "if(item_name=name, '', item_name) as item_name",
"item_group as parent_item_group", "stock_uom", "brand"],
"order_by": "name",
"links": {
"parent_item_group": ["Item Group", "name"],
}
},
"Fiscal Year": {
"columns": ["name", "year_start_date",
"adddate(adddate(year_start_date, interval 1 year), interval -1 day) as year_end_date"]
"Item Group": {
"columns": ["name", "parent_item_group"],
"order_by": "lft"
},
"Warehouse": {
"columns": ["name"],
"order_by": "name"
},
"Stock Ledger Entry": {
"columns": ["posting_date", "posting_time", "item_code", "warehouse", "actual_qty as qty",
@@ -59,17 +79,41 @@ data_map = {
},
"force_index": "posting_sort_index"
},
"Item": {
"columns": ["name", "if(item_name=name, '', item_name) as item_name",
"item_group as parent_item_group", "stock_uom", "brand"],
"order_by": "name"
# Sales
"Customer": {
"columns": ["name", "if(customer_name=name, '', customer_name) as customer_name",
"customer_group as parent_customer_group", "territory as parent_territory"],
"order_by": "name",
"links": {
"parent_customer_group": ["Customer Group", "name"],
"parent_territory": ["Territory", "name"],
}
},
"Item Group": {
"columns": ["name", "lft", "rgt", "parent_item_group"],
"Customer Group": {
"columns": ["name", "parent_customer_group"],
"order_by": "lft"
},
"Warehouse": {
"columns": ["name"],
"order_by": "name"
"Territory": {
"columns": ["name", "parent_territory"],
"order_by": "lft"
},
"Sales Invoice": {
"columns": ["name", "customer", "posting_date", "company"],
"conditions": ["docstatus=1"],
"order_by": "posting_date",
"links": {
"customer": ["Customer", "name"],
"company":["Company", "name"]
}
},
"Sales Invoice Item": {
"columns": ["parent", "item_code", "qty", "amount"],
"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
"order_by": "parent",
"links": {
"parent": ["Sales Invoice", "name"],
"item_code": ["Item", "name"]
}
}
}

View File

@@ -25,7 +25,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
enc_value: encodeURIComponent(item.name)
});
}
}
},
})
},
setup_columns: function() {
@@ -35,10 +35,6 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
{id: "name", name: "Item", field: "name", width: 300,
formatter: this.tree_formatter, doctype: "Item"},
{id: "opening", name: "Opening", field: "opening", hidden: true,
formatter: this.currency_formatter},
{id: "balance_qty", name: "Balance Qty", field: "balance_qty", hidden: true,
formatter: this.currency_formatter},
{id: "balance_value", name: "Balance Value", field: "balance_value", hidden: true,
formatter: this.currency_formatter}
];
@@ -46,7 +42,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
this.columns = std_columns.concat(this.columns);
},
filters: [
{fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"],
{fieldtype:"Select", label: "Value or Qty", options:["Value (Weighted Average)", "Value (FIFO)", "Quantity"],
filter: function(val, item, opts, me) {
return me.apply_zero_filter(val, item, opts, me);
}},
@@ -90,9 +86,9 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
},
prepare_data: function() {
var me = this;
if(!this.data) {
var items = this.get_item_tree();
var items = this.prepare_tree("Item", "Item Group");
me.parent_map = {};
me.item_by_name = {};
@@ -109,6 +105,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
me.reset_item_values(d);
});
this.set_indent();
this.data[0].checked = true;
} else {
// otherwise, only reset values
$.each(this.data, function(i, d) {
@@ -120,30 +117,20 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
this.update_groups();
},
get_item_tree: function() {
// prepare map with items in respective item group
var item_group_map = {};
$.each(wn.report_dump.data["Item"], function(i, item) {
var parent = item.parent_item_group
if(!item_group_map[parent]) item_group_map[parent] = [];
item_group_map[parent].push(item);
});
// arrange items besides their parent item groups
var items = [];
$.each(wn.report_dump.data["Item Group"], function(i, group){
group.is_group = true;
items.push(group);
items = items.concat(item_group_map[group.name] || []);
});
return items;
},
prepare_balances: function() {
var me = this;
var from_date = dateutil.str_to_obj(this.from_date);
var to_date = dateutil.str_to_obj(this.to_date);
var data = wn.report_dump.data["Stock Ledger Entry"];
var is_value = me.value_or_qty == "Value";
var warehouse_item = {};
var get_warehouse_item = function(warehouse, item) {
if(!warehouse_item[warehouse]) warehouse_item[warehouse] = {};
if(!warehouse_item[warehouse][item]) warehouse_item[warehouse][item] = {
balance_qty: 0.0, balance_value: 0.0, fifo_stack: []
};
return warehouse_item[warehouse][item];
}
for(var i=0, j=data.length; i<j; i++) {
var sl = data[i];
@@ -152,17 +139,13 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
var item = me.item_by_name[sl.item_code];
// value
var rate = sl.qty > 0 ? sl.incoming_rate :
(item.balance_qty.toFixed(2) == 0.00 ? 0 : flt(item.balance_value) / flt(item.balance_qty));
var value_diff = (rate * sl.qty);
// update balance
item.balance_qty += sl.qty;
item.balance_value += value_diff;
var diff = is_value ? value_diff : sl.qty;
if(me.value_or_qty!="Quantity") {
var wh = get_warehouse_item(sl.warehouse, sl.item_code);
var diff = me.get_value_diff(wh, sl);
} else {
var diff = sl.qty;
}
if(posting_datetime < from_date) {
item.opening += diff;
@@ -174,6 +157,92 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
}
}
},
get_value_diff: function(wh, sl) {
var is_fifo = this.value_or_qty== "Value (FIFO)";
// value
if(sl.qty > 0) {
// incoming - rate is given
var rate = sl.incoming_rate;
var add_qty = sl.qty;
if(wh.balance_qty < 0) {
// negative valuation
// only add value of quantity if
// the balance goes above 0
add_qty = wh.balance_qty + sl.qty;
if(add_qty < 0) {
add_qty = 0;
}
}
var value_diff = (rate * add_qty);
if(add_qty)
wh.fifo_stack.push([add_qty, sl.incoming_rate]);
} else {
// outgoing
if(is_fifo) {
var value_diff = this.get_fifo_value_diff(wh, sl);
} else {
// average rate for weighted average
var rate = (wh.balance_qty.toFixed(2) == 0.00 ? 0 :
flt(wh.balance_value) / flt(wh.balance_qty));
// no change in value if negative qty
if((wh.balance_qty + sl.qty).toFixed(2) >= 0.00)
var value_diff = (rate * sl.qty);
else
var value_diff = -wh.balance_value;
}
}
// update balance (only needed in case of valuation)
wh.balance_qty += sl.qty;
wh.balance_value += value_diff;
if(sl.item_code=="0.5Motor") {
console.log([sl.voucher_no, sl.qty, sl.warehouse, value_diff]);
console.log(wh.fifo_stack);
}
return value_diff;
},
get_fifo_value_diff: function(wh, sl) {
// get exact rate from fifo stack
var fifo_stack = wh.fifo_stack.reverse();
var fifo_value_diff = 0.0;
var qty = -sl.qty;
for(var i=0, j=fifo_stack.length; i<j; i++) {
var batch = fifo_stack.pop();
if(batch[0] >= qty) {
batch[0] = batch[0] - qty;
fifo_value_diff += (qty * batch[1]);
qty = 0.0;
if(batch[0]) {
// batch still has qty put it back
fifo_stack.push(batch);
}
// all qty found
break;
} else {
// consume this batch fully
fifo_value_diff += (batch[0] * batch[1]);
qty = qty - batch[0];
}
}
if(qty) {
// msgprint("Negative values not allowed for FIFO valuation!\
// Item " + sl.item_code.bold() + " on " + dateutil.str_to_user(sl.posting_datetime).bold() +
// " becomes negative. Values computed will not be accurate.");
}
// reset the updated stack
wh.fifo_stack = fifo_stack.reverse();
return -fifo_value_diff;
},
update_groups: function() {
var me = this;
@@ -203,36 +272,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
}
});
},
get_plot_data: function() {
var data = [];
var me = this;
$.each(this.data, function(i, item) {
if (item.checked) {
data.push({
label: item.name,
data: $.map(me.columns, function(col, idx) {
if(col.formatter==me.currency_formatter && !col.hidden) {
return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
}
}),
points: {show: true},
lines: {show: true, fill: true},
});
// prepend opening
data[data.length-1].data = [[dateutil.str_to_obj(me.from_date).getTime(),
item.opening]].concat(data[data.length-1].data);
}
});
return data;
},
get_plot_options: function() {
return {
grid: { hoverable: true, clickable: true },
xaxis: { mode: "time",
min: dateutil.str_to_obj(this.from_date).getTime(),
max: dateutil.str_to_obj(this.to_date).getTime() }
}
get_plot_points: function(item, col, idx) {
return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
}
});
});