mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
[REF] account_fiscal_year : factorize code
This commit is contained in:
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user