[cleanup] [minor] budget code cleaned up with testcases
diff --git a/accounts/doctype/budget_distribution/test_budget_distribution.py b/accounts/doctype/budget_distribution/test_budget_distribution.py
index 629fd87..bf18cde 100644
--- a/accounts/doctype/budget_distribution/test_budget_distribution.py
+++ b/accounts/doctype/budget_distribution/test_budget_distribution.py
@@ -9,6 +9,21 @@
 	}, {
 		"doctype": "Budget Distribution Detail",
 		"parentfield": "budget_distribution_details",
+		"month": "January",
+		"percentage_allocation": "8"
+	}, {
+		"doctype": "Budget Distribution Detail",
+		"parentfield": "budget_distribution_details",
+		"month": "February",
+		"percentage_allocation": "8"
+	}, {
+		"doctype": "Budget Distribution Detail",
+		"parentfield": "budget_distribution_details",
+		"month": "March",
+		"percentage_allocation": "8"
+	}, {
+		"doctype": "Budget Distribution Detail",
+		"parentfield": "budget_distribution_details",
 		"month": "April",
 		"percentage_allocation": "8"
 	}, {
@@ -45,26 +60,11 @@
 		"doctype": "Budget Distribution Detail",
 		"parentfield": "budget_distribution_details",
 		"month": "November",
-		"percentage_allocation": "8"
-	}, {
-		"doctype": "Budget Distribution Detail",
-		"parentfield": "budget_distribution_details",
-		"month": "December",
-		"percentage_allocation": "8"
-	}, {
-		"doctype": "Budget Distribution Detail",
-		"parentfield": "budget_distribution_details",
-		"month": "January",
-		"percentage_allocation": "8"
-	}, {
-		"doctype": "Budget Distribution Detail",
-		"parentfield": "budget_distribution_details",
-		"month": "February",
 		"percentage_allocation": "10"
 	}, {
 		"doctype": "Budget Distribution Detail",
 		"parentfield": "budget_distribution_details",
-		"month": "March",
+		"month": "December",
 		"percentage_allocation": "10"
 	}]
 ]
\ No newline at end of file
diff --git a/accounts/doctype/journal_voucher/test_journal_voucher.py b/accounts/doctype/journal_voucher/test_journal_voucher.py
index 2065232..6c24c91 100644
--- a/accounts/doctype/journal_voucher/test_journal_voucher.py
+++ b/accounts/doctype/journal_voucher/test_journal_voucher.py
@@ -8,6 +8,7 @@
 
 class TestJournalVoucher(unittest.TestCase):
 	def test_journal_voucher_with_against_jv(self):
+		self.clear_account_balance()
 		jv_invoice = webnotes.bean(copy=test_records[2])
 		jv_invoice.insert()
 		jv_invoice.submit()
@@ -32,20 +33,87 @@
 		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
 			where against_jv=%s""", jv_invoice.doc.name))
 			
-	def test_budget(self):
+	def test_monthly_budget_crossed_ignore(self):
+		webnotes.conn.set_value("Company", "_Test Company", "monthly_bgt_flag", "Ignore")
+		self.clear_account_balance()
+		
+		jv = webnotes.bean(copy=test_records[0])
+		jv.doclist[2].account = "_Test Account Cost for Goods Sold - _TC"
+		jv.doclist[2].cost_center = "_Test Cost Center - _TC"
+		jv.doclist[2].debit = 20000.0
+		jv.doclist[1].credit = 20000.0
+		jv.insert()
+		jv.submit()
+		self.assertTrue(webnotes.conn.get_value("GL Entry", 
+			{"voucher_type": "Journal Voucher", "voucher_no": jv.doc.name}))
+			
+	def test_monthly_budget_crossed_stop(self):
 		from accounts.utils import BudgetError
 		webnotes.conn.set_value("Company", "_Test Company", "monthly_bgt_flag", "Stop")
+		self.clear_account_balance()
+		
+		jv = webnotes.bean(copy=test_records[0])
+		jv.doclist[2].account = "_Test Account Cost for Goods Sold - _TC"
+		jv.doclist[2].cost_center = "_Test Cost Center - _TC"
+		jv.doclist[2].debit = 20000.0
+		jv.doclist[1].credit = 20000.0
+		jv.insert()
+		
+		self.assertRaises(BudgetError, jv.submit)
+		
+		webnotes.conn.set_value("Company", "_Test Company", "monthly_bgt_flag", "Ignore")
+		
+	def test_yearly_budget_crossed_stop(self):
+		from accounts.utils import BudgetError
+		self.clear_account_balance()
+		self.test_monthly_budget_crossed_ignore()
+		
+		webnotes.conn.set_value("Company", "_Test Company", "yearly_bgt_flag", "Stop")
+		
+		jv = webnotes.bean(copy=test_records[0])
+		jv.doc.posting_date = "2013-08-12"
+		jv.doclist[2].account = "_Test Account Cost for Goods Sold - _TC"
+		jv.doclist[2].cost_center = "_Test Cost Center - _TC"
+		jv.doclist[2].debit = 150000.0
+		jv.doclist[1].credit = 150000.0
+		jv.insert()
+		
+		self.assertRaises(BudgetError, jv.submit)
+		
+		webnotes.conn.set_value("Company", "_Test Company", "yearly_bgt_flag", "Ignore")
+		
+	def test_monthly_budget_on_cancellation(self):
+		from accounts.utils import BudgetError
+		webnotes.conn.set_value("Company", "_Test Company", "monthly_bgt_flag", "Stop")
+		self.clear_account_balance()
+		
+		jv = webnotes.bean(copy=test_records[0])
+		jv.doclist[1].account = "_Test Account Cost for Goods Sold - _TC"
+		jv.doclist[1].cost_center = "_Test Cost Center - _TC"
+		jv.doclist[1].credit = 30000.0
+		jv.doclist[2].debit = 30000.0
+		jv.submit()
+		
+		self.assertTrue(webnotes.conn.get_value("GL Entry", 
+			{"voucher_type": "Journal Voucher", "voucher_no": jv.doc.name}))
 		
 		jv1 = webnotes.bean(copy=test_records[0])
-		jv1.doc.posting_date = "2013-02-12"
 		jv1.doclist[2].account = "_Test Account Cost for Goods Sold - _TC"
 		jv1.doclist[2].cost_center = "_Test Cost Center - _TC"
-		jv1.doclist[2].debit = 20000.0
-		jv1.doclist[1].credit = 20000.0
-		jv1.insert()
+		jv1.doclist[2].debit = 40000.0
+		jv1.doclist[1].credit = 40000.0
+		jv1.submit()
 		
-		self.assertRaises(BudgetError, jv1.submit)
-
+		self.assertTrue(webnotes.conn.get_value("GL Entry", 
+			{"voucher_type": "Journal Voucher", "voucher_no": jv1.doc.name}))
+		
+		self.assertRaises(BudgetError, jv.cancel)
+		
+		webnotes.conn.set_value("Company", "_Test Company", "monthly_bgt_flag", "Ignore")
+		
+	def clear_account_balance(self):
+		webnotes.conn.sql("""delete from `tabGL Entry`""")
+		
 
 test_records = [
 	[{
diff --git a/accounts/general_ledger.py b/accounts/general_ledger.py
index f2dc748..c415e2d 100644
--- a/accounts/general_ledger.py
+++ b/accounts/general_ledger.py
@@ -65,14 +65,14 @@
 		# toggle debit, credit if negative entry
 		if flt(entry["debit"]) < 0 or flt(entry["credit"]) < 0:
 			_swap(entry)
-
-		validate_expense_against_budget(entry)
 		
 		gle = Document('GL Entry', fielddata=entry)
 		gle_obj = webnotes.get_obj(doc=gle)
 		gle_obj.validate()
 		gle.save(1)
 		gle_obj.on_update(adv_adj, update_outstanding)
+		
+		validate_expense_against_budget(entry)
 
 		# update total debit / credit
 		total_debit += flt(gle.debit)
diff --git a/accounts/utils.py b/accounts/utils.py
index 4110c99..5cf9957 100644
--- a/accounts/utils.py
+++ b/accounts/utils.py
@@ -394,17 +394,16 @@
 					budget_amount = get_allocated_budget(budget[0].distribution_id, 
 						args.posting_date, args.fiscal_year, budget[0].budget_allocated)
 					
-					month_end_date = webnotes.conn.sql("select LAST_DAY(%s)", args.posting_date)
-					args["condition"] = " and posting_date<='%s'" % month_end_date
+					args["month_end_date"] = webnotes.conn.sql("select LAST_DAY(%s)", 
+						args.posting_date)[0][0]
 					action_for, action = "Monthly", monthly_action
 					
 				elif yearly_action in ["Stop", "Warn"]:
 					budget_amount = budget[0].budget_allocated
 					action_for, action = "Monthly", yearly_action
-				print budget_amount
+
 				if action_for:
 					actual_expense = get_actual_expense(args)
-					print actual_expense
 					if actual_expense > budget_amount:
 						webnotes.msgprint(action_for + _(" budget ") + cstr(budget_amount) + 
 							_(" for account ") + args.account + _(" against cost center ") + 
@@ -419,16 +418,13 @@
 			from `tabBudget Distribution Detail` bdd, `tabBudget Distribution` bd
 			where bdd.parent=bd.name and bd.fiscal_year=%s""", fiscal_year, as_dict=1):
 				distribution.setdefault(d.month, d.percentage_allocation)
-	print distribution
+
 	dt = webnotes.conn.get_value("Fiscal Year", fiscal_year, "year_start_date")
 	budget_percentage = 0.0
 	
 	while(dt <= getdate(posting_date)):
-		print dt, posting_date
 		if distribution_id:
-			print getdate(dt).month
-			print distribution.get(getdate(dt).month)
-			budget_percentage += distribution.get(getdate(dt).month, 0)
+			budget_percentage += distribution.get(getdate(dt).strftime("%B"), 0)
 		else:
 			budget_percentage += 100.0/12
 			
@@ -437,9 +433,12 @@
 	return yearly_budget * budget_percentage / 100
 				
 def get_actual_expense(args):
+	args["condition"] = " and posting_date<='%s'" % args.month_end_date \
+		if args.get("month_end_date") else ""
+		
 	return webnotes.conn.sql("""
 		select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
 		from `tabGL Entry`
-		where account=%(account)s and cost_center=%(cost_center)s 
-		and fiscal_year=%(fiscal_year)s and company=%(company)s %(condition)s
-	""", (args))[0][0]
\ No newline at end of file
+		where account='%(account)s' and cost_center='%(cost_center)s' 
+		and fiscal_year='%(fiscal_year)s' and company='%(company)s' %(condition)s
+	""" % (args))[0][0]
\ No newline at end of file