[REF] account_fiscal_year : factorize code

This commit is contained in:
Sylvain LE GAL
2022-11-26 21:43:05 +01:00
parent 3f314c35e6
commit 3a083d4773
2 changed files with 23 additions and 23 deletions

View File

@@ -107,3 +107,17 @@ class AccountFiscalYear(models.Model):
intersection_domain,
]
)
@api.model
def _get_fiscal_year(self, company, date_from, date_to):
"""Return a fiscal year for the given company
that contains the two dates. (or False if no fiscal years)
matches the selection"""
return self.search(
[
("company_id", "=", company.id),
("date_from", "<=", date_from),
("date_to", ">=", date_to),
],
limit=1,
)

View File

@@ -20,15 +20,13 @@ class ResCompany(models.Model):
"""
self.ensure_one()
AccountFiscalYear = self.env["account.fiscal.year"]
# Search a fiscal year record containing the date.
fiscalyear = self.env["account.fiscal.year"].search(
[
("company_id", "=", self.id),
("date_from", "<=", current_date),
("date_to", ">=", current_date),
],
limit=1,
fiscalyear = AccountFiscalYear._get_fiscal_year(
self, current_date, current_date
)
if fiscalyear:
return {
"date_from": fiscalyear.date_from,
@@ -51,25 +49,13 @@ class ResCompany(models.Model):
# =>
# The period 2017-02-02 - 2017-02-30 is not covered by a fiscal year record.
fiscalyear_from = self.env["account.fiscal.year"].search(
[
("company_id", "=", self.id),
("date_from", "<=", date_from),
("date_to", ">=", date_from),
],
limit=1,
)
fiscalyear_from = AccountFiscalYear._get_fiscal_year(self, date_from, date_from)
if fiscalyear_from:
date_from = fiscalyear_from.date_to + timedelta(days=1)
fiscalyear_to = self.env["account.fiscal.year"].search(
[
("company_id", "=", self.id),
("date_from", "<=", date_to),
("date_to", ">=", date_to),
],
limit=1,
)
fiscalyear_to = AccountFiscalYear._get_fiscal_year(self, date_to, date_to)
if fiscalyear_to:
date_to = fiscalyear_to.date_from - timedelta(days=1)