Stock Ageing & release of Analytics

This commit is contained in:
Rushabh Mehta
2012-09-21 19:46:24 +05:30
parent f200c5296d
commit 09d84b6575
22 changed files with 869 additions and 176 deletions

View File

@@ -0,0 +1,168 @@
// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
wn.pages['stock-ageing'].onload = function(wrapper) {
wn.ui.make_app_page({
parent: wrapper,
title: 'Stock Ageing',
single_column: true
});
new erpnext.StockAgeing(wrapper);
}
wn.require("js/app/stock_grid_report.js");
erpnext.StockAgeing = erpnext.StockGridReport.extend({
init: function(wrapper) {
this._super({
title: "Stock Ageing",
page: wrapper,
parent: $(wrapper).find('.layout-main'),
appframe: wrapper.appframe,
doctypes: ["Item", "Warehouse", "Stock Ledger Entry", "Item Group"],
})
},
setup_columns: function() {
this.columns = [
{id: "name", name: "Item", field: "name", width: 300,
formatter: this.link_formatter},
{id: "average_age", name: "Average Age", field: "average_age",
formatter: this.currency_formatter},
{id: "earliest", name: "Earliest", field: "earliest",
formatter: this.currency_formatter},
{id: "latest", name: "Latest", field: "latest",
formatter: this.currency_formatter}
];
},
filters: [
{fieldtype:"Select", label: "Warehouse", link:"Warehouse",
default_value: "Select Warehouse..."},
{fieldtype:"Select", label: "Plot By",
options: ["Average Age", "Earliest", "Latest"]},
{fieldtype:"Date", label: "To Date"},
{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.warehouse.change(function() {
me.filter_inputs.refresh.click();
});
this.filter_inputs.plot_by.change(function() {
me.filter_inputs.refresh.click();
});
},
init_filter_values: function() {
this._super();
this.filter_inputs.to_date.val(dateutil.obj_to_user(new Date()));
},
prepare_data: function() {
var me = this;
if(!this.data) {
me.data = wn.report_dump.data["Item"];
me.item_by_name = me.make_name_map(me.data);
}
$.each(this.data, function(i, d) {
me.reset_item_values(d);
});
this.prepare_balances();
},
prepare_balances: function() {
var me = this;
var to_date = dateutil.str_to_obj(this.to_date);
var data = wn.report_dump.data["Stock Ledger Entry"];
this.item_warehouse = {};
for(var i=0, j=data.length; i<j; i++) {
var sl = data[i];
var posting_date = dateutil.str_to_obj(sl.posting_date);
if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
// call diff to build fifo stack in item_warehouse
var diff = me.get_value_diff(wh, sl, true);
if(posting_date > to_date)
break;
}
}
$.each(me.data, function(i, item) {
var full_fifo_stack = [];
if(me.is_default("warehouse")) {
$.each(me.item_warehouse[item.name] || {}, function(i, wh) {
full_fifo_stack = full_fifo_stack.concat(wh.fifo_stack || [])
});
} else {
full_fifo_stack = me.get_item_warehouse(me.warehouse, item.name) || [];
}
var age_qty = total_qty = 0.0;
var min_age = max_age = null;
$.each(full_fifo_stack, function(i, batch) {
var batch_age = dateutil.get_diff(me.to_date, batch[2]);
age_qty += batch_age * batch[1];
total_qty += batch[1];
max_age = Math.max(max_age, batch_age);
if(min_age===null) min_age=batch_age; else min_age = Math.min(min_age, batch_age)
});
item.average_age = total_qty.toFixed(2)==0.0 ? 0 : (age_qty / total_qty).toFixed(2);
item.earliest = max_age || 0.0;
item.latest = min_age || 0.0;
});
this.data = this.data.sort(function(a, b) {
var sort_by = me.plot_by.replace(" ", "_").toLowerCase();
return b[sort_by] - a[sort_by];
});
},
get_plot_data: function() {
var data = [];
var me = this;
data.push({
label: me.plot_by,
data: $.map(me.data, function(item, idx) {
return [[idx+1, item[me.plot_by.replace(" ", "_").toLowerCase() ]]]
}),
bars: {show: true},
});
return data.length ? data : false;
},
get_plot_options: function() {
var me = this;
return {
grid: { hoverable: true, clickable: true },
xaxis: {
ticks: $.map(me.data, function(item, idx) { return [[idx+1, item.name]] }),
max: 20
}
}
}
});

View File

@@ -0,0 +1,28 @@
# Page, stock-ageing
[
# These values are common in all dictionaries
{
u'creation': '2012-09-21 18:21:31',
u'docstatus': 0,
u'modified': '2012-09-21 18:21:31',
u'modified_by': u'Administrator',
u'owner': u'Administrator'
},
# These values are common for all Page
{
u'doctype': u'Page',
'module': u'Stock',
u'name': u'__common__',
'page_name': u'stock-ageing',
'standard': u'Yes',
'title': u'Stock Ageing'
},
# Page, stock-ageing
{
u'doctype': u'Page',
u'name': u'stock-ageing'
}
]

View File

@@ -1,3 +1,19 @@
// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
wn.pages['stock-analytics'].onload = function(wrapper) {
wn.ui.make_app_page({
parent: wrapper,
@@ -8,7 +24,9 @@ wn.pages['stock-analytics'].onload = function(wrapper) {
new erpnext.StockAnalytics(wrapper);
}
erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
wn.require("js/app/stock_grid_report.js");
erpnext.StockAnalytics = erpnext.StockGridReport.extend({
init: function(wrapper) {
this._super({
title: "Stock Analytics",
@@ -33,7 +51,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
{id: "check", name: "Plot", field: "check", width: 30,
formatter: this.check_formatter},
{id: "name", name: "Item", field: "name", width: 300,
formatter: this.tree_formatter, doctype: "Item"},
formatter: this.tree_formatter},
{id: "opening", name: "Opening", field: "opening", hidden: true,
formatter: this.currency_formatter}
];
@@ -42,7 +60,8 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
this.columns = std_columns.concat(this.columns);
},
filters: [
{fieldtype:"Select", label: "Value or Qty", options:["Value (Weighted Average)", "Value (FIFO)", "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);
}},
@@ -123,15 +142,8 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
var to_date = dateutil.str_to_obj(this.to_date);
var data = wn.report_dump.data["Stock Ledger Entry"];
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];
}
this.item_warehouse = {};
for(var i=0, j=data.length; i<j; i++) {
var sl = data[i];
sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
@@ -141,8 +153,9 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({
var item = me.item_by_name[sl.item_code];
if(me.value_or_qty!="Quantity") {
var wh = get_warehouse_item(sl.warehouse, sl.item_code);
var diff = me.get_value_diff(wh, sl);
var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
var is_fifo = this.value_or_qty== "Value (FIFO)";
var diff = me.get_value_diff(wh, sl, is_fifo);
} else {
var diff = sl.qty;
}
@@ -157,92 +170,6 @@ 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;

View File

@@ -23,6 +23,21 @@
<br>
<h4><a href="#!List/Warehouse">Warehouse</a></h4>
<p class="help">Warehouse is where items are stored</p>
<br>
<h4><a href="#stock-ledger" data-role="Analytics">Stock Ledger</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">Log of stock movements</p>
<br>
<h4><a href="#stock-analytics" data-role="Analytics">Stock Analytics</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">Visual representation of stock trends</p>
<br>
<h4><a href="#stock-ageing" data-role="Analytics">Stock Ageing</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">Analysis of slow moving stock</p>
</div>
<div style="clear: both"></div>
<hr>

View File

@@ -1,3 +1,19 @@
// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
wn.pages['stock-ledger'].onload = function(wrapper) {
wn.ui.make_app_page({
parent: wrapper,
@@ -8,7 +24,9 @@ wn.pages['stock-ledger'].onload = function(wrapper) {
new erpnext.StockLedger(wrapper);
}
erpnext.StockLedger = wn.views.GridReportWithPlot.extend({
wn.require("js/app/stock_grid_report.js");
erpnext.StockLedger = erpnext.StockGridReport.extend({
init: function(wrapper) {
this._super({
title: "Stock Ledger",
@@ -28,15 +46,16 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({
link_formatter: {
filter_input: "item_code",
open_btn: true,
doctype: '"Item"'
}},
{id: "warehouse", name: "Warehouse", field: "warehouse", width: 100,
link_formatter: {filter_input: "warehouse"}},
{id: "qty", name: "Qty", field: "qty", width: 100,
formatter: this.currency_formatter},
{id: "balance", name: "Balance", field: "balance", width: 100,
{id: "balance", name: "Balance Qty", field: "balance", width: 100,
formatter: this.currency_formatter,
hidden: this.hide_balance},
{id: "balance_value", name: "Balance Value", field: "balance_value", width: 100,
formatter: this.currency_formatter, hidden: this.hide_balance},
{id: "voucher_type", name: "Voucher Type", field: "voucher_type", width: 120},
{id: "voucher_no", name: "Voucher No", field: "voucher_no", width: 160,
link_formatter: {
@@ -86,36 +105,53 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({
var opening = {
item_code: "On " + dateutil.str_to_user(this.from_date), qty: 0.0, balance: 0.0,
id:"_opening", _show: true, _style: "font-weight: bold"
id:"_opening", _show: true, _style: "font-weight: bold", balance_value: 0.0
}
var total_in = {
item_code: "Total In", qty: 0.0, balance: 0.0,
item_code: "Total In", qty: 0.0, balance: 0.0, balance_value: 0.0,
id:"_total_in", _show: true, _style: "font-weight: bold"
}
var total_out = {
item_code: "Total Out", qty: 0.0, balance: 0.0,
item_code: "Total Out", qty: 0.0, balance: 0.0, balance_value: 0.0,
id:"_total_out", _show: true, _style: "font-weight: bold"
}
// clear balance
$.each(wn.report_dump.data["Item"], function(i, item) { item.balance = 0.0; });
$.each(wn.report_dump.data["Item"], function(i, item) {
item.balance = item.balance_value = 0.0;
});
// initialize warehouse-item map
this.item_warehouse = {};
//
for(var i=0, j=data.length; i<j; i++) {
var sl = data[i];
sl.description = me.item_by_name[sl.item_code].description;
var item = me.item_by_name[sl.item_code]
var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
sl.description = item.description;
sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
var is_fifo = item.valuation_method ? item.valuation_method=="FIFO"
: sys_defaults.valuation_method=="FIFO";
var value_diff = me.get_value_diff(wh, sl, is_fifo);
// opening, transactions, closing, total in, total out
var before_end = posting_datetime <= dateutil.str_to_obj(me.to_date + " 23:59:59");
if((!me.is_default("item_code") ? me.apply_filter(sl, "item_code") : true)
&& me.apply_filter(sl, "warehouse") && me.apply_filter(sl, "voucher_no")) {
if(posting_datetime < dateutil.str_to_obj(me.from_date)) {
opening.balance += sl.qty;
opening.balance_value += value_diff;
} else if(before_end) {
if(sl.qty > 0) total_in.qty += sl.qty;
else total_out.qty += (-1 * sl.qty);
if(sl.qty > 0) {
total_in.qty += sl.qty;
total_in.balance_value += value_diff;
} else {
total_out.qty += (-1 * sl.qty);
total_out.balance_value += value_diff;
}
}
}
@@ -129,7 +165,10 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({
// update balance
if((!me.is_default("warehouse") ? me.apply_filter(sl, "warehouse") : true)) {
sl.balance = me.item_by_name[sl.item_code].balance + sl.qty;
me.item_by_name[sl.item_code].balance = sl.balance;
me.item_by_name[sl.item_code].balance = sl.balance;
sl.balance_value = me.item_by_name[sl.item_code].balance_value + value_diff;
me.item_by_name[sl.item_code].balance_value = sl.balance_value;
}
}
@@ -137,8 +176,10 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({
var closing = {
item_code: "On " + dateutil.str_to_user(this.to_date),
balance: (out ? out[out.length-1].balance : 0), qty: 0,
balance_value: (out ? out[out.length-1].balance_value : 0),
id:"_closing", _show: true, _style: "font-weight: bold"
};
total_out.balance_value = -total_out.balance_value;
var out = [opening].concat(out).concat([total_in, total_out, closing]);
}