Merge pull request #5626 from saurabh6790/stock_analytics
[fix] load dependancies on page load event
This commit is contained in:
commit
3d047c0be5
@ -1,211 +1,210 @@
|
|||||||
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||||
// License: GNU General Public License v3. See license.txt
|
// License: GNU General Public License v3. See license.txt
|
||||||
|
|
||||||
frappe.require("assets/erpnext/js/stock_grid_report.js", function() {
|
|
||||||
erpnext.StockAnalytics = erpnext.StockGridReport.extend({
|
|
||||||
init: function(wrapper, opts) {
|
|
||||||
var args = {
|
|
||||||
title: __("Stock Analytics"),
|
|
||||||
page: wrapper,
|
|
||||||
parent: $(wrapper).find('.layout-main'),
|
|
||||||
page: wrapper.page,
|
|
||||||
doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand",
|
|
||||||
"Fiscal Year", "Serial No"],
|
|
||||||
tree_grid: {
|
|
||||||
show: true,
|
|
||||||
parent_field: "parent_item_group",
|
|
||||||
formatter: function(item) {
|
|
||||||
if(!item.is_group) {
|
|
||||||
return repl("<a \
|
|
||||||
onclick='frappe.cur_grid_report.show_stock_ledger(\"%(value)s\")'>\
|
|
||||||
%(value)s</a>", {
|
|
||||||
value: item.name,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return item.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
erpnext.StockAnalytics = erpnext.StockGridReport.extend({
|
||||||
},
|
init: function(wrapper, opts) {
|
||||||
}
|
var args = {
|
||||||
|
title: __("Stock Analytics"),
|
||||||
if(opts) $.extend(args, opts);
|
page: wrapper,
|
||||||
|
parent: $(wrapper).find('.layout-main'),
|
||||||
this._super(args);
|
page: wrapper.page,
|
||||||
},
|
doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand",
|
||||||
setup_columns: function() {
|
"Fiscal Year", "Serial No"],
|
||||||
var std_columns = [
|
tree_grid: {
|
||||||
{id: "_check", name: __("Plot"), field: "_check", width: 30,
|
show: true,
|
||||||
formatter: this.check_formatter},
|
parent_field: "parent_item_group",
|
||||||
{id: "name", name: __("Item"), field: "name", width: 300,
|
formatter: function(item) {
|
||||||
formatter: this.tree_formatter},
|
if(!item.is_group) {
|
||||||
{id: "brand", name: __("Brand"), field: "brand", width: 100},
|
return repl("<a \
|
||||||
{id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100},
|
onclick='frappe.cur_grid_report.show_stock_ledger(\"%(value)s\")'>\
|
||||||
{id: "opening", name: __("Opening"), field: "opening", hidden: true,
|
%(value)s</a>", {
|
||||||
formatter: this.currency_formatter}
|
value: item.name,
|
||||||
];
|
});
|
||||||
|
|
||||||
this.make_date_range_columns();
|
|
||||||
this.columns = std_columns.concat(this.columns);
|
|
||||||
},
|
|
||||||
filters: [
|
|
||||||
{fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty",
|
|
||||||
options:[{label:__("Value"), value:"Value"}, {label:__("Quantity"), value:"Quantity"}],
|
|
||||||
filter: function(val, item, opts, me) {
|
|
||||||
return me.apply_zero_filter(val, item, opts, me);
|
|
||||||
}},
|
|
||||||
{fieldtype:"Select", label: __("Brand"), link:"Brand", fieldname: "brand",
|
|
||||||
default_value: __("Select Brand..."), filter: function(val, item, opts) {
|
|
||||||
return val == opts.default_value || item.brand == val || item._show;
|
|
||||||
}, link_formatter: {filter_input: "brand"}},
|
|
||||||
{fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse",
|
|
||||||
default_value: __("Select Warehouse...")},
|
|
||||||
{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_filters: function() {
|
|
||||||
var me = this;
|
|
||||||
this._super();
|
|
||||||
|
|
||||||
this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
|
|
||||||
|
|
||||||
this.show_zero_check();
|
|
||||||
this.setup_chart_check();
|
|
||||||
},
|
|
||||||
init_filter_values: function() {
|
|
||||||
this._super();
|
|
||||||
this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
|
|
||||||
},
|
|
||||||
prepare_data: function() {
|
|
||||||
var me = this;
|
|
||||||
|
|
||||||
if(!this.data) {
|
|
||||||
var items = this.prepare_tree("Item", "Item Group");
|
|
||||||
|
|
||||||
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.parent_item_group) {
|
|
||||||
me.parent_map[d.name] = d.parent_item_group;
|
|
||||||
}
|
|
||||||
me.reset_item_values(d);
|
|
||||||
});
|
|
||||||
this.set_indent();
|
|
||||||
this.data[0].checked = true;
|
|
||||||
} else {
|
|
||||||
// otherwise, only reset values
|
|
||||||
$.each(this.data, function(i, d) {
|
|
||||||
me.reset_item_values(d);
|
|
||||||
d["closing_qty_value"] = 0;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.prepare_balances();
|
|
||||||
this.update_groups();
|
|
||||||
|
|
||||||
},
|
|
||||||
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 = frappe.report_dump.data["Stock Ledger Entry"];
|
|
||||||
|
|
||||||
this.item_warehouse = {};
|
|
||||||
this.serialized_buying_rates = this.get_serialized_buying_rates();
|
|
||||||
|
|
||||||
for(var i=0, j=data.length; i<j; i++) {
|
|
||||||
var sl = data[i];
|
|
||||||
sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
|
|
||||||
var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
|
|
||||||
|
|
||||||
if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
|
|
||||||
var item = me.item_by_name[sl.item_code];
|
|
||||||
if(item.closing_qty_value==undefined) item.closing_qty_value = 0;
|
|
||||||
|
|
||||||
if(me.value_or_qty!="Quantity") {
|
|
||||||
var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
|
|
||||||
var valuation_method = item.valuation_method ?
|
|
||||||
item.valuation_method : sys_defaults.valuation_method;
|
|
||||||
var is_fifo = valuation_method == "FIFO";
|
|
||||||
|
|
||||||
if(sl.voucher_type=="Stock Reconciliation") {
|
|
||||||
var diff = (sl.qty_after_transaction * sl.valuation_rate) - item.closing_qty_value;
|
|
||||||
wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]];
|
|
||||||
wh.balance_qty = sl.qty_after_transaction;
|
|
||||||
wh.balance_value = sl.valuation_rate * sl.qty_after_transaction;
|
|
||||||
} else {
|
|
||||||
var diff = me.get_value_diff(wh, sl, is_fifo);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if(sl.voucher_type=="Stock Reconciliation") {
|
return item.name;
|
||||||
var diff = sl.qty_after_transaction - item.closing_qty_value;
|
|
||||||
} else {
|
|
||||||
var diff = sl.qty;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(posting_datetime < from_date) {
|
|
||||||
item.opening += diff;
|
|
||||||
} else if(posting_datetime <= to_date) {
|
|
||||||
item[me.column_map[sl.posting_date].field] += diff;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
item.closing_qty_value += diff;
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if(opts) $.extend(args, opts);
|
||||||
|
|
||||||
|
this._super(args);
|
||||||
|
},
|
||||||
|
setup_columns: function() {
|
||||||
|
var std_columns = [
|
||||||
|
{id: "_check", name: __("Plot"), field: "_check", width: 30,
|
||||||
|
formatter: this.check_formatter},
|
||||||
|
{id: "name", name: __("Item"), field: "name", width: 300,
|
||||||
|
formatter: this.tree_formatter},
|
||||||
|
{id: "brand", name: __("Brand"), field: "brand", width: 100},
|
||||||
|
{id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100},
|
||||||
|
{id: "opening", name: __("Opening"), field: "opening", hidden: true,
|
||||||
|
formatter: this.currency_formatter}
|
||||||
|
];
|
||||||
|
|
||||||
|
this.make_date_range_columns();
|
||||||
|
this.columns = std_columns.concat(this.columns);
|
||||||
|
},
|
||||||
|
filters: [
|
||||||
|
{fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty",
|
||||||
|
options:[{label:__("Value"), value:"Value"}, {label:__("Quantity"), value:"Quantity"}],
|
||||||
|
filter: function(val, item, opts, me) {
|
||||||
|
return me.apply_zero_filter(val, item, opts, me);
|
||||||
|
}},
|
||||||
|
{fieldtype:"Select", label: __("Brand"), link:"Brand", fieldname: "brand",
|
||||||
|
default_value: __("Select Brand..."), filter: function(val, item, opts) {
|
||||||
|
return val == opts.default_value || item.brand == val || item._show;
|
||||||
|
}, link_formatter: {filter_input: "brand"}},
|
||||||
|
{fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse",
|
||||||
|
default_value: __("Select Warehouse...")},
|
||||||
|
{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_filters: function() {
|
||||||
|
var me = this;
|
||||||
|
this._super();
|
||||||
|
|
||||||
|
this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
|
||||||
|
|
||||||
|
this.show_zero_check();
|
||||||
|
this.setup_chart_check();
|
||||||
|
},
|
||||||
|
init_filter_values: function() {
|
||||||
|
this._super();
|
||||||
|
this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
|
||||||
|
},
|
||||||
|
prepare_data: function() {
|
||||||
|
var me = this;
|
||||||
|
|
||||||
|
if(!this.data) {
|
||||||
|
var items = this.prepare_tree("Item", "Item Group");
|
||||||
|
|
||||||
|
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.parent_item_group) {
|
||||||
|
me.parent_map[d.name] = d.parent_item_group;
|
||||||
|
}
|
||||||
|
me.reset_item_values(d);
|
||||||
|
});
|
||||||
|
this.set_indent();
|
||||||
|
this.data[0].checked = true;
|
||||||
|
} else {
|
||||||
|
// otherwise, only reset values
|
||||||
|
$.each(this.data, function(i, d) {
|
||||||
|
me.reset_item_values(d);
|
||||||
|
d["closing_qty_value"] = 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.prepare_balances();
|
||||||
|
this.update_groups();
|
||||||
|
|
||||||
|
},
|
||||||
|
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 = frappe.report_dump.data["Stock Ledger Entry"];
|
||||||
|
|
||||||
|
this.item_warehouse = {};
|
||||||
|
this.serialized_buying_rates = this.get_serialized_buying_rates();
|
||||||
|
|
||||||
|
for(var i=0, j=data.length; i<j; i++) {
|
||||||
|
var sl = data[i];
|
||||||
|
sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
|
||||||
|
var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
|
||||||
|
|
||||||
|
if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
|
||||||
|
var item = me.item_by_name[sl.item_code];
|
||||||
|
if(item.closing_qty_value==undefined) item.closing_qty_value = 0;
|
||||||
|
|
||||||
|
if(me.value_or_qty!="Quantity") {
|
||||||
|
var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
|
||||||
|
var valuation_method = item.valuation_method ?
|
||||||
|
item.valuation_method : sys_defaults.valuation_method;
|
||||||
|
var is_fifo = valuation_method == "FIFO";
|
||||||
|
|
||||||
|
if(sl.voucher_type=="Stock Reconciliation") {
|
||||||
|
var diff = (sl.qty_after_transaction * sl.valuation_rate) - item.closing_qty_value;
|
||||||
|
wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]];
|
||||||
|
wh.balance_qty = sl.qty_after_transaction;
|
||||||
|
wh.balance_value = sl.valuation_rate * sl.qty_after_transaction;
|
||||||
|
} else {
|
||||||
|
var diff = me.get_value_diff(wh, sl, is_fifo);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(sl.voucher_type=="Stock Reconciliation") {
|
||||||
|
var diff = sl.qty_after_transaction - item.closing_qty_value;
|
||||||
|
} else {
|
||||||
|
var diff = sl.qty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(posting_datetime < from_date) {
|
||||||
|
item.opening += diff;
|
||||||
|
} else if(posting_datetime <= to_date) {
|
||||||
|
item[me.column_map[sl.posting_date].field] += diff;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
item.closing_qty_value += diff;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
update_groups: function() {
|
},
|
||||||
var me = this;
|
update_groups: function() {
|
||||||
$.each(this.data, function(i, item) {
|
var me = this;
|
||||||
// update groups
|
$.each(this.data, function(i, item) {
|
||||||
if(!item.is_group && me.apply_filter(item, "brand")) {
|
// update groups
|
||||||
var balance = item.opening;
|
if(!item.is_group && me.apply_filter(item, "brand")) {
|
||||||
$.each(me.columns, function(i, col) {
|
var balance = item.opening;
|
||||||
if(col.formatter==me.currency_formatter && !col.hidden) {
|
$.each(me.columns, function(i, col) {
|
||||||
item[col.field] = balance + item[col.field];
|
if(col.formatter==me.currency_formatter && !col.hidden) {
|
||||||
balance = item[col.field];
|
item[col.field] = balance + item[col.field];
|
||||||
|
balance = item[col.field];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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];
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
},
|
});
|
||||||
show_stock_ledger: function(item_code) {
|
},
|
||||||
frappe.route_options = {
|
show_stock_ledger: function(item_code) {
|
||||||
item_code: item_code,
|
frappe.route_options = {
|
||||||
from_date: this.from_date,
|
item_code: item_code,
|
||||||
to_date: this.to_date
|
from_date: this.from_date,
|
||||||
};
|
to_date: this.to_date
|
||||||
frappe.set_route("query-report", "Stock Ledger");
|
};
|
||||||
}
|
frappe.set_route("query-report", "Stock Ledger");
|
||||||
});
|
}
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||||
// License: GNU General Public License v3. See license.txt
|
// License: GNU General Public License v3. See license.txt
|
||||||
|
|
||||||
frappe.require("assets/erpnext/js/stock_analytics.js", function() {
|
frappe.pages['stock-analytics'].on_page_load = function(wrapper) {
|
||||||
frappe.pages['stock-analytics'].on_page_load = function(wrapper) {
|
frappe.ui.make_app_page({
|
||||||
frappe.ui.make_app_page({
|
parent: wrapper,
|
||||||
parent: wrapper,
|
title: __('Stock Analytics'),
|
||||||
title: __('Stock Analytics'),
|
single_column: true
|
||||||
single_column: true
|
});
|
||||||
});
|
|
||||||
|
frappe.require(["assets/erpnext/js/stock_grid_report.js",
|
||||||
|
"assets/erpnext/js/stock_analytics.js"], function() {
|
||||||
new erpnext.StockAnalytics(wrapper);
|
new erpnext.StockAnalytics(wrapper);
|
||||||
|
|
||||||
frappe.breadcrumbs.add("Stock")
|
frappe.breadcrumbs.add("Stock")
|
||||||
};
|
});
|
||||||
}, false);
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user