brotherton-erpnext/erpnext/buying/page/purchase_analytics/purchase_analytics.js

257 lines
7.4 KiB
JavaScript
Raw Normal View History

2013-11-20 07:29:58 +00:00
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
2012-09-21 14:16:24 +00:00
2014-02-14 10:17:51 +00:00
frappe.pages['purchase-analytics'].onload = function(wrapper) {
frappe.ui.make_app_page({
2012-09-21 14:16:24 +00:00
parent: wrapper,
2014-02-14 10:17:51 +00:00
title: frappe._('Purchase Analytics'),
2012-09-21 14:16:24 +00:00
single_column: true
});
new erpnext.PurchaseAnalytics(wrapper);
2012-11-26 13:17:54 +00:00
wrapper.appframe.add_module_icon("Buying")
2012-09-21 14:16:24 +00:00
}
2014-02-14 10:17:51 +00:00
erpnext.PurchaseAnalytics = frappe.views.TreeGridReport.extend({
2012-09-21 14:16:24 +00:00
init: function(wrapper) {
this._super({
2014-02-14 10:17:51 +00:00
title: frappe._("Purchase Analytics"),
2012-09-21 14:16:24 +00:00
page: wrapper,
parent: $(wrapper).find('.layout-main'),
appframe: wrapper.appframe,
doctypes: ["Item", "Item Group", "Supplier", "Supplier Type", "Company", "Fiscal Year",
"Purchase Invoice", "Purchase Invoice Item",
"Purchase Order", "Purchase Order Item[Purchase Analytics]",
"Purchase Receipt", "Purchase Receipt Item[Purchase Analytics]"],
2012-09-21 14:16:24 +00:00
tree_grid: { show: true }
});
this.tree_grids = {
"Supplier Type": {
2014-02-14 10:17:51 +00:00
label: frappe._("Supplier Type / Supplier"),
2012-09-21 14:16:24 +00:00
show: true,
item_key: "supplier",
parent_field: "parent_supplier_type",
formatter: function(item) {
// return repl('<a href="#Report/stock-invoices/customer=%(enc_value)s">%(value)s</a>', {
2012-09-21 14:16:24 +00:00
// value: item.name,
// enc_value: encodeURIComponent(item.name)
// });
return item.name;
}
},
"Supplier": {
2014-02-14 10:17:51 +00:00
label: frappe._("Supplier"),
2012-09-21 14:16:24 +00:00
show: false,
item_key: "supplier",
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;
}
},
}
},
setup_columns: function() {
this.tree_grid = this.tree_grids[this.tree_type];
var std_columns = [
2014-02-14 10:17:51 +00:00
{id: "check", name: frappe._("Plot"), field: "check", width: 30,
2012-09-21 14:16:24 +00:00
formatter: this.check_formatter},
{id: "name", name: this.tree_grid.label, field: "name", width: 300,
formatter: this.tree_formatter},
{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: [
2014-02-14 10:17:51 +00:00
{fieldtype:"Select", label: frappe._("Tree Type"), options:["Supplier Type", "Supplier",
2012-09-21 14:16:24 +00:00
"Item Group", "Item"],
filter: function(val, item, opts, me) {
return me.apply_zero_filter(val, item, opts, me);
}},
2014-02-14 10:17:51 +00:00
{fieldtype:"Select", label: frappe._("Based On"), options:["Purchase Invoice",
"Purchase Order", "Purchase Receipt"]},
2014-02-14 10:17:51 +00:00
{fieldtype:"Select", label: frappe._("Value or Qty"), options:["Value", "Quantity"]},
{fieldtype:"Select", label: frappe._("Company"), link:"Company",
2012-09-21 14:16:24 +00:00
default_value: "Select Company..."},
2014-02-14 10:17:51 +00:00
{fieldtype:"Date", label: frappe._("From Date")},
{fieldtype:"Label", label: frappe._("To")},
{fieldtype:"Date", label: frappe._("To Date")},
{fieldtype:"Select", label: frappe._("Range"),
2012-09-21 14:16:24 +00:00
options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]},
2014-02-14 10:17:51 +00:00
{fieldtype:"Button", label: frappe._("Refresh"), icon:"icon-refresh icon-white"},
{fieldtype:"Button", label: frappe._("Reset Filters")}
2012-09-21 14:16:24 +00:00
],
setup_filters: function() {
var me = this;
this._super();
this.trigger_refresh_on_change(["value_or_qty", "tree_type", "based_on", "company"]);
2012-09-21 14:16:24 +00:00
this.show_zero_check()
this.setup_plot_check();
},
init_filter_values: function() {
this._super();
this.filter_inputs.range.val('Monthly');
2012-09-21 14:16:24 +00:00
},
prepare_data: function() {
var me = this;
if (!this.tl) {
// add 'Not Set' Supplier & Item
// Add 'All Supplier Types' Supplier Type
// (Supplier / Item are not mandatory!!)
// Set parent supplier type for tree view
2014-02-14 10:17:51 +00:00
$.each(frappe.report_dump.data["Supplier Type"], function(i, v) {
2012-09-21 14:16:24 +00:00
v['parent_supplier_type'] = "All Supplier Types"
})
2014-02-14 10:17:51 +00:00
frappe.report_dump.data["Supplier Type"] = [{
name: frappe._("All Supplier Types"),
2012-09-21 14:16:24 +00:00
id: "All Supplier Types",
2014-02-14 10:17:51 +00:00
}].concat(frappe.report_dump.data["Supplier Type"]);
2012-09-21 14:16:24 +00:00
2014-02-14 10:17:51 +00:00
frappe.report_dump.data["Supplier"].push({
name: frappe._("Not Set"),
2012-09-21 14:16:24 +00:00
parent_supplier_type: "All Supplier Types",
id: "Not Set",
});
2014-02-14 10:17:51 +00:00
frappe.report_dump.data["Item"].push({
name: frappe._("Not Set"),
2012-09-21 14:16:24 +00:00
parent_item_group: "All Item Groups",
id: "Not Set",
});
}
if (!this.tl || !this.tl[this.based_on]) {
this.make_transaction_list(this.based_on, this.based_on + " Item");
}
2012-09-21 14:16:24 +00:00
if(!this.data || me.item_type != me.tree_type) {
if(me.tree_type=='Supplier') {
2014-02-14 10:17:51 +00:00
var items = frappe.report_dump.data["Supplier"];
2012-09-21 14:16:24 +00:00
} if(me.tree_type=='Supplier Type') {
var items = this.prepare_tree("Supplier", "Supplier Type");
} 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 14:16:24 +00:00
}
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);
}
},
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[this.based_on], function(i, tl) {
if (me.is_default('company') ? true : tl.company === me.company) {
2012-09-21 14:16:24 +00:00
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'];
2012-09-21 14:16:24 +00:00
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 b.total - a.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]]];
}
});