2013-11-20 07:29:58 +00:00
|
|
|
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
2012-12-25 12:34:10 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.require("assets/erpnext/js/stock_analytics.js");
|
2012-12-25 12:34:10 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.pages['stock-balance'].onload = function(wrapper) {
|
|
|
|
frappe.ui.make_app_page({
|
2012-12-25 12:34:10 +00:00
|
|
|
parent: wrapper,
|
2014-02-14 10:17:51 +00:00
|
|
|
title: frappe._('Stock Balance'),
|
2012-12-25 12:34:10 +00:00
|
|
|
single_column: true
|
|
|
|
});
|
|
|
|
|
|
|
|
new erpnext.StockBalance(wrapper);
|
|
|
|
|
2013-11-13 11:25:38 +00:00
|
|
|
|
2013-04-19 07:31:32 +00:00
|
|
|
wrapper.appframe.add_module_icon("Stock")
|
2013-11-13 11:25:38 +00:00
|
|
|
|
2012-12-25 12:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
erpnext.StockBalance = erpnext.StockAnalytics.extend({
|
|
|
|
init: function(wrapper) {
|
|
|
|
this._super(wrapper, {
|
2014-02-14 10:17:51 +00:00
|
|
|
title: frappe._("Stock Balance"),
|
2012-12-25 12:34:10 +00:00
|
|
|
doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand",
|
2013-11-15 08:08:23 +00:00
|
|
|
"Stock Entry", "Project", "Serial No"],
|
2012-12-25 12:34:10 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
setup_columns: function() {
|
|
|
|
this.columns = [
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "name", name: frappe._("Item"), field: "name", width: 300,
|
2012-12-25 12:34:10 +00:00
|
|
|
formatter: this.tree_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "item_name", name: frappe._("Item Name"), field: "item_name", width: 100},
|
|
|
|
{id: "description", name: frappe._("Description"), field: "description", width: 200,
|
2013-02-26 06:53:39 +00:00
|
|
|
formatter: this.text_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "brand", name: frappe._("Brand"), field: "brand", width: 100},
|
|
|
|
{id: "stock_uom", name: frappe._("UOM"), field: "stock_uom", width: 100},
|
|
|
|
{id: "opening_qty", name: frappe._("Opening Qty"), field: "opening_qty", width: 100,
|
2013-01-22 08:33:34 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "inflow_qty", name: frappe._("In Qty"), field: "inflow_qty", width: 100,
|
2012-12-25 12:34:10 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "outflow_qty", name: frappe._("Out Qty"), field: "outflow_qty", width: 100,
|
2012-12-25 12:34:10 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "closing_qty", name: frappe._("Closing Qty"), field: "closing_qty", width: 100,
|
2013-01-22 08:33:34 +00:00
|
|
|
formatter: this.currency_formatter},
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "opening_value", name: frappe._("Opening Value"), field: "opening_value", width: 100,
|
2012-12-25 12:34:10 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "inflow_value", name: frappe._("In Value"), field: "inflow_value", width: 100,
|
2012-12-25 12:34:10 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "outflow_value", name: frappe._("Out Value"), field: "outflow_value", width: 100,
|
2013-01-22 08:33:34 +00:00
|
|
|
formatter: this.currency_formatter},
|
2014-02-14 10:17:51 +00:00
|
|
|
{id: "closing_value", name: frappe._("Closing Value"), field: "closing_value", width: 100,
|
2013-01-22 08:33:34 +00:00
|
|
|
formatter: this.currency_formatter},
|
2012-12-25 12:34:10 +00:00
|
|
|
];
|
|
|
|
},
|
|
|
|
|
|
|
|
filters: [
|
2014-02-14 10:17:51 +00:00
|
|
|
{fieldtype:"Select", label: frappe._("Brand"), link:"Brand",
|
2012-12-25 12:34:10 +00:00
|
|
|
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"}},
|
2014-02-14 10:17:51 +00:00
|
|
|
{fieldtype:"Select", label: frappe._("Warehouse"), link:"Warehouse",
|
2013-01-22 09:10:20 +00:00
|
|
|
default_value: "Select Warehouse...", 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._("Project"), link:"Project",
|
2013-03-25 11:33:52 +00:00
|
|
|
default_value: "Select Project...", filter: function(val, item, opts, me) {
|
|
|
|
return me.apply_zero_filter(val, item, opts, me);
|
|
|
|
}, link_formatter: {filter_input: "project"}},
|
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:"Button", label: frappe._("Refresh"), icon:"icon-refresh icon-white"},
|
|
|
|
{fieldtype:"Button", label: frappe._("Reset Filters")}
|
2012-12-25 12:34:10 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
setup_plot_check: function() {
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
|
|
|
|
prepare_data: function() {
|
2014-02-14 10:17:51 +00:00
|
|
|
this.stock_entry_map = this.make_name_map(frappe.report_dump.data["Stock Entry"], "name");
|
2012-12-25 12:34:10 +00:00
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
2014-02-14 10:17:51 +00:00
|
|
|
var data = frappe.report_dump.data["Stock Ledger Entry"];
|
2012-12-25 12:34:10 +00:00
|
|
|
|
|
|
|
this.item_warehouse = {};
|
2013-03-08 07:16:48 +00:00
|
|
|
this.serialized_buying_rates = this.get_serialized_buying_rates();
|
2012-12-25 12:34:10 +00:00
|
|
|
|
|
|
|
for(var i=0, j=data.length; i<j; i++) {
|
|
|
|
var sl = data[i];
|
2013-02-28 11:39:08 +00:00
|
|
|
var sl_posting_date = dateutil.str_to_obj(sl.posting_date);
|
2012-12-25 12:34:10 +00:00
|
|
|
|
2013-03-25 11:33:52 +00:00
|
|
|
if((me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) &&
|
|
|
|
(me.is_default("project") ? true : me.project == sl.project)) {
|
2012-12-25 12:34:10 +00:00
|
|
|
var item = me.item_by_name[sl.item_code];
|
2013-01-22 08:33:34 +00:00
|
|
|
var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
|
2013-01-22 09:10:20 +00:00
|
|
|
var valuation_method = item.valuation_method ?
|
|
|
|
item.valuation_method : sys_defaults.valuation_method;
|
|
|
|
var is_fifo = valuation_method == "FIFO";
|
2013-01-22 08:33:34 +00:00
|
|
|
|
|
|
|
var qty_diff = sl.qty;
|
|
|
|
var value_diff = me.get_value_diff(wh, sl, is_fifo);
|
2013-06-26 06:35:37 +00:00
|
|
|
|
2013-02-28 11:39:08 +00:00
|
|
|
if(sl_posting_date < from_date) {
|
2013-01-22 08:33:34 +00:00
|
|
|
item.opening_qty += qty_diff;
|
|
|
|
item.opening_value += value_diff;
|
2013-02-28 11:39:08 +00:00
|
|
|
} else if(sl_posting_date <= to_date) {
|
2012-12-25 12:34:10 +00:00
|
|
|
var ignore_inflow_outflow = this.is_default("warehouse")
|
2012-12-31 07:13:36 +00:00
|
|
|
&& sl.voucher_type=="Stock Entry"
|
|
|
|
&& this.stock_entry_map[sl.voucher_no].purpose=="Material Transfer";
|
2012-12-25 12:34:10 +00:00
|
|
|
|
|
|
|
if(!ignore_inflow_outflow) {
|
2013-01-22 08:33:34 +00:00
|
|
|
if(qty_diff < 0) {
|
|
|
|
item.outflow_qty += Math.abs(qty_diff);
|
|
|
|
} else {
|
|
|
|
item.inflow_qty += qty_diff;
|
|
|
|
}
|
|
|
|
if(value_diff < 0) {
|
|
|
|
item.outflow_value += Math.abs(value_diff);
|
2012-12-25 12:34:10 +00:00
|
|
|
} else {
|
2013-01-22 08:33:34 +00:00
|
|
|
item.inflow_value += value_diff;
|
2012-12-25 12:34:10 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 10:59:45 +00:00
|
|
|
item.closing_qty += qty_diff;
|
|
|
|
item.closing_value += value_diff;
|
|
|
|
}
|
|
|
|
|
2012-12-25 12:34:10 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 07:58:40 +00:00
|
|
|
|
|
|
|
// opening + diff = closing
|
|
|
|
// adding opening, since diff already added to closing
|
|
|
|
$.each(me.item_by_name, function(key, item) {
|
2013-01-22 08:33:34 +00:00
|
|
|
item.closing_qty += item.opening_qty;
|
|
|
|
item.closing_value += item.opening_value;
|
2013-01-07 07:58:40 +00:00
|
|
|
});
|
2012-12-25 12:34:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
update_groups: function() {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
$.each(this.data, function(i, item) {
|
|
|
|
// update groups
|
|
|
|
if(!item.is_group && me.apply_filter(item, "brand")) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// show parent if filtered by brand
|
|
|
|
if(item.brand == me.brand)
|
|
|
|
parent_group._show = true;
|
|
|
|
|
|
|
|
parent = me.parent_map[parent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
get_plot_data: function() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|