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

@@ -17,6 +17,21 @@
<br>
<h4><a href="#!Accounts Browser/Cost Center">Chart of Cost Centers</a></h4>
<p class="help">Structure cost centers</p>
<br>
<h4><a href="#general-ledger" data-role="Analytics">General Ledger</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">General Ledger Report</p>
<br>
<h4><a href="#trial-balance" data-role="Analytics">Trial Balance</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">Tree view of all Account balances</p>
<br>
<h4><a href="#financial-analytics" data-role="Analytics">Financial Analytics</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">Visual representation of financial trends</p>
</div>
<div style="clear: both"></div>
<hr>

View File

@@ -32,7 +32,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
if(item._show) return true;
// pl or bs
var out = (val=='Profit and Loss') ? item.is_pl_account=='Yes' : item.is_pl_account!='Yes';
var out = (val!='Balance Sheet') ? item.is_pl_account=='Yes' : item.is_pl_account!='Yes';
if(!out) return false;
return me.apply_zero_filter(val, item, opts, me);
@@ -69,7 +69,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
this._super();
this.filter_inputs.pl_or_bs.change(function() {
me.filter_inputs.refresh.click();
});
}).add_options($.map(wn.report_dump.data["Cost Center"], function(v) {return v.name;}));
this.setup_plot_check();
},
init_filter_values: function() {
@@ -79,29 +79,51 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
prepare_balances: function() {
var me = this;
// setup cost center map
if(!this.cost_center_by_name) {
this.cost_center_by_name = this.make_name_map(wn.report_dump.data["Cost Center"]);
}
var cost_center = inList(["Balance Sheet", "Profit and Loss"], this.pl_or_bs)
? null : this.cost_center_by_name[this.pl_or_bs];
$.each(wn.report_dump.data['GL Entry'], function(i, gl) {
var posting_date = dateutil.str_to_obj(gl.posting_date);
var account = me.item_by_name[gl.account];
var col = me.column_map[gl.posting_date];
if(col) {
if(gl.voucher_type=='Period Closing Voucher') {
// period closing voucher not to be added
// to profit and loss accounts (else will become zero!!)
if(account.is_pl_account!='Yes')
me.add_balance(col.field, account, gl);
var filter_by_cost_center = (function() {
if(cost_center) {
if(gl.cost_center) {
var gl_cost_center = me.cost_center_by_name[gl.cost_center];
return gl_cost_center.lft >= cost_center.lft && gl_cost_center.rgt <= cost_center.rgt;
} else {
return false;
}
} else {
me.add_balance(col.field, account, gl);
return true;
}
})();
if(filter_by_cost_center) {
var posting_date = dateutil.str_to_obj(gl.posting_date);
var account = me.item_by_name[gl.account];
var col = me.column_map[gl.posting_date];
if(col) {
if(gl.voucher_type=='Period Closing Voucher') {
// period closing voucher not to be added
// to profit and loss accounts (else will become zero!!)
if(account.is_pl_account!='Yes')
me.add_balance(col.field, account, gl);
} else {
me.add_balance(col.field, account, gl);
}
} else if(account.is_pl_account!='Yes'
&& (posting_date < dateutil.str_to_obj(me.from_date))) {
me.add_balance('opening', account, gl);
}
} else if(account.is_pl_account!='Yes'
&& (posting_date < dateutil.str_to_obj(me.from_date))) {
me.add_balance('opening', account, gl);
}
});
// make balances as cumulative
if(me.filter_inputs.pl_or_bs.val()=='Balance Sheet') {
if(me.pl_or_bs=='Balance Sheet') {
$.each(me.data, function(i, ac) {
if((ac.rgt - ac.lft)==1 && ac.is_pl_account!='Yes') {
var opening = flt(ac.opening);
@@ -135,15 +157,16 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({
get_plot_data: function() {
var data = [];
var me = this;
var pl_or_bs = this.filter_inputs.pl_or_bs.val();
var pl_or_bs = this.pl_or_bs;
$.each(this.data, function(i, account) {
var show = pl_or_bs == "Profit and Loss" ? account.is_pl_account=="Yes" : account.is_pl_account!="Yes";
var show = pl_or_bs != "Balance Sheet" ? account.is_pl_account=="Yes" : account.is_pl_account!="Yes";
if (show && account.checked && me.apply_filter(account, "company")) {
data.push({
label: account.name,
data: $.map(me.columns, function(col, idx) {
if(col.formatter==me.currency_formatter && !col.hidden) {
if (pl_or_bs == "Profit and Loss") {
if (pl_or_bs != "Balance Sheet") {
return [[dateutil.str_to_obj(col.id).getTime(), account[col.field]],
[dateutil.user_to_obj(col.name).getTime(), account[col.field]]];
} else {

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['general-ledger'].onload = function(wrapper) {
wn.ui.make_app_page({
parent: wrapper,
@@ -10,7 +26,7 @@ wn.pages['general-ledger'].onload = function(wrapper) {
page: wrapper,
parent: $(wrapper).find('.layout-main'),
appframe: wrapper.appframe,
doctypes: ["Company", "Account", "GL Entry"],
doctypes: ["Company", "Account", "GL Entry", "Cost Center"],
setup_columns: function() {
this.columns = [
@@ -20,7 +36,6 @@ wn.pages['general-ledger'].onload = function(wrapper) {
link_formatter: {
filter_input: "account",
open_btn: true,
doctype: '"Account"'
}},
{id: "debit", name: "Debit", field: "debit", width: 100,
formatter: this.currency_formatter},

View File

@@ -23,6 +23,11 @@
<br>
<h4><a href="#!List/Address">Address</a></h4>
<p class="help">Address Master</p>
<br>
<h4><a href="#purchase-analytics" data-role="Analytics">Purchase Analytics</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">Purchase trends based on Purchase Invoice</p>
</div>
<div style="clear: both"></div>
<hr>

View File

@@ -0,0 +1,264 @@
// 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['purchase-analytics'].onload = function(wrapper) {
wn.ui.make_app_page({
parent: wrapper,
title: 'Purchase Analytics',
single_column: true
});
new erpnext.PurchaseAnalytics(wrapper);
}
erpnext.PurchaseAnalytics = wn.views.TreeGridReport.extend({
init: function(wrapper) {
this._super({
title: "Purchase Analytics",
page: wrapper,
parent: $(wrapper).find('.layout-main'),
appframe: wrapper.appframe,
doctypes: ["Item", "Item Group", "Supplier", "Supplier Type", "Company",
"Purchase Invoice", "Purchase Invoice Item"],
tree_grid: { show: true }
});
this.tree_grids = {
"Supplier Type": {
label: "Supplier Type / Supplier",
show: true,
item_key: "supplier",
parent_field: "parent_supplier_type",
formatter: function(item) {
// return repl('<a href="#Report2/stock-invoices/customer=%(enc_value)s">%(value)s</a>', {
// value: item.name,
// enc_value: encodeURIComponent(item.name)
// });
return item.name;
}
},
"Supplier": {
label: "Supplier",
show: false,
item_key: "supplier",
formatter: function(item) {
return item.name;
}
},
"Item Group": {
label: "Item",
show: true,
parent_field: "parent_item_group",
item_key: "item_code",
formatter: function(item) {
return item.name;
}
},
"Item": {
label: "Item",
show: false,
item_key: "item_code",
formatter: function(item) {
return item.name;
}
},
}
},
setup_columns: function() {
this.tree_grid = this.tree_grids[this.tree_type];
var std_columns = [
{id: "check", name: "Plot", field: "check", width: 30,
formatter: this.check_formatter},
{id: "name", name: this.tree_grid.label, field: "name", width: 300,
formatter: this.tree_formatter},
{id: "total", name: "Total", field: "total", plot: false,
formatter: this.currency_formatter}
];
this.make_date_range_columns();
this.columns = std_columns.concat(this.columns);
},
filters: [
{fieldtype:"Select", label: "Tree Type", options:["Supplier Type", "Supplier",
"Item Group", "Item"],
filter: function(val, item, opts, me) {
return me.apply_zero_filter(val, item, opts, me);
}},
{fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"]},
{fieldtype:"Select", label: "Company", link:"Company",
default_value: "Select Company..."},
{fieldtype:"Date", label: "From Date"},
{fieldtype:"Label", label: "To"},
{fieldtype:"Date", label: "To Date"},
{fieldtype:"Select", label: "Range",
options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]},
{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.value_or_qty.change(function() {
me.filter_inputs.refresh.click();
});
this.filter_inputs.tree_type.change(function() {
me.filter_inputs.refresh.click();
});
this.show_zero_check()
this.setup_plot_check();
},
init_filter_values: function() {
this._super();
this.filter_inputs.range.val('Weekly');
},
prepare_data: function() {
var me = this;
if (!this.tl) {
this.make_transaction_list("Purchase Invoice", "Purchase Invoice Item");
// add 'Not Set' Supplier & Item
// Add 'All Supplier Types' Supplier Type
// (Supplier / Item are not mandatory!!)
// Set parent supplier type for tree view
$.each(wn.report_dump.data["Supplier Type"], function(i, v) {
v['parent_supplier_type'] = "All Supplier Types"
})
wn.report_dump.data["Supplier Type"] = [{
name: "All Supplier Types",
id: "All Supplier Types",
}].concat(wn.report_dump.data["Supplier Type"]);
wn.report_dump.data["Supplier"].push({
name: "Not Set",
parent_supplier_type: "All Supplier Types",
id: "Not Set",
});
wn.report_dump.data["Item"].push({
name: "Not Set",
parent_item_group: "All Item Groups",
id: "Not Set",
});
}
if(!this.data || me.item_type != me.tree_type) {
if(me.tree_type=='Supplier') {
var items = wn.report_dump.data["Supplier"];
} if(me.tree_type=='Supplier Type') {
var items = this.prepare_tree("Supplier", "Supplier Type");
} else if(me.tree_type=="Item Group") {
var items = this.prepare_tree("Item", "Item Group");
} else if(me.tree_type=="Item") {
var items = wn.report_dump.data["Item"];
}
me.item_type = me.tree_type
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[me.tree_grid.parent_field]) {
me.parent_map[d.name] = d[me.tree_grid.parent_field];
}
me.reset_item_values(d);
});
this.set_indent();
} else {
// otherwise, only reset values
$.each(this.data, function(i, d) {
me.reset_item_values(d);
});
}
this.prepare_balances();
if(me.tree_grid.show) {
this.set_totals(false);
this.update_groups();
} else {
this.set_totals(true);
}
},
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 is_val = this.value_or_qty == 'Value';
$.each(this.tl, function(i, tl) {
if (me.is_default('company') ? true : me.apply_filter(tl, "company")) {
var posting_date = dateutil.str_to_obj(tl.posting_date);
if (posting_date >= from_date && posting_date <= to_date) {
var item = me.item_by_name[tl[me.tree_grid.item_key]] || me.item_by_name['Not Set'];
item[me.column_map[tl.posting_date].field] += (is_val ? tl.amount : tl.qty);
}
}
});
},
update_groups: function() {
var me = this;
$.each(this.data, function(i, item) {
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];
}
});
},
set_totals: function(sort) {
var me = this;
var checked = false;
$.each(this.data, function(i, d) {
d.total = 0.0;
$.each(me.columns, function(i, col) {
if(col.formatter==me.currency_formatter && !col.hidden && col.field!="total")
d.total += d[col.field];
if(d.checked) checked = true;
})
});
if(sort)this.data = this.data.sort(function(a, b) { return b.total - a.total; });
if(!this.checked) {
this.data[0].checked = true;
}
},
get_plot_points: function(item, col, idx) {
return [[dateutil.str_to_obj(col.id).getTime(), item[col.field]],
[dateutil.user_to_obj(col.name).getTime(), item[col.field]]];
}
});

View File

@@ -0,0 +1,28 @@
# Page, purchase-analytics
[
# These values are common in all dictionaries
{
u'creation': '2012-09-21 15:21:10',
u'docstatus': 0,
u'modified': '2012-09-21 15:21:10',
u'modified_by': u'Administrator',
u'owner': u'Administrator'
},
# These values are common for all Page
{
u'doctype': u'Page',
'module': u'Buying',
u'name': u'__common__',
'page_name': u'purchase-analytics',
'standard': u'Yes',
'title': u'Purchase Analytics'
},
# Page, purchase-analytics
{
u'doctype': u'Page',
u'name': u'purchase-analytics'
}
]

View File

@@ -3,6 +3,7 @@ def execute():
set_master_name_in_accounts()
set_customer_in_sales_invoices()
reset_lft_rgt()
add_analytics_role()
def set_master_name_in_accounts():
accounts = webnotes.conn.sql("""select name, account_name, master_type from tabAccount
@@ -29,4 +30,11 @@ def reset_lft_rgt():
rebuild_tree("Account", "parent_account")
rebuild_tree("Cost Center", "parent_cost_center")
rebuild_tree("Sales Person", "parent_sales_person")
def add_analytics_role():
from webnotes.model.doc import Document
Document("Role", fielddata={
"name": "Analytics",
"role_name": "Analytics",
"module": "Setup",
}).save(1);

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['sales-analytics'].onload = function(wrapper) {
wn.ui.make_app_page({
parent: wrapper,
@@ -7,7 +23,7 @@ wn.pages['sales-analytics'].onload = function(wrapper) {
new erpnext.SalesAnalytics(wrapper);
}
erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({
erpnext.SalesAnalytics = wn.views.TreeGridReport.extend({
init: function(wrapper) {
this._super({
title: "Sales Analytics",
@@ -76,7 +92,7 @@ erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({
{id: "check", name: "Plot", field: "check", width: 30,
formatter: this.check_formatter},
{id: "name", name: this.tree_grid.label, field: "name", width: 300,
formatter: this.tree_formatter, doctype: "Customer"},
formatter: this.tree_formatter},
{id: "total", name: "Total", field: "total", plot: false,
formatter: this.currency_formatter}
];
@@ -123,7 +139,7 @@ erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({
prepare_data: function() {
var me = this;
if (!this.tl) {
this.make_tl("Sales Invoice", "Sales Invoice Item");
this.make_transaction_list("Sales Invoice", "Sales Invoice Item");
// add 'Not Set' Customer & Item
// (Customer / Item are not mandatory!!)
@@ -188,20 +204,6 @@ erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({
}
},
make_tl: function(parent_doctype, doctype) {
var me = this;
var tmap = {};
$.each(wn.report_dump.data[doctype], function(i, v) {
if(!tmap[v.parent]) tmap[v.parent] = [];
tmap[v.parent].push(v);
});
this.tl = [];
$.each(wn.report_dump.data[parent_doctype], function(i, parent) {
$.each(tmap[parent.name], function(i, d) {
me.tl.push($.extend(copy_dict(parent), d));
});
});
},
prepare_balances: function() {
var me = this;
var from_date = dateutil.str_to_obj(this.from_date);

View File

@@ -26,6 +26,11 @@
<br>
<h4><a href="#!List/Address">Address</a></h4>
<p class="help">Address Master</p>
<br>
<h4><a href="#sales-analytics" data-role="Analytics">Sales Analytics</a>
<span style="background-color: #fed; font-weight: normal; font-size: 80%">beta</span>
</h4>
<p class="help">Sales trends based on Sales Invoice</p>
</div>
<div style="clear: both"></div>
<hr>

View File

@@ -1,4 +1,5 @@
from __future__ import unicode_literals
install_docs = [
{'doctype':'Role', 'role_name': 'System Manager', 'name': 'System Manager'}
{'doctype':'Role', 'role_name': 'System Manager', 'name': 'System Manager'},
{'doctype':'Role', 'role_name': 'Analytics', 'name': 'Analytics'}
]

View File

@@ -55,7 +55,17 @@ erpnext.module_page.hide_links = function(wrapper) {
var txt = $(this).text();
$(this).parent().css('color', '#999').html(txt);
}
});}
});
// pages
$(wrapper).find('[data-role]').each(function() {
if(!has_common(user_roles, [$(this).attr("data-role"), "System Manager"])) {
var html = $(this).html();
$(this).parent().css('color', '#999');
$(this).replaceWith(html);
}
});
}
// make list of reports

View File

@@ -36,8 +36,7 @@ data_map = {
"order_by": "lft"
},
"Cost Center": {
"columns": ["name", "parent_cost_center", "lft", "rgt", "debit_or_credit",
"company"],
"columns": ["name", "lft", "rgt"],
"order_by": "lft"
},
"GL Entry": {
@@ -47,14 +46,15 @@ data_map = {
"order_by": "posting_date, account",
"links": {
"account": ["Account", "name"],
"company": ["Company", "name"]
"company": ["Company", "name"],
"cost_center": ["Cost Center", "name"]
}
},
# Stock
"Item": {
"columns": ["name", "if(item_name=name, '', item_name) as item_name",
"item_group as parent_item_group", "stock_uom", "brand"],
"item_group as parent_item_group", "stock_uom", "brand", "valuation_method"],
"order_by": "name",
"links": {
"parent_item_group": ["Item Group", "name"],
@@ -115,5 +115,36 @@ data_map = {
"parent": ["Sales Invoice", "name"],
"item_code": ["Item", "name"]
}
},
"Supplier": {
"columns": ["name", "if(supplier_name=name, '', supplier_name) as supplier_name",
"supplier_type as parent_supplier_type"],
"order_by": "name",
"links": {
"parent_supplier_type": ["Supplier Type", "name"],
}
},
"Supplier Type": {
"columns": ["name"],
"order_by": "name"
},
"Purchase Invoice": {
"columns": ["name", "supplier", "posting_date", "company"],
"conditions": ["docstatus=1"],
"order_by": "posting_date",
"links": {
"supplier": ["Supplier", "name"],
"company":["Company", "name"]
}
},
"Purchase Invoice Item": {
"columns": ["parent", "item_code", "qty", "amount"],
"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
"order_by": "parent",
"links": {
"parent": ["Purchase Invoice", "name"],
"item_code": ["Item", "name"]
}
}
}

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]);
}