2012-09-21 14:16:24 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2012-09-18 13:22:05 +00:00
|
|
|
wn.pages['stock-ledger'].onload = function(wrapper) {
|
|
|
|
wn.ui.make_app_page({
|
|
|
|
parent: wrapper,
|
|
|
|
title: 'Stock Ledger',
|
|
|
|
single_column: true
|
|
|
|
});
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
new erpnext.StockLedger(wrapper);
|
2012-11-26 13:17:54 +00:00
|
|
|
|
2012-12-22 08:20:35 +00:00
|
|
|
wrapper.appframe.add_home_breadcrumb()
|
|
|
|
wrapper.appframe.add_module_breadcrumb("Stock")
|
|
|
|
wrapper.appframe.add_breadcrumb("icon-bar-chart")
|
2012-09-20 13:33:14 +00:00
|
|
|
}
|
2012-09-18 13:22:05 +00:00
|
|
|
|
2012-09-24 13:43:42 +00:00
|
|
|
wn.require("app/js/stock_grid_report.js");
|
2012-09-21 14:16:24 +00:00
|
|
|
|
|
|
|
erpnext.StockLedger = erpnext.StockGridReport.extend({
|
2012-09-20 13:33:14 +00:00
|
|
|
init: function(wrapper) {
|
|
|
|
this._super({
|
|
|
|
title: "Stock Ledger",
|
|
|
|
page: wrapper,
|
|
|
|
parent: $(wrapper).find('.layout-main'),
|
|
|
|
appframe: wrapper.appframe,
|
2012-12-25 12:34:10 +00:00
|
|
|
doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand"],
|
2012-09-20 13:33:14 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
setup_columns: function() {
|
|
|
|
this.hide_balance = (this.is_default("item_code") || this.voucher_no) ? true : false;
|
|
|
|
this.columns = [
|
|
|
|
{id: "posting_datetime", name: "Posting Date", field: "posting_datetime", width: 120,
|
|
|
|
formatter: this.date_formatter},
|
|
|
|
{id: "item_code", name: "Item Code", field: "item_code", width: 160,
|
|
|
|
link_formatter: {
|
|
|
|
filter_input: "item_code",
|
|
|
|
open_btn: true,
|
2012-10-05 15:03:47 +00:00
|
|
|
doctype: '"Item"',
|
2012-09-18 13:22:05 +00:00
|
|
|
}},
|
2013-02-26 06:53:39 +00:00
|
|
|
{id: "description", name: "Description", field: "description", width: 200,
|
|
|
|
formatter: this.text_formatter},
|
2012-09-20 13:33:14 +00:00
|
|
|
{id: "warehouse", name: "Warehouse", field: "warehouse", width: 100,
|
|
|
|
link_formatter: {filter_input: "warehouse"}},
|
2012-12-25 12:34:10 +00:00
|
|
|
{id: "brand", name: "Brand", field: "brand", width: 100},
|
2013-02-26 06:53:39 +00:00
|
|
|
{id: "stock_uom", name: "UOM", field: "stock_uom", width: 100},
|
2012-09-20 13:33:14 +00:00
|
|
|
{id: "qty", name: "Qty", field: "qty", width: 100,
|
|
|
|
formatter: this.currency_formatter},
|
2012-09-21 14:16:24 +00:00
|
|
|
{id: "balance", name: "Balance Qty", field: "balance", width: 100,
|
2012-09-20 13:33:14 +00:00
|
|
|
formatter: this.currency_formatter,
|
|
|
|
hidden: this.hide_balance},
|
2012-09-21 14:16:24 +00:00
|
|
|
{id: "balance_value", name: "Balance Value", field: "balance_value", width: 100,
|
|
|
|
formatter: this.currency_formatter, hidden: this.hide_balance},
|
2012-09-20 13:33:14 +00:00
|
|
|
{id: "voucher_type", name: "Voucher Type", field: "voucher_type", width: 120},
|
|
|
|
{id: "voucher_no", name: "Voucher No", field: "voucher_no", width: 160,
|
|
|
|
link_formatter: {
|
|
|
|
filter_input: "voucher_no",
|
|
|
|
open_btn: true,
|
|
|
|
doctype: "dataContext.voucher_type"
|
2012-09-18 13:22:05 +00:00
|
|
|
}},
|
2012-09-20 13:33:14 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
},
|
|
|
|
filters: [
|
2012-12-25 12:34:10 +00:00
|
|
|
{fieldtype:"Select", label: "Warehouse", link:"Warehouse",
|
|
|
|
default_value: "Select Warehouse...", filter: function(val, item, opts) {
|
2012-09-20 13:33:14 +00:00
|
|
|
return item.warehouse == val || val == opts.default_value;
|
2012-09-18 13:22:05 +00:00
|
|
|
}},
|
2013-01-21 12:49:52 +00:00
|
|
|
{fieldtype:"Link", label: "Item Code", link:"Item", default_value: "Select Item...",
|
2012-09-20 13:33:14 +00:00
|
|
|
filter: function(val, item, opts) {
|
2013-01-21 12:49:52 +00:00
|
|
|
return item.item_code == val || !val;
|
2012-09-18 13:22:05 +00:00
|
|
|
}},
|
2012-12-25 12:34:10 +00:00
|
|
|
{fieldtype:"Select", label: "Brand", link:"Brand",
|
|
|
|
default_value: "Select Brand...", filter: function(val, item, opts) {
|
|
|
|
return val == opts.default_value || item.brand == val || item._show;
|
2012-12-28 10:09:55 +00:00
|
|
|
}, link_formatter: {filter_input: "brand"}},
|
2012-09-20 13:33:14 +00:00
|
|
|
{fieldtype:"Data", label: "Voucher No",
|
|
|
|
filter: function(val, item, opts) {
|
|
|
|
if(!val) return true;
|
|
|
|
return (item.voucher_no && item.voucher_no.indexOf(val)!=-1);
|
|
|
|
}},
|
|
|
|
{fieldtype:"Date", label: "From Date", filter: function(val, item) {
|
|
|
|
return dateutil.str_to_obj(val) <= dateutil.str_to_obj(item.posting_date);
|
|
|
|
}},
|
|
|
|
{fieldtype:"Label", label: "To"},
|
|
|
|
{fieldtype:"Date", label: "To Date", filter: function(val, item) {
|
|
|
|
return dateutil.str_to_obj(val) >= dateutil.str_to_obj(item.posting_date);
|
|
|
|
}},
|
|
|
|
{fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"},
|
|
|
|
{fieldtype:"Button", label: "Reset Filters"}
|
|
|
|
],
|
2012-12-25 12:34:10 +00:00
|
|
|
|
|
|
|
setup_filters: function() {
|
|
|
|
var me = this;
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
this.wrapper.bind("apply_filters_from_route", function() { me.toggle_enable_brand(); });
|
|
|
|
this.filter_inputs.item_code.change(function() { me.toggle_enable_brand(); });
|
|
|
|
|
|
|
|
this.trigger_refresh_on_change(["item_code", "warehouse", "brand"]);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle_enable_brand: function() {
|
2013-02-08 08:34:13 +00:00
|
|
|
if(!this.filter_inputs.item_code.val()) {
|
2012-12-25 12:34:10 +00:00
|
|
|
this.filter_inputs.brand.removeAttr("disabled");
|
|
|
|
} else {
|
|
|
|
this.filter_inputs.brand
|
|
|
|
.val(this.filter_inputs.brand.get(0).opts.default_value)
|
|
|
|
.attr("disabled", "disabled");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
init_filter_values: function() {
|
|
|
|
this._super();
|
|
|
|
this.filter_inputs.warehouse.get(0).selectedIndex = 0;
|
|
|
|
},
|
|
|
|
prepare_data: function() {
|
|
|
|
var me = this;
|
|
|
|
if(!this.item_by_name)
|
|
|
|
this.item_by_name = this.make_name_map(wn.report_dump.data["Item"]);
|
|
|
|
var data = wn.report_dump.data["Stock Ledger Entry"];
|
|
|
|
var out = [];
|
2012-09-18 13:22:05 +00:00
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
var opening = {
|
|
|
|
item_code: "On " + dateutil.str_to_user(this.from_date), qty: 0.0, balance: 0.0,
|
2012-09-21 14:16:24 +00:00
|
|
|
id:"_opening", _show: true, _style: "font-weight: bold", balance_value: 0.0
|
2012-09-20 13:33:14 +00:00
|
|
|
}
|
|
|
|
var total_in = {
|
2012-09-21 14:16:24 +00:00
|
|
|
item_code: "Total In", qty: 0.0, balance: 0.0, balance_value: 0.0,
|
2012-09-20 13:33:14 +00:00
|
|
|
id:"_total_in", _show: true, _style: "font-weight: bold"
|
|
|
|
}
|
|
|
|
var total_out = {
|
2012-09-21 14:16:24 +00:00
|
|
|
item_code: "Total Out", qty: 0.0, balance: 0.0, balance_value: 0.0,
|
2012-09-20 13:33:14 +00:00
|
|
|
id:"_total_out", _show: true, _style: "font-weight: bold"
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear balance
|
2012-09-21 14:16:24 +00:00
|
|
|
$.each(wn.report_dump.data["Item"], function(i, item) {
|
|
|
|
item.balance = item.balance_value = 0.0;
|
|
|
|
});
|
|
|
|
|
|
|
|
// initialize warehouse-item map
|
|
|
|
this.item_warehouse = {};
|
2013-03-08 07:16:48 +00:00
|
|
|
this.serialized_buying_rates = this.get_serialized_buying_rates();
|
2013-01-15 06:28:19 +00:00
|
|
|
var from_datetime = dateutil.str_to_obj(me.from_date + " 00:00:00");
|
|
|
|
var to_datetime = dateutil.str_to_obj(me.to_date + " 23:59:59");
|
2012-09-20 13:33:14 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
for(var i=0, j=data.length; i<j; i++) {
|
|
|
|
var sl = data[i];
|
2012-09-21 14:16:24 +00:00
|
|
|
var item = me.item_by_name[sl.item_code]
|
|
|
|
var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
|
|
|
|
sl.description = item.description;
|
2013-01-15 06:28:19 +00:00
|
|
|
sl.posting_datetime = sl.posting_date + " " + (sl.posting_time || "00:00:00");
|
2012-12-25 12:34:10 +00:00
|
|
|
sl.brand = item.brand;
|
2012-09-20 13:33:14 +00:00
|
|
|
var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
|
2012-09-18 13:22:05 +00:00
|
|
|
|
2012-09-21 14:16:24 +00:00
|
|
|
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);
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
// opening, transactions, closing, total in, total out
|
2013-01-15 06:28:19 +00:00
|
|
|
var before_end = posting_datetime <= to_datetime;
|
2012-09-20 13:33:14 +00:00
|
|
|
if((!me.is_default("item_code") ? me.apply_filter(sl, "item_code") : true)
|
2012-12-25 12:34:10 +00:00
|
|
|
&& me.apply_filter(sl, "warehouse") && me.apply_filter(sl, "voucher_no")
|
|
|
|
&& me.apply_filter(sl, "brand")) {
|
2013-01-15 06:28:19 +00:00
|
|
|
if(posting_datetime < from_datetime) {
|
2012-09-20 13:33:14 +00:00
|
|
|
opening.balance += sl.qty;
|
2012-09-21 14:16:24 +00:00
|
|
|
opening.balance_value += value_diff;
|
2012-09-20 13:33:14 +00:00
|
|
|
} else if(before_end) {
|
2012-09-21 14:16:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-09-18 13:22:05 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 13:33:14 +00:00
|
|
|
|
|
|
|
if(!before_end) break;
|
2013-01-15 06:28:19 +00:00
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
// apply filters
|
|
|
|
if(me.apply_filters(sl)) {
|
|
|
|
out.push(sl);
|
2012-09-18 13:22:05 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 13:33:14 +00:00
|
|
|
// 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;
|
2012-09-21 14:16:24 +00:00
|
|
|
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;
|
2012-09-19 11:11:19 +00:00
|
|
|
}
|
2012-09-20 13:33:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 08:27:54 +00:00
|
|
|
if(me.item_code && !me.voucher_no) {
|
2012-09-20 13:33:14 +00:00
|
|
|
var closing = {
|
|
|
|
item_code: "On " + dateutil.str_to_user(this.to_date),
|
2012-09-24 12:33:35 +00:00
|
|
|
balance: (out.length ? out[out.length-1].balance : 0), qty: 0,
|
|
|
|
balance_value: (out.length ? out[out.length-1].balance_value : 0),
|
2012-09-20 13:33:14 +00:00
|
|
|
id:"_closing", _show: true, _style: "font-weight: bold"
|
|
|
|
};
|
2012-09-21 14:16:24 +00:00
|
|
|
total_out.balance_value = -total_out.balance_value;
|
2012-09-20 13:33:14 +00:00
|
|
|
var out = [opening].concat(out).concat([total_in, total_out, closing]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.data = out;
|
|
|
|
},
|
|
|
|
get_plot_data: function() {
|
|
|
|
var data = [];
|
|
|
|
var me = this;
|
|
|
|
if(me.hide_balance) return false;
|
|
|
|
data.push({
|
|
|
|
label: me.item_code,
|
|
|
|
data: [[dateutil.str_to_obj(me.from_date).getTime(), me.data[0].balance]]
|
|
|
|
.concat($.map(me.data, function(col, idx) {
|
|
|
|
if (col.posting_datetime) {
|
|
|
|
return [[dateutil.str_to_obj(col.posting_datetime).getTime(), col.balance - col.qty],
|
|
|
|
[dateutil.str_to_obj(col.posting_datetime).getTime(), col.balance]]
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})).concat([
|
|
|
|
// closing
|
|
|
|
[dateutil.str_to_obj(me.to_date).getTime(), me.data[me.data.length - 1].balance]
|
|
|
|
]),
|
|
|
|
points: {show: true},
|
|
|
|
lines: {show: true, fill: true},
|
|
|
|
});
|
|
|
|
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_tooltip_text: function(label, x, y) {
|
|
|
|
var d = new Date(x);
|
|
|
|
var date = dateutil.obj_to_user(d) + " " + d.getHours() + ":" + d.getMinutes();
|
2013-01-23 14:09:51 +00:00
|
|
|
var value = format_number(y);
|
2012-09-20 13:33:14 +00:00
|
|
|
return value.bold() + " on " + date;
|
|
|
|
}
|
|
|
|
});
|