refactor: validate dates in accounts module

This commit is contained in:
barredterra 2022-12-04 14:41:21 +01:00
parent 66dbf94151
commit eb66b749b2
4 changed files with 6 additions and 22 deletions

View File

@ -9,10 +9,6 @@ from frappe.model.document import Document
from frappe.utils import add_days, add_years, cstr, getdate from frappe.utils import add_days, add_years, cstr, getdate
class FiscalYearIncorrectDate(frappe.ValidationError):
pass
class FiscalYear(Document): class FiscalYear(Document):
@frappe.whitelist() @frappe.whitelist()
def set_as_default(self): def set_as_default(self):
@ -53,23 +49,18 @@ class FiscalYear(Document):
) )
def validate_dates(self): def validate_dates(self):
self.validate_from_to_dates("year_start_date", "year_end_date")
if self.is_short_year: if self.is_short_year:
# Fiscal Year can be shorter than one year, in some jurisdictions # Fiscal Year can be shorter than one year, in some jurisdictions
# under certain circumstances. For example, in the USA and Germany. # under certain circumstances. For example, in the USA and Germany.
return 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) date = getdate(self.year_start_date) + relativedelta(years=1) - relativedelta(days=1)
if getdate(self.year_end_date) != date: if getdate(self.year_end_date) != date:
frappe.throw( frappe.throw(
_("Fiscal Year End Date should be one year after Fiscal Year Start Date"), _("Fiscal Year End Date should be one year after Fiscal Year Start Date"),
FiscalYearIncorrectDate, frappe.exceptions.InvalidDates,
) )
def on_update(self): def on_update(self):

View File

@ -7,8 +7,6 @@ import unittest
import frappe import frappe
from frappe.utils import now_datetime from frappe.utils import now_datetime
from erpnext.accounts.doctype.fiscal_year.fiscal_year import FiscalYearIncorrectDate
test_ignore = ["Company"] 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(): def test_record_generator():

View File

@ -10,7 +10,7 @@ import re
import frappe import frappe
from frappe import _, throw from frappe import _, throw
from frappe.model.document import Document 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"} 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): 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")) 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): self.validate_from_to_dates("valid_from", "valid_upto")
frappe.throw(_("Valid from date must be less than valid upto date"))
def validate_condition(self): def validate_condition(self):
if ( if (

View File

@ -32,7 +32,7 @@ class TaxRule(Document):
def validate(self): def validate(self):
self.validate_tax_template() self.validate_tax_template()
self.validate_date() self.validate_from_to_dates("from_date", "to_date")
self.validate_filters() self.validate_filters()
self.validate_use_for_shopping_cart() self.validate_use_for_shopping_cart()
@ -51,10 +51,6 @@ class TaxRule(Document):
if not (self.sales_tax_template or self.purchase_tax_template): if not (self.sales_tax_template or self.purchase_tax_template):
frappe.throw(_("Tax Template is mandatory.")) 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): def validate_filters(self):
filters = { filters = {
"tax_type": self.tax_type, "tax_type": self.tax_type,