From c32d6022216ffbcf7bc6f1eb9463e8eda504cc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 11 Feb 2023 00:13:24 +0100 Subject: [PATCH] Add cache to fiscalyear find, test state and return instance --- modules/account/account.py | 16 +++++--- modules/account/fiscalyear.py | 57 ++++++++++++++++++++--------- modules/account_be/account.py | 8 ++-- modules/account_budget/account.py | 30 +++++++-------- modules/account_es/reporting_tax.py | 6 ++- modules/account_eu/account.py | 8 ++-- 6 files changed, 78 insertions(+), 47 deletions(-) diff --git a/modules/account/account.py b/modules/account/account.py index 108ed1ca31..ae4a37b4a3 100644 --- a/modules/account/account.py +++ b/modules/account/account.py @@ -2167,7 +2167,9 @@ class GeneralLedgerAccountContext(ModelView): period = Period(context['period']) return period.fiscalyear.id else: - return FiscalYear.find(context.get('company'), exception=False) + fiscalyear = FiscalYear.find( + context.get('company'), exception=False, test_state=False) + return fiscalyear.id if fiscalyear else None @classmethod def default_start_period(cls): @@ -2672,11 +2674,13 @@ class IncomeStatementContext(ModelView): 'invisible': ~Eval('comparison', False), }) - @staticmethod - def default_fiscalyear(): - FiscalYear = Pool().get('account.fiscalyear') - return FiscalYear.find( - Transaction().context.get('company'), exception=False) + @classmethod + def default_fiscalyear(cls): + pool = Pool() + FiscalYear = pool.get('account.fiscalyear') + fiscalyear = FiscalYear.find( + cls.default_company(), exception=False, test_state=False) + return fiscalyear.id if fiscalyear else None @staticmethod def default_company(): diff --git a/modules/account/fiscalyear.py b/modules/account/fiscalyear.py index 8a3e8cbf7e..4d1bc809c8 100644 --- a/modules/account/fiscalyear.py +++ b/modules/account/fiscalyear.py @@ -3,6 +3,7 @@ from dateutil.relativedelta import relativedelta +from trytond.cache import Cache from trytond.i18n import gettext from trytond.model import ModelSQL, ModelView, Workflow, fields from trytond.model.exceptions import AccessError @@ -51,6 +52,7 @@ class FiscalYear(Workflow, ModelSQL, ModelView): company = fields.Many2One( 'company.company', "Company", required=True) icon = fields.Function(fields.Char("Icon"), 'get_icon') + _find_cache = Cache(__name__ + '.find', context=False) @classmethod def __setup__(cls): @@ -163,6 +165,12 @@ class FiscalYear(Workflow, ModelSQL, ModelView): first=fiscalyear.rec_name, second=years[0].rec_name)) + @classmethod + def create(cls, vlist): + fiscalyears = super().create(vlist) + cls._find_cache.clear() + return fiscalyears + @classmethod def write(cls, *args): pool = Pool() @@ -183,12 +191,14 @@ class FiscalYear(Workflow, ModelSQL, ModelView): 'msg_change_fiscalyear_post_move_sequence', fiscalyear=fiscalyear.rec_name)) super(FiscalYear, cls).write(*args) + cls._find_cache.clear() @classmethod def delete(cls, fiscalyears): Period = Pool().get('account.period') Period.delete([p for f in fiscalyears for p in f.periods]) super(FiscalYear, cls).delete(fiscalyears) + cls._find_cache.clear() @classmethod def create_period(cls, fiscalyears, interval=1, end_day=31): @@ -226,34 +236,45 @@ class FiscalYear(Workflow, ModelSQL, ModelView): pass @classmethod - def find(cls, company_id, date=None, exception=True): + def find(cls, company, date=None, exception=True, test_state=True): ''' - Return the fiscal year for the company_id - at the date or the current date. - If exception is set the function will raise an exception - if any fiscal year is found. + Return the fiscal year for the company at the date or the current date. + If exception is set it raises an exception if any fiscal year is found. + If test_state is true, it searches on non-closed fiscal years ''' pool = Pool() Lang = pool.get('ir.lang') Date = pool.get('ir.date') + company_id = int(company) if company is not None else None if not date: with Transaction().set_context(company=company_id): date = Date.today() - fiscalyears = cls.search([ - ('start_date', '<=', date), - ('end_date', '>=', date), - ('company', '=', company_id), - ], order=[('start_date', 'DESC')], limit=1) - if not fiscalyears: - if exception: - lang = Lang.get() - raise FiscalYearNotFoundError( - gettext('account.msg_no_fiscalyear_date', - date=lang.strftime(date))) + key = (company_id, date) + fiscalyear = cls._find_cache.get(key, -1) + if fiscalyear is not None and fiscalyear < 0: + clause = [ + ('start_date', '<=', date), + ('end_date', '>=', date), + ('company', '=', company_id), + ] + if test_state: + clause.append(('state', '=', 'open')) + fiscalyears = cls.search( + clause, order=[('start_date', 'DESC')], limit=1) + if fiscalyears: + fiscalyear, = fiscalyears else: - return None - return fiscalyears[0].id + fiscalyear = None + cls._find_cache.set(key, int(fiscalyear) if fiscalyear else None) + elif fiscalyear is not None: + fiscalyear = cls(fiscalyear) + if not fiscalyear and exception: + lang = Lang.get() + raise FiscalYearNotFoundError( + gettext('account.msg_no_fiscalyear_date', + date=lang.strftime(date))) + return fiscalyear def get_deferral(self, account): 'Computes deferrals for accounts' diff --git a/modules/account_be/account.py b/modules/account_be/account.py index a0aa354318..e63b70c80a 100644 --- a/modules/account_be/account.py +++ b/modules/account_be/account.py @@ -162,6 +162,8 @@ class BEVATCustomerContext(ModelView): pool = Pool() FiscalYear = pool.get('account.fiscalyear') context = Transaction().context - return context.get( - 'fiscalyear', - FiscalYear.find(context.get('company'), exception=False)) + if 'fiscalyear' not in context: + fiscalyear = FiscalYear.find( + cls.default_company(), exception=False, test_state=False) + return fiscalyear.id if fiscalyear else None + return context['fiscalyear'] diff --git a/modules/account_budget/account.py b/modules/account_budget/account.py index 5b6b886553..17056ebacc 100644 --- a/modules/account_budget/account.py +++ b/modules/account_budget/account.py @@ -282,14 +282,15 @@ class BudgetContext(ModelView): context = Transaction().context if 'budget' in context: return context.get('budget') - fiscalyear_id = FiscalYear.find( - context.get('company'), exception=False) - budgets = Budget.search([ - ('fiscalyear', '=', fiscalyear_id), - ], limit=1) - if budgets: - budget, = budgets - return budget.id + fiscalyear = FiscalYear.find( + context.get('company'), exception=False, test_state=False) + if fiscalyear: + budgets = Budget.search([ + ('fiscalyear', '=', fiscalyear.id), + ], limit=1) + if budgets: + budget, = budgets + return budget.id @fields.depends('budget') def on_change_with_fiscalyear(self, name=None): @@ -333,18 +334,17 @@ class Budget(BudgetMixin, ModelSQL, ModelView): def default_fiscalyear(cls): pool = Pool() FiscalYear = pool.get('account.fiscalyear') - return FiscalYear.find(cls.default_company(), exception=False) + fiscalyear = FiscalYear.find( + cls.default_company(), exception=False, test_state=False) + return fiscalyear.id if fiscalyear else None @fields.depends('company', 'fiscalyear') def on_change_company(self): pool = Pool() FiscalYear = pool.get('account.fiscalyear') - if self.company: - if not self.fiscalyear or self.fiscalyear.company != self.company: - self.fiscalyear = FiscalYear.find( - self.company.id, exception=False) - else: - self.fiscalyear = None + if not self.fiscalyear or self.fiscalyear.company != self.company: + self.fiscalyear = FiscalYear.find( + self.company, exception=False, test_state=False) def get_rec_name(self, name): return '%s - %s' % (self.name, self.fiscalyear.rec_name) diff --git a/modules/account_es/reporting_tax.py b/modules/account_es/reporting_tax.py index e5c05f7f98..1dddfb3156 100644 --- a/modules/account_es/reporting_tax.py +++ b/modules/account_es/reporting_tax.py @@ -756,8 +756,10 @@ class ESVATBookContext(ModelView): @classmethod def default_fiscalyear(cls): pool = Pool() - Fiscalyear = pool.get('account.fiscalyear') - return Fiscalyear.find(cls.default_company(), exception=False) + FiscalYear = pool.get('account.fiscalyear') + fiscalyear = FiscalYear.find( + cls.default_company(), exception=False, test_state=False) + return fiscalyear.id if fiscalyear else None class ESVATBook(ModelSQL, ModelView): diff --git a/modules/account_eu/account.py b/modules/account_eu/account.py index f2b1b01713..c611aae7f3 100644 --- a/modules/account_eu/account.py +++ b/modules/account_eu/account.py @@ -132,6 +132,8 @@ class ECSalesListContext(ModelView): pool = Pool() FiscalYear = pool.get('account.fiscalyear') context = Transaction().context - return context.get( - 'fiscalyear', - FiscalYear.find(context.get('company'), exception=False)) + if 'fiscalyear' not in context: + fiscalyear = FiscalYear.find( + cls.default_company(), exception=False, test_state=False) + return fiscalyear.id if fiscalyear else None + return context['fiscalyear']