diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py index 2f2b549cf46..8e58f294505 100644 --- a/erpnext/accounts/doctype/budget/budget.py +++ b/erpnext/accounts/doctype/budget/budget.py @@ -510,7 +510,7 @@ def get_accumulated_monthly_budget(monthly_distribution, posting_date, fiscal_ye accumulated_percentage = 0.0 while dt <= getdate(posting_date): - if monthly_distribution: + if monthly_distribution and distribution: accumulated_percentage += distribution.get(getdate(dt).strftime("%B"), 0) else: accumulated_percentage += 100.0 / 12 diff --git a/erpnext/accounts/doctype/budget/test_budget.py b/erpnext/accounts/doctype/budget/test_budget.py index 6fa655c0a36..7c071da1acc 100644 --- a/erpnext/accounts/doctype/budget/test_budget.py +++ b/erpnext/accounts/doctype/budget/test_budget.py @@ -5,7 +5,11 @@ import unittest import frappe from frappe.utils import now_datetime, nowdate -from erpnext.accounts.doctype.budget.budget import BudgetError, get_actual_expense +from erpnext.accounts.doctype.budget.budget import ( + BudgetError, + get_accumulated_monthly_budget, + get_actual_expense, +) from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry from erpnext.accounts.utils import get_fiscal_year from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order @@ -47,10 +51,13 @@ class TestBudget(ERPNextTestSuite): frappe.db.set_value("Budget", budget.name, "action_if_accumulated_monthly_budget_exceeded", "Stop") + accumulated_limit = get_accumulated_monthly_budget( + budget.monthly_distribution, nowdate(), budget.fiscal_year, budget.accounts[0].budget_amount + ) jv = make_journal_entry( "_Test Account Cost for Goods Sold - _TC", "_Test Bank - _TC", - 40000, + accumulated_limit + 1, "_Test Cost Center - _TC", posting_date=nowdate(), ) @@ -67,10 +74,13 @@ class TestBudget(ERPNextTestSuite): frappe.db.set_value("Budget", budget.name, "action_if_accumulated_monthly_budget_exceeded", "Stop") + accumulated_limit = get_accumulated_monthly_budget( + budget.monthly_distribution, nowdate(), budget.fiscal_year, budget.accounts[0].budget_amount + ) jv = make_journal_entry( "_Test Account Cost for Goods Sold - _TC", "_Test Bank - _TC", - 40000, + accumulated_limit + 1, "_Test Cost Center - _TC", posting_date=nowdate(), ) @@ -139,7 +149,12 @@ class TestBudget(ERPNextTestSuite): frappe.db.set_value("Budget", budget.name, "action_if_accumulated_monthly_budget_exceeded", "Stop") frappe.db.set_value("Budget", budget.name, "fiscal_year", fiscal_year) - po = create_purchase_order(transaction_date=nowdate(), do_not_submit=True) + accumulated_limit = get_accumulated_monthly_budget( + budget.monthly_distribution, nowdate(), budget.fiscal_year, budget.accounts[0].budget_amount + ) + po = create_purchase_order( + transaction_date=nowdate(), qty=1, rate=accumulated_limit + 1, do_not_submit=True + ) po.set_missing_values() @@ -157,11 +172,13 @@ class TestBudget(ERPNextTestSuite): frappe.db.set_value("Budget", budget.name, "action_if_accumulated_monthly_budget_exceeded", "Stop") project = frappe.get_value("Project", {"project_name": "_Test Project"}) - + accumulated_limit = get_accumulated_monthly_budget( + budget.monthly_distribution, nowdate(), budget.fiscal_year, budget.accounts[0].budget_amount + ) jv = make_journal_entry( "_Test Account Cost for Goods Sold - _TC", "_Test Bank - _TC", - 40000, + accumulated_limit + 1, "_Test Cost Center - _TC", project=project, posting_date=nowdate(), @@ -276,10 +293,13 @@ class TestBudget(ERPNextTestSuite): budget = make_budget(budget_against="Cost Center", cost_center="_Test Company - _TC") frappe.db.set_value("Budget", budget.name, "action_if_accumulated_monthly_budget_exceeded", "Stop") + accumulated_limit = get_accumulated_monthly_budget( + budget.monthly_distribution, nowdate(), budget.fiscal_year, budget.accounts[0].budget_amount + ) jv = make_journal_entry( "_Test Account Cost for Goods Sold - _TC", "_Test Bank - _TC", - 40000, + accumulated_limit + 1, "_Test Cost Center 2 - _TC", posting_date=nowdate(), ) @@ -306,10 +326,13 @@ class TestBudget(ERPNextTestSuite): budget = make_budget(budget_against="Cost Center", cost_center=cost_center) frappe.db.set_value("Budget", budget.name, "action_if_accumulated_monthly_budget_exceeded", "Stop") + accumulated_limit = get_accumulated_monthly_budget( + budget.monthly_distribution, nowdate(), budget.fiscal_year, budget.accounts[0].budget_amount + ) jv = make_journal_entry( "_Test Account Cost for Goods Sold - _TC", "_Test Bank - _TC", - 40000, + accumulated_limit + 1, cost_center, posting_date=nowdate(), ) @@ -427,6 +450,7 @@ def make_budget(**args): monthly_distribution = frappe.get_doc("Monthly Distribution", "_Test Distribution") monthly_distribution.fiscal_year = fiscal_year + monthly_distribution.save() budget.fiscal_year = fiscal_year budget.monthly_distribution = "_Test Distribution"