frappe/frappe#478 fixes
diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py
index cf95845..8a19bb1 100644
--- a/erpnext/stock/stock_ledger.py
+++ b/erpnext/stock/stock_ledger.py
@@ -17,36 +17,35 @@
 def make_sl_entries(sl_entries, is_amended=None):
 	if sl_entries:
 		from erpnext.stock.utils import update_bin
-	
+
 		cancel = True if sl_entries[0].get("is_cancelled") == "Yes" else False
 		if cancel:
 			set_as_cancel(sl_entries[0].get('voucher_no'), sl_entries[0].get('voucher_type'))
-	
+
 		for sle in sl_entries:
 			sle_id = None
 			if sle.get('is_cancelled') == 'Yes':
 				sle['actual_qty'] = -flt(sle['actual_qty'])
-		
+
 			if sle.get("actual_qty"):
 				sle_id = make_entry(sle)
-			
+
 			args = sle.copy()
 			args.update({
 				"sle_id": sle_id,
 				"is_amended": is_amended
 			})
 			update_bin(args)
-		
 		if cancel:
-			delete_cancelled_entry(sl_entries[0].get('voucher_type'), 
+			delete_cancelled_entry(sl_entries[0].get('voucher_type'),
 				sl_entries[0].get('voucher_no'))
-			
+
 def set_as_cancel(voucher_type, voucher_no):
 	frappe.db.sql("""update `tabStock Ledger Entry` set is_cancelled='Yes',
 		modified=%s, modified_by=%s
-		where voucher_no=%s and voucher_type=%s""", 
+		where voucher_no=%s and voucher_type=%s""",
 		(now(), frappe.session.user, voucher_type, voucher_no))
-		
+
 def make_entry(args):
 	args.update({"doctype": "Stock Ledger Entry"})
 	sle = frappe.get_doc(args)
@@ -54,16 +53,16 @@
 	sle.insert()
 	sle.submit()
 	return sle.name
-	
+
 def delete_cancelled_entry(voucher_type, voucher_no):
-	frappe.db.sql("""delete from `tabStock Ledger Entry` 
+	frappe.db.sql("""delete from `tabStock Ledger Entry`
 		where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
 
 def update_entries_after(args, verbose=1):
 	"""
-		update valution rate and qty after transaction 
+		update valution rate and qty after transaction
 		from the current time-bucket onwards
-		
+
 		args = {
 			"item_code": "ABC",
 			"warehouse": "XYZ",
@@ -73,15 +72,15 @@
 	"""
 	if not _exceptions:
 		frappe.local.stockledger_exceptions = []
-	
+
 	previous_sle = get_sle_before_datetime(args)
-	
+
 	qty_after_transaction = flt(previous_sle.get("qty_after_transaction"))
 	valuation_rate = flt(previous_sle.get("valuation_rate"))
 	stock_queue = json.loads(previous_sle.get("stock_queue") or "[]")
 	stock_value = flt(previous_sle.get("stock_value"))
 	prev_stock_value = flt(previous_sle.get("stock_value"))
-	
+
 	entries_to_fix = get_sle_after_datetime(previous_sle or \
 		{"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True)
 
@@ -90,7 +89,7 @@
 
 	for sle in entries_to_fix:
 		if sle.serial_no or not cint(frappe.db.get_default("allow_negative_stock")):
-			# validate negative stock for serialized items, fifo valuation 
+			# validate negative stock for serialized items, fifo valuation
 			# or when negative stock is not allowed for moving average
 			if not validate_negative_stock(qty_after_transaction, sle):
 				qty_after_transaction += flt(sle.actual_qty)
@@ -102,9 +101,9 @@
 			valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate)
 		else:
 			valuation_rate = get_fifo_values(qty_after_transaction, sle, stock_queue)
-				
+
 		qty_after_transaction += flt(sle.actual_qty)
-		
+
 		# get stock value
 		if sle.serial_no:
 			stock_value = qty_after_transaction * valuation_rate
@@ -113,29 +112,29 @@
 				(qty_after_transaction * valuation_rate) or 0
 		else:
 			stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
-		
+
 		# rounding as per precision
 		from frappe.model.meta import get_field_precision
 		meta = frappe.get_meta("Stock Ledger Entry")
-		
-		stock_value = flt(stock_value, get_field_precision(meta.get_field("stock_value"), 
+
+		stock_value = flt(stock_value, get_field_precision(meta.get_field("stock_value"),
 			frappe._dict({"fields": sle})))
-		
+
 		stock_value_difference = stock_value - prev_stock_value
 		prev_stock_value = stock_value
-			
+
 		# update current sle
 		frappe.db.sql("""update `tabStock Ledger Entry`
 			set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s,
-			stock_value=%s, stock_value_difference=%s where name=%s""", 
+			stock_value=%s, stock_value_difference=%s where name=%s""",
 			(qty_after_transaction, valuation_rate,
 			json.dumps(stock_queue), stock_value, stock_value_difference, sle.name))
-	
+
 	if _exceptions:
 		_raise_exceptions(args, verbose)
-	
+
 	# update bin
-	if not frappe.db.exists({"doctype": "Bin", "item_code": args["item_code"], 
+	if not frappe.db.exists({"doctype": "Bin", "item_code": args["item_code"],
 			"warehouse": args["warehouse"]}):
 		bin_wrapper = frappe.get_doc({
 			"doctype": "Bin",
@@ -144,13 +143,13 @@
 		})
 		bin_wrapper.ignore_permissions = 1
 		bin_wrapper.insert()
-	
+
 	frappe.db.sql("""update `tabBin` set valuation_rate=%s, actual_qty=%s,
-		stock_value=%s, 
+		stock_value=%s,
 		projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty)
 		where item_code=%s and warehouse=%s""", (valuation_rate, qty_after_transaction,
 		stock_value, args["item_code"], args["warehouse"]))
-		
+
 def get_sle_before_datetime(args, for_update=False):
 	"""
 		get previous stock ledger entry before current time-bucket
@@ -164,23 +163,23 @@
 	sle = get_stock_ledger_entries(args,
 		["timestamp(posting_date, posting_time) < timestamp(%(posting_date)s, %(posting_time)s)"],
 		"desc", "limit 1", for_update=for_update)
-	
+
 	return sle and sle[0] or frappe._dict()
-	
+
 def get_sle_after_datetime(args, for_update=False):
 	"""get Stock Ledger Entries after a particular datetime, for reposting"""
-	# NOTE: using for update of 
+	# NOTE: using for update of
 	return get_stock_ledger_entries(args,
 		["timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s)"],
 		"asc", for_update=for_update)
-				
+
 def get_stock_ledger_entries(args, conditions=None, order="desc", limit=None, for_update=False):
 	"""get stock ledger entries filtered by specific posting datetime conditions"""
 	if not args.get("posting_date"):
 		args["posting_date"] = "1900-01-01"
 	if not args.get("posting_time"):
 		args["posting_time"] = "00:00"
-	
+
 	return frappe.db.sql("""select * from `tabStock Ledger Entry`
 		where item_code = %%(item_code)s
 		and warehouse = %%(warehouse)s
@@ -193,7 +192,7 @@
 			"for_update": for_update and "for update" or "",
 			"order": order
 		}, args, as_dict=1)
-		
+
 def validate_negative_stock(qty_after_transaction, sle):
 	"""
 		validate negative stock for entries current datetime onwards
@@ -203,7 +202,7 @@
 
 	if not _exceptions:
 		frappe.local.stockledger_exceptions = []
-	
+
 	if diff < 0 and abs(diff) > 0.0001:
 		# negative stock!
 		exc = sle.copy().update({"diff": diff})
@@ -211,12 +210,12 @@
 		return False
 	else:
 		return True
-	
+
 def get_serialized_values(qty_after_transaction, sle, valuation_rate):
 	incoming_rate = flt(sle.incoming_rate)
 	actual_qty = flt(sle.actual_qty)
 	serial_no = cstr(sle.serial_no).split("\n")
-	
+
 	if incoming_rate < 0:
 		# wrong incoming rate
 		incoming_rate = valuation_rate
@@ -226,7 +225,7 @@
 		incoming_rate = flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0))
 			from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))),
 			tuple(serial_no))[0][0])
-	
+
 	if incoming_rate and not valuation_rate:
 		valuation_rate = incoming_rate
 	else:
@@ -237,33 +236,33 @@
 				# calculate new valuation rate only if stock value is positive
 				# else it remains the same as that of previous entry
 				valuation_rate = new_stock_value / new_stock_qty
-				
+
 	return valuation_rate
-	
+
 def get_moving_average_values(qty_after_transaction, sle, valuation_rate):
 	incoming_rate = flt(sle.incoming_rate)
-	actual_qty = flt(sle.actual_qty)	
-	
+	actual_qty = flt(sle.actual_qty)
+
 	if not incoming_rate:
 		# In case of delivery/stock issue in_rate = 0 or wrong incoming rate
 		incoming_rate = valuation_rate
-	
+
 	elif qty_after_transaction < 0:
 		# if negative stock, take current valuation rate as incoming rate
 		valuation_rate = incoming_rate
-		
+
 	new_stock_qty = qty_after_transaction + actual_qty
 	new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate
-	
+
 	if new_stock_qty > 0 and new_stock_value > 0:
 		valuation_rate = new_stock_value / flt(new_stock_qty)
 	elif new_stock_qty <= 0:
 		valuation_rate = 0.0
-	
+
 	# NOTE: val_rate is same as previous entry if new stock value is negative
-	
+
 	return valuation_rate
-	
+
 def get_fifo_values(qty_after_transaction, sle, stock_queue):
 	incoming_rate = flt(sle.incoming_rate)
 	actual_qty = flt(sle.actual_qty)
@@ -282,9 +281,9 @@
 		while qty_to_pop:
 			if not stock_queue:
 				stock_queue.append([0, 0])
-			
+
 			batch = stock_queue[0]
-			
+
 			if 0 < batch[0] <= qty_to_pop:
 				# if batch qty > 0
 				# not enough or exactly same qty in current batch, clear batch
@@ -296,7 +295,7 @@
 				incoming_cost += flt(qty_to_pop) * flt(batch[1])
 				batch[0] -= qty_to_pop
 				qty_to_pop = 0
-		
+
 	stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
 	stock_qty = sum((flt(batch[0]) for batch in stock_queue))
 
@@ -306,9 +305,9 @@
 
 def _raise_exceptions(args, verbose=1):
 	deficiency = min(e["diff"] for e in _exceptions)
-	msg = """Negative stock error: 
+	msg = """Negative stock error:
 		Cannot complete this transaction because stock will start
-		becoming negative (%s) for Item <b>%s</b> in Warehouse 
+		becoming negative (%s) for Item <b>%s</b> in Warehouse
 		<b>%s</b> on <b>%s %s</b> in Transaction %s %s.
 		Total Quantity Deficiency: <b>%s</b>""" % \
 		(_exceptions[0]["diff"], args.get("item_code"), args.get("warehouse"),
@@ -319,13 +318,13 @@
 		msgprint(msg, raise_exception=NegativeStockError)
 	else:
 		raise NegativeStockError, msg
-		
+
 def get_previous_sle(args, for_update=False):
 	"""
-		get the last sle on or before the current time-bucket, 
+		get the last sle on or before the current time-bucket,
 		to get actual qty before transaction, this function
 		is called from various transaction like stock entry, reco etc
-		
+
 		args = {
 			"item_code": "ABC",
 			"warehouse": "XYZ",
@@ -335,7 +334,7 @@
 		}
 	"""
 	if not args.get("sle"): args["sle"] = ""
-	
+
 	sle = get_stock_ledger_entries(args, ["name != %(sle)s",
 		"timestamp(posting_date, posting_time) <= timestamp(%(posting_date)s, %(posting_time)s)"],
 		"desc", "limit 1", for_update=for_update)