Maintain negative stock balance if balance qty is negative
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 3d25c21..66d5792 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -269,7 +269,7 @@
 			# get raw materials rate
 			if self.doctype == "Purchase Receipt":
 				from erpnext.stock.utils import get_incoming_rate
-				rm.rate = get_incoming_rate({
+				item_rate = get_incoming_rate({
 					"item_code": bom_item.item_code,
 					"warehouse": self.supplier_warehouse,
 					"posting_date": self.posting_date,
@@ -277,6 +277,7 @@
 					"qty": -1 * required_qty,
 					"serial_no": rm.serial_no
 				})
+				rm.rate = item_rate or bom_item.rate
 			else:
 				rm.rate = bom_item.rate
 
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:
diff --git a/erpnext/stock/doctype/stock_entry/test_stock_entry.py b/erpnext/stock/doctype/stock_entry/test_stock_entry.py
index 038606c..7654cbc 100644
--- a/erpnext/stock/doctype/stock_entry/test_stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/test_stock_entry.py
@@ -9,14 +9,64 @@
 from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory
 from erpnext.stock.doctype.stock_ledger_entry.stock_ledger_entry import StockFreezeError
 
+def get_sle(**args):
+	condition, values = "", []
+	for key, value in args.iteritems():
+		condition += " and " if condition else " where "
+		condition += "`{0}`=%s".format(key)
+		values.append(value)
+
+	return frappe.db.sql("""select * from `tabStock Ledger Entry` %s
+		order by timestamp(posting_date, posting_time) desc, name desc limit 1"""% condition,
+		values, as_dict=1)
+
+def make_zero(item_code, warehouse):
+	sle = get_sle(item_code = item_code, warehouse = warehouse)
+	qty = sle[0].qty_after_transaction if sle else 0
+	if qty < 0:
+		make_stock_entry(item_code, None, warehouse, abs(qty), incoming_rate=10)
+	elif qty > 0:
+		make_stock_entry(item_code, warehouse, None, qty, incoming_rate=10)
+
 class TestStockEntry(unittest.TestCase):
-	
 	def tearDown(self):
 		frappe.set_user("Administrator")
 		set_perpetual_inventory(0)
 		if hasattr(self, "old_default_company"):
 			frappe.db.set_default("company", self.old_default_company)
 
+	def test_fifo(self):
+		frappe.db.set_default("allow_negative_stock", 1)
+		item_code = "_Test Item 2"
+		warehouse = "_Test Warehouse - _TC"
+		make_zero(item_code, warehouse)
+
+		make_stock_entry(item_code, None, warehouse, 1, incoming_rate=10)
+		sle = get_sle(item_code = item_code, warehouse = warehouse)[0]
+
+		self.assertEqual([[1, 10]], eval(sle.stock_queue))
+
+		# negative qty
+		make_zero(item_code, warehouse)
+		make_stock_entry(item_code, warehouse, None, 1, incoming_rate=10)
+		sle = get_sle(item_code = item_code, warehouse = warehouse)[0]
+
+		self.assertEqual([[-1, 10]], eval(sle.stock_queue))
+
+		# further negative
+		make_stock_entry(item_code, warehouse, None, 1)
+		sle = get_sle(item_code = item_code, warehouse = warehouse)[0]
+
+		self.assertEqual([[-2, 10]], eval(sle.stock_queue))
+
+		# move stock to positive
+		make_stock_entry(item_code, None, warehouse, 3, incoming_rate=10)
+		sle = get_sle(item_code = item_code, warehouse = warehouse)[0]
+
+		self.assertEqual([[1, 10]], eval(sle.stock_queue))
+
+		frappe.db.set_default("allow_negative_stock", 0)
+
 	def test_auto_material_request(self):
 		frappe.db.sql("""delete from `tabMaterial Request Item`""")
 		frappe.db.sql("""delete from `tabMaterial Request`""")
@@ -821,19 +871,19 @@
 		se = frappe.copy_doc(test_records[0]).insert()
 		self.assertRaises (StockFreezeError, se.submit)
 		frappe.db.set_value("Stock Settings", None, "stock_frozen_upto_days", 0)
-		
+
 	def test_production_order(self):
-		bom_no = frappe.db.get_value("BOM", {"item": "_Test FG Item 2", 
+		bom_no = frappe.db.get_value("BOM", {"item": "_Test FG Item 2",
 			"is_default": 1, "docstatus": 1})
-		
+
 		production_order = frappe.new_doc("Production Order")
 		production_order.update({
 			"company": "_Test Company",
-			"fg_warehouse": "_Test Warehouse 1 - _TC", 
-			"production_item": "_Test FG Item 2", 
+			"fg_warehouse": "_Test Warehouse 1 - _TC",
+			"production_item": "_Test FG Item 2",
 			"bom_no": bom_no,
 			"qty": 1.0,
-			"stock_uom": "Nos", 
+			"stock_uom": "Nos",
 			"wip_warehouse": "_Test Warehouse - _TC"
 		})
 		production_order.insert()
@@ -889,5 +939,3 @@
 	s.insert()
 	s.submit()
 	return s
-
-test_records = frappe.get_test_records('Stock Entry')
diff --git a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
index 183e3e5..91775d9 100644
--- a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
+++ b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
@@ -28,7 +28,7 @@
 			[20, "", "2012-12-26", "12:05", 16000, 15, 18000],
 			[10, 2000, "2012-12-26", "12:10", 20000, 5, 6000],
 			[1, 1000, "2012-12-01", "00:00", 1000, 11, 13200],
-			[0, "", "2012-12-26", "12:10", 0, -5, 0]
+			[0, "", "2012-12-26", "12:10", 0, -5, -6000]
 		]
 
 		for d in input_data:
@@ -63,16 +63,16 @@
 		input_data = [
 			[50, 1000, "2012-12-26", "12:00", 50000, 45, 48000],
 			[5, 1000, "2012-12-26", "12:00", 5000, 0, 0],
-			[15, 1000, "2012-12-26", "12:00", 15000, 10, 12000],
+			[15, 1000, "2012-12-26", "12:00", 15000, 10, 11500],
 			[25, 900, "2012-12-26", "12:00", 22500, 20, 22500],
 			[20, 500, "2012-12-26", "12:00", 10000, 15, 18000],
 			[50, 1000, "2013-01-01", "12:00", 50000, 65, 68000],
 			[5, 1000, "2013-01-01", "12:00", 5000, 20, 23000],
-			["", 1000, "2012-12-26", "12:05", 15000, 10, 12000],
+			["", 1000, "2012-12-26", "12:05", 15000, 10, 11500],
 			[20, "", "2012-12-26", "12:05", 18000, 15, 18000],
-			[10, 2000, "2012-12-26", "12:10", 20000, 5, 6000],
-			[1, 1000, "2012-12-01", "00:00", 1000, 11, 13200],
-			[0, "", "2012-12-26", "12:10", 0, -5, 0]
+			[10, 2000, "2012-12-26", "12:10", 20000, 5, 7600],
+			[1, 1000, "2012-12-01", "00:00", 1000, 11, 12512.73],
+			[0, "", "2012-12-26", "12:10", 0, -5, -5142.86]
 
 		]
 
diff --git a/erpnext/stock/page/stock_balance/stock_balance.js b/erpnext/stock/page/stock_balance/stock_balance.js
index ecd1108..198d317 100644
--- a/erpnext/stock/page/stock_balance/stock_balance.js
+++ b/erpnext/stock/page/stock_balance/stock_balance.js
@@ -126,6 +126,7 @@
 						&& this.stock_entry_map[sl.voucher_no].purpose=="Material Transfer";
 
 					if(!ignore_inflow_outflow) {
+
 						if(qty_diff < 0) {
 							item.outflow_qty += Math.abs(qty_diff);
 						} else {
diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py
index e8a84c2..f1ba94e 100644
--- a/erpnext/stock/stock_ledger.py
+++ b/erpnext/stock/stock_ledger.py
@@ -106,18 +106,19 @@
 				stock_queue = [[qty_after_transaction, valuation_rate]]
 			else:
 				if valuation_method == "Moving Average":
-					valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate)
+					if flt(sle.actual_qty) > 0:
+						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
 		elif valuation_method == "Moving Average":
-			stock_value = (qty_after_transaction > 0) and \
-				(qty_after_transaction * valuation_rate) or 0
+			stock_value = qty_after_transaction * valuation_rate
 		else:
 			stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
 
@@ -256,64 +257,73 @@
 	actual_qty = flt(sle.actual_qty)
 
 	if not incoming_rate:
-		# In case of delivery/stock issue in_rate = 0 or wrong incoming rate
+		# If wrong incoming rate
 		incoming_rate = valuation_rate
 
-	elif qty_after_transaction < 0:
+	elif qty_after_transaction < 0 and not valuation_rate:
 		# 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
+	new_stock_qty = abs(qty_after_transaction) + actual_qty
+	new_stock_value = (abs(qty_after_transaction) * valuation_rate) + (actual_qty * incoming_rate)
 
-	if new_stock_qty > 0 and new_stock_value > 0:
+	if new_stock_qty:
 		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
+	return abs(valuation_rate)
 
 def get_fifo_values(qty_after_transaction, sle, stock_queue):
 	incoming_rate = flt(sle.incoming_rate)
 	actual_qty = flt(sle.actual_qty)
-	if not stock_queue:
-		stock_queue.append([0, 0])
+
+	intialize_stock_queue(stock_queue, sle.item_code, sle.warehouse)
 
 	if actual_qty > 0:
 		if stock_queue[-1][0] > 0:
 			stock_queue.append([actual_qty, incoming_rate])
 		else:
 			qty = stock_queue[-1][0] + actual_qty
-			stock_queue[-1] = [qty, qty > 0 and incoming_rate or 0]
+			if qty == 0:
+				stock_queue.pop(-1)
+			else:
+				stock_queue[-1] = [qty, incoming_rate]
 	else:
-		incoming_cost = 0
 		qty_to_pop = abs(actual_qty)
 		while qty_to_pop:
-			if not stock_queue:
-				stock_queue.append([0, 0])
+			intialize_stock_queue(stock_queue, sle.item_code, sle.warehouse)
 
 			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
-				incoming_cost += flt(batch[0]) * flt(batch[1])
-				qty_to_pop -= batch[0]
+			# print qty_to_pop, batch
+
+			if qty_to_pop >= batch[0]:
+				# consume current batch
+				qty_to_pop = qty_to_pop - batch[0]
 				stock_queue.pop(0)
+				if not stock_queue and qty_to_pop:
+					# stock finished, qty still remains to be withdrawn
+					# negative stock, keep in as a negative batch
+					stock_queue.append([-qty_to_pop, batch[1]])
+					break
+
 			else:
-				# all from current batch
-				incoming_cost += flt(qty_to_pop) * flt(batch[1])
-				batch[0] -= qty_to_pop
+				# qty found in current batch
+				# consume it and exit
+				batch[0] = 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))
 
-	valuation_rate = stock_qty and (stock_value / flt(stock_qty)) or 0
+	valuation_rate = (stock_value / flt(stock_qty)) if stock_qty else 0
 
-	return valuation_rate
+	return abs(valuation_rate)
+
+def intialize_stock_queue(stock_queue, item_code, warehouse):
+	if not stock_queue:
+		from erpnext.controllers.stock_controller import get_valuation_rate
+		estimated_val_rate = get_valuation_rate(item_code, warehouse)
+		stock_queue.append([0, estimated_val_rate])
 
 def _raise_exceptions(args, verbose=1):
 	deficiency = min(e["diff"] for e in _exceptions)
diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py
index 7264f36..b444e84 100644
--- a/erpnext/stock/utils.py
+++ b/erpnext/stock/utils.py
@@ -5,7 +5,6 @@
 from frappe import _
 import json
 from frappe.utils import flt, cstr, nowdate, add_days, cint
-from frappe.defaults import get_global_default
 from frappe.utils.email_lib import sendmail
 from erpnext.accounts.utils import get_fiscal_year, FiscalYearError
 
@@ -94,7 +93,7 @@
 	"""get valuation method from item or default"""
 	val_method = frappe.db.get_value('Item', item_code, 'valuation_method')
 	if not val_method:
-		val_method = get_global_default('valuation_method') or "FIFO"
+		val_method = frappe.db.get_value("Stock Settings", None, "valuation_method") or "FIFO"
 	return val_method
 
 def get_fifo_rate(previous_stock_queue, qty):
diff --git a/erpnext/utilities/repost_stock.py b/erpnext/utilities/repost_stock.py
index 51b472e..b5eec23 100644
--- a/erpnext/utilities/repost_stock.py
+++ b/erpnext/utilities/repost_stock.py
@@ -213,10 +213,13 @@
 	vouchers = frappe.db.sql("""select distinct voucher_type, voucher_no
 		from `tabStock Ledger Entry` order by posting_date, posting_time, name""")
 
+	print len(vouchers)
 	rejected = []
-	# vouchers = [["Purchase Receipt", "GRN00062"]]
+	# vouchers = [["Delivery Note", "DN00060"]]
+	i = 0
 	for voucher_type, voucher_no in vouchers:
-		print voucher_type, voucher_no
+		i+=1
+		print i
 		try:
 			for dt in ["Stock Ledger Entry", "GL Entry"]:
 				frappe.db.sql("""delete from `tab%s` where voucher_type=%s and voucher_no=%s"""%