Maintain negative stock balance if balance qty is negative
diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py
index 20eb40c..ad553d8 100644
--- a/erpnext/controllers/stock_controller.py
+++ b/erpnext/controllers/stock_controller.py
@@ -305,9 +305,15 @@
 	last_valuation_rate = frappe.db.sql("""select valuation_rate
 		from `tabStock Ledger Entry`
 		where item_code = %s and warehouse = %s
-		and ifnull(qty_after_transaction, 0) > 0
+		and ifnull(valuation_rate, 0) > 0
 		order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, warehouse))
 
+	if not last_valuation_rate:
+		last_valuation_rate = frappe.db.sql("""select valuation_rate
+			from `tabStock Ledger Entry`
+			where item_code = %s and ifnull(valuation_rate, 0) > 0
+			order by posting_date desc, posting_time desc, name desc limit 1""", item_code)
+
 	valuation_rate = flt(last_valuation_rate[0][0]) if last_valuation_rate else 0
 
 	if not valuation_rate: