Skip to content
Snippets Groups Projects

Draft: Add find() method on fiscal years needed by other modules.

Open Etienne Ferriere requested to merge topic/16.0/MO16-00581 into branch/16.0
Files
3
# Copyright 2020 Simone Rubino - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from datetime import date
from dateutil.relativedelta import relativedelta
from odoo import fields, models
@@ -91,90 +87,23 @@
period.unlink()
return super().unlink()
def _get_duration(self):
"""
:return: relativedelta of duration of a fiscal year
"""
# This is a method so it can be changed in the case of needing to
# handle shorter or longer fiscal year
return relativedelta(years=1, days=-1)
def find(self, dt=None, create=False, code="{0:%Y}", name="{0:%Y}"):
"""
:param dt: date (odoo format so string), if None, use today
:return: fiscal years that include the indicated date, using either
the company_id in the context or if not set, the user company_id
"""
if not dt:
dt = fields.Date.context_today(self)
company_id = self.env.context.get("company_id") or self.env.company.id
result = self.search(
[
("date_start", "<=", dt),
("date_stop", ">=", dt),
("company_id", "=", company_id),
],
limit=1,
)
if not result and create:
oldest_fy = self.search(
[("company_id", "=", company_id)],
order="date_start DESC",
limit=1,
)
# Store date stop for the fiscal year to create
to_create_stop = None
if oldest_fy:
if dt < oldest_fy.date_start:
# the fiscal year to create is before any created so far
# backtrack by duration until we find the one to create
to_create_start = oldest_fy.date_start
# to_create_stop = oldest_fy.date_stop
while to_create_start > dt:
to_create_stop = to_create_start + relativedelta(days=-1)
to_create_start = to_create_stop - self._get_duration()
else:
newest_fy = self.search(
[("company_id", "=", company_id)],
order="date_stop ASC",
limit=1,
)
if dt > newest_fy.date_stop:
# the fiscal year to create is after any created so far
to_create_stop = newest_fy.date_stop
while to_create_stop < dt:
to_create_start = to_create_stop + relativedelta(days=1)
to_create_stop = to_create_start + self._get_duration()
else:
# the fiscal year to create is between any created so
# far
to_create_stop = oldest_fy.date_stop
while to_create_stop < dt:
to_create_start = to_create_stop + relativedelta(days=1)
to_create_stop = to_create_start + self._get_duration()
else:
# there’s no fy in the system, if the duration is changed,
# this will not work as is
company = self.env["res.company"].browse(company_id)
to_create_stop = date(
dt.year,
int(company.fiscalyear_last_month),
company.fiscalyear_last_day,
)
if dt > to_create_stop:
to_create_stop.year += 1
# create the fiscal year
result = self.create(
[
{
"date_stop": to_create_stop,
"company_id": company_id,
"name": name.format(to_create_stop),
"code": code.format(to_create_stop),
}
]
)
return result
# def find(self, date=None):
# """
# :param date: date (odoo format so string), if None, use today
# :return: fiscal years that include the indicated date, using either
# the company_id in the context or if not set, the user company_id
# """
#
# if not date:
# date = fields.Date.context_today(self)
# company_id = self.env.context.get("company_id") or self.env.company.id
# result = self.search(
# [
# ("date_from", "<=", date),
# ("date_to", ">=", date),
# ("company_id", "=", company_id),
# ],
# limit=1,
# )
#
# return result
Loading