Country and chart fields added in company master
diff --git a/erpnext/setup/doctype/company/company.js b/erpnext/setup/doctype/company/company.js
index 5556858..4d1e20d 100644
--- a/erpnext/setup/doctype/company/company.js
+++ b/erpnext/setup/doctype/company/company.js
@@ -98,8 +98,17 @@
 	}  
 }
 
+cur_frm.get_field("chart_of_accounts").get_query = function(doc) {
+	return {
+		filters: {
+			"country": doc.country
+		}
+	}
+}
+
 cur_frm.fields_dict.payables_group.get_query = cur_frm.fields_dict.receivables_group.get_query;
 
+
 cur_frm.fields_dict.default_expense_account.get_query = function(doc) {    
 	return{
 		filters:{
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index 7f6d846..7ceff79 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -57,6 +57,16 @@
 	def create_default_warehouses(self):
 		for whname in ("Stores", "Work In Progress", "Finished Goods"):
 			if not frappe.db.exists("Warehouse", whname + " - " + self.doc.abbr):
+				if not frappe.db.exists("Account", "Stock Assets - " + self.doc.abbr):
+					frappe.bean({
+						"doctype": "Account",
+						"account_name": "Stock Assets",
+						"company": self.doc.name,
+						"group_or_ledger": "Group",
+						"root_type": "Asset",
+						"is_pl_account": "No"
+					}).insert()
+				
 				frappe.bean({
 					"doctype":"Warehouse",
 					"warehouse_name": whname,
@@ -101,6 +111,108 @@
 				style_settings.save()
 
 	def create_default_accounts(self):
+		if self.doc.chart_of_accounts:
+			self.import_chart_of_account()
+		else:
+			self.create_standard_accounts()
+			
+	def import_chart_of_account(self):
+		chart = frappe.bean("Chart of Accounts", self.doc.chart_of_accounts)
+		chart.make_controller().create_accounts(self.doc.name)
+
+	def add_acc(self,lst):
+		account = frappe.bean({
+			"doctype": "Account",
+			"freeze_account": "No",
+			"master_type": "",
+		})
+		for d in self.fld_dict.keys():
+			account.doc.fields[d] = (d == 'parent_account' and lst[self.fld_dict[d]]) and lst[self.fld_dict[d]] +' - '+ self.doc.abbr or lst[self.fld_dict[d]]
+		
+		account.insert()
+
+	def set_default_accounts(self):
+		def _set_default_accounts(accounts):
+			for a in accounts:
+				account_name = accounts[a] + " - " + self.doc.abbr
+				if not self.doc.fields.get(a) and frappe.db.exists("Account", account_name):
+					frappe.db.set(self.doc, a, account_name)
+			
+		_set_default_accounts({
+			"receivables_group": "Accounts Receivable",
+			"payables_group": "Accounts Payable",
+			"default_cash_account": "Cash"
+		})
+		
+		if cint(frappe.db.get_value("Accounts Settings", None, "auto_accounting_for_stock")):
+			_set_default_accounts({
+				"stock_received_but_not_billed": "Stock Received But Not Billed",
+				"stock_adjustment_account": "Stock Adjustment",
+				"expenses_included_in_valuation": "Expenses Included In Valuation"
+			})
+
+	def create_default_cost_center(self):
+		cc_list = [
+			{
+				'cost_center_name': self.doc.name,
+				'company':self.doc.name,
+				'group_or_ledger':'Group',
+				'parent_cost_center':''
+			}, 
+			{
+				'cost_center_name':'Main', 
+				'company':self.doc.name,
+				'group_or_ledger':'Ledger',
+				'parent_cost_center':self.doc.name + ' - ' + self.doc.abbr
+			},
+		]
+		for cc in cc_list:
+			cc.update({"doctype": "Cost Center"})
+			cc_bean = frappe.bean(cc)
+			cc_bean.ignore_permissions = True
+		
+			if cc.get("cost_center_name") == self.doc.name:
+				cc_bean.ignore_mandatory = True
+			cc_bean.insert()
+			
+		frappe.db.set(self.doc, "cost_center", "Main - " + self.doc.abbr)
+
+	def on_trash(self):
+		"""
+			Trash accounts and cost centers for this company if no gl entry exists
+		"""
+		rec = frappe.db.sql("SELECT name from `tabGL Entry` where company = %s", self.doc.name)
+		if not rec:
+			#delete tabAccount
+			frappe.db.sql("delete from `tabAccount` where company = %s order by lft desc, rgt desc", self.doc.name)
+			
+			#delete cost center child table - budget detail
+			frappe.db.sql("delete bd.* from `tabBudget Detail` bd, `tabCost Center` cc where bd.parent = cc.name and cc.company = %s", self.doc.name)
+			#delete cost center
+			frappe.db.sql("delete from `tabCost Center` WHERE company = %s order by lft desc, rgt desc", self.doc.name)
+			
+		if not frappe.db.get_value("Stock Ledger Entry", {"company": self.doc.name}):
+			frappe.db.sql("""delete from `tabWarehouse` where company=%s""", self.doc.name)
+			
+		frappe.defaults.clear_default("company", value=self.doc.name)
+			
+		frappe.db.sql("""update `tabSingles` set value=""
+			where doctype='Global Defaults' and field='default_company' 
+			and value=%s""", self.doc.name)
+			
+	def before_rename(self, olddn, newdn, merge=False):
+		if merge:
+			frappe.throw(_("Sorry, companies cannot be merged"))
+	
+	def after_rename(self, olddn, newdn, merge=False):
+		frappe.db.set(self.doc, "company_name", newdn)
+
+		frappe.db.sql("""update `tabDefaultValue` set defvalue=%s 
+			where defkey='Company' and defvalue=%s""", (newdn, olddn))
+
+		frappe.defaults.clear_cache()
+		
+	def create_standard_accounts(self):
 		self.fld_dict = {'account_name':0,'parent_account':1,'group_or_ledger':2,'is_pl_account':3,'account_type':4,'root_type':5,'company':6,'tax_rate':7}
 		acc_list_common = [
 			['Application of Funds (Assets)','','Group','No','','Asset',self.doc.name,''],
@@ -225,98 +337,6 @@
 			for d in acc_list_india:
 				self.add_acc(d)
 
-	def add_acc(self,lst):
-		account = frappe.bean({
-			"doctype": "Account",
-			"freeze_account": "No",
-			"master_type": "",
-		})
-		for d in self.fld_dict.keys():
-			account.doc.fields[d] = (d == 'parent_account' and lst[self.fld_dict[d]]) and lst[self.fld_dict[d]] +' - '+ self.doc.abbr or lst[self.fld_dict[d]]
-			
-		account.insert()
-
-	def set_default_accounts(self):
-		def _set_default_accounts(accounts):
-			for a in accounts:
-				account_name = accounts[a] + " - " + self.doc.abbr
-				if not self.doc.fields.get(a) and frappe.db.exists("Account", account_name):
-					frappe.db.set(self.doc, a, account_name)
-			
-		_set_default_accounts({
-			"receivables_group": "Accounts Receivable",
-			"payables_group": "Accounts Payable",
-			"default_cash_account": "Cash"
-		})
-		
-		if cint(frappe.db.get_value("Accounts Settings", None, "auto_accounting_for_stock")):
-			_set_default_accounts({
-				"stock_received_but_not_billed": "Stock Received But Not Billed",
-				"stock_adjustment_account": "Stock Adjustment",
-				"expenses_included_in_valuation": "Expenses Included In Valuation"
-			})
-
-	def create_default_cost_center(self):
-		cc_list = [
-			{
-				'cost_center_name': self.doc.name,
-				'company':self.doc.name,
-				'group_or_ledger':'Group',
-				'parent_cost_center':''
-			}, 
-			{
-				'cost_center_name':'Main', 
-				'company':self.doc.name,
-				'group_or_ledger':'Ledger',
-				'parent_cost_center':self.doc.name + ' - ' + self.doc.abbr
-			},
-		]
-		for cc in cc_list:
-			cc.update({"doctype": "Cost Center"})
-			cc_bean = frappe.bean(cc)
-			cc_bean.ignore_permissions = True
-		
-			if cc.get("cost_center_name") == self.doc.name:
-				cc_bean.ignore_mandatory = True
-			cc_bean.insert()
-			
-		frappe.db.set(self.doc, "cost_center", "Main - " + self.doc.abbr)
-
-	def on_trash(self):
-		"""
-			Trash accounts and cost centers for this company if no gl entry exists
-		"""
-		rec = frappe.db.sql("SELECT name from `tabGL Entry` where company = %s", self.doc.name)
-		if not rec:
-			#delete tabAccount
-			frappe.db.sql("delete from `tabAccount` where company = %s order by lft desc, rgt desc", self.doc.name)
-			
-			#delete cost center child table - budget detail
-			frappe.db.sql("delete bd.* from `tabBudget Detail` bd, `tabCost Center` cc where bd.parent = cc.name and cc.company = %s", self.doc.name)
-			#delete cost center
-			frappe.db.sql("delete from `tabCost Center` WHERE company = %s order by lft desc, rgt desc", self.doc.name)
-			
-		if not frappe.db.get_value("Stock Ledger Entry", {"company": self.doc.name}):
-			frappe.db.sql("""delete from `tabWarehouse` where company=%s""", self.doc.name)
-			
-		frappe.defaults.clear_default("company", value=self.doc.name)
-			
-		frappe.db.sql("""update `tabSingles` set value=""
-			where doctype='Global Defaults' and field='default_company' 
-			and value=%s""", self.doc.name)
-			
-	def before_rename(self, olddn, newdn, merge=False):
-		if merge:
-			frappe.throw(_("Sorry, companies cannot be merged"))
-	
-	def after_rename(self, olddn, newdn, merge=False):
-		frappe.db.set(self.doc, "company_name", newdn)
-
-		frappe.db.sql("""update `tabDefaultValue` set defvalue=%s 
-			where defkey='Company' and defvalue=%s""", (newdn, olddn))
-
-		frappe.defaults.clear_cache()
-
 @frappe.whitelist()
 def replace_abbr(company, old, new):
 	frappe.db.set_value("Company", company, "abbr", new)
diff --git a/erpnext/setup/doctype/company/company.txt b/erpnext/setup/doctype/company/company.txt
index 4832ae0..3aca525 100644
--- a/erpnext/setup/doctype/company/company.txt
+++ b/erpnext/setup/doctype/company/company.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-04-10 08:35:39", 
   "docstatus": 0, 
-  "modified": "2014-01-30 16:32:41", 
+  "modified": "2014-03-05 14:54:29", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -87,6 +87,28 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "charts_section", 
+  "fieldtype": "Section Break", 
+  "label": "Chart of Accounts"
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "country", 
+  "fieldtype": "Link", 
+  "in_list_view": 1, 
+  "label": "Country", 
+  "options": "Country", 
+  "reqd": 0
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "chart_of_accounts", 
+  "fieldtype": "Link", 
+  "label": "Chart of Accounts", 
+  "options": "Chart of Accounts"
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "default_settings", 
   "fieldtype": "Section Break", 
   "label": "Default Settings", 
diff --git a/erpnext/setup/doctype/company/test_company.py b/erpnext/setup/doctype/company/test_company.py
index bae6fc9..e21ebfd 100644
--- a/erpnext/setup/doctype/company/test_company.py
+++ b/erpnext/setup/doctype/company/test_company.py
@@ -3,6 +3,25 @@
 
 test_ignore = ["Account", "Cost Center"]
 
+import frappe
+import unittest
+
+class TestCompany(unittest.TestCase):
+	def test_coa(self):
+		company_bean = frappe.bean({
+			"doctype": "Company",
+			"company_name": "_Test Company 2",
+			"abbr": "_TC2",
+			"default_currency": "INR",
+			"country": "India",
+			"chart_of_accounts": "India - Chart of Accounts for Public Ltd"
+		})
+
+		company_bean.insert()
+		
+		self.assertTrue(frappe.db.get_value("Account", "Balance Sheet - _TC2"))
+		
+
 test_records = [
 	[{
 		"doctype": "Company",
diff --git a/erpnext/setup/doctype/country/country.txt b/erpnext/setup/doctype/country/country.txt
index 991c8a9..57d2025 100644
--- a/erpnext/setup/doctype/country/country.txt
+++ b/erpnext/setup/doctype/country/country.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-19 10:23:30", 
   "docstatus": 0, 
-  "modified": "2013-12-20 19:24:00", 
+  "modified": "2014-03-05 14:36:16", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -63,6 +63,12 @@
   "label": "Time Zones"
  }, 
  {
+  "doctype": "DocField", 
+  "fieldname": "code", 
+  "fieldtype": "Data", 
+  "label": "Code"
+ }, 
+ {
   "amend": 0, 
   "cancel": 0, 
   "create": 1,