From eb66b749b2b82f934c5de0b9dea52ec2972d25b6 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Sun, 4 Dec 2022 14:41:21 +0100 Subject: [PATCH 1/2] refactor: validate dates in accounts module --- erpnext/accounts/doctype/fiscal_year/fiscal_year.py | 13 ++----------- .../doctype/fiscal_year/test_fiscal_year.py | 4 +--- .../accounts/doctype/pricing_rule/pricing_rule.py | 5 ++--- erpnext/accounts/doctype/tax_rule/tax_rule.py | 6 +----- 4 files changed, 6 insertions(+), 22 deletions(-) diff --git a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py index 4592421304..69cfe3109f 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.model.document import Document 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 @@ class FiscalYear(Document): ) 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 6e946f7466..0fed1a17b6 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 unittest 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 @@ class TestFiscalYear(unittest.TestCase): } ) - self.assertRaises(FiscalYearIncorrectDate, fy.insert) + self.assertRaises(frappe.exceptions.InvalidDates, fy.insert) def test_record_generator(): diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py index ed46d85e3a..b666e0d7c6 100644 --- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py +++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py @@ -10,7 +10,7 @@ import re 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 @@ class PricingRule(Document): 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 4d201292ed..87c5e6d588 100644 --- a/erpnext/accounts/doctype/tax_rule/tax_rule.py +++ b/erpnext/accounts/doctype/tax_rule/tax_rule.py @@ -32,7 +32,7 @@ class TaxRule(Document): 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 @@ class TaxRule(Document): 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, From a26a29f33b2f87a2f84db6290982cae3e3407de0 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 5 Dec 2022 19:49:05 +0100 Subject: [PATCH 2/2] fix: incorrect dates in test records --- erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py index 0fed1a17b6..181406bad7 100644 --- a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py +++ b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py @@ -33,8 +33,8 @@ def test_record_generator(): "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", } ]