From f63a3d4a80ba293c3216ea0a0ea7c95e388259b5 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Fri, 3 Apr 2015 10:59:48 +0530 Subject: [PATCH] [fiscal year] automatically set year end date if more than one year --- .../doctype/fiscal_year/fiscal_year.py | 12 +++++++----- .../doctype/fiscal_year/test_fiscal_year.py | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py index 5d0c20dc231..1baed417006 100644 --- a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py +++ b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py @@ -5,12 +5,11 @@ from __future__ import unicode_literals import frappe from frappe import msgprint, _ from frappe.utils import getdate, add_days, add_years, cstr -from datetime import timedelta +from dateutil.relativedelta import relativedelta from frappe.model.document import Document class FiscalYear(Document): - def set_as_default(self): frappe.db.set_value("Global Defaults", None, "current_fiscal_year", self.name) frappe.get_doc("Global Defaults").on_update() @@ -24,18 +23,21 @@ class FiscalYear(Document): year_start_end_dates = frappe.db.sql("""select year_start_date, year_end_date from `tabFiscal Year` where name=%s""", (self.name)) + self.validate_dates() + if year_start_end_dates: if getdate(self.year_start_date) != year_start_end_dates[0][0] or getdate(self.year_end_date) != year_start_end_dates[0][1]: frappe.throw(_("Cannot change Fiscal Year Start Date and Fiscal Year End Date once the Fiscal Year is saved.")) - def on_update(self): - # validate year start date and year end date + def validate_dates(self): if getdate(self.year_start_date) > getdate(self.year_end_date): frappe.throw(_("Fiscal Year Start Date should not be greater than Fiscal Year End Date")) if (getdate(self.year_end_date) - getdate(self.year_start_date)).days > 366: - frappe.throw(_("Fiscal Year Start Date and Fiscal Year End Date cannot be more than a year apart.")) + date = getdate(self.year_start_date) + relativedelta(years=1) - relativedelta(days=1) + self.year_end_date = date.strftime("%Y-%m-%d") + def on_update(self): check_duplicate_fiscal_year(self) @frappe.whitelist() diff --git a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py index 9d524ac6e93..092d34a939e 100644 --- a/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py +++ b/erpnext/accounts/doctype/fiscal_year/test_fiscal_year.py @@ -3,6 +3,20 @@ from __future__ import unicode_literals -import frappe +import frappe, unittest + +test_records = frappe.get_test_records('Fiscal Year') + +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.assertEquals(fy.year_end_date, '2001-03-31') -test_records = frappe.get_test_records('Fiscal Year') \ No newline at end of file