From 3a083d47739e664c4e99ee6d9c3c950f7ca0cd61 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Sat, 26 Nov 2022 21:43:05 +0100 Subject: [PATCH] [REF] account_fiscal_year : factorize code --- .../models/account_fiscal_year.py | 14 ++++++++ account_fiscal_year/models/res_company.py | 32 ++++++------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/account_fiscal_year/models/account_fiscal_year.py b/account_fiscal_year/models/account_fiscal_year.py index 9054f79ee..f6e891e77 100644 --- a/account_fiscal_year/models/account_fiscal_year.py +++ b/account_fiscal_year/models/account_fiscal_year.py @@ -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, + ) diff --git a/account_fiscal_year/models/res_company.py b/account_fiscal_year/models/res_company.py index cc5e2ac6c..afe6b0f1c 100644 --- a/account_fiscal_year/models/res_company.py +++ b/account_fiscal_year/models/res_company.py @@ -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)