Get standard/historical valuation rate where missing: some minor fixes
diff --git a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
index eff5df5..a91d974 100755
--- a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
+++ b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
@@ -1316,7 +1316,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
@@ -1816,7 +1816,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-02-06 17:05:03.737297", 
+ "modified": "2017-02-07 01:21:03.737800", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Purchase Invoice Item", 
diff --git a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json
index c36f001..bf6d986 100644
--- a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json
+++ b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.json
@@ -1382,7 +1382,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
@@ -1910,7 +1910,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-02-06 17:01:16.812686", 
+ "modified": "2017-02-07 01:21:47.142162", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Sales Invoice Item", 
diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py
index 6e3047c..b08826a 100644
--- a/erpnext/controllers/stock_controller.py
+++ b/erpnext/controllers/stock_controller.py
@@ -43,7 +43,7 @@
 
 		gl_list = []
 		warehouse_with_no_account = []
-		
+
 		for item_row in voucher_details:
 			sle_list = sle_map.get(item_row.name)
 			if sle_list:
@@ -58,9 +58,10 @@
 						# or incoming entry not found while delivering the item), 
 						# try to pick valuation rate from previous sle or Item master and update in SLE
 						# Otherwise, throw an exception
-						
-						if not sle.stock_value_difference and sle.voucher_type != "Stock Reconciliation" \
+
+						if not sle.stock_value_difference and self.doctype != "Stock Reconciliation" \
 							and not item_row.get("is_sample_item"):
+
 							sle = self.update_stock_ledger_entries(sle)
 
 						gl_list.append(self.get_gl_dict({
@@ -95,7 +96,7 @@
 
 	def update_stock_ledger_entries(self, sle):
 		sle.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, 
-			sle.voucher_type, sle.voucher_no)
+			self.doctype, self.name)
 
 		sle.stock_value = flt(sle.qty_after_transaction) * flt(sle.valuation_rate)
 		sle.stock_value_difference = flt(sle.actual_qty) * flt(sle.valuation_rate)
@@ -162,18 +163,17 @@
 		stock_ledger = {}
 		stock_ledger_entries = frappe.db.sql("""
 			select 
-				name, warehouse, stock_value_difference, valuation_rate
+				name, warehouse, stock_value_difference, valuation_rate,
 				voucher_detail_no, item_code, posting_date, posting_time, 
-				actual_qty, qty_after_transaction, voucher_type, voucher_no
+				actual_qty, qty_after_transaction
 			from
 				`tabStock Ledger Entry`
 			where
 				voucher_type=%s and voucher_no=%s
 		""", (self.doctype, self.name), as_dict=True)
-			
+
 		for sle in stock_ledger_entries:
 				stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
-
 		return stock_ledger
 
 	def make_adjustment_entry(self, expected_gle, voucher_obj):
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
index 9108edd..1147d8d 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -1477,7 +1477,7 @@
    "width": "120px"
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
@@ -1719,7 +1719,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-02-06 17:00:24.354000", 
+ "modified": "2017-02-07 01:22:03.047137", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Delivery Note Item", 
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 43e2f3b..5903713 100755
--- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
+++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
@@ -1575,7 +1575,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
@@ -1913,7 +1913,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-02-06 17:04:23.305884", 
+ "modified": "2017-02-07 01:21:36.348032", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Purchase Receipt Item", 
diff --git a/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json b/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
index fadaeb0..34b3c85 100644
--- a/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
+++ b/erpnext/stock/doctype/stock_entry_detail/stock_entry_detail.json
@@ -1004,7 +1004,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
@@ -1183,7 +1183,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-02-06 17:08:06.841514", 
+ "modified": "2017-02-07 01:21:14.367586", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Stock Entry Detail", 
diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py
index c387e0f..10722cc 100644
--- a/erpnext/stock/stock_ledger.py
+++ b/erpnext/stock/stock_ledger.py
@@ -261,10 +261,7 @@
 				
 			# Get valuation rate from previous SLE or Item master, if item is not a sample item
 			if not self.valuation_rate and sle.voucher_detail_no:
-				ref_item_dt = sle.voucher_type + " Detail" if sle.voucher_type == "Stock Entry" else " Item"
-				
-				is_sample_item = frappe.db.get_value(ref_item_dt, sle.voucher_detail_no, "is_sample_item")
-					
+				is_sample_item = self.check_if_sample_item(sle.voucher_type, sle.voucher_detail_no)
 				if not is_sample_item:
 					self.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, 
 						sle.voucher_type, sle.voucher_no, self.allow_zero_rate)
@@ -292,8 +289,13 @@
 			while qty_to_pop:
 				if not self.stock_queue:
 					# Get valuation rate from last sle if exists or from valuation rate field in item master
-					_rate = get_valuation_rate(sle.item_code, sle.warehouse, 
-						sle.voucher_type, sle.voucher_no, self.allow_zero_rate)
+					is_sample_item = self.check_if_sample_item(sle.voucher_type, sle.voucher_detail_no)
+					if not is_sample_item:
+						_rate = get_valuation_rate(sle.item_code, sle.warehouse, 
+							sle.voucher_type, sle.voucher_no, self.allow_zero_rate)
+					else:
+						_rate = 0
+						
 					self.stock_queue.append([0, _rate])
 
 				index = None
@@ -339,7 +341,11 @@
 
 		if not self.stock_queue:
 			self.stock_queue.append([0, sle.incoming_rate or sle.outgoing_rate or self.valuation_rate])
-
+			
+	def check_if_sample_item(self, voucher_type, voucher_detail_no):
+		ref_item_dt = voucher_type + (" Detail" if voucher_type == "Stock Entry" else " Item")
+		return frappe.db.get_value(ref_item_dt, voucher_detail_no, "is_sample_item")
+		
 	def get_sle_before_datetime(self):
 		"""get previous stock ledger entry before current time-bucket"""
 		return get_stock_ledger_entries(self.args, "<", "desc", "limit 1", for_update=False)
@@ -438,6 +444,6 @@
 	if not allow_zero_rate and not valuation_rate \
 			and cint(frappe.db.get_value("Accounts Settings", None, "auto_accounting_for_stock")):
 			
-		frappe.throw(_("Valuation rate not found for the Item {0}, which is required to do accounting entries. If the item is transacting as a sample item in {1} {2}, please mention that in the {1} Item table. Otherwise, please create an incoming stock transaction for the item or mention valuation rate in the Item record, and then try submiting this entry").format(item_code, voucher_type, voucher_no))
+		frappe.throw(_("Valuation rate not found for the Item {0}, which is required to do accounting entries for {1} {2}. If the item is transacting as a sample item in the {1}, please mention that in the {1} Item table. Otherwise, please create an incoming stock transaction for the item or mention valuation rate in the Item record, and then try submiting/cancelling this entry").format(item_code, voucher_type, voucher_no))
 
 	return valuation_rate