Validate warehouse for allowed users and matched company
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index 7e49e60..25d76aa 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -24,7 +24,7 @@
 			self.doc.supplier_name = webnotes.conn.get_value("Supplier", 
 				self.doc.supplier, "supplier_name")
 		self.validate_stock_or_nonstock_items()
-		self.validate_warehouse_belongs_to_company()
+		self.validate_warehouse()
 		
 	def set_missing_values(self, for_validate=False):
 		super(BuyingController, self).set_missing_values(for_validate)
@@ -49,17 +49,20 @@
 				if supplier:
 					self.doc.supplier = supplier
 					break
+					
+	def validate_warehouse(self):
+		from stock.utils import validate_warehouse_user, validate_warehouse_company
+		
+		warehouses = list(set([d.warehouse for d in 
+			self.doclist.get({"doctype": self.tname}) if d.warehouse]))
+				
+		for w in warehouses:
+			validate_warehouse_user(w)
+			validate_warehouse_company(w, self.doc.company)
 
 	def get_purchase_tax_details(self):
 		self.doclist = self.doc.clear_table(self.doclist, "purchase_tax_details")
 		self.set_taxes("purchase_tax_details", "purchase_other_charges")
-		
-	def validate_warehouse_belongs_to_company(self):
-		for warehouse, company in webnotes.conn.get_values("Warehouse", 
-			self.doclist.get_distinct_values("warehouse"), "company").items():
-			if company and company != self.doc.company:
-				webnotes.msgprint(_("Company mismatch for Warehouse") + (": %s" % (warehouse,)),
-					raise_exception=WrongWarehouseCompany)
 
 	def validate_stock_or_nonstock_items(self):
 		if not self.get_stock_items():