Merge pull request #33216 from barredterra/validate-accounts-dates

refactor: validate dates in accounts module
diff --git a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py
index 4592421..69cfe31 100644
--- a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py
+++ b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py
@@ -9,10 +9,6 @@
 from frappe.utils import add_days, add_years, cstr, getdate
 
 
-class FiscalYearIncorrectDate(frappe.ValidationError):
-	pass
-
-
 class FiscalYear(Document):
 	@frappe.whitelist()
 	def set_as_default(self):
@@ -53,23 +49,18 @@
 					)
 
 	def validate_dates(self):
+		self.validate_from_to_dates("year_start_date", "year_end_date")
 		if self.is_short_year:
 			# Fiscal Year can be shorter than one year, in some jurisdictions
 			# under certain circumstances. For example, in the USA and Germany.
 			return
 
-		if getdate(self.year_start_date) > getdate(self.year_end_date):
-			frappe.throw(
-				_("Fiscal Year Start Date should be one year earlier than Fiscal Year End Date"),
-				FiscalYearIncorrectDate,
-			)
-
 		date = getdate(self.year_start_date) + relativedelta(years=1) - relativedelta(days=1)
 
 		if getdate(self.year_end_date) != date:
 			frappe.throw(
 				_("Fiscal Year End Date should be one year after Fiscal Year Start Date"),
-				FiscalYearIncorrectDate,
+				frappe.exceptions.InvalidDates,
 			)
 
 	def on_update(self):
diff --git a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py
index 6e946f7..181406b 100644
--- a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py
+++ b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py
@@ -7,8 +7,6 @@
 import frappe
 from frappe.utils import now_datetime
 
-from erpnext.accounts.doctype.fiscal_year.fiscal_year import FiscalYearIncorrectDate
-
 test_ignore = ["Company"]
 
 
@@ -26,7 +24,7 @@
 			}
 		)
 
-		self.assertRaises(FiscalYearIncorrectDate, fy.insert)
+		self.assertRaises(frappe.exceptions.InvalidDates, fy.insert)
 
 
 def test_record_generator():
@@ -35,8 +33,8 @@
 			"doctype": "Fiscal Year",
 			"year": "_Test Short Fiscal Year 2011",
 			"is_short_year": 1,
-			"year_end_date": "2011-04-01",
-			"year_start_date": "2011-12-31",
+			"year_start_date": "2011-04-01",
+			"year_end_date": "2011-12-31",
 		}
 	]
 
diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
index ed46d85..b666e0d 100644
--- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
@@ -10,7 +10,7 @@
 import frappe
 from frappe import _, throw
 from frappe.model.document import Document
-from frappe.utils import cint, flt, getdate
+from frappe.utils import cint, flt
 
 apply_on_dict = {"Item Code": "items", "Item Group": "item_groups", "Brand": "brands"}
 
@@ -184,8 +184,7 @@
 		if self.is_cumulative and not (self.valid_from and self.valid_upto):
 			frappe.throw(_("Valid from and valid upto fields are mandatory for the cumulative"))
 
-		if self.valid_from and self.valid_upto and getdate(self.valid_from) > getdate(self.valid_upto):
-			frappe.throw(_("Valid from date must be less than valid upto date"))
+		self.validate_from_to_dates("valid_from", "valid_upto")
 
 	def validate_condition(self):
 		if (
diff --git a/erpnext/accounts/doctype/tax_rule/tax_rule.py b/erpnext/accounts/doctype/tax_rule/tax_rule.py
index 4d20129..87c5e6d 100644
--- a/erpnext/accounts/doctype/tax_rule/tax_rule.py
+++ b/erpnext/accounts/doctype/tax_rule/tax_rule.py
@@ -32,7 +32,7 @@
 
 	def validate(self):
 		self.validate_tax_template()
-		self.validate_date()
+		self.validate_from_to_dates("from_date", "to_date")
 		self.validate_filters()
 		self.validate_use_for_shopping_cart()
 
@@ -51,10 +51,6 @@
 		if not (self.sales_tax_template or self.purchase_tax_template):
 			frappe.throw(_("Tax Template is mandatory."))
 
-	def validate_date(self):
-		if self.from_date and self.to_date and self.from_date > self.to_date:
-			frappe.throw(_("From Date cannot be greater than To Date"))
-
 	def validate_filters(self):
 		filters = {
 			"tax_type": self.tax_type,