From e4bae765807b6477e4ad05c8ec78d790122f1db6 Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Wed, 5 Nov 2025 01:21:54 +0530 Subject: [PATCH] refactor: add budget start and end date field on the parent --- erpnext/accounts/doctype/budget/budget.json | 26 +++++++------ erpnext/accounts/doctype/budget/budget.py | 37 ++++++++++--------- .../accounts/doctype/budget/test_budget.py | 1 - .../budget_distribution.json | 8 ++-- 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/erpnext/accounts/doctype/budget/budget.json b/erpnext/accounts/doctype/budget/budget.json index e88daf00565..9565f16e673 100644 --- a/erpnext/accounts/doctype/budget/budget.json +++ b/erpnext/accounts/doctype/budget/budget.json @@ -17,7 +17,8 @@ "amended_from", "from_fiscal_year", "to_fiscal_year", - "distribution_type", + "budget_start_date", + "budget_end_date", "allocation_frequency", "budget_amount", "section_break_nwug", @@ -237,15 +238,6 @@ "read_only_depends_on": "eval: doc.revision_of", "reqd": 1 }, - { - "default": "Percent", - "fieldname": "distribution_type", - "fieldtype": "Select", - "label": "Distribution Type", - "options": "Amount\nPercent", - "read_only_depends_on": "eval: doc.revision_of", - "reqd": 1 - }, { "default": "Monthly", "fieldname": "allocation_frequency", @@ -298,13 +290,25 @@ "options": "Fiscal Year", "read_only_depends_on": "eval: doc.revision_of", "reqd": 1 + }, + { + "fieldname": "budget_start_date", + "fieldtype": "Date", + "hidden": 1, + "label": "Budget Start Date" + }, + { + "fieldname": "budget_end_date", + "fieldtype": "Date", + "hidden": 1, + "label": "Budget End Date" } ], "grid_page_length": 50, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2025-10-31 01:13:15.114440", + "modified": "2025-11-05 01:00:46.470251", "modified_by": "Administrator", "module": "Accounts", "name": "Budget", diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py index b9a0909ef45..2eff6cbac02 100644 --- a/erpnext/accounts/doctype/budget/budget.py +++ b/erpnext/accounts/doctype/budget/budget.py @@ -54,10 +54,11 @@ class Budget(Document): budget_against: DF.Literal["", "Cost Center", "Project"] budget_amount: DF.Currency budget_distribution: DF.Table[BudgetDistribution] + budget_end_date: DF.Date | None + budget_start_date: DF.Date | None company: DF.Link cost_center: DF.Link | None distribute_equally: DF.Check - distribution_type: DF.Literal["Amount", "Percent"] from_fiscal_year: DF.Link naming_series: DF.Literal["BUDGET-.YYYY.-"] project: DF.Link | None @@ -68,11 +69,23 @@ class Budget(Document): def validate(self): if not self.get(frappe.scrub(self.budget_against)): frappe.throw(_("{0} is mandatory").format(self.budget_against)) + self.set_fiscal_year_dates() self.validate_duplicate() self.validate_account() self.set_null_value() self.validate_applicable_for() + def set_fiscal_year_dates(self): + if self.from_fiscal_year: + self.budget_start_date = frappe.get_cached_value( + "Fiscal Year", self.from_fiscal_year, "year_start_date" + ) + + if self.to_fiscal_year: + self.budget_end_date = frappe.get_cached_value( + "Fiscal Year", self.to_fiscal_year, "year_end_date" + ) + def validate_duplicate(self): budget_against_field = frappe.scrub(self.budget_against) budget_against = self.get(budget_against_field) @@ -275,7 +288,7 @@ class Budget(Document): ) ) - if round(total_percent, 2) != 100: + if flt(abs(total_percent - 100), 2) > 0.10: frappe.throw( _("Total distribution percent must equal 100 (currently {0})").format(round(total_percent, 2)) ) @@ -361,6 +374,8 @@ def validate_expense_against_budget(args, expense_amount=0): b.budget_amount, b.from_fiscal_year, b.to_fiscal_year, + b.budget_start_date, + b.budget_end_date, IFNULL(b.applicable_on_material_request, 0) AS for_material_request, IFNULL(b.applicable_on_purchase_order, 0) AS for_purchase_order, IFNULL(b.applicable_on_booking_actual_expenses, 0) AS for_actual_expenses, @@ -375,12 +390,7 @@ def validate_expense_against_budget(args, expense_amount=0): WHERE b.company = %s AND b.docstatus = 1 - AND ( - %s BETWEEN - (SELECT year_start_date FROM `tabFiscal Year` WHERE name = b.from_fiscal_year) - AND - (SELECT year_end_date FROM `tabFiscal Year` WHERE name = b.to_fiscal_year) - ) + AND %s BETWEEN b.budget_start_date AND b.budget_end_date AND b.account = %s {condition} """, @@ -627,14 +637,7 @@ def get_actual_expense(args): budget_against_field = args.get("budget_against_field") condition1 = " and gle.posting_date <= %(month_end_date)s" if args.get("month_end_date") else "" - from_start, _ = frappe.get_cached_value( - "Fiscal Year", args.from_fiscal_year, ["year_start_date", "year_end_date"] - ) - _, to_end = frappe.get_cached_value( - "Fiscal Year", args.to_fiscal_year, ["year_start_date", "year_end_date"] - ) - - date_condition = f"and gle.posting_date between '{from_start}' and '{to_end}'" + date_condition = f"and gle.posting_date between '{args.budget_start_date}' and '{args.budget_end_date}'" if args.is_tree: lft_rgt = frappe.db.get_value( @@ -687,7 +690,7 @@ def get_accumulated_monthly_budget(budget_name, posting_date): .on(bd.parent == b.name) .select(Sum(bd.amount).as_("accumulated_amount")) .where(b.name == budget_name) - .where(bd.end_date >= posting_date) + .where(bd.start_date <= posting_date) .run(as_dict=True) ) diff --git a/erpnext/accounts/doctype/budget/test_budget.py b/erpnext/accounts/doctype/budget/test_budget.py index 1768d69349a..a6261e6188c 100644 --- a/erpnext/accounts/doctype/budget/test_budget.py +++ b/erpnext/accounts/doctype/budget/test_budget.py @@ -725,7 +725,6 @@ def make_budget(**args): budget.budget_against = budget_against budget.allocation_frequency = "Monthly" - budget.distribution_type = "Amount" budget.distribute_equally = args.get("distribute_equally", 1) if args.applicable_on_material_request: diff --git a/erpnext/accounts/doctype/budget_distribution/budget_distribution.json b/erpnext/accounts/doctype/budget_distribution/budget_distribution.json index 1a367010c98..85d14599cec 100644 --- a/erpnext/accounts/doctype/budget_distribution/budget_distribution.json +++ b/erpnext/accounts/doctype/budget_distribution/budget_distribution.json @@ -31,22 +31,20 @@ "fieldname": "amount", "fieldtype": "Currency", "in_list_view": 1, - "label": "Amount", - "read_only_depends_on": "eval:doc.end_date < frappe.datetime.get_today()" + "label": "Amount" }, { "fieldname": "percent", "fieldtype": "Percent", "in_list_view": 1, - "label": "Percent", - "read_only_depends_on": "eval:doc.end_date < frappe.datetime.get_today()" + "label": "Percent" } ], "grid_page_length": 50, "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2025-10-30 12:35:31.310931", + "modified": "2025-11-03 13:18:28.398198", "modified_by": "Administrator", "module": "Accounts", "name": "Budget Distribution",