validation for stock and nonstock items in purchase cycle
diff --git a/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index 621604b..a70c932 100644
--- a/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -72,7 +72,38 @@
 			["Stock Received But Not Billed - _TC", 750.0, 0],
 			["_Test Account Shipping Charges - _TC", 100.0, 0],
 			["_Test Account VAT - _TC", 120.0, 0],
-			["Expenses Included In Valuation - _TC", 0, 250.0]
+			["Expenses Included In Valuation - _TC", 0, 250.0],
+		])
+		
+		for i, gle in enumerate(gl_entries):
+			self.assertEquals(expected_values[i][0], gle.account)
+			self.assertEquals(expected_values[i][1], gle.debit)
+			self.assertEquals(expected_values[i][2], gle.credit)
+		
+		webnotes.defaults.set_global_default("auto_inventory_accounting", 0)
+
+	def test_gl_entries_with_aia_for_non_stock_items(self):
+		webnotes.defaults.set_global_default("auto_inventory_accounting", 1)
+		self.assertEqual(cint(webnotes.defaults.get_global_default("auto_inventory_accounting")), 1)
+		
+		pi = webnotes.bean(copy=test_records[1])
+		pi.doclist[1].item_code = "_Test Non Stock Item"
+		pi.doclist[1].expense_head = "_Test Account Cost for Goods Sold - _TC"
+		pi.doclist.pop(2)
+		pi.doclist.pop(3)
+		pi.run_method("calculate_taxes_and_totals")
+		pi.insert()
+		pi.submit()
+		
+		gl_entries = webnotes.conn.sql("""select account, debit, credit
+			from `tabGL Entry` where voucher_type='Purchase Invoice' and voucher_no=%s
+			order by account asc""", pi.doc.name, as_dict=1)
+		self.assertTrue(gl_entries)
+		
+		expected_values = sorted([
+			["_Test Supplier - _TC", 0, 620],
+			["_Test Account Cost for Goods Sold - _TC", 500.0, 0],
+			["_Test Account VAT - _TC", 120.0, 0],
 		])
 		
 		for i, gle in enumerate(gl_entries):
@@ -106,7 +137,6 @@
 			self.assertEqual(tax.account_head, expected_values[i][0])
 			self.assertEqual(tax.tax_amount, expected_values[i][1])
 			self.assertEqual(tax.total, expected_values[i][2])
-			# print tax.account_head, tax.tax_amount, tax.item_wise_tax_detail
 
 		expected_values = [
 			["_Test Item Home Desktop 100", 90, 59],
@@ -142,7 +172,6 @@
 			self.assertEqual(tax.account_head, expected_values[i][0])
 			self.assertEqual(tax.tax_amount, expected_values[i][1])
 			self.assertEqual(tax.total, expected_values[i][2])
-			# print tax.account_head, tax.tax_amount, tax.item_wise_tax_detail
 
 		expected_values = [
 			["_Test FG Item", 90, 7059],
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index 429737e..85d8b9d 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -29,6 +29,7 @@
 class BuyingController(StockController):
 	def validate(self):
 		super(BuyingController, self).validate()
+		self.validate_stock_or_nonstock_items()
 		if self.meta.get_field("currency"):
 			self.company_currency = get_company_currency(self.doc.company)
 			self.validate_conversion_rate("currency", "conversion_rate")
@@ -41,7 +42,24 @@
 						
 			# set total in words
 			self.set_total_in_words()
-		
+			
+	def validate_stock_or_nonstock_items(self):
+		items = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
+		if self.stock_items and len(items) > len(self.stock_items):
+			nonstock_items = list(set(items) - set(self.stock_items))
+			webnotes.msgprint(_("Stock and non-stock items can not be entered at the same ") + 
+				self.doc.doctype + _(""". You should make separate documents for them.
+				Stock Items: """) + ", ".join(self.stock_items) + _("""
+				Non-stock Items: """) + ", ".join(nonstock_items), raise_exception=1)
+				
+		elif items and not self.stock_items:
+			tax_for_valuation = [d.account_head for d in 
+				self.doclist.get({"parentfield": "purchase_tax_details"}) 
+				if d.category in ["Valuation", "Valuation and Total"]]
+			if tax_for_valuation:
+				webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total' 
+					as all items are non-stock items"""), raise_exception=1)
+			
 	def update_item_details(self):
 		for item in self.doclist.get({"parentfield": self.fname}):
 			ret = get_item_details({