landed cost fix
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 9f2b78e..8bc0c9d 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -194,7 +194,7 @@
 					"conversion_factor")) or 1
 				qty_in_stock_uom = flt(item.qty * item.conversion_factor)
 				rm_supp_cost = flt(item.rm_supp_cost) if self.doctype=="Purchase Receipt" else 0.0
-				
+
 				landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \
 					if self.doctype == "Purchase Receipt" else 0.0
 				
diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
index 9653242..dd3fd90 100644
--- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
+++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
@@ -14,12 +14,12 @@
 	def get_items_from_purchase_receipts(self):
 		self.set("landed_cost_items", [])
 		for pr in self.get("landed_cost_purchase_receipts"):
-			pr_items = frappe.db.sql("""select pr_tem.item_code, pr_tem.description, 
-				pr_tem.qty, pr_tem.rate, pr_tem.amount, pr_tem.name
+			pr_items = frappe.db.sql("""select pr_item.item_code, pr_item.description, 
+				pr_item.qty, pr_item.rate, pr_item.amount, pr_item.name
 				from `tabPurchase Receipt Item` pr_item where parent = %s 
-				and (select name form tabItem where name = pr_item.item_code and is_stock_item = 'Yes')""", 
+				and exists(select name from tabItem where name = pr_item.item_code and is_stock_item = 'Yes')""", 
 				pr.purchase_receipt, as_dict=True)
-				
+			
 			for d in pr_items:
 				item = self.append("landed_cost_items")
 				item.item_code = d.item_code
@@ -75,11 +75,19 @@
 			item.applicable_charges = flt(item.amount) *  flt(self.total_taxes_and_charges) / flt(total_item_cost)
 		
 	def on_submit(self):
+		self.update_landed_cost()
+	
+	def on_cancel(self):
+		self.update_landed_cost()
+		
+	def update_landed_cost(self):
 		purchase_receipts = list(set([d.purchase_receipt for d in self.get("landed_cost_items")]))
 		self.delete_sle_and_gle(purchase_receipts)
 		for purchase_receipt in purchase_receipts:
 			pr = frappe.get_doc("Purchase Receipt", purchase_receipt)
-			pr.update_valuation_rate()
+			pr.set_landed_cost_voucher_amount()
+			pr.update_valuation_rate("purchase_receipt_details")
+			pr.save()
 			pr.update_stock()
 			pr.make_gl_entries()
 		
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index d627386..adcc255 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -70,11 +70,11 @@
 		self.set_landed_cost_voucher_amount()
 		self.update_valuation_rate("purchase_receipt_details")
 		
-	def set_landed_cost_voucher_amount(self, voucher_detail):
+	def set_landed_cost_voucher_amount(self):
 		for d in self.get("purchase_receipt_details"):
-			lc_voucher_amount = frappe.db.sql("""select sum(ifnull(applicable_charges)) 
+			lc_voucher_amount = frappe.db.sql("""select sum(ifnull(applicable_charges, 0)) 
 				from `tabLanded Cost Item` 
-				where docstatus = 1 and pr_item_row_id = %s""", voucher_detail)
+				where docstatus = 1 and pr_item_row_id = %s""", d.name)
 			d.landed_cost_voucher_amount = lc_voucher_amount[0][0] if lc_voucher_amount else 0.0
 			
 	def validate_rejected_warehouse(self):
@@ -297,42 +297,41 @@
 		for d in self.get("purchase_receipt_details"):
 			if d.item_code in stock_items and flt(d.valuation_rate):
 				if warehouse_account.get(d.warehouse) or warehouse_account.get(d.rejected_warehouse):
-					self.check_expense_account(d)
-					
+
 					# warehouse account
 					if flt(d.qty):
-						gl_list.append(self.get_gl_dict({
+						gl_entries.append(self.get_gl_dict({
 							"account": warehouse_account[d.warehouse],
 							"against": against_expense_account,
-							"cost_center": default_cost_center,
+							"cost_center": d.cost_center,
 							"remarks": self.get("remarks") or "Accounting Entry for Stock",
 							"debit": flt(d.valuation_rate) * flt(d.qty) * flt(d.conversion_factor)
 						}))
 					
 					# rejected warehouse
 					if flt(d.rejected_qty):
-						gl_list.append(self.get_gl_dict({
+						gl_entries.append(self.get_gl_dict({
 							"account": warehouse_account[d.rejected_warehouse],
 							"against": against_expense_account,
-							"cost_center": default_cost_center,
+							"cost_center": d.cost_center,
 							"remarks": self.get("remarks") or "Accounting Entry for Stock",
 							"debit": flt(d.valuation_rate) * flt(d.rejected_qty) * flt(d.conversion_factor)
 						}))
 
 					# stock received but not billed
-					gl_list.append(self.get_gl_dict({
+					gl_entries.append(self.get_gl_dict({
 						"account": stock_rbnb,
 						"against": warehouse_account[d.warehouse],
-						"cost_center": default_cost_center,
+						"cost_center": d.cost_center,
 						"remarks": self.get("remarks") or "Accounting Entry for Stock",
 						"credit": flt(d.base_amount, 2)
 					}))
 					
 					if flt(d.landed_cost_voucher_amount):
-						gl_list.append(self.get_gl_dict({
+						gl_entries.append(self.get_gl_dict({
 							"account": expenses_included_in_valuation,
 							"against": warehouse_account[d.warehouse],
-							"cost_center": default_cost_center,
+							"cost_center": d.cost_center,
 							"remarks": self.get("remarks") or "Accounting Entry for Stock",
 							"credit": flt(d.landed_cost_voucher_amount)
 						}))
@@ -353,7 +352,10 @@
 				valuation_tax.setdefault(tax.cost_center, 0)
 				valuation_tax[tax.cost_center] += \
 					(tax.add_deduct_tax == "Add" and 1 or -1) * flt(tax.tax_amount)
-				
+					
+		if frappe.db.get_value("Purchase Invoice Item", {"purchase_receipt": self.name, "docstatus": 1}):
+			expenses_included_in_valuation = stock_rbnb
+			
 		for cost_center, amount in valuation_tax.items():
 			gl_entries.append(
 				self.get_gl_dict({
@@ -364,7 +366,7 @@
 					"remarks": self.remarks or "Accounting Entry for Stock"
 				})
 			)
-		
+
 		return process_gl_map(gl_entries)
 
 
diff --git a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
index 76d6294..3b45ca2 100755
--- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
+++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
@@ -502,6 +502,7 @@
    "width": "150px"
   }, 
   {
+   "allow_on_submit": 1, 
    "fieldname": "landed_cost_voucher_amount", 
    "fieldtype": "Currency", 
    "label": "Landed Cost Voucher Amount", 
@@ -509,6 +510,7 @@
    "read_only": 1
   }, 
   {
+   "allow_on_submit": 1, 
    "fieldname": "valuation_rate", 
    "fieldtype": "Currency", 
    "hidden": 1,