diff --git a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py index 6bb71b667f..d80bc7fad1 100644 --- a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py +++ b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py @@ -9,6 +9,8 @@ from dateutil.relativedelta import relativedelta from frappe.model.document import Document +class FiscalYearIncorrectDate(frappe.ValidationError): pass + class FiscalYear(Document): def set_as_default(self): frappe.db.set_value("Global Defaults", None, "current_fiscal_year", self.name) @@ -35,12 +37,14 @@ class FiscalYear(Document): def validate_dates(self): 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")) + 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")) + frappe.throw(_("Fiscal Year End Date should be one year after Fiscal Year Start Date"), + FiscalYearIncorrectDate) def on_update(self): check_duplicate_fiscal_year(self) diff --git a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py index 5f90bb3867..f7b7782766 100644 --- a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py +++ b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py @@ -5,6 +5,8 @@ from __future__ import unicode_literals import frappe, unittest +from erpnext.accounts.doctype.fiscal_year.fiscal_year import FiscalYearIncorrectDate + test_records = frappe.get_test_records('Fiscal Year') test_ignore = ["Company"] @@ -12,12 +14,12 @@ class TestFiscalYear(unittest.TestCase): def test_extra_year(self): if frappe.db.exists("Fiscal Year", "_Test Fiscal Year 2000"): frappe.delete_doc("Fiscal Year", "_Test Fiscal Year 2000") + fy = frappe.get_doc({ "doctype": "Fiscal Year", "year": "_Test Fiscal Year 2000", "year_end_date": "2002-12-31", "year_start_date": "2000-04-01" }) - fy.insert() - self.assertEqual(fy.year_end_date, '2001-03-31') + self.assertRaises(FiscalYearIncorrectDate, fy.insert)