From 33d3696385b914a6d2a853db48091a0bf9522cfd Mon Sep 17 00:00:00 2001 From: pandiyan Date: Wed, 29 Jul 2026 06:12:17 +0530 Subject: [PATCH] refactor: reuse shared date range validation across reports --- .../tax_withholding_details.py | 5 ++--- .../tds_computation_summary.py | 4 ++-- erpnext/accounts/report/utils.py | 14 ++++++++++++++ .../batch_wise_balance_history.py | 4 ++-- .../cogs_by_item_group/cogs_by_item_group.py | 4 ++-- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py index 99ac592097b..72f80b62dd1 100644 --- a/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py +++ b/erpnext/accounts/report/tax_withholding_details/tax_withholding_details.py @@ -7,6 +7,7 @@ from frappe import _ from frappe.utils import flt, getdate from pypika import Tuple +from erpnext.accounts.report.utils import validate_mandatory_date_range from erpnext.accounts.utils import get_currency_precision @@ -33,9 +34,7 @@ def execute(filters=None): def validate_filters(filters): """Validate if dates are properly set""" - filters = frappe._dict(filters or {}) - if filters.from_date > filters.to_date: - frappe.throw(_("From Date must be before To Date")) + validate_mandatory_date_range(filters or {}) def get_result(filters, tds_accounts, tax_category_map, net_total_map): diff --git a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py index a1b9c22f63f..5e7c5d46a98 100644 --- a/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py +++ b/erpnext/accounts/report/tds_computation_summary/tds_computation_summary.py @@ -5,6 +5,7 @@ from erpnext.accounts.report.tax_withholding_details.tax_withholding_details imp get_result, get_tds_docs, ) +from erpnext.accounts.report.utils import validate_mandatory_date_range from erpnext.accounts.utils import get_fiscal_year @@ -33,8 +34,7 @@ def execute(filters=None): def validate_filters(filters): """Validate if dates are properly set and lie in the same fiscal year""" - if filters.from_date > filters.to_date: - frappe.throw(_("From Date must be before To Date")) + validate_mandatory_date_range(filters) from_year = get_fiscal_year(filters.from_date)[0] to_year = get_fiscal_year(filters.to_date)[0] diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py index 8d1730ab294..3661e787f41 100644 --- a/erpnext/accounts/report/utils.py +++ b/erpnext/accounts/report/utils.py @@ -1,4 +1,5 @@ import frappe +from frappe import _ from frappe.query_builder.custom import ConstantColumn from frappe.query_builder.functions import Sum from frappe.utils import flt, formatdate, get_datetime_str, get_table_name @@ -16,6 +17,19 @@ from erpnext.setup.utils import get_exchange_rate __exchange_rates = {} +def validate_mandatory_date_range(filters, from_field="from_date", to_field="to_date"): + from_date = filters.get(from_field) + to_date = filters.get(to_field) + + if not from_date or not to_date: + frappe.throw( + _("{0} and {1} are mandatory").format(frappe.bold(_("From Date")), frappe.bold(_("To Date"))) + ) + + if from_date > to_date: + frappe.throw(_("From Date must be before To Date")) + + def get_currency(filters): """ Returns a dictionary containing currency information. The keys of the dict are diff --git a/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py b/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py index e5cb69ff816..a50061de1e3 100644 --- a/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py +++ b/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py @@ -8,6 +8,7 @@ from frappe.utils import add_to_date, cint, flt, get_datetime, get_table_name, g from frappe.utils.deprecations import deprecated from pypika import functions as fn +from erpnext.accounts.report.utils import validate_mandatory_date_range from erpnext.stock.doctype.warehouse.warehouse import apply_warehouse_filter SLE_COUNT_LIMIT = 100_000 @@ -29,8 +30,7 @@ def execute(filters=None): _("Please select either the Item or Warehouse or Warehouse Type filter to generate the report.") ) - if filters.from_date > filters.to_date: - frappe.throw(_("From Date must be before To Date")) + validate_mandatory_date_range(filters) float_precision = cint(frappe.db.get_default("float_precision")) or 3 diff --git a/erpnext/stock/report/cogs_by_item_group/cogs_by_item_group.py b/erpnext/stock/report/cogs_by_item_group/cogs_by_item_group.py index 000aca9f43e..afae69c6ce0 100644 --- a/erpnext/stock/report/cogs_by_item_group/cogs_by_item_group.py +++ b/erpnext/stock/report/cogs_by_item_group/cogs_by_item_group.py @@ -9,6 +9,7 @@ from frappe import _ from frappe.utils import date_diff from erpnext.accounts.report.general_ledger.general_ledger import get_gl_entries +from erpnext.accounts.report.utils import validate_mandatory_date_range Filters = frappe._dict Row = frappe._dict @@ -34,8 +35,7 @@ def update_filters_with_account(filters: Filters) -> None: def validate_filters(filters: Filters) -> None: - if filters.from_date > filters.to_date: - frappe.throw(_("From Date must be before To Date")) + validate_mandatory_date_range(filters) def get_columns() -> Columns: