Merge pull request #5080 from nabinhait/fy_overlap
[fix] Added overlap validation in fiscal year
This commit is contained in:
commit
fff8e6c733
@ -21,6 +21,7 @@ class FiscalYear(Document):
|
|||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.validate_dates()
|
self.validate_dates()
|
||||||
|
self.validate_overlap()
|
||||||
|
|
||||||
if not self.is_new():
|
if not self.is_new():
|
||||||
year_start_end_dates = frappe.db.sql("""select year_start_date, year_end_date
|
year_start_end_dates = frappe.db.sql("""select year_start_date, year_end_date
|
||||||
@ -41,6 +42,37 @@ class FiscalYear(Document):
|
|||||||
def on_update(self):
|
def on_update(self):
|
||||||
check_duplicate_fiscal_year(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()
|
@frappe.whitelist()
|
||||||
def check_duplicate_fiscal_year(doc):
|
def check_duplicate_fiscal_year(doc):
|
||||||
year_start_end_dates = frappe.db.sql("""select name, year_start_date, year_end_date from `tabFiscal Year` where name!=%s""", (doc.name))
|
year_start_end_dates = frappe.db.sql("""select name, year_start_date, year_end_date from `tabFiscal Year` where name!=%s""", (doc.name))
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _, throw
|
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.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.utilities.transaction_base import TransactionBase
|
||||||
from erpnext.controllers.recurring_document import convert_to_recurring, validate_recurring_document
|
from erpnext.controllers.recurring_document import convert_to_recurring, validate_recurring_document
|
||||||
from erpnext.controllers.sales_and_purchase_return import validate_return
|
from erpnext.controllers.sales_and_purchase_return import validate_return
|
||||||
@ -64,8 +64,6 @@ class AccountsController(TransactionBase):
|
|||||||
for fieldname in ["posting_date", "transaction_date"]:
|
for fieldname in ["posting_date", "transaction_date"]:
|
||||||
if not self.get(fieldname) and self.meta.get_field(fieldname):
|
if not self.get(fieldname) and self.meta.get_field(fieldname):
|
||||||
self.set(fieldname, today())
|
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
|
break
|
||||||
|
|
||||||
def calculate_taxes_and_totals(self):
|
def calculate_taxes_and_totals(self):
|
||||||
@ -222,10 +220,17 @@ class AccountsController(TransactionBase):
|
|||||||
|
|
||||||
def get_gl_dict(self, args, account_currency=None):
|
def get_gl_dict(self, args, account_currency=None):
|
||||||
"""this method populates the common properties of a gl entry record"""
|
"""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({
|
gl_dict = frappe._dict({
|
||||||
'company': self.company,
|
'company': self.company,
|
||||||
'posting_date': self.posting_date,
|
'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_type': self.doctype,
|
||||||
'voucher_no': self.name,
|
'voucher_no': self.name,
|
||||||
'remarks': self.get("remarks"),
|
'remarks': self.get("remarks"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user