Merge pull request #5080 from nabinhait/fy_overlap
[fix] Added overlap validation in fiscal year
diff --git a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py
index 9456924..ce76354 100644
--- a/erpnext/accounts/doctype/fiscal_year/fiscal_year.py
+++ b/erpnext/accounts/doctype/fiscal_year/fiscal_year.py
@@ -21,6 +21,7 @@
def validate(self):
self.validate_dates()
+ self.validate_overlap()
if not self.is_new():
year_start_end_dates = frappe.db.sql("""select year_start_date, year_end_date
@@ -40,6 +41,37 @@
def on_update(self):
check_duplicate_fiscal_year(self)
+
+ def validate_overlap(self):
+ existing_fiscal_years = frappe.db.sql("""select name from `tabFiscal Year`
+ where (
+ (%(year_start_date)s between year_start_date and year_end_date)
+ or (%(year_end_date)s between year_start_date and year_end_date)
+ or (year_start_date between %(year_start_date)s and %(year_end_date)s)
+ or (year_end_date between %(year_start_date)s and %(year_end_date)s)
+ ) and name!=%(name)s""",
+ {
+ "year_start_date": self.year_start_date,
+ "year_end_date": self.year_end_date,
+ "name": self.name or "No Name"
+ }, as_dict=True)
+
+ if existing_fiscal_years:
+ for existing in existing_fiscal_years:
+ company_for_existing = frappe.db.sql_list("""select company from `tabFiscal Year Company`
+ where parent=%s""", existing.name)
+
+ overlap = False
+ if not self.get("companies") or not company_for_existing:
+ overlap = True
+
+ for d in self.get("companies"):
+ if d.company in company_for_existing:
+ overlap = True
+
+ if overlap:
+ frappe.throw(_("Year start date or end date is overlapping with {0}. To avoid please set company")
+ .format(existing.name))
@frappe.whitelist()
def check_duplicate_fiscal_year(doc):
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 83155f1..50d4d8a 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -4,9 +4,9 @@
from __future__ import unicode_literals
import frappe
from frappe import _, throw
-from frappe.utils import today, flt, cint, fmt_money
+from frappe.utils import today, flt, cint, fmt_money, formatdate
from erpnext.setup.utils import get_company_currency, get_exchange_rate
-from erpnext.accounts.utils import get_fiscal_year, validate_fiscal_year, get_account_currency
+from erpnext.accounts.utils import get_fiscal_years, validate_fiscal_year, get_account_currency
from erpnext.utilities.transaction_base import TransactionBase
from erpnext.controllers.recurring_document import convert_to_recurring, validate_recurring_document
from erpnext.controllers.sales_and_purchase_return import validate_return
@@ -64,8 +64,6 @@
for fieldname in ["posting_date", "transaction_date"]:
if not self.get(fieldname) and self.meta.get_field(fieldname):
self.set(fieldname, today())
- if self.meta.get_field("fiscal_year") and not self.fiscal_year:
- self.fiscal_year = get_fiscal_year(self.get(fieldname))[0]
break
def calculate_taxes_and_totals(self):
@@ -222,10 +220,17 @@
def get_gl_dict(self, args, account_currency=None):
"""this method populates the common properties of a gl entry record"""
+
+ fiscal_years = get_fiscal_years(self.posting_date, company=self.company)
+ if len(fiscal_years) > 1:
+ frappe.throw(_("Multiple fiscal years exist for the date {0}. Please set company in Fiscal Year").format(formatdate(self.posting_date)))
+ else:
+ fiscal_year = fiscal_years[0][0]
+
gl_dict = frappe._dict({
'company': self.company,
'posting_date': self.posting_date,
- 'fiscal_year': get_fiscal_year(self.posting_date, company=self.company)[0],
+ 'fiscal_year': fiscal_year,
'voucher_type': self.doctype,
'voucher_no': self.name,
'remarks': self.get("remarks"),