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
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
erpnext.StockGridReport = class StockGridReport extends frappe.views.TreeGridReport {
|
2012-09-21 14:16:24 +00:00
|
|
|
get_item_warehouse(warehouse, item) {
|
|
|
|
if(!this.item_warehouse[item]) this.item_warehouse[item] = {};
|
|
|
|
if(!this.item_warehouse[item][warehouse]) this.item_warehouse[item][warehouse] = {
|
|
|
|
balance_qty: 0.0, balance_value: 0.0, fifo_stack: []
|
|
|
|
};
|
|
|
|
return this.item_warehouse[item][warehouse];
|
|
|
|
}
|
2014-10-08 12:36:14 +00:00
|
|
|
|
|
|
|
get_value_diff(wh, sl, is_fifo) {
|
2012-09-21 14:16:24 +00:00
|
|
|
// value
|
|
|
|
if(sl.qty > 0) {
|
|
|
|
// incoming - rate is given
|
2013-11-18 06:46:48 +00:00
|
|
|
var rate = sl.incoming_rate;
|
2012-09-21 14:16:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 08:08:23 +00:00
|
|
|
if(sl.serial_no) {
|
|
|
|
var value_diff = this.get_serialized_value_diff(sl);
|
|
|
|
} else {
|
|
|
|
var value_diff = (rate * add_qty);
|
|
|
|
}
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
if(add_qty)
|
2014-10-08 12:36:14 +00:00
|
|
|
wh.fifo_stack.push([add_qty, sl.incoming_rate, sl.posting_date]);
|
2012-09-21 14:16:24 +00:00
|
|
|
} else {
|
2013-11-18 06:46:48 +00:00
|
|
|
// called everytime for maintaining fifo stack
|
|
|
|
var fifo_value_diff = this.get_fifo_value_diff(wh, sl);
|
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
// outgoing
|
2013-03-08 07:16:48 +00:00
|
|
|
if(sl.serial_no) {
|
|
|
|
var value_diff = -1 * this.get_serialized_value_diff(sl);
|
|
|
|
} else if(is_fifo) {
|
2013-11-18 06:46:48 +00:00
|
|
|
var value_diff = fifo_value_diff;
|
2012-09-21 14:16:24 +00:00
|
|
|
} else {
|
|
|
|
// average rate for weighted average
|
2014-10-08 12:36:14 +00:00
|
|
|
var rate = (wh.balance_qty.toFixed(2) == 0.00 ? 0 :
|
2012-09-21 14:16:24 +00:00
|
|
|
flt(wh.balance_value) / flt(wh.balance_qty));
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
// no change in value if negative qty
|
|
|
|
if((wh.balance_qty + sl.qty).toFixed(2) >= 0.00)
|
2012-12-25 12:34:10 +00:00
|
|
|
var value_diff = (rate * sl.qty);
|
2014-10-08 12:36:14 +00:00
|
|
|
else
|
2012-09-21 14:16:24 +00:00
|
|
|
var value_diff = -wh.balance_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update balance (only needed in case of valuation)
|
|
|
|
wh.balance_qty += sl.qty;
|
|
|
|
wh.balance_value += value_diff;
|
|
|
|
return value_diff;
|
|
|
|
}
|
|
|
|
get_fifo_value_diff(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;
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
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]);
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
qty = 0.0;
|
|
|
|
if(batch[0]) {
|
|
|
|
// batch still has qty put it back
|
|
|
|
fifo_stack.push(batch);
|
|
|
|
}
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
// all qty found
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// consume this batch fully
|
|
|
|
fifo_value_diff += (batch[0] * batch[1]);
|
|
|
|
qty = qty - batch[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reset the updated stack
|
|
|
|
wh.fifo_stack = fifo_stack.reverse();
|
|
|
|
return -fifo_value_diff;
|
|
|
|
}
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2013-03-08 07:16:48 +00:00
|
|
|
get_serialized_value_diff(sl) {
|
|
|
|
var me = this;
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2013-03-08 07:16:48 +00:00
|
|
|
var value_diff = 0.0;
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2013-03-22 07:59:06 +00:00
|
|
|
$.each(sl.serial_no.trim().split("\n"), function(i, sr) {
|
|
|
|
if(sr) {
|
2013-10-02 10:59:45 +00:00
|
|
|
value_diff += flt(me.serialized_buying_rates[sr.trim().toLowerCase()]);
|
2013-03-08 07:16:48 +00:00
|
|
|
}
|
|
|
|
});
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2013-03-08 07:16:48 +00:00
|
|
|
return value_diff;
|
|
|
|
}
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2013-03-08 07:16:48 +00:00
|
|
|
get_serialized_buying_rates() {
|
|
|
|
var serialized_buying_rates = {};
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
if (frappe.report_dump.data["Serial No"]) {
|
|
|
|
$.each(frappe.report_dump.data["Serial No"], function(i, sn) {
|
2013-11-18 05:51:04 +00:00
|
|
|
serialized_buying_rates[sn.name.toLowerCase()] = flt(sn.incoming_rate);
|
|
|
|
});
|
|
|
|
}
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2013-03-08 07:16:48 +00:00
|
|
|
return serialized_buying_rates;
|
|
|
|
}
|
2014-10-08 12:36:14 +00:00
|
|
|
};
|