2021-03-31 10:31:33 +02:00
|
|
|
# Copyright (C) 2021 - Today: GRAP (http://www.grap.coop)
|
|
|
|
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
|
|
|
from datetime import datetime
|
2022-11-04 00:00:49 +01:00
|
|
|
|
2021-03-31 10:31:33 +02:00
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
|
|
from odoo import _, api, models
|
|
|
|
|
|
|
|
|
|
|
|
class AccountFiscalYear(models.Model):
|
|
|
|
_inherit = "account.fiscal.year"
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def cron_auto_create(self):
|
|
|
|
companies = self.env["res.company"].search([])
|
|
|
|
for company in companies:
|
|
|
|
last_fiscal_year = self.search(
|
2022-11-04 00:00:49 +01:00
|
|
|
[("company_id", "=", company.id)], order="date_to desc", limit=1
|
|
|
|
)
|
2021-03-31 10:31:33 +02:00
|
|
|
|
|
|
|
if last_fiscal_year and (
|
2022-11-04 00:00:49 +01:00
|
|
|
last_fiscal_year.date_to < datetime.now().date() + relativedelta(days=1)
|
2021-03-31 10:31:33 +02:00
|
|
|
):
|
|
|
|
self.create(last_fiscal_year._prepare_next_fiscal_year())
|
|
|
|
|
|
|
|
def _prepare_next_fiscal_year(self):
|
|
|
|
self.ensure_one()
|
|
|
|
# try to generate a new name, based on the previous
|
|
|
|
# name replacing YYYY pattern by YYYY+1 value
|
|
|
|
# - "FY 2018" will be replace by "FY 2019"
|
|
|
|
# - "FY 2018-2019" will be replace by "FY 2019-2020"
|
|
|
|
new_name = self.name.replace(
|
2022-11-04 00:00:49 +01:00
|
|
|
str(self.date_to.year), str(self.date_to.year + 1)
|
|
|
|
).replace(str(self.date_from.year), str(self.date_from.year + 1))
|
|
|
|
if self.search(
|
|
|
|
[("name", "=", new_name), ("company_id", "=", self.company_id.id)]
|
|
|
|
):
|
2021-03-31 10:31:33 +02:00
|
|
|
# the replace process fail to guess a correct unique name
|
2022-11-04 00:10:13 +01:00
|
|
|
new_name = _(
|
|
|
|
"FY %(date_to)s - %(date_from)s",
|
|
|
|
date_to=str(self.date_to),
|
|
|
|
date_from=str(self.date_from),
|
|
|
|
)
|
2021-03-31 10:31:33 +02:00
|
|
|
# compute new dates, handling leap years
|
|
|
|
new_date_from = self.date_to + relativedelta(days=1)
|
|
|
|
new_date_to = new_date_from + relativedelta(years=1, days=-1)
|
|
|
|
return {
|
|
|
|
"name": new_name,
|
|
|
|
"company_id": self.company_id.id,
|
|
|
|
"date_from": new_date_from.strftime("%Y-%m-%d"),
|
|
|
|
"date_to": new_date_to.strftime("%Y-%m-%d"),
|
|
|
|
}
|