Add cache to fiscalyear find, test state and return instance

This commit is contained in:
Cédric Krier
2023-02-11 00:13:24 +01:00
parent 45bbfa8fb5
commit c32d602221
6 changed files with 78 additions and 47 deletions
+10 -6
View File
@@ -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():
+39 -18
View File
@@ -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'
+5 -3
View File
@@ -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']
+15 -15
View File
@@ -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)
+4 -2
View File
@@ -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):
+5 -3
View File
@@ -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']