Merge branch 'develop'
diff --git a/erpnext/__version__.py b/erpnext/__version__.py
index 158ae35..5440c3d 100644
--- a/erpnext/__version__.py
+++ b/erpnext/__version__.py
@@ -1,2 +1,2 @@
 from __future__ import unicode_literals
-__version__ = '6.21.4'
+__version__ = '6.21.5'
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index 55e846b..4b5ce43 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -356,18 +356,20 @@
 	def set_print_format_fields(self):
 		total_amount = 0.0
 		bank_account_currency = None
-		self.pay_to_recd_from = None
+		pay_to_recd_from = None
 		for d in self.get('accounts'):
 			if d.party_type and d.party:
-				if not self.pay_to_recd_from:
-					self.pay_to_recd_from = frappe.db.get_value(d.party_type, d.party,
+				if not pay_to_recd_from:
+					pay_to_recd_from = frappe.db.get_value(d.party_type, d.party,
 						"customer_name" if d.party_type=="Customer" else "supplier_name")
 
 			elif frappe.db.get_value("Account", d.account, "account_type") in ["Bank", "Cash"]:
 				total_amount += (d.debit_in_account_currency or d.credit_in_account_currency)
 				bank_account_currency = d.account_currency
 
-		if not self.pay_to_recd_from:
+		if pay_to_recd_from:
+			self.pay_to_recd_from = pay_to_recd_from
+		else:
 			total_amount = 0
 
 		self.set_total_amount(total_amount, bank_account_currency)
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 3700590..d42d43e 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -7,7 +7,7 @@
 app_description = """ERP made simple"""
 app_icon = "icon-th"
 app_color = "#e74c3c"
-app_version = "6.21.4"
+app_version = "6.21.5"
 app_email = "info@erpnext.com"
 app_license = "GNU General Public License (v3)"
 source_link = "https://github.com/frappe/erpnext"
diff --git a/erpnext/public/js/pos/pos.js b/erpnext/public/js/pos/pos.js
index db60025..b8a6d32 100644
--- a/erpnext/public/js/pos/pos.js
+++ b/erpnext/public/js/pos/pos.js
@@ -238,6 +238,10 @@
 	},
 	refresh_fields: function() {
 		this.party_field.set_input(this.frm.doc[this.party.toLowerCase()]);
+		this.party_field.frm = this.frm;
+		this.party_field.doctype = this.frm.doctype;
+		this.party_field.docname = this.frm.docname;
+
 		this.wrapper.find('input.discount-percentage').val(this.frm.doc.additional_discount_percentage);
 		this.wrapper.find('input.discount-amount').val(this.frm.doc.discount_amount);
 
@@ -415,7 +419,7 @@
 				// prefer cash payment!
 				var default_mode = me.frm.doc.mode_of_payment ? me.frm.doc.mode_of_payment :
 					me.modes_of_payment.indexOf(__("Cash"))!==-1 ? __("Cash") : undefined;
-					
+
 				// show payment wizard
 				var dialog = new frappe.ui.Dialog({
 					width: 400,
@@ -431,18 +435,18 @@
 						{fieldtype:'Currency', fieldname:'paid_amount', label:__('Amount Paid'),
 							reqd:1, "default": me.frm.doc.grand_total, hidden: 1, change: function() {
 								var values = dialog.get_values();
-								
-								var actual_change = flt(values.paid_amount - values.total_amount, 
+
+								var actual_change = flt(values.paid_amount - values.total_amount,
 									precision("paid_amount"));
-								
+
 								if (actual_change > 0) {
-									var rounded_change = 
-										round_based_on_smallest_currency_fraction(actual_change, 
+									var rounded_change =
+										round_based_on_smallest_currency_fraction(actual_change,
 											me.frm.doc.currency, precision("paid_amount"));
 								} else {
 									var rounded_change = 0;
 								}
-								
+
 								dialog.set_value("change", rounded_change);
 								dialog.get_input("change").trigger("change");
 
diff --git a/erpnext/stock/report/stock_balance/stock_balance.py b/erpnext/stock/report/stock_balance/stock_balance.py
index 7d82da1..0a9abc2 100644
--- a/erpnext/stock/report/stock_balance/stock_balance.py
+++ b/erpnext/stock/report/stock_balance/stock_balance.py
@@ -14,21 +14,19 @@
 	iwb_map = get_item_warehouse_map(filters)
 
 	data = []
-	for company in sorted(iwb_map):
-		for item in sorted(iwb_map[company]):
-			for wh in sorted(iwb_map[company][item]):
-				qty_dict = iwb_map[company][item][wh]
-				data.append([item, item_map[item]["item_name"],
-					item_map[item]["item_group"],
-					item_map[item]["brand"],
-					item_map[item]["description"], wh,
-					item_map[item]["stock_uom"], qty_dict.opening_qty,
-					qty_dict.opening_val, qty_dict.in_qty,
-					qty_dict.in_val, qty_dict.out_qty,
-					qty_dict.out_val, qty_dict.bal_qty,
-					qty_dict.bal_val, qty_dict.val_rate,
-					company
-				])
+	for (company, item, warehouse) in sorted(iwb_map):
+		qty_dict = iwb_map[(company, item, warehouse)]
+		data.append([item, item_map[item]["item_name"],
+			item_map[item]["item_group"],
+			item_map[item]["brand"],
+			item_map[item]["description"], warehouse,
+			item_map[item]["stock_uom"], qty_dict.opening_qty,
+			qty_dict.opening_val, qty_dict.in_qty,
+			qty_dict.in_val, qty_dict.out_qty,
+			qty_dict.out_val, qty_dict.bal_qty,
+			qty_dict.bal_val, qty_dict.val_rate,
+			company
+		])
 
 	return columns, data
 
@@ -36,22 +34,22 @@
 	"""return columns based on filters"""
 
 	columns = [
-		_("Item")+":Link/Item:100", 
-		_("Item Name")+"::150", 
-		_("Item Group")+"::100", 
-		_("Brand")+"::90", 
-		_("Description")+"::140", 
-		_("Warehouse")+":Link/Warehouse:100", 
-		_("Stock UOM")+":Link/UOM:90", 
-		_("Opening Qty")+":Float:100", 
-		_("Opening Value")+":Float:110", 
-		_("In Qty")+":Float:80", 
-		_("In Value")+":Float:80", 
-		_("Out Qty")+":Float:80", 
-		_("Out Value")+":Float:80", 
-		_("Balance Qty")+":Float:100", 
-		_("Balance Value")+":Float:100", 
-		_("Valuation Rate")+":Float:90", 
+		_("Item")+":Link/Item:100",
+		_("Item Name")+"::150",
+		_("Item Group")+"::100",
+		_("Brand")+"::90",
+		_("Description")+"::140",
+		_("Warehouse")+":Link/Warehouse:100",
+		_("Stock UOM")+":Link/UOM:90",
+		_("Opening Qty")+":Float:100",
+		_("Opening Value")+":Float:110",
+		_("In Qty")+":Float:80",
+		_("In Value")+":Float:80",
+		_("Out Qty")+":Float:80",
+		_("Out Value")+":Float:80",
+		_("Balance Qty")+":Float:100",
+		_("Balance Value")+":Float:100",
+		_("Valuation Rate")+":Float:90",
 		_("Company")+":Link/Company:100"
 	]
 
@@ -63,7 +61,7 @@
 		frappe.throw(_("'From Date' is required"))
 
 	if filters.get("to_date"):
-		conditions += " and posting_date <= '%s'" % filters["to_date"]
+		conditions += " and posting_date <= '%s'" % frappe.db.escape(filters["to_date"])
 	else:
 		frappe.throw(_("'To Date' is required"))
 
@@ -76,25 +74,30 @@
 def get_stock_ledger_entries(filters):
 	conditions = get_conditions(filters)
 	return frappe.db.sql("""select item_code, warehouse, posting_date, actual_qty, valuation_rate,
-	company, voucher_type, qty_after_transaction, stock_value_difference
-		from `tabStock Ledger Entry`
+			company, voucher_type, qty_after_transaction, stock_value_difference
+		from `tabStock Ledger Entry` force index (posting_sort_index)
 		where docstatus < 2 %s order by posting_date, posting_time, name""" %
 		conditions, as_dict=1)
 
 def get_item_warehouse_map(filters):
-	sle = get_stock_ledger_entries(filters)
 	iwb_map = {}
+	from_date = getdate(filters["from_date"])
+	to_date = getdate(filters["to_date"])
+
+	sle = get_stock_ledger_entries(filters)
 
 	for d in sle:
-		iwb_map.setdefault(d.company, {}).setdefault(d.item_code, {}).\
-		setdefault(d.warehouse, frappe._dict({\
+		key = (d.company, d.item_code, d.warehouse)
+		if key not in iwb_map:
+			iwb_map[key] = frappe._dict({
 				"opening_qty": 0.0, "opening_val": 0.0,
 				"in_qty": 0.0, "in_val": 0.0,
 				"out_qty": 0.0, "out_val": 0.0,
 				"bal_qty": 0.0, "bal_val": 0.0,
 				"val_rate": 0.0, "uom": None
-			}))
-		qty_dict = iwb_map[d.company][d.item_code][d.warehouse]
+			})
+
+		qty_dict = iwb_map[(d.company, d.item_code, d.warehouse)]
 
 		if d.voucher_type == "Stock Reconciliation":
 			qty_diff = flt(d.qty_after_transaction) - qty_dict.bal_qty
@@ -102,18 +105,19 @@
 			qty_diff = flt(d.actual_qty)
 
 		value_diff = flt(d.stock_value_difference)
-		
-		if d.posting_date < getdate(filters["from_date"]):
+
+		if d.posting_date < from_date:
 			qty_dict.opening_qty += qty_diff
 			qty_dict.opening_val += value_diff
-		elif d.posting_date >= getdate(filters["from_date"]) and d.posting_date <= getdate(filters["to_date"]):
+
+		elif d.posting_date >= from_date and d.posting_date <= to_date:
 			if qty_diff > 0:
 				qty_dict.in_qty += qty_diff
 				qty_dict.in_val += value_diff
 			else:
 				qty_dict.out_qty += abs(qty_diff)
 				qty_dict.out_val += abs(value_diff)
-				
+
 		qty_dict.val_rate = d.valuation_rate
 		qty_dict.bal_qty += qty_diff
 		qty_dict.bal_val += value_diff
diff --git a/setup.py b/setup.py
index fc52b1d..ab62def 100644
--- a/setup.py
+++ b/setup.py
@@ -1,7 +1,7 @@
 from setuptools import setup, find_packages
 from pip.req import parse_requirements
 
-version = "6.21.4"
+version = "6.21.5"
 requirements = parse_requirements("requirements.txt", session="")
 
 setup(