Merge branch 'master' of https://github.com/webnotes/erpnext
diff --git a/accounts/page/general_ledger/general_ledger.js b/accounts/page/general_ledger/general_ledger.js
index a462b70..dfba26c 100644
--- a/accounts/page/general_ledger/general_ledger.js
+++ b/accounts/page/general_ledger/general_ledger.js
@@ -63,11 +63,21 @@
 					open_btn: true,
 					doctype: "dataContext.voucher_type"
 				}},
+			{id: "against_voucher_type", name: "Against Voucher Type", 
+				field: "against_voucher_type", width: 120},
+			{id: "against_voucher", name: "Against Voucher", 
+				field: "against_voucher", width: 160,
+				link_formatter: {
+					filter_input: "against_voucher",
+					open_btn: true,
+					doctype: "dataContext.against_voucher_type"
+				}},
 			{id: "remarks", name: "Remarks", field: "remarks", width: 200,
 				formatter: this.text_formatter},
 				
 		];
 	},
+	
 	filters: [
 		{fieldtype:"Select", label: "Company", link:"Company", default_value: "Select Company...",
 			filter: function(val, item, opts) {
@@ -96,6 +106,7 @@
 			return dateutil.str_to_obj(val) >= dateutil.str_to_obj(item.posting_date);
 		}},
 		{fieldtype: "Check", label: "Group by Ledger"},
+		{fieldtype: "Check", label: "Group by Voucher"},
 		{fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"},
 		{fieldtype:"Button", label: "Reset Filters"}
 	],
@@ -116,9 +127,14 @@
 			me.filter_inputs.group_by_ledger
 				.parent().toggle(!!(me.account_by_name[$(this).val()] 
 					&& me.account_by_name[$(this).val()].group_or_ledger==="Group"));
+					
+			me.filter_inputs.group_by_voucher
+				.parent().toggle(!!(me.account_by_name[$(this).val()] 
+					&& me.account_by_name[$(this).val()].group_or_ledger==="Ledger"));
 		});
 		
 		this.trigger_refresh_on_change(["group_by_ledger"]);
+		this.trigger_refresh_on_change(["group_by_voucher"]);
 	},
 	setup_account_filter: function(company_filter) {
 		var me = this;
@@ -139,12 +155,14 @@
 	init_filter_values: function() {
 		this._super();
 		this.filter_inputs.group_by_ledger.parent().toggle(false);
+		this.filter_inputs.group_by_voucher.parent().toggle(false);
 		this.filter_inputs.company.change();
 		this.filter_inputs.account.change();
 	},
 	apply_filters_from_route: function() {
 		this._super();
 		this.filter_inputs.group_by_ledger.parent().toggle(false);
+		this.filter_inputs.group_by_voucher.parent().toggle(false);
 		this.filter_inputs.company.change();
 		this.filter_inputs.account.change();
 	},
@@ -196,13 +214,21 @@
 				if(!grouped_ledgers[item.account]) {
 					grouped_ledgers[item.account] = {
 						entries: [],
+						entries_group_by_voucher: {},
 						opening: me.make_summary_row("Opening", item.account),
 						totals: me.make_summary_row("Totals", item.account),
 						closing: me.make_summary_row("Closing (Opening + Totals)",
 							item.account)
 					};
 				}
-
+				
+				if(!grouped_ledgers[item.account].entries_group_by_voucher[item.voucher_no]) {
+					grouped_ledgers[item.account].entries_group_by_voucher[item.voucher_no] = {
+						entries: [],
+						totals: me.make_summary_row("Totals", item.voucher_no)
+					}
+				}
+				
 				if(date < from_date || item.is_opening=="Yes") {
 					opening.debit += item.debit;
 					opening.credit += item.credit;
@@ -215,6 +241,11 @@
 
 					grouped_ledgers[item.account].totals.debit += item.debit;
 					grouped_ledgers[item.account].totals.credit += item.credit;
+					
+					grouped_ledgers[item.account].entries_group_by_voucher[item.voucher_no]
+						.totals.debit += item.debit;
+					grouped_ledgers[item.account].entries_group_by_voucher[item.voucher_no]
+						.totals.credit += item.credit;
 				}
 				if(item.account) {
 					item.against_account = me.voucher_accounts[item.voucher_type + ":"
@@ -224,6 +255,9 @@
 				if(me.apply_filters(item) && item.is_opening=="No") {
 					out.push(item);
 					grouped_ledgers[item.account].entries.push(item);
+					
+					grouped_ledgers[item.account].entries_group_by_voucher[item.voucher_no]
+						.entries.push(item);
 				}
 			}
 		});
@@ -241,6 +275,11 @@
 					out = this.group_data_by_ledger(grouped_ledgers);
 			}
 			
+			if(this.account_by_name[this.account].group_or_ledger==="Ledger"
+				&& this.group_by_voucher) {
+					out = this.group_data_by_voucher(grouped_ledgers);
+			}
+			
 			opening = me.get_balance(me.account_by_name[me.account].debit_or_credit, opening)
 			closing = me.get_balance(me.account_by_name[me.account].debit_or_credit, closing)
 			
@@ -283,6 +322,28 @@
 		return [{id: "_blank_first", _no_format: true, debit: "", credit: ""}].concat(out);
 	},
 	
+	group_data_by_voucher: function(grouped_ledgers) {
+		var me = this;
+		var out = []
+		$.each(Object.keys(grouped_ledgers).sort(), function(i, account) {
+			if(grouped_ledgers[account].entries.length) {
+				$.each(Object.keys(grouped_ledgers[account].entries_group_by_voucher).sort(),
+				 	function(j, voucher) {
+						if(grouped_ledgers[account].entries_group_by_voucher[voucher]
+							.entries.length) {
+							out = out.concat(grouped_ledgers[account]
+								.entries_group_by_voucher[voucher].entries)
+								.concat([
+									grouped_ledgers[account].entries_group_by_voucher[voucher]
+									.totals, {id: "_blank" + j, _no_format: true, 
+									debit: "", credit: ""}]);
+						}
+				});
+			}
+		});
+		return [{id: "_blank_first", _no_format: true, debit: "", credit: ""}].concat(out);
+	},
+	
 	get_balance: function(debit_or_credit, balance) {
 		if(debit_or_credit == "Debit") {
 			balance.debit -= balance.credit; balance.credit = 0;
diff --git a/buying/doctype/purchase_common/purchase_common.js b/buying/doctype/purchase_common/purchase_common.js
index dacee80..9e291f5 100644
--- a/buying/doctype/purchase_common/purchase_common.js
+++ b/buying/doctype/purchase_common/purchase_common.js
@@ -128,23 +128,27 @@
 	},
 	
 	currency: function() {
-		this.set_dynamic_labels();
+		if(this.frm.doc.currency === this.get_company_currency())
+			this.frm.set_value("conversion_rate", 1.0);
+		
+		this.price_list_currency();
 	},
 	
 	company: function() {
-		this.set_dynamic_labels();
+		if(this.frm.fields_dict.currency)
+			this.set_dynamic_labels();
 	},
 	
 	price_list_currency: function() {
 		this.frm.toggle_reqd("plc_conversion_rate",
 			!!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
 		
-		this.set_dynamic_labels();
-				
 		if(this.frm.doc.price_list_currency === this.get_company_currency())
 			this.frm.set_value("plc_conversion_rate", 1.0);
 		else if(this.frm.doc.price_list_currency === this.frm.doc.currency)
 			this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate || 1.0);		
+		
+		this.set_dynamic_labels();
 	},
 	
 	set_dynamic_labels: function(doc, dt, dn) {
diff --git a/controllers/trends.py b/controllers/trends.py
index 2cdccb1..eaaf4d1 100644
--- a/controllers/trends.py
+++ b/controllers/trends.py
@@ -89,9 +89,10 @@
 			row = webnotes.conn.sql("""select DISTINCT(%s) from `tab%s` t1, `tab%s Item` t2 %s
 						where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s 
 						and t1.docstatus = 1 and %s = %s 
-					"""%(sel_col,  conditions["trans"],  conditions["trans"], conditions["addl_tables"], "%s", "%s",
-				 	conditions["group_by"], "%s"),	(filters.get("company"), filters.get("fiscal_year"), 
-				 	data1[d][0]), as_list=1)
+					""" % 
+					(sel_col,  conditions["trans"],  conditions["trans"], conditions["addl_tables"], 
+						"%s", "%s", conditions["group_by"], "%s"),
+					(filters.get("company"), filters.get("fiscal_year"), data1[d][0]), as_list=1)
 
 			for i in range(len(row)):
 				des = ['' for q in range(len(conditions["columns"]))]
@@ -100,9 +101,12 @@
 				row1 = webnotes.conn.sql(""" select %s , %s from `tab%s` t1, `tab%s Item` t2 %s
 							where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s 
 							and t1.docstatus = 1 and %s = %s and %s = %s 
-						""" % (sel_col, conditions["period_wise_select"], conditions["trans"],  conditions["trans"], 
-						conditions["addl_tables"], "%s", "%s", sel_col, "%s", conditions["group_by"], "%s"), 
-						(filters.get("company"), filters.get("fiscal_year"), row[i][0], data1[d][0]), as_list=1)
+						""" % 
+						(sel_col, conditions["period_wise_select"], conditions["trans"], 
+						 	conditions["trans"], conditions["addl_tables"], "%s", "%s", sel_col, 
+							"%s", conditions["group_by"], "%s"), 
+						(filters.get("company"), filters.get("fiscal_year"), row[i][0], 
+							data1[d][0]), as_list=1)
 
 				des[ind] = row[i]
 				for j in range(1,len(conditions["columns"])-inc):	
@@ -114,9 +118,10 @@
 					where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s and 
 					t1.docstatus = 1 %s 
 					group by %s	
-				"""%(query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"], "%s", 
-					"%s", cond,conditions["group_by"]), (filters.get("company"), 
-					filters.get("fiscal_year")), as_list=1)
+				""" % 
+				(query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"], 
+					"%s", "%s", cond,conditions["group_by"]), 
+				(filters.get("company"), filters.get("fiscal_year")), as_list=1)
 
 	return data
 
@@ -138,7 +143,8 @@
 			get_period_wise_columns(dt, filters.get("period"), pwc)
 			query_details = get_period_wise_query(dt, trans_date, query_details)
 	else:
-		pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"]
+		pwc = [filters.get("fiscal_year") + " (Qty):Float:120", 
+			filters.get("fiscal_year") + " (Amt):Currency:120"]
 		query_details = " SUM(t2.qty), SUM(t1.grand_total),"
 
 	query_details += 'SUM(t2.qty), SUM(t1.grand_total)'
@@ -153,10 +159,9 @@
 			get_mon(bet_dates[0]) + "-" + get_mon(bet_dates[1]) + " (Amt):Currency:120"]
 
 def get_period_wise_query(bet_dates, trans_date, query_details):
-
 	query_details += """SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)), 
 					SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t1.grand_total, NULL)),
-				"""%{"trans_date": trans_date, "sd": bet_dates[0],"ed": bet_dates[1]}
+				""" % {"trans_date": trans_date, "sd": bet_dates[0],"ed": bet_dates[1]}
 	return query_details
 
 def get_period_date_ranges(period, fiscal_year):
diff --git a/patches/september_2012/repost_stock.py b/patches/september_2012/repost_stock.py
index 9720701..deb2186 100644
--- a/patches/september_2012/repost_stock.py
+++ b/patches/september_2012/repost_stock.py
@@ -22,7 +22,7 @@
 	i=0
 	for d in res:
 	    try:
-	        update_entries_after({ "item_code": d[0], "warehouse": d[1]	})
+			update_entries_after({ "item_code": d[0], "warehouse": d[1]})
 	    except:
 	        pass
 	    i += 1
diff --git a/startup/report_data_map.py b/startup/report_data_map.py
index 501db4f..ae3a53f 100644
--- a/startup/report_data_map.py
+++ b/startup/report_data_map.py
@@ -47,8 +47,9 @@
 		"order_by": "lft"
 	},
 	"GL Entry": {
-		"columns": ["name", "account", "posting_date", "cost_center", "debit", "credit", "is_opening",
-			"company", "voucher_type", "voucher_no", "remarks"],
+		"columns": ["name", "account", "posting_date", "cost_center", "debit", "credit", 
+			"is_opening", "company", "voucher_type", "voucher_no", "remarks",
+			"against_voucher_type", "against_voucher"],
 		"conditions": ["ifnull(is_cancelled, 'No')='No'"],
 		"order_by": "posting_date, account",
 		"links": {