pr and pi gl entries
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index e465e2c..68a21d9 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -275,7 +275,8 @@
 			cint(frappe.defaults.get_global_default("auto_accounting_for_stock"))
 			
 		stock_received_but_not_billed = self.get_company_default("stock_received_but_not_billed")
-
+		expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
+		
 		gl_entries = []
 
 		# parent's gl entry
@@ -331,13 +332,13 @@
 			
 			if auto_accounting_for_stock and item.item_code in stock_items and item.item_tax_amount:
 					# Post reverse entry for Stock-Received-But-Not-Billed if it is booked in Purchase Receipt
-					stock_rbnb_booked_in_pr = None
+					negative_expense_booked_in_pi = None
 					if item.purchase_receipt:
-						stock_rbnb_booked_in_pr = frappe.db.sql("""select name from `tabGL Entry`
+						negative_expense_booked_in_pi = frappe.db.sql("""select name from `tabGL Entry`
 							where voucher_type='Purchase Receipt' and voucher_no=%s and account=%s""", 
-							(item.purchase_receipt, stock_received_but_not_billed))
+							(item.purchase_receipt, expenses_included_in_valuation))
 					
-					if stock_rbnb_booked_in_pr:
+					if not negative_expense_booked_in_pi:
 						gl_entries.append(
 							self.get_gl_dict({
 								"account": stock_received_but_not_billed,
@@ -353,7 +354,6 @@
 		if negative_expense_to_be_booked and valuation_tax:
 			# credit valuation tax amount in "Expenses Included In Valuation"
 			# this will balance out valuation amount included in cost of goods sold
-			expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
 			
 			total_valuation_amount = sum(valuation_tax.values())
 			amount_including_divisional_loss = negative_expense_to_be_booked
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index 206d407..12bb96d 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -37,7 +37,7 @@
 
 	def onload(self):
 		billed_qty = frappe.db.sql("""select sum(ifnull(qty, 0)) from `tabPurchase Invoice Item`
-			where purchase_receipt=%s""", self.name)
+			where purchase_receipt=%s and docstatus=1""", self.name)
 		if billed_qty:
 			total_qty = sum((item.qty for item in self.get("purchase_receipt_details")))
 			self.get("__onload").billing_complete = (billed_qty[0][0] == total_qty)
@@ -358,12 +358,12 @@
 			# and charges added via Landed Cost Voucher, 
 			# post valuation related charges on "Stock Received But Not Billed" 
 
-			stock_rbnb_booked_in_pi = frappe.db.sql("""select name from `tabPurchase Invoice Item` pi
+			negative_expense_booked_in_pi = frappe.db.sql("""select name from `tabPurchase Invoice Item` pi
 				where docstatus = 1 and purchase_receipt=%s 
 				and exists(select name from `tabGL Entry` where voucher_type='Purchase Invoice' 
-					and voucher_no=pi.parent and account=%s)""", (self.name, stock_rbnb))
+					and voucher_no=pi.parent and account=%s)""", (self.name, expenses_included_in_valuation))
 		
-			if stock_rbnb_booked_in_pi:
+			if negative_expense_booked_in_pi:
 				expenses_included_in_valuation = stock_rbnb
 			
 			against_account = ", ".join([d.account for d in gl_entries if flt(d.debit) > 0])