From 1cb03db43bfb5a55e67e9383a495e4dcb6242ef9 Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Fri, 31 Oct 2025 00:56:25 +0530 Subject: [PATCH] test: test cases to validate budget distribution and revision --- erpnext/accounts/doctype/budget/budget.py | 10 ++-- .../accounts/doctype/budget/test_budget.py | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py index e0eaff8b4a3..120d78b7430 100644 --- a/erpnext/accounts/doctype/budget/budget.py +++ b/erpnext/accounts/doctype/budget/budget.py @@ -81,11 +81,8 @@ class Budget(Document): if not account: return - from_start, _ = frappe.get_cached_value( - "Fiscal Year", self.from_fiscal_year, ["year_start_date", "year_end_date"] - ) - _, to_end = frappe.get_cached_value( - "Fiscal Year", self.to_fiscal_year, ["year_start_date", "year_end_date"] + year_start_date, year_end_date = get_fiscal_year_date_range( + self.from_fiscal_year, self.to_fiscal_year ) existing_budget = frappe.db.sql( @@ -103,12 +100,13 @@ class Budget(Document): AND (SELECT year_end_date FROM `tabFiscal Year` WHERE name = to_fiscal_year) >= %s ) """, - (self.company, budget_against, account, self.name, to_end, from_start), + (self.company, budget_against, account, self.name, year_end_date, year_start_date), as_dict=True, ) if existing_budget: d = existing_budget[0] + print(d) frappe.throw( _( "Another Budget record '{0}' already exists against {1} '{2}' and account '{3}' with overlapping fiscal years." diff --git a/erpnext/accounts/doctype/budget/test_budget.py b/erpnext/accounts/doctype/budget/test_budget.py index d2723bc0db0..6f1e751993b 100644 --- a/erpnext/accounts/doctype/budget/test_budget.py +++ b/erpnext/accounts/doctype/budget/test_budget.py @@ -570,6 +570,62 @@ class TestBudget(ERPNextTestSuite): self.assertEqual(total_child_amount, budget.budget_amount) + def test_fiscal_year_company_mismatch(self): + budget = make_budget(budget_against="Cost Center", do_not_save=True, submit_budget=False) + + fy = frappe.get_doc( + { + "doctype": "Fiscal Year", + "year": "2099", + "year_start_date": "2099-04-01", + "year_end_date": "2100-03-31", + "company": "_Test Company 2", + } + ).insert(ignore_permissions=True) + + budget.from_fiscal_year = fy.name + budget.to_fiscal_year = fy.name + + with self.assertRaises(frappe.ValidationError): + budget.save() + + def test_manual_distribution_total_equals_budget_amount(self): + budget = make_budget( + budget_against="Cost Center", + cost_center="_Test Cost Center - _TC", + distribute_equally=0, + budget_amount=12000, + do_not_save=False, + submit_budget=False, + ) + + for d in budget.budget_distribution: + d.amount = 2000 + + with self.assertRaises(frappe.ValidationError): + budget.save() + + def test_duplicate_budget_validation(self): + make_budget( + budget_against="Cost Center", + distribute_equally=0, + budget_amount=15000, + do_not_save=False, + submit_budget=False, + ) + + budget = frappe.new_doc("Budget") + budget.company = "_Test Company" + budget.from_fiscal_year = frappe.db.get_value("Fiscal Year", {}, "name") + budget.to_fiscal_year = budget.from_fiscal_year + budget.budget_against = "Cost Center" + budget.cost_center = "_Test Cost Center - _TC" + budget.account = "_Test Account Cost for Goods Sold - _TC" + budget.budget_amount = 10000 + + with self.assertRaises(frappe.ValidationError): + budget.insert() + def set_total_expense_zero(posting_date, budget_against_field=None, budget_against_CC=None): if budget_against_field == "project":