[cleanup] [minor] budget code cleaned up with testcases

This commit is contained in:
Nabin Hait 2013-08-23 10:46:41 +05:30
parent 2b06aaa291
commit a391606a8f
4 changed files with 104 additions and 37 deletions

View File

@ -6,6 +6,21 @@ test_records = [
"doctype": "Budget Distribution",
"distribution_id": "_Test Distribution",
"fiscal_year": "_Test Fiscal Year 2013",
}, {
"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",
@ -45,26 +60,11 @@ test_records = [
"doctype": "Budget Distribution Detail",
"parentfield": "budget_distribution_details",
"month": "November",
"percentage_allocation": "8"
"percentage_allocation": "10"
}, {
"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",
"percentage_allocation": "10"
}]
]

View File

@ -8,6 +8,7 @@ import webnotes
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 @@ class TestJournalVoucher(unittest.TestCase):
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.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`""")
self.assertRaises(BudgetError, jv1.submit)
test_records = [
[{

View File

@ -65,14 +65,14 @@ def save_entries(gl_map, adv_adj, update_outstanding):
# 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)

View File

@ -394,17 +394,16 @@ def validate_expense_against_budget(args):
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 @@ def get_allocated_budget(distribution_id, posting_date, fiscal_year, yearly_budg
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 @@ def get_allocated_budget(distribution_id, posting_date, fiscal_year, yearly_budg
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]
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]