mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-13 02:01:21 +00:00
refactor: Fiscal Year DocType cleanup
(cherry picked from commit 74ac28fc70)
This commit is contained in:
@@ -33,24 +33,6 @@ class FiscalYear(Document):
|
|||||||
self.validate_dates()
|
self.validate_dates()
|
||||||
self.validate_overlap()
|
self.validate_overlap()
|
||||||
|
|
||||||
if not self.is_new():
|
|
||||||
year_start_end_dates = frappe.db.sql(
|
|
||||||
"""select year_start_date, year_end_date
|
|
||||||
from `tabFiscal Year` where name=%s""",
|
|
||||||
(self.name),
|
|
||||||
)
|
|
||||||
|
|
||||||
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 validate_dates(self):
|
def validate_dates(self):
|
||||||
self.validate_from_to_dates("year_start_date", "year_end_date")
|
self.validate_from_to_dates("year_start_date", "year_end_date")
|
||||||
if self.is_short_year:
|
if self.is_short_year:
|
||||||
@@ -66,28 +48,20 @@ class FiscalYear(Document):
|
|||||||
frappe.exceptions.InvalidDates,
|
frappe.exceptions.InvalidDates,
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_update(self):
|
|
||||||
check_duplicate_fiscal_year(self)
|
|
||||||
frappe.cache().delete_value("fiscal_years")
|
|
||||||
|
|
||||||
def on_trash(self):
|
|
||||||
frappe.cache().delete_value("fiscal_years")
|
|
||||||
|
|
||||||
def validate_overlap(self):
|
def validate_overlap(self):
|
||||||
existing_fiscal_years = frappe.db.sql(
|
fy = frappe.qb.DocType("Fiscal Year")
|
||||||
"""select name from `tabFiscal Year`
|
|
||||||
where (
|
name = self.name or self.year
|
||||||
(%(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)
|
existing_fiscal_years = (
|
||||||
or (year_start_date between %(year_start_date)s and %(year_end_date)s)
|
frappe.qb.from_(fy)
|
||||||
or (year_end_date between %(year_start_date)s and %(year_end_date)s)
|
.select(fy.name)
|
||||||
) and name!=%(name)s""",
|
.where(
|
||||||
{
|
(fy.year_start_date <= self.year_end_date)
|
||||||
"year_start_date": self.year_start_date,
|
& (fy.year_end_date >= self.year_start_date)
|
||||||
"year_end_date": self.year_end_date,
|
& (fy.name != name)
|
||||||
"name": self.name or "No Name",
|
)
|
||||||
},
|
.run(as_dict=True)
|
||||||
as_dict=True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if existing_fiscal_years:
|
if existing_fiscal_years:
|
||||||
@@ -110,28 +84,11 @@ class FiscalYear(Document):
|
|||||||
frappe.throw(
|
frappe.throw(
|
||||||
_(
|
_(
|
||||||
"Year start date or end date is overlapping with {0}. To avoid please set company"
|
"Year start date or end date is overlapping with {0}. To avoid please set company"
|
||||||
).format(existing.name),
|
).format(frappe.get_desk_link("Fiscal Year", existing.name, open_in_new_tab=True)),
|
||||||
frappe.NameError,
|
frappe.NameError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
|
||||||
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),
|
|
||||||
)
|
|
||||||
for fiscal_year, ysd, yed in year_start_end_dates:
|
|
||||||
if (getdate(doc.year_start_date) == ysd and getdate(doc.year_end_date) == yed) and (
|
|
||||||
not frappe.in_test
|
|
||||||
):
|
|
||||||
frappe.throw(
|
|
||||||
_(
|
|
||||||
"Fiscal Year Start Date and Fiscal Year End Date are already set in Fiscal Year {0}"
|
|
||||||
).format(fiscal_year)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def auto_create_fiscal_year():
|
def auto_create_fiscal_year():
|
||||||
for d in frappe.db.sql(
|
for d in frappe.db.sql(
|
||||||
|
|||||||
Reference in New Issue
Block a user