[purchase] [validation] validate warehouse belongs to company if set
diff --git a/buying/doctype/purchase_order/test_purchase_order.py b/buying/doctype/purchase_order/test_purchase_order.py
index bd27f07..be2e294 100644
--- a/buying/doctype/purchase_order/test_purchase_order.py
+++ b/buying/doctype/purchase_order/test_purchase_order.py
@@ -27,6 +27,12 @@
 		po.insert()		
 		self.assertEquals(len(po.doclist.get({"parentfield": "po_raw_material_details"})), 2)
 
+	def test_warehouse_company_validation(self):
+		from controllers.buying_controller import WrongWarehouseCompany
+		po = webnotes.bean(copy=test_records[0])
+		po.doc.company = "_Test Company 1"
+		self.assertRaises(WrongWarehouseCompany, po.insert)
+
 
 test_dependencies = ["BOM"]
 
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index e167dc5..cd822e6 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -26,10 +26,13 @@
 
 from controllers.stock_controller import StockController
 
+class WrongWarehouseCompany(Exception): pass
+
 class BuyingController(StockController):
 	def validate(self):
 		super(BuyingController, self).validate()
 		self.validate_stock_or_nonstock_items()
+		self.validate_warehouse_belongs_to_company()
 		if self.meta.get_field("currency"):
 			self.company_currency = get_company_currency(self.doc.company)
 			self.validate_conversion_rate("currency", "conversion_rate")
@@ -42,7 +45,15 @@
 						
 			# set total in words
 			self.set_total_in_words()
-			
+	
+	def validate_warehouse_belongs_to_company(self):
+		for d in self.doclist.get({"warehouse": True}):
+			warehouse_company = webnotes.conn.get_value("Warehouse", d.warehouse, "company")
+			if warehouse_company and warehouse_company != self.doc.company:
+				webnotes.msgprint(_("Warehouse must belong to company") + \
+					(": %s (%s, %s)" % (d.warehouse, warehouse_company, self.doc.company)),
+					raise_exception=WrongWarehouseCompany)
+	
 	def validate_stock_or_nonstock_items(self):
 		items = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
 		if self.stock_items:
diff --git a/stock/doctype/material_request/test_material_request.py b/stock/doctype/material_request/test_material_request.py
index 080989f..f5dbb52 100644
--- a/stock/doctype/material_request/test_material_request.py
+++ b/stock/doctype/material_request/test_material_request.py
@@ -263,6 +263,12 @@
 		se = webnotes.bean(copy=se_doclist)
 		self.assertRaises(webnotes.MappingMismatchError, se.insert)
 		
+	def test_warehouse_company_validation(self):
+		from controllers.buying_controller import WrongWarehouseCompany
+		mr = webnotes.bean(copy=test_records[0])
+		mr.doc.company = "_Test Company 1"
+		self.assertRaises(WrongWarehouseCompany, mr.insert)
+		
 test_records = [
 	[
 		{
diff --git a/stock/doctype/warehouse/test_warehouse.py b/stock/doctype/warehouse/test_warehouse.py
index 26501be..f3a0458 100644
--- a/stock/doctype/warehouse/test_warehouse.py
+++ b/stock/doctype/warehouse/test_warehouse.py
@@ -2,7 +2,8 @@
 	[{
 		"doctype": "Warehouse",
 		"warehouse_name": "_Test Warehouse",
-		"warehouse_type": "_Test Warehouse Type"
+		"warehouse_type": "_Test Warehouse Type",
+		"company": "_Test Company"
 	}],
 	[{
 		"doctype": "Warehouse",