[Fix] Patch for budget_against
This commit is contained in:
parent
2e0fbdeebe
commit
482fa672c2
@ -14,7 +14,8 @@ class DuplicateBudgetError(frappe.ValidationError): pass
|
|||||||
|
|
||||||
class Budget(Document):
|
class Budget(Document):
|
||||||
def autoname(self):
|
def autoname(self):
|
||||||
self.name = make_autoname(self.get(frappe.scrub(self.budget_against)) + "/" + self.fiscal_year + "/.###")
|
self.name = make_autoname(self.get(frappe.scrub(self.budget_against))
|
||||||
|
+ "/" + self.fiscal_year + "/.###")
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
if not self.get(frappe.scrub(self.budget_against)):
|
if not self.get(frappe.scrub(self.budget_against)):
|
||||||
@ -58,32 +59,36 @@ def validate_expense_against_budget(args):
|
|||||||
if not args.cost_center and not args.project:
|
if not args.cost_center and not args.project:
|
||||||
return
|
return
|
||||||
for budget_against in [args.project, args.cost_center]:
|
for budget_against in [args.project, args.cost_center]:
|
||||||
if budget_against:
|
if budget_against \
|
||||||
if frappe.db.get_value("Account", {"name": args.account, "root_type": "Expense"}):
|
and frappe.db.get_value("Account", {"name": args.account, "root_type": "Expense"}):
|
||||||
|
|
||||||
if args.project:
|
if args.project:
|
||||||
budget_against_field = "b.project"
|
|
||||||
condition = "and exists(select name from `tabProject` where name=b.project)"
|
condition = "and exists(select name from `tabProject` where name=b.project)"
|
||||||
args.update({"budget_against_field":"Project"})
|
args.budget_against_field = "Project"
|
||||||
args.update({"budget_against":budget_against})
|
|
||||||
|
|
||||||
elif args.cost_center:
|
elif args.cost_center:
|
||||||
cc_lft, cc_rgt = frappe.db.get_value("Cost Center", args.cost_center, ["lft", "rgt"])
|
cc_lft, cc_rgt = frappe.db.get_value("Cost Center", args.cost_center, ["lft", "rgt"])
|
||||||
budget_against_field = "b.cost_center"
|
|
||||||
condition = """and exists(select name from `tabCost Center`
|
condition = """and exists(select name from `tabCost Center`
|
||||||
where lft<=%s and rgt>=%s and name=b.cost_center)""" % (cc_lft, cc_rgt)
|
where lft<=%s and rgt>=%s and name=b.cost_center)""" % (cc_lft, cc_rgt)
|
||||||
args.update({"budget_against_field":"Cost Center"})
|
args.budget_against_field = "Cost Center"
|
||||||
args.update({"budget_against":budget_against})
|
|
||||||
|
args.budget_against = budget_against
|
||||||
|
|
||||||
budget_records = frappe.db.sql("""
|
budget_records = frappe.db.sql("""
|
||||||
select
|
select
|
||||||
{budget_against_field}, ba.budget_amount, b.monthly_distribution,
|
b.{budget_against_field}, ba.budget_amount, b.monthly_distribution,
|
||||||
b.action_if_annual_budget_exceeded, b.action_if_accumulated_monthly_budget_exceeded
|
b.action_if_annual_budget_exceeded,
|
||||||
|
b.action_if_accumulated_monthly_budget_exceeded
|
||||||
from
|
from
|
||||||
`tabBudget` b, `tabBudget Account` ba
|
`tabBudget` b, `tabBudget Account` ba
|
||||||
where
|
where
|
||||||
b.name=ba.parent and b.fiscal_year=%s and ba.account=%s and b.docstatus=1
|
b.name=ba.parent and b.fiscal_year=%s
|
||||||
|
and ba.account=%s and b.docstatus=1
|
||||||
{condition}
|
{condition}
|
||||||
""".format(condition=condition, budget_against_field=budget_against_field),(args.fiscal_year, args.account),
|
""".format(condition=condition,
|
||||||
as_dict=True)
|
budget_against_field=frappe.scrub(args.get("budget_against_field"))),
|
||||||
|
(args.fiscal_year, args.account), as_dict=True)
|
||||||
|
|
||||||
validate_budget_records(args, budget_records)
|
validate_budget_records(args, budget_records)
|
||||||
|
|
||||||
def validate_budget_records(args, budget_records):
|
def validate_budget_records(args, budget_records):
|
||||||
@ -91,15 +96,19 @@ def validate_budget_records(args, budget_records):
|
|||||||
if budget.budget_amount:
|
if budget.budget_amount:
|
||||||
yearly_action = budget.action_if_annual_budget_exceeded
|
yearly_action = budget.action_if_annual_budget_exceeded
|
||||||
monthly_action = budget.action_if_accumulated_monthly_budget_exceeded
|
monthly_action = budget.action_if_accumulated_monthly_budget_exceeded
|
||||||
|
|
||||||
if monthly_action in ["Stop", "Warn"]:
|
if monthly_action in ["Stop", "Warn"]:
|
||||||
budget_amount = get_accumulated_monthly_budget(budget.monthly_distribution,
|
budget_amount = get_accumulated_monthly_budget(budget.monthly_distribution,
|
||||||
args.posting_date, args.fiscal_year, budget.budget_amount)
|
args.posting_date, args.fiscal_year, budget.budget_amount)
|
||||||
args["month_end_date"] = get_last_day(args.posting_date)
|
args["month_end_date"] = get_last_day(args.posting_date)
|
||||||
compare_expense_with_budget(args, budget_amount, _("Accumulated Monthly"), monthly_action)
|
|
||||||
|
compare_expense_with_budget(args, budget_amount,
|
||||||
|
_("Accumulated Monthly"), monthly_action)
|
||||||
|
|
||||||
if yearly_action in ("Stop", "Warn") and monthly_action != "Stop" \
|
if yearly_action in ("Stop", "Warn") and monthly_action != "Stop" \
|
||||||
and yearly_action != monthly_action:
|
and yearly_action != monthly_action:
|
||||||
compare_expense_with_budget(args, flt(budget.budget_amount), _("Annual"), yearly_action)
|
compare_expense_with_budget(args, flt(budget.budget_amount),
|
||||||
|
_("Annual"), yearly_action)
|
||||||
|
|
||||||
|
|
||||||
def compare_expense_with_budget(args, budget_amount, action_for, action):
|
def compare_expense_with_budget(args, budget_amount, action_for, action):
|
||||||
@ -108,9 +117,11 @@ def compare_expense_with_budget(args, budget_amount, action_for, action):
|
|||||||
diff = actual_expense - budget_amount
|
diff = actual_expense - budget_amount
|
||||||
currency = frappe.db.get_value('Company', args.company, 'default_currency')
|
currency = frappe.db.get_value('Company', args.company, 'default_currency')
|
||||||
|
|
||||||
msg = _("{0} Budget for Account {1} against {2} {3} is {4}. It will exceed by {5}").format(_(action_for),
|
msg = _("{0} Budget for Account {1} against {2} {3} is {4}. It will exceed by {5}").format(
|
||||||
frappe.bold(args.account), args.budget_against_field, frappe.bold(args.budget_against),
|
_(action_for), frappe.bold(args.account), args.budget_against_field,
|
||||||
frappe.bold(fmt_money(budget_amount, currency=currency)), frappe.bold(fmt_money(diff, currency=currency)))
|
frappe.bold(args.budget_against),
|
||||||
|
frappe.bold(fmt_money(budget_amount, currency=currency)),
|
||||||
|
frappe.bold(fmt_money(diff, currency=currency)))
|
||||||
|
|
||||||
if action=="Stop":
|
if action=="Stop":
|
||||||
frappe.throw(msg, BudgetError)
|
frappe.throw(msg, BudgetError)
|
||||||
@ -119,24 +130,27 @@ def compare_expense_with_budget(args, budget_amount, action_for, action):
|
|||||||
|
|
||||||
|
|
||||||
def get_actual_expense(args):
|
def get_actual_expense(args):
|
||||||
condition1 = " and gle.posting_date <= %(month_end_date)s" if args.get("month_end_date") else ""
|
condition1 = " and gle.posting_date <= %(month_end_date)s" \
|
||||||
|
if args.get("month_end_date") else ""
|
||||||
if args.budget_against_field == "Cost Center":
|
if args.budget_against_field == "Cost Center":
|
||||||
lft_rgt = frappe.db.get_value(args.budget_against_field, args.budget_against, ["lft", "rgt"], as_dict=1)
|
lft_rgt = frappe.db.get_value(args.budget_against_field,
|
||||||
|
args.budget_against, ["lft", "rgt"], as_dict=1)
|
||||||
args.update(lft_rgt)
|
args.update(lft_rgt)
|
||||||
condition2 = """and exists(select name from `tabCost Center`
|
condition2 = """and exists(select name from `tabCost Center`
|
||||||
where lft>=%(lft)s and rgt<=%(rgt)s and name=gle.cost_center)"""
|
where lft>=%(lft)s and rgt<=%(rgt)s and name=gle.cost_center)"""
|
||||||
|
|
||||||
elif args.budget_against_field == "Project":
|
elif args.budget_against_field == "Project":
|
||||||
condition2 = "and exists(select name from `tabProject` where name=gle.project)"
|
condition2 = "and exists(select name from `tabProject` where name=gle.project)"
|
||||||
|
|
||||||
return flt(frappe.db.sql("""
|
return flt(frappe.db.sql("""
|
||||||
select sum(gle.debit) - sum(gle.credit)
|
select sum(gle.debit) - sum(gle.credit)
|
||||||
from `tabGL Entry` gle
|
from `tabGL Entry` gle
|
||||||
where gle.account=%(account)s
|
where gle.account=%(account)s
|
||||||
{condition2}
|
{condition1}
|
||||||
and gle.fiscal_year=%(fiscal_year)s
|
and gle.fiscal_year=%(fiscal_year)s
|
||||||
and gle.company=%(company)s
|
and gle.company=%(company)s
|
||||||
and gle.docstatus=1
|
and gle.docstatus=1
|
||||||
{condition1}
|
{condition2}
|
||||||
""".format(condition1=condition1, condition2=condition2), (args))[0][0])
|
""".format(condition1=condition1, condition2=condition2), (args))[0][0])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -354,3 +354,4 @@ erpnext.patches.v7_1.rename_quality_inspection_field
|
|||||||
erpnext.patches.v7_0.update_autoname_field
|
erpnext.patches.v7_0.update_autoname_field
|
||||||
erpnext.patches.v7_1.update_bom_base_currency
|
erpnext.patches.v7_1.update_bom_base_currency
|
||||||
erpnext.patches.v7_0.update_status_of_po_so
|
erpnext.patches.v7_0.update_status_of_po_so
|
||||||
|
erpnext.patches.v7_1.set_budget_against_as_cost_center
|
||||||
10
erpnext/patches/v7_1/set_budget_against_as_cost_center.py
Normal file
10
erpnext/patches/v7_1/set_budget_against_as_cost_center.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import frappe
|
||||||
|
|
||||||
|
def execute():
|
||||||
|
frappe.reload_doc("account", "doctype", "budget")
|
||||||
|
frappe.db.sql("""
|
||||||
|
update
|
||||||
|
`tabBudget`
|
||||||
|
set
|
||||||
|
budget_against = 'Cost Center'
|
||||||
|
""")
|
||||||
Loading…
x
Reference in New Issue
Block a user