Stock Ageing & release of Analytics
diff --git a/erpnext/accounts/page/accounts_home/accounts_home.html b/erpnext/accounts/page/accounts_home/accounts_home.html
index 5e1b1c8..11cf126 100644
--- a/erpnext/accounts/page/accounts_home/accounts_home.html
+++ b/erpnext/accounts/page/accounts_home/accounts_home.html
@@ -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>
diff --git a/erpnext/accounts/page/financial_analytics/financial_analytics.js b/erpnext/accounts/page/financial_analytics/financial_analytics.js
index 6fbecd6..95350a0 100644
--- a/erpnext/accounts/page/financial_analytics/financial_analytics.js
+++ b/erpnext/accounts/page/financial_analytics/financial_analytics.js
@@ -32,7 +32,7 @@
 				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 @@
 		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 @@
 	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;
 				}
-				
-			} else if(account.is_pl_account!='Yes' 
-				&& (posting_date < dateutil.str_to_obj(me.from_date))) {
-				me.add_balance('opening', account, gl);
+			})();
+
+			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);
+				}
 			}
 		});
 
 		// 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 @@
 	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 {
diff --git a/erpnext/accounts/page/general_ledger/general_ledger.js b/erpnext/accounts/page/general_ledger/general_ledger.js
index a47c076..cbe88fb 100644
--- a/erpnext/accounts/page/general_ledger/general_ledger.js
+++ b/erpnext/accounts/page/general_ledger/general_ledger.js
@@ -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 @@
 		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 @@
 					link_formatter: {
 						filter_input: "account",
 						open_btn: true,
-						doctype: '"Account"'
 					}},
 				{id: "debit", name: "Debit", field: "debit", width: 100,
 					formatter: this.currency_formatter},
diff --git a/erpnext/buying/page/buying_home/buying_home.html b/erpnext/buying/page/buying_home/buying_home.html
index c2eead1..384a77e 100644
--- a/erpnext/buying/page/buying_home/buying_home.html
+++ b/erpnext/buying/page/buying_home/buying_home.html
@@ -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>
diff --git a/erpnext/buying/page/purchase_analytics/__init__.py b/erpnext/buying/page/purchase_analytics/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/buying/page/purchase_analytics/__init__.py
diff --git a/erpnext/buying/page/purchase_analytics/purchase_analytics.js b/erpnext/buying/page/purchase_analytics/purchase_analytics.js
new file mode 100644
index 0000000..ac0e41f
--- /dev/null
+++ b/erpnext/buying/page/purchase_analytics/purchase_analytics.js
@@ -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]]];
+	}
+});
\ No newline at end of file
diff --git a/erpnext/buying/page/purchase_analytics/purchase_analytics.txt b/erpnext/buying/page/purchase_analytics/purchase_analytics.txt
new file mode 100644
index 0000000..71d1c9b
--- /dev/null
+++ b/erpnext/buying/page/purchase_analytics/purchase_analytics.txt
@@ -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'
+	}
+]
\ No newline at end of file
diff --git a/erpnext/patches/september_2012/plot_patch.py b/erpnext/patches/september_2012/plot_patch.py
index fda0a27..822194d 100644
--- a/erpnext/patches/september_2012/plot_patch.py
+++ b/erpnext/patches/september_2012/plot_patch.py
@@ -3,6 +3,7 @@
 	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 @@
 	rebuild_tree("Account", "parent_account")
 	rebuild_tree("Cost Center", "parent_cost_center")
 	rebuild_tree("Sales Person", "parent_sales_person")
-	
\ No newline at end of file
+	
+def add_analytics_role():
+	from webnotes.model.doc import Document
+	Document("Role", fielddata={
+		"name": "Analytics",
+		"role_name": "Analytics",
+		"module": "Setup",
+	}).save(1);
\ No newline at end of file
diff --git a/erpnext/selling/page/sales_analytics/sales_analytics.js b/erpnext/selling/page/sales_analytics/sales_analytics.js
index 8a0d9b2..86ab0eb 100644
--- a/erpnext/selling/page/sales_analytics/sales_analytics.js
+++ b/erpnext/selling/page/sales_analytics/sales_analytics.js
@@ -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 @@
 	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 @@
 			{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 @@
 	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 @@
 		}
 
 	},
-	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);
diff --git a/erpnext/selling/page/selling_home/selling_home.html b/erpnext/selling/page/selling_home/selling_home.html
index 164d365..1917b01 100644
--- a/erpnext/selling/page/selling_home/selling_home.html
+++ b/erpnext/selling/page/selling_home/selling_home.html
@@ -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>
diff --git a/erpnext/setup/__init__.py b/erpnext/setup/__init__.py
index dfc93b5..7492052 100644
--- a/erpnext/setup/__init__.py
+++ b/erpnext/setup/__init__.py
@@ -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'}
 ]
diff --git a/erpnext/startup/js/modules.js b/erpnext/startup/js/modules.js
index a3e7ee8..71d012e 100644
--- a/erpnext/startup/js/modules.js
+++ b/erpnext/startup/js/modules.js
@@ -55,7 +55,17 @@
 			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
 
diff --git a/erpnext/startup/report_data_map.py b/erpnext/startup/report_data_map.py
index 0de1e73..b2be566 100644
--- a/erpnext/startup/report_data_map.py
+++ b/erpnext/startup/report_data_map.py
@@ -36,8 +36,7 @@
 		"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 @@
 		"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 @@
 			"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"]
+		}
 	}
+	
 }
diff --git a/erpnext/stock/page/stock_ageing/__init__.py b/erpnext/stock/page/stock_ageing/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/stock/page/stock_ageing/__init__.py
diff --git a/erpnext/stock/page/stock_ageing/stock_ageing.js b/erpnext/stock/page/stock_ageing/stock_ageing.js
new file mode 100644
index 0000000..39c2613
--- /dev/null
+++ b/erpnext/stock/page/stock_ageing/stock_ageing.js
@@ -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
+			}
+		}
+	}	
+});
\ No newline at end of file
diff --git a/erpnext/stock/page/stock_ageing/stock_ageing.txt b/erpnext/stock/page/stock_ageing/stock_ageing.txt
new file mode 100644
index 0000000..bda41e5
--- /dev/null
+++ b/erpnext/stock/page/stock_ageing/stock_ageing.txt
@@ -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'
+	}
+]
\ No newline at end of file
diff --git a/erpnext/stock/page/stock_analytics/stock_analytics.js b/erpnext/stock/page/stock_analytics/stock_analytics.js
index 42b60b2..1989043 100644
--- a/erpnext/stock/page/stock_analytics/stock_analytics.js
+++ b/erpnext/stock/page/stock_analytics/stock_analytics.js
@@ -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 @@
 	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 @@
 			{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 @@
 		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 @@
 		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 @@
 				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 @@
 			}
 		}
 	},
-	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;
 
diff --git a/erpnext/stock/page/stock_home/stock_home.html b/erpnext/stock/page/stock_home/stock_home.html
index 0939a77..b3a2848 100644
--- a/erpnext/stock/page/stock_home/stock_home.html
+++ b/erpnext/stock/page/stock_home/stock_home.html
@@ -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>
diff --git a/erpnext/stock/page/stock_ledger/stock_ledger.js b/erpnext/stock/page/stock_ledger/stock_ledger.js
index deb3743..f3b7a66 100644
--- a/erpnext/stock/page/stock_ledger/stock_ledger.js
+++ b/erpnext/stock/page/stock_ledger/stock_ledger.js
@@ -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 @@
 	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 @@
 				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 @@
 
 		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 @@
 			// 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 @@
 			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]);
 		}
 		
diff --git a/public/js/all-app.js b/public/js/all-app.js
index d6c99a0..342b51d 100644
--- a/public/js/all-app.js
+++ b/public/js/all-app.js
@@ -1076,9 +1076,8 @@
  *	lib/js/wn/views/grid_report.js
  */
 wn.provide("wn.report_dump");$.extend(wn.report_dump,{data:{},with_data:function(doctypes,callback,progress_bar){var missing=[];$.each(doctypes,function(i,v){if(!wn.report_dump.data[v])missing.push(v);})
-if(missing.length){wn.call({method:"webnotes.widgets.report_dump.get_data",args:{doctypes:doctypes,missing:missing},callback:function(r){$.each(r.message,function(doctype,doctype_data){var data=[];$.each(doctype_data.data,function(i,d){var row={};$.each(doctype_data.columns,function(idx,col){row[col]=d[idx];});row.id=row.name||doctype+"-"+i
-data.push(row);});wn.report_dump.data[doctype]=data;});$.each(r.message,function(doctype,doctype_data){if(doctype_data.links){$.each(wn.report_dump.data[doctype],function(row_idx,row){$.each(doctype_data.links,function(link_key,link){if(wn.report_dump.data[link[0]][row[link_key]]){row[link_key]=wn.report_dump.data[link[0]][row[link_key]][link[1]];}else{row[link_key]=null;}})})}});callback();},progress_bar:progress_bar})}else{callback();}}});wn.provide("wn.views");wn.views.GridReport=Class.extend({init:function(opts){this.filter_inputs={};this.preset_checks=[];this.tree_grid={show:false};$.extend(this,opts);this.wrapper=$('<div>').appendTo(this.parent);if(this.filters){this.make_filters();}
-this.make_waiting();this.import_slickgrid();var me=this;this.get_data();},bind_show:function(){var me=this;$(this.page).bind('show',function(){wn.cur_grid_report=me;me.apply_filters_from_route();me.refresh();});},get_data:function(){var me=this;wn.report_dump.with_data(this.doctypes,function(){me.setup_filters();me.init_filter_values();me.refresh();},this.wrapper.find(".progress .bar"));},setup_filters:function(){var me=this;$.each(me.filter_inputs,function(i,v){var opts=v.get(0).opts;if(opts.fieldtype=="Select"&&inList(me.doctypes,opts.link)){$(v).add_options($.map(wn.report_dump.data[opts.link],function(d){return d.name;}));}});this.filter_inputs.refresh&&this.filter_inputs.refresh.click(function(){me.set_route();});this.filter_inputs.reset_filters&&this.filter_inputs.reset_filters.click(function(){me.init_filter_values();me.set_route();});},init_filter_values:function(){var me=this;$.each(this.filter_inputs,function(key,filter){var opts=filter.get(0).opts;if(sys_defaults[key]){filter.val(sys_defaults[key]);}else if(opts.fieldtype=='Select'){filter.get(0).selectedIndex=0;}else if(opts.fieldtype=='Data'){filter.val("");}})
+if(missing.length){wn.call({method:"webnotes.widgets.report_dump.get_data",args:{doctypes:doctypes,missing:missing},callback:function(r){$.each(r.message,function(doctype,doctype_data){var data=[];$.each(doctype_data.data,function(i,d){var row={};$.each(doctype_data.columns,function(idx,col){row[col]=d[idx];});row.id=row.name||doctype+"-"+i;row.doctype=doctype;data.push(row);});wn.report_dump.data[doctype]=data;});$.each(r.message,function(doctype,doctype_data){if(doctype_data.links){$.each(wn.report_dump.data[doctype],function(row_idx,row){$.each(doctype_data.links,function(link_key,link){if(wn.report_dump.data[link[0]][row[link_key]]){row[link_key]=wn.report_dump.data[link[0]][row[link_key]][link[1]];}else{row[link_key]=null;}})})}});callback();},progress_bar:progress_bar})}else{callback();}}});wn.provide("wn.views");wn.views.GridReport=Class.extend({init:function(opts){this.filter_inputs={};this.preset_checks=[];this.tree_grid={show:false};$.extend(this,opts);this.wrapper=$('<div>').appendTo(this.parent);if(this.filters){this.make_filters();}
+this.make_waiting();this.import_slickgrid();var me=this;this.get_data();},bind_show:function(){var me=this;$(this.page).bind('show',function(){wn.cur_grid_report=me;me.apply_filters_from_route();me.refresh();});},get_data:function(){var me=this;wn.report_dump.with_data(this.doctypes,function(){me.setup_filters();me.init_filter_values();me.refresh();},this.wrapper.find(".progress .bar"));},setup_filters:function(){var me=this;$.each(me.filter_inputs,function(i,v){var opts=v.get(0).opts;if(opts.fieldtype=="Select"&&inList(me.doctypes,opts.link)){$(v).add_options($.map(wn.report_dump.data[opts.link],function(d){return d.name;}));}});this.filter_inputs.refresh&&this.filter_inputs.refresh.click(function(){me.set_route();});this.filter_inputs.reset_filters&&this.filter_inputs.reset_filters.click(function(){me.init_filter_values();me.set_route();});this.filter_inputs.range&&this.filter_inputs.range.change(function(){me.set_route();});},init_filter_values:function(){var me=this;$.each(this.filter_inputs,function(key,filter){var opts=filter.get(0).opts;if(sys_defaults[key]){filter.val(sys_defaults[key]);}else if(opts.fieldtype=='Select'){filter.get(0).selectedIndex=0;}else if(opts.fieldtype=='Data'){filter.val("");}})
 if(this.filter_inputs.from_date)
 this.filter_inputs.from_date.val(dateutil.str_to_user(sys_defaults.year_start_date));if(this.filter_inputs.to_date)
 this.filter_inputs.to_date.val(dateutil.str_to_user(sys_defaults.year_end_date));},make_filters:function(){var me=this;$.each(this.filters,function(i,v){v.fieldname=v.fieldname||v.label.replace(/ /g,'_').toLowerCase();var input=null;if(v.fieldtype=='Select'){input=me.appframe.add_select(v.label,v.options||[v.default_value]);}else if(v.fieldtype=='Button'){input=me.appframe.add_button(v.label);if(v.icon){$('<i class="icon '+v.icon+'"></i>').prependTo(input);}}else if(v.fieldtype=='Date'){input=me.appframe.add_date(v.label);}else if(v.fieldtype=='Label'){input=me.appframe.add_label(v.label);}else if(v.fieldtype=='Data'){input=me.appframe.add_data(v.label);}
@@ -1096,19 +1095,12 @@
    <a href="#" class="grid-report-print"><i class="icon icon-print"></i> Print</a> \
    <span style="color: #aaa; margin: 0px 10px;"> | </span> \
    <a href="#" class="grid-report-export"><i class="icon icon-download-alt"></i> Export</a> \
-  </div>').appendTo(this.wrapper);this.wrapper.find(".grid-report-export").click(function(){return me.export();});this.grid_wrapper=$("<div style='height: 500px; border: 1px solid #aaa; \
+  </div>').appendTo(this.wrapper);this.wrapper.find(".grid-report-export").click(function(){return me.export();});this.wrapper.find(".grid-report-print").click(function(){msgprint("Coming Soon");return false;});this.grid_wrapper=$("<div style='height: 500px; border: 1px solid #aaa; \
    background-color: #eee; margin-top: 15px;'>").appendTo(this.wrapper);this.id=wn.dom.set_unique_id(this.grid_wrapper.get(0));$('<div style="margin: 10px 0px; text-align: right; display: none" class="show-zero">\
     <input type="checkbox"> Show rows with zero values\
    </div>').appendTo(this.wrapper);this.bind_show();wn.cur_grid_report=this;this.apply_filters_from_route();$(this.wrapper).trigger('make');},apply_filters_from_route:function(){var hash=decodeURIComponent(window.location.hash);var me=this;if(hash.indexOf('/')!=-1){$.each(hash.split('/').splice(1).join('/').split('&'),function(i,f){var f=f.split("=");if(me.filter_inputs[f[0]]){me.filter_inputs[f[0]].val(decodeURIComponent(f[1]));}else{console.log("Invalid filter: "+f[0]);}});}else{this.init_filter_values();}},set_route:function(){wn.set_route(wn.container.page.page_name,$.map(this.filter_inputs,function(v){var val=v.val();var opts=v.get(0).opts;if(val&&val!=opts.default_value)
 return encodeURIComponent(opts.fieldname)
-+'='+encodeURIComponent(val);}).join('&'))},options:{editable:false,enableColumnReorder:false},render:function(){this.grid=new Slick.Grid("#"+this.id,this.dataView,this.dataview_columns,this.options);var me=this;this.dataView.onRowsChanged.subscribe(function(e,args){me.grid.invalidateRows(args.rows);me.grid.render();});this.dataView.onRowCountChanged.subscribe(function(e,args){me.grid.updateRowCount();me.grid.render();});this.tree_grid.show&&this.add_tree_grid_events();},prepare_data_view:function(){this.dataView=new Slick.Data.DataView({inlineFilters:true});this.dataView.beginUpdate();this.dataView.setItems(this.data);if(this.dataview_filter)this.dataView.setFilter(this.dataview_filter);if(this.tree_grid.show)this.dataView.setFilter(this.tree_dataview_filter);this.dataView.endUpdate();},export:function(){var me=this;var res=[$.map(this.columns,function(v){return v.name;})].concat(this.get_view_data());wn.require("js/lib/downloadify/downloadify.min.js");wn.require("js/lib/downloadify/swfobject.js");var id=wn.dom.set_unique_id();var msgobj=msgprint('<p id="'+id+'">You must have Flash 10 installed to download this file.</p>');Downloadify.create(id,{filename:function(){return me.title+'.csv';},data:function(){return wn.to_csv(res);},swf:'js/lib/downloadify/downloadify.swf',downloadImage:'js/lib/downloadify/download.png',onComplete:function(){msgobj.hide();},onCancel:function(){msgobj.hide();},onError:function(){msgobj.hide();},width:100,height:30,transparent:true,append:false});return false;},add_tree_grid_events:function(){var me=this;this.grid.onClick.subscribe(function(e,args){if($(e.target).hasClass("toggle")){var item=me.dataView.getItem(args.row);if(item){if(!item._collapsed){item._collapsed=true;}else{item._collapsed=false;}
-me.dataView.updateItem(item.id,item);}
-e.stopImmediatePropagation();}});},tree_formatter:function(row,cell,value,columnDef,dataContext){var me=wn.cur_grid_report;value=value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");var data=me.data;var spacer="<span style='display:inline-block;height:1px;width:"+
-(15*dataContext["indent"])+"px'></span>";var idx=me.dataView.getIdxById(dataContext.id);var link=me.tree_grid.formatter(dataContext);if(columnDef.doctype){link+=me.get_link_open_icon(columnDef.doctype,value);}
-if(data[idx+1]&&data[idx+1].indent>data[idx].indent){if(dataContext._collapsed){return spacer+" <span class='toggle expand'></span>&nbsp;"+link;}else{return spacer+" <span class='toggle collapse'></span>&nbsp;"+link;}}else{return spacer+" <span class='toggle'></span>&nbsp;"+link;}},tree_dataview_filter:function(item){var me=wn.cur_grid_report;if(!me.apply_filters(item))return false;var parent=item[me.tree_grid.parent_field];while(parent){if(me.item_by_name[parent]._collapsed){return false;}
-parent=me.parent_map[parent];}
-return true;},prepare_tree:function(item_dt,group_dt){var group_data=wn.report_dump.data[group_dt];var item_data=wn.report_dump.data[item_dt];var me=this;var item_group_map={};var group_ids=$.map(group_data,function(v){return v.id;});$.each(item_data,function(i,item){var parent=item[me.tree_grid.parent_field];if(!item_group_map[parent])item_group_map[parent]=[];if(group_ids.indexOf(item.name)==-1){item_group_map[parent].push(item);}else{msgprint("Ignoring Item "+item.name.bold()+", because a group exists with the same name!");}});var items=[];$.each(group_data,function(i,group){group.is_group=true;items.push(group);items=items.concat(item_group_map[group.name]||[]);});return items;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}}
-d.indent=indent;});},apply_filters:function(item){var filters=this.filter_inputs;if(item._show)return true;for(i in filters){if(!this.apply_filter(item,i))return false;}
++'='+encodeURIComponent(val);}).join('&'))},options:{editable:false,enableColumnReorder:false},render:function(){this.grid=new Slick.Grid("#"+this.id,this.dataView,this.dataview_columns,this.options);var me=this;this.dataView.onRowsChanged.subscribe(function(e,args){me.grid.invalidateRows(args.rows);me.grid.render();});this.dataView.onRowCountChanged.subscribe(function(e,args){me.grid.updateRowCount();me.grid.render();});this.tree_grid.show&&this.add_tree_grid_events();},prepare_data_view:function(){this.dataView=new Slick.Data.DataView({inlineFilters:true});this.dataView.beginUpdate();this.dataView.setItems(this.data);if(this.dataview_filter)this.dataView.setFilter(this.dataview_filter);if(this.tree_grid.show)this.dataView.setFilter(this.tree_dataview_filter);this.dataView.endUpdate();},export:function(){var me=this;var res=[$.map(this.columns,function(v){return v.name;})].concat(this.get_view_data());wn.require("js/lib/downloadify/downloadify.min.js");wn.require("js/lib/downloadify/swfobject.js");var id=wn.dom.set_unique_id();var msgobj=msgprint('<p id="'+id+'">You must have Flash 10 installed to download this file.</p>');Downloadify.create(id,{filename:function(){return me.title+'.csv';},data:function(){return wn.to_csv(res);},swf:'js/lib/downloadify/downloadify.swf',downloadImage:'js/lib/downloadify/download.png',onComplete:function(){msgobj.hide();},onCancel:function(){msgobj.hide();},onError:function(){msgobj.hide();},width:100,height:30,transparent:true,append:false});return false;},apply_filters:function(item){var filters=this.filter_inputs;if(item._show)return true;for(i in filters){if(!this.apply_filter(item,i))return false;}
 return true;},apply_filter:function(item,fieldname){var filter=this.filter_inputs[fieldname].get(0);if(filter.opts.filter){if(!filter.opts.filter(this[filter.opts.fieldname],item,filter.opts,this)){return false;}}
 return true;},apply_zero_filter:function(val,item,opts,me){if(!me.show_zero){for(var i=0,j=me.columns.length;i<j;i++){var col=me.columns[i];if(col.formatter==me.currency_formatter&&!col.hidden){if(flt(item[col.field])>0.001||flt(item[col.field])<-0.001){return true;}}}
 return false;}
@@ -1118,20 +1110,27 @@
       onclick="wn.cur_grid_report.filter_inputs.%(col_name)s.val(\'%(value)s\'); \
        wn.cur_grid_report.set_route(); return false;">\
       %(value)s</a>',{value:value,col_name:link_formatter.filter_input,page_name:wn.container.page.page_name})
-if(link_formatter.open_btn){html+=me.get_link_open_icon(eval(link_formatter.doctype),value);}
+if(link_formatter.open_btn){var doctype=link_formatter.doctype?eval(link_formatter.doctype):dataContext.doctype;html+=me.get_link_open_icon(doctype,value);}
 return html;}}})},get_link_open_icon:function(doctype,name){return repl(' <i class="icon icon-share" style="cursor: pointer;"\
    onclick="wn.set_route(\'Form\', \'%(doctype)s\', \'%(name)s\');">\
   </i>',{name:name,doctype:doctype});},make_date_range_columns:function(){this.columns=[];var me=this;var range=this.filter_inputs.range.val();this.from_date=dateutil.user_to_str(this.filter_inputs.from_date.val());this.to_date=dateutil.user_to_str(this.filter_inputs.to_date.val());var date_diff=dateutil.get_diff(this.to_date,this.from_date);me.column_map={};var add_column=function(date){me.columns.push({id:date,name:dateutil.str_to_user(date),field:date,formatter:me.currency_formatter,width:100});}
 var build_columns=function(condition){for(var i=0;i<date_diff;i++){var date=dateutil.add_days(me.from_date,i);if(!condition)condition=function(){return true;}
 if(condition(date))add_column(date);me.last_date=date;if(me.columns.length){me.column_map[date]=me.columns[me.columns.length-1];}}}
 if(range=='Daily'){build_columns();}else if(range=='Weekly'){build_columns(function(date){if(!me.last_date)return true;return!(dateutil.get_diff(date,me.from_date)%7)});}else if(range=='Monthly'){build_columns(function(date){if(!me.last_date)return true;return dateutil.str_to_obj(me.last_date).getMonth()!=dateutil.str_to_obj(date).getMonth()});}else if(range=='Quarterly'){build_columns(function(date){if(!me.last_date)return true;return dateutil.str_to_obj(date).getDate()==1&&in_list([0,3,6,9],dateutil.str_to_obj(date).getMonth())});}else if(range=='Yearly'){build_columns(function(date){if(!me.last_date)return true;return $.map(wn.report_dump.data['Fiscal Year'],function(v){return date==v.year_start_date?true:null;}).length;});}
-$.each(this.columns,function(i,col){col.name=me.columns[i+1]?dateutil.str_to_user(dateutil.add_days(me.columns[i+1].id,-1)):dateutil.str_to_user(me.to_date);});}});wn.views.GridReportWithPlot=wn.views.GridReport.extend({render_plot:function(){var plot_data=this.get_plot_data?this.get_plot_data():null;if(!plot_data){this.plot_area.toggle(false);return;}
+$.each(this.columns,function(i,col){col.name=me.columns[i+1]?dateutil.str_to_user(dateutil.add_days(me.columns[i+1].id,-1)):dateutil.str_to_user(me.to_date);});},});wn.views.GridReportWithPlot=wn.views.GridReport.extend({render_plot:function(){var plot_data=this.get_plot_data?this.get_plot_data():null;if(!plot_data){this.plot_area.toggle(false);return;}
 wn.require('js/lib/flot/jquery.flot.js');this.plot=$.plot(this.plot_area.toggle(true),plot_data,this.get_plot_options());this.setup_plot_hover();},setup_plot_check:function(){var me=this;me.wrapper.bind('make',function(){me.wrapper.on("click",".plot-check",function(){var checked=$(this).attr("checked");me.item_by_name[$(this).attr("data-id")].checked=checked?true:false;me.render_plot();});});},setup_plot_hover:function(){var me=this;this.tooltip_id=wn.dom.set_unique_id();function showTooltip(x,y,contents){$('<div id="'+me.tooltip_id+'">'+contents+'</div>').css({position:'absolute',display:'none',top:y+5,left:x+5,border:'1px solid #fdd',padding:'2px','background-color':'#fee',opacity:0.80}).appendTo("body").fadeIn(200);}
 this.previousPoint=null;this.wrapper.find('.plot').bind("plothover",function(event,pos,item){if(item){if(me.previousPoint!=item.dataIndex){me.previousPoint=item.dataIndex;$("#"+me.tooltip_id).remove();showTooltip(item.pageX,item.pageY,me.get_tooltip_text(item.series.label,item.datapoint[0],item.datapoint[1]));}}
 else{$("#"+me.tooltip_id).remove();me.previousPoint=null;}});},get_tooltip_text:function(label,x,y){var date=dateutil.obj_to_user(new Date(x));var value=fmt_money(y);return value+" on "+date;},get_view_data:function(){var res=[];var col_map=$.map(this.columns,function(v){return v.field;});for(var i=0,len=this.dataView.getLength();i<len;i++){var d=this.dataView.getItem(i);var row=[];$.each(col_map,function(i,col){var val=d[col];if(val===null||val===undefined){val=""}
 row.push(val);})
 res.push(row);}
-return res;},get_plot_data:function(){var data=[];var me=this;$.each(this.data,function(i,item){if(item.checked){data.push({label:item.name,data:$.map(me.columns,function(col,idx){if(col.formatter==me.currency_formatter&&!col.hidden&&col.plot!==false){return me.get_plot_points(item,col,idx)}}),points:{show:true},lines:{show:true,fill:true},});data[data.length-1].data=[[dateutil.str_to_obj(me.from_date).getTime(),item.opening]].concat(data[data.length-1].data);}});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()}}}})
+return res;},get_plot_data:function(){var data=[];var me=this;$.each(this.data,function(i,item){if(item.checked){data.push({label:item.name,data:$.map(me.columns,function(col,idx){if(col.formatter==me.currency_formatter&&!col.hidden&&col.plot!==false){return me.get_plot_points(item,col,idx)}}),points:{show:true},lines:{show:true,fill:true},});data[data.length-1].data=[[dateutil.str_to_obj(me.from_date).getTime(),item.opening]].concat(data[data.length-1].data);}});return data.length?data:false;},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()}}}});wn.views.TreeGridReport=wn.views.GridReportWithPlot.extend({make_transaction_list: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));});});},add_tree_grid_events:function(){var me=this;this.grid.onClick.subscribe(function(e,args){if($(e.target).hasClass("toggle")){var item=me.dataView.getItem(args.row);if(item){if(!item._collapsed){item._collapsed=true;}else{item._collapsed=false;}
+me.dataView.updateItem(item.id,item);}
+e.stopImmediatePropagation();}});},tree_formatter:function(row,cell,value,columnDef,dataContext){var me=wn.cur_grid_report;value=value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");var data=me.data;var spacer="<span style='display:inline-block;height:1px;width:"+
+(15*dataContext["indent"])+"px'></span>";var idx=me.dataView.getIdxById(dataContext.id);var link=me.tree_grid.formatter(dataContext);if(dataContext.doctype){link+=me.get_link_open_icon(dataContext.doctype,value);}
+if(data[idx+1]&&data[idx+1].indent>data[idx].indent){if(dataContext._collapsed){return spacer+" <span class='toggle expand'></span>&nbsp;"+link;}else{return spacer+" <span class='toggle collapse'></span>&nbsp;"+link;}}else{return spacer+" <span class='toggle'></span>&nbsp;"+link;}},tree_dataview_filter:function(item){var me=wn.cur_grid_report;if(!me.apply_filters(item))return false;var parent=item[me.tree_grid.parent_field];while(parent){if(me.item_by_name[parent]._collapsed){return false;}
+parent=me.parent_map[parent];}
+return true;},prepare_tree:function(item_dt,group_dt){var group_data=wn.report_dump.data[group_dt];var item_data=wn.report_dump.data[item_dt];var me=this;var item_group_map={};var group_ids=$.map(group_data,function(v){return v.id;});$.each(item_data,function(i,item){var parent=item[me.tree_grid.parent_field];if(!item_group_map[parent])item_group_map[parent]=[];if(group_ids.indexOf(item.name)==-1){item_group_map[parent].push(item);}else{msgprint("Ignoring Item "+item.name.bold()+", because a group exists with the same name!");}});var items=[];$.each(group_data,function(i,group){group.is_group=true;items.push(group);items=items.concat(item_group_map[group.name]||[]);});return items;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}}
+d.indent=indent;});},});
 /*
  *	lib/js/legacy/widgets/dialog.js
  */
@@ -2340,7 +2339,7 @@
  *	erpnext/startup/js/modules.js
  */
 wn.provide('erpnext.module_page');erpnext.module_page.setup_page=function(module,wrapper){erpnext.module_page.hide_links(wrapper);erpnext.module_page.make_list(module,wrapper);$(wrapper).find("a[title]").tooltip({delay:{show:500,hide:100}});}
-erpnext.module_page.hide_links=function(wrapper){$(wrapper).find('[href*="List/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[data-doctype]').each(function(){var dt=$(this).attr('data-doctype');if(wn.boot.profile.all_read.indexOf(dt)==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[href*="Form/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});}
+erpnext.module_page.hide_links=function(wrapper){$(wrapper).find('[href*="List/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[data-doctype]').each(function(){var dt=$(this).attr('data-doctype');if(wn.boot.profile.all_read.indexOf(dt)==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[href*="Form/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(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);}});}
 erpnext.module_page.make_list=function(module,wrapper){var $w=$(wrapper).find('.reports-list');var $parent1=$('<div style="width: 45%; float: left; margin-right: 4.5%"></div>').appendTo($w);var $parent2=$('<div style="width: 45%; float: left;"></div>').appendTo($w);wrapper.list1=new wn.ui.Listing({parent:$parent1,method:'utilities.get_sc_list',render_row:function(row,data){if(!data.parent_doc_type)data.parent_doc_type=data.doc_type;$(row).html(repl('<a href="#!Report/%(doc_type)s/%(criteria_name)s" \
     data-doctype="%(parent_doc_type)s">\
     %(criteria_name)s</a>',data))},args:{module:module},no_refresh:true,callback:function(r){erpnext.module_page.hide_links($parent1)}});wrapper.list1.run();wrapper.list2=new wn.ui.Listing({parent:$parent2,method:'utilities.get_report_list',render_row:function(row,data){$(row).html(repl('<a href="#!Report2/%(ref_doctype)s/%(name)s" \
diff --git a/public/js/app/account_tree_grid.js b/public/js/app/account_tree_grid.js
index 56d1c09..e8bf26e 100644
--- a/public/js/app/account_tree_grid.js
+++ b/public/js/app/account_tree_grid.js
@@ -11,14 +11,14 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.	If not, see <http://www.gnu.org/licenses/>.
 
-erpnext.AccountTreeGrid = wn.views.GridReportWithPlot.extend({
+erpnext.AccountTreeGrid = wn.views.TreeGridReport.extend({
 	init: function(wrapper, title) {
 		this._super({
 			title: title,
 			page: wrapper,
 			parent: $(wrapper).find('.layout-main'),
 			appframe: wrapper.appframe,
-			doctypes: ["Company", "Fiscal Year", "Account", "GL Entry"],
+			doctypes: ["Company", "Fiscal Year", "Account", "GL Entry", "Cost Center"],
 			tree_grid: {
 				show: true, 
 				parent_field: "parent_account", 
@@ -111,6 +111,9 @@
 		this.prepare_balances();
 		
 	},
+	init_account: function(d) {
+		this.reset_item_values(d);	
+	},
 
 	prepare_balances: function() {
 		var gl = wn.report_dump.data['GL Entry'];
diff --git a/public/js/app/stock_grid_report.js b/public/js/app/stock_grid_report.js
new file mode 100644
index 0000000..6f86e24
--- /dev/null
+++ b/public/js/app/stock_grid_report.js
@@ -0,0 +1,105 @@
+// 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/>.
+
+erpnext.StockGridReport = wn.views.TreeGridReport.extend({
+	get_item_warehouse: function(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];
+	},
+	
+	get_value_diff: function(wh, sl, is_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, sl.posting_date]);					
+		} 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;
+
+		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;
+	},
+});
\ No newline at end of file