fixes in test cases and added fiscal year field to stock reco
diff --git a/accounts/doctype/fiscal_year/test_fiscal_year.py b/accounts/doctype/fiscal_year/test_fiscal_year.py
index b209b39..456b15a 100644
--- a/accounts/doctype/fiscal_year/test_fiscal_year.py
+++ b/accounts/doctype/fiscal_year/test_fiscal_year.py
@@ -3,6 +3,11 @@
 test_records = [
 	[{
 		"doctype": "Fiscal Year", 
+		"year": "_Test Fiscal Year 2012", 
+		"year_start_date": "2012-01-01"
+	}],
+	[{
+		"doctype": "Fiscal Year", 
 		"year": "_Test Fiscal Year 2013", 
 		"year_start_date": "2013-01-01"
 	}],
diff --git a/patches/march_2013/p03_update_buying_amount.py b/patches/march_2013/p03_update_buying_amount.py
index e45a3db..ae0c5ea 100644
--- a/patches/march_2013/p03_update_buying_amount.py
+++ b/patches/march_2013/p03_update_buying_amount.py
@@ -3,8 +3,8 @@
 def execute():
 	dn_list = webnotes.conn.sql("""select name from `tabDelivery Note` where docstatus < 2""")
 	for dn in dn_list:
-		webnotes.bean("Delivery Note", dn[0]).set_buying_amount()
+		webnotes.bean("Delivery Note", dn[0]).run_method("set_buying_amount")
 		
 	si_list = webnotes.conn.sql("""select name from `tabSales Invoice` where docstatus < 2""")
 	for si in si_list:
-		webnotes.bean("Sales Invoice", si[0]).set_buying_amount()
\ No newline at end of file
+		webnotes.bean("Sales Invoice", si[0]).run_method("set_buying_amount")
\ No newline at end of file
diff --git a/patches/march_2013/p07_update_valuation_rate.py b/patches/march_2013/p07_update_valuation_rate.py
index 0bce8a4..51e556b 100644
--- a/patches/march_2013/p07_update_valuation_rate.py
+++ b/patches/march_2013/p07_update_valuation_rate.py
@@ -1,7 +1,7 @@
 import webnotes
 
 def execute():
-	for purchase_invoice in webnotes.conn.sql("""select distinct parent 
+	for purchase_invoice in webnotes.conn.sql_list("""select distinct parent 
 		from `tabPurchase Invoice Item` where docstatus = 1 and ifnull(valuation_rate, 0)=0"""):
 		pi = webnotes.get_obj("Purchase Invoice", purchase_invoice)
 		pi.calculate_taxes_and_totals()
diff --git a/patches/march_2013/p08_create_aii_accounts.py b/patches/march_2013/p08_create_aii_accounts.py
index c39c206..ff53d8b 100644
--- a/patches/march_2013/p08_create_aii_accounts.py
+++ b/patches/march_2013/p08_create_aii_accounts.py
@@ -1,24 +1,59 @@
 import webnotes
 def execute():
+	add_group_accounts()
+	add_ledger_accounts()
+	
+def _check(parent_account, company):
+	def _get_root(is_pl_account, debit_or_credit):
+		res = webnotes.conn.sql("""select name from `tabAccount`
+			where company=%s and is_pl_account = %s and debit_or_credit = %s
+			and ifnull(parent_account, "") ="" """, (company, is_pl_account, debit_or_credit))
+		return res and res[0][0] or None
+		
+	if not webnotes.conn.exists("Account", parent_account):
+		if parent_account.startswith("Current Assets"):
+			parent_account = _get_root("No", "Debit")
+		elif parent_account.startswith("Direct Expenses"):
+			parent_account = _get_root("Yes", "Debit")
+		elif parent_account.startswith("Current Liabilities"):
+			parent_account = _get_root("No", "Credit")
+
+	return parent_account
+	
+	
+def add_group_accounts():
 	accounts_to_add = [
 		["Stock Assets", "Current Assets", "Group", ""],
+		["Stock Expenses", "Direct Expenses", "Group", "Expense Account"],
+		["Stock Liabilities", "Current Liabilities", "Group", ""],
+	]
+		
+	add_accounts(accounts_to_add, _check)
+	
+	
+def add_ledger_accounts():
+	accounts_to_add = [
 		["Stock In Hand", "Stock Assets", "Ledger", ""],
 		["Stock Debit But Not Billed", "Stock Assets", "Ledger", ""],
-		["Stock Expenses", "Direct Expenses", "Group", "Expense Account"],
 		["Cost of Goods Sold", "Stock Expenses", "Ledger", "Expense Account"],
 		["Stock Adjustment", "Stock Expenses", "Ledger", "Expense Account"],
 		["Expenses Included In Valuation", "Stock Expenses", "Ledger", "Expense Account"],
-		["Stock Liabilities", "Current Liabilities", "Group", ""],
 		["Stock Received But Not Billed", "Stock Liabilities", "Ledger", ""],
 	]
+	add_accounts(accounts_to_add)
 	
-	for company, abbr in webnotes.conn.sql_list("""select name, abbr from `tabCompany`"""):
+	
+def add_accounts(accounts_to_add, check_fn=None):	
+	for company, abbr in webnotes.conn.sql("""select name, abbr from `tabCompany`"""):
 		for account_name, parent_account_name, group_or_ledger, account_type in accounts_to_add:
 			if not webnotes.conn.exists("Account", "%s - %s" % (account_name, abbr)):
+				parent_account = "%s - %s" % (parent_account_name, abbr)
+				if check_fn:
+					parent_account = check_fn(parent_account, company)
 				account = webnotes.bean({
 					"doctype": "Account",
 					"account_name": account_name,
-					"parent_account": "%s - %s" % (parent_account_name, abbr),
+					"parent_account": parent_account,
 					"group_or_ledger": group_or_ledger,
 					"account_type": account_type,
 					"company": company
diff --git a/stock/doctype/stock_entry/stock_entry.py b/stock/doctype/stock_entry/stock_entry.py
index c437135..a176188 100644
--- a/stock/doctype/stock_entry/stock_entry.py
+++ b/stock/doctype/stock_entry/stock_entry.py
@@ -610,7 +610,8 @@
 			'is_cancelled': (is_cancelled ==1) and 'Yes' or 'No',
 			'batch_no': cstr(d.batch_no).strip(),
 			'serial_no': cstr(d.serial_no).strip(),
-			"project": self.doc.project_name
+			"project": self.doc.project_name,
+			"fiscal_year": self.doc.fiscal_year,
 		})
 	
 	def get_cust_values(self):
diff --git a/stock/doctype/stock_entry/test_stock_entry.py b/stock/doctype/stock_entry/test_stock_entry.py
index 3f8012a..7c406f8 100644
--- a/stock/doctype/stock_entry/test_stock_entry.py
+++ b/stock/doctype/stock_entry/test_stock_entry.py
@@ -230,6 +230,7 @@
 		se.doc.purpose = "Sales Return"
 		se.doc.sales_invoice_no = si.doc.name
 		se.doc.posting_date = "2013-03-10"
+		se.doc.fiscal_year = "_Test Fiscal Year 2013"
 		se.doclist[1].item_code = "_Test Item Home Desktop 200"
 		se.doclist[1].qty = returned_qty
 		se.doclist[1].transfer_qty = returned_qty
@@ -241,6 +242,7 @@
 		se = webnotes.bean(copy=test_records[0])
 		se.doc.purpose = "Sales Return"
 		se.doc.posting_date = "2013-03-10"
+		se.doc.fiscal_year = "_Test Fiscal Year 2013"
 		se.doc.sales_invoice_no = si.doc.name
 		se.doclist[1].qty = returned_qty
 		se.doclist[1].transfer_qty = returned_qty
@@ -300,6 +302,7 @@
 		se.doc.purpose = "Sales Return"
 		se.doc.delivery_note_no = dn.doc.name
 		se.doc.posting_date = "2013-03-10"
+		se.doc.fiscal_year = "_Test Fiscal Year 2013"
 		se.doclist[1].qty = se.doclist[1].transfer_qty = returned_qty
 		
 		se.insert()
@@ -399,6 +402,7 @@
 		se.doc.purpose = "Sales Return"
 		se.doc.delivery_note_no = dn.doc.name
 		se.doc.posting_date = "2013-03-10"
+		se.doc.fiscal_year = "_Test Fiscal Year 2013"
 		se.doclist[1].qty = se.doclist[1].transfer_qty = returned_qty
 
 		se.insert()
@@ -449,6 +453,7 @@
 		se.doc.purpose = "Purchase Return"
 		se.doc.purchase_receipt_no = pr.doc.name
 		se.doc.posting_date = "2013-03-01"
+		se.doc.fiscal_year = "_Test Fiscal Year 2013"
 		se.doclist[1].qty = se.doclist[1].transfer_qty = 5
 		se.doclist[1].s_warehouse = "_Test Warehouse"
 		se.insert()
@@ -471,6 +476,7 @@
 		se.doc.purpose = "Purchase Return"
 		se.doc.purchase_receipt_no = pr_docname
 		se.doc.posting_date = "2013-03-01"
+		se.doc.fiscal_year = "_Test Fiscal Year 2013"
 		se.doclist[1].qty = se.doclist[1].transfer_qty = 6
 		se.doclist[1].s_warehouse = "_Test Warehouse"
 		
@@ -547,6 +553,7 @@
 		se.doc.purpose = "Purchase Return"
 		se.doc.purchase_receipt_no = pr.doc.name
 		se.doc.posting_date = "2013-03-01"
+		se.doc.fiscal_year = "_Test Fiscal Year 2013"
 		se.doclist[1].qty = se.doclist[1].transfer_qty = 5
 		se.doclist[1].s_warehouse = "_Test Warehouse"
 		se.insert()
diff --git a/stock/doctype/stock_ledger_entry/stock_ledger_entry.py b/stock/doctype/stock_ledger_entry/stock_ledger_entry.py
index 5ca7dd5..4262113 100644
--- a/stock/doctype/stock_ledger_entry/stock_ledger_entry.py
+++ b/stock/doctype/stock_ledger_entry/stock_ledger_entry.py
@@ -16,17 +16,13 @@
 
 from __future__ import unicode_literals
 import webnotes
-from webnotes import _
-
+from webnotes import _, msgprint
 from webnotes.utils import cint, flt, getdate
-
-sql = webnotes.conn.sql
-msgprint = webnotes.msgprint
-from accounts.utils import get_fiscal_year
+from webnotes.model.controller import DocListController
 
 class InvalidWarehouseCompany(Exception): pass
 
-class DocType:
+class DocType(DocListController):
 	def __init__(self, doc, doclist=[]):
 		self.doc = doc
 		self.doclist = doclist
@@ -39,12 +35,14 @@
 		self.actual_amt_check()
 		self.check_stock_frozen_date()
 		self.scrub_posting_time()
-		self.doc.fiscal_year = get_fiscal_year(self.doc.posting_date)[0]
+		
+		from accounts.utils import validate_fiscal_year
+		validate_fiscal_year(self.doc.posting_date, self.doc.fiscal_year, self.meta.get_label("posting_date"))
 		
 	#check for item quantity available in stock
 	def actual_amt_check(self):
 		if self.doc.batch_no:
-			batch_bal = flt(sql("select sum(actual_qty) from `tabStock Ledger Entry` where warehouse = '%s' and item_code = '%s' and batch_no = '%s'"%(self.doc.warehouse,self.doc.item_code,self.doc.batch_no))[0][0])
+			batch_bal = flt(webnotes.conn.sql("select sum(actual_qty) from `tabStock Ledger Entry` where warehouse = '%s' and item_code = '%s' and batch_no = '%s'"%(self.doc.warehouse,self.doc.item_code,self.doc.batch_no))[0][0])
 			self.doc.fields.update({'batch_bal': batch_bal})
 
 			if (batch_bal + self.doc.actual_qty) < 0:
@@ -77,11 +75,11 @@
 			if self.doc.fields.get(k)==None:
 				msgprint("Stock Ledger Entry: '%s' is mandatory" % k, raise_exception = 1)
 			elif k == 'warehouse':
-				if not sql("select name from tabWarehouse where name = '%s'" % self.doc.fields.get(k)):
+				if not webnotes.conn.sql("select name from tabWarehouse where name = '%s'" % self.doc.fields.get(k)):
 					msgprint("Warehouse: '%s' does not exist in the system. Please check." % self.doc.fields.get(k), raise_exception = 1)
 
 	def validate_item(self):
-		item_det = sql("""select name, has_batch_no, docstatus, 
+		item_det = webnotes.conn.sql("""select name, has_batch_no, docstatus, 
 			ifnull(is_stock_item, 'No') from tabItem where name=%s""", 
 			self.doc.item_code)
 
@@ -106,7 +104,7 @@
 				raise Exception
 		
 			# check if batch belongs to item
-			if not sql("select name from `tabBatch` where item='%s' and name ='%s' and docstatus != 2" % (self.doc.item_code, self.doc.batch_no)):
+			if not webnotes.conn.sql("select name from `tabBatch` where item='%s' and name ='%s' and docstatus != 2" % (self.doc.item_code, self.doc.batch_no)):
 				msgprint("'%s' is not a valid Batch Number for Item '%s'" % (self.doc.batch_no, self.doc.item_code), raise_exception = 1)
 	
 	# Nobody can do SL Entries where posting date is before freezing date except authorized person
diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.py b/stock/doctype/stock_reconciliation/stock_reconciliation.py
index 13c8ee4..5604179 100644
--- a/stock/doctype/stock_reconciliation/stock_reconciliation.py
+++ b/stock/doctype/stock_reconciliation/stock_reconciliation.py
@@ -252,7 +252,8 @@
 			"voucher_no": self.doc.name,
 			"company": self.doc.company,
 			"is_cancelled": "No",
-			"voucher_detail_no": row.voucher_detail_no
+			"voucher_detail_no": row.voucher_detail_no,
+			"fiscal_year": self.doc.fiscal_year,
 		})
 		args.update(opts)
 		# create stock ledger entry
diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.txt b/stock/doctype/stock_reconciliation/stock_reconciliation.txt
index 9137cae..760620b 100644
--- a/stock/doctype/stock_reconciliation/stock_reconciliation.txt
+++ b/stock/doctype/stock_reconciliation/stock_reconciliation.txt
@@ -1,8 +1,8 @@
 [
  {
-  "creation": "2013-01-22 16:50:41", 
+  "creation": "2013-03-26 06:51:17", 
   "docstatus": 0, 
-  "modified": "2013-03-18 12:48:42", 
+  "modified": "2013-03-26 08:32:03", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -30,7 +30,6 @@
   "permlevel": 0
  }, 
  {
-  "amend": 1, 
   "cancel": 1, 
   "create": 1, 
   "doctype": "DocPerm", 
@@ -41,7 +40,6 @@
   "permlevel": 0, 
   "read": 1, 
   "report": 1, 
-  "role": "Material Manager", 
   "submit": 1, 
   "write": 1
  }, 
@@ -83,6 +81,15 @@
  }, 
  {
   "doctype": "DocField", 
+  "fieldname": "fiscal_year", 
+  "fieldtype": "Select", 
+  "label": "Fiscal Year", 
+  "options": "link:Fiscal Year", 
+  "print_hide": 1, 
+  "reqd": 1
+ }, 
+ {
+  "doctype": "DocField", 
   "fieldname": "company", 
   "fieldtype": "Link", 
   "label": "Company", 
@@ -145,6 +152,12 @@
   "print_hide": 1
  }, 
  {
-  "doctype": "DocPerm"
+  "amend": 0, 
+  "doctype": "DocPerm", 
+  "role": "Material Manager"
+ }, 
+ {
+  "doctype": "DocPerm", 
+  "role": "System Manager"
  }
 ]
\ No newline at end of file
diff --git a/stock/doctype/stock_reconciliation/test_stock_reconciliation.py b/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
index cebc6ff..1e7e716 100644
--- a/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
+++ b/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
@@ -228,35 +228,40 @@
 				"voucher_type": "Stock Entry", "voucher_no": "TEST",
 				"item_code": "_Test Item", "warehouse": "_Test Warehouse",
 				"posting_date": "2012-12-12", "posting_time": "01:00",
-				"actual_qty": 20, "incoming_rate": 1000, "company": "_Test Company"
+				"actual_qty": 20, "incoming_rate": 1000, "company": "_Test Company",
+				"fiscal_year": "_Test Fiscal Year 2012",
 			},
 			{
 				"doctype": "Stock Ledger Entry", "__islocal": 1,
 				"voucher_type": "Stock Entry", "voucher_no": "TEST",
 				"item_code": "_Test Item", "warehouse": "_Test Warehouse",
 				"posting_date": "2012-12-15", "posting_time": "02:00",
-				"actual_qty": 10, "incoming_rate": 700, "company": "_Test Company"
+				"actual_qty": 10, "incoming_rate": 700, "company": "_Test Company",
+				"fiscal_year": "_Test Fiscal Year 2012",
 			},
 			{
 				"doctype": "Stock Ledger Entry", "__islocal": 1,
 				"voucher_type": "Stock Entry", "voucher_no": "TEST",
 				"item_code": "_Test Item", "warehouse": "_Test Warehouse",
 				"posting_date": "2012-12-25", "posting_time": "03:00",
-				"actual_qty": -15, "company": "_Test Company"
+				"actual_qty": -15, "company": "_Test Company",
+				"fiscal_year": "_Test Fiscal Year 2012",
 			},
 			{
 				"doctype": "Stock Ledger Entry", "__islocal": 1,
 				"voucher_type": "Stock Entry", "voucher_no": "TEST",
 				"item_code": "_Test Item", "warehouse": "_Test Warehouse",
 				"posting_date": "2012-12-31", "posting_time": "08:00",
-				"actual_qty": -20, "company": "_Test Company"
+				"actual_qty": -20, "company": "_Test Company",
+				"fiscal_year": "_Test Fiscal Year 2012",
 			},
 			{
 				"doctype": "Stock Ledger Entry", "__islocal": 1,
 				"voucher_type": "Stock Entry", "voucher_no": "TEST",
 				"item_code": "_Test Item", "warehouse": "_Test Warehouse",
 				"posting_date": "2013-01-05", "posting_time": "07:00",
-				"actual_qty": 15, "incoming_rate": 1200, "company": "_Test Company"
+				"actual_qty": 15, "incoming_rate": 1200, "company": "_Test Company",
+				"fiscal_year": "_Test Fiscal Year 2013",
 			},
 		]