Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/erpnext/accounts/page/accounts_browser/accounts_browser.js b/erpnext/accounts/page/accounts_browser/accounts_browser.js
index 4f35fd4..c58f73c 100644
--- a/erpnext/accounts/page/accounts_browser/accounts_browser.js
+++ b/erpnext/accounts/page/accounts_browser/accounts_browser.js
@@ -22,7 +22,12 @@
 
 pscript['onload_Accounts Browser'] = function(wrapper){
 	wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.appframe-area'));
-	wrapper.appframe.add_button('New Company', function() { newdoc('Company'); }, 'icon-plus');
+	
+	if (wn.boot.profile.can_create.indexOf("Company") !== -1) {
+		wrapper.appframe.add_button('New Company', function() { newdoc('Company'); },
+			'icon-plus');
+	}
+	
 	wrapper.appframe.add_button('Refresh', function() {  	
 			wrapper.$company_select.change();
 		}, 'icon-refresh');
@@ -35,17 +40,6 @@
 		})
 		.appendTo(wrapper.appframe.$w.find('.appframe-toolbar'));
 		
-	// default company
-	if(sys_defaults.company) {
-		$('<option>')
-			.html(sys_defaults.company)
-			.attr('value', sys_defaults.company)
-			.appendTo(wrapper.$company_select);
-
-		wrapper.$company_select
-			.val(sys_defaults.company).change();
-	}
-
 	// load up companies
 	wn.call({
 		method:'accounts.page.accounts_browser.accounts_browser.get_companies',
@@ -54,7 +48,7 @@
 			$.each(r.message, function(i, v) {
 				$('<option>').html(v).attr('value', v).appendTo(wrapper.$company_select);
 			});
-			wrapper.$company_select.val(sys_defaults.company || r[0]);
+			wrapper.$company_select.val(sys_defaults.company || r[0]).change();
 		}
 	});
 }
diff --git a/erpnext/accounts/page/accounts_browser/accounts_browser.py b/erpnext/accounts/page/accounts_browser/accounts_browser.py
index 56b79d1..690bf7a 100644
--- a/erpnext/accounts/page/accounts_browser/accounts_browser.py
+++ b/erpnext/accounts/page/accounts_browser/accounts_browser.py
@@ -3,7 +3,21 @@
 
 @webnotes.whitelist()
 def get_companies():
-	return [r[0] for r in webnotes.conn.sql("""select name from tabCompany where docstatus!=2""")]
+	"""get a list of companies based on permission"""
+	
+	# check if match permission exists
+	res = webnotes.conn.sql("""select role, `match` from `tabDocPerm`
+		where parent='Account' and permlevel=0 and `read`=1""", as_dict=1)
+		
+	match = any((r["match"] for r in res 
+		if r["role"] in webnotes.user.roles and r["match"]=="company"))
+	
+	# if match == company is specified and companies are specified in user defaults
+	if match and webnotes.user.get_defaults().get("company"):
+		return webnotes.user.get_defaults().get("company")
+	else:
+		return [r[0] for r in webnotes.conn.sql("""select name from tabCompany
+			where docstatus!=2""")]
 	
 @webnotes.whitelist()
 def get_children():