diff --git a/modules/account/move.py b/modules/account/move.py index 4f0cd8c17f..d7f0d5bda1 100644 --- a/modules/account/move.py +++ b/modules/account/move.py @@ -152,11 +152,12 @@ class Move(ModelSQL, ModelView): def default_company(): return Transaction().context.get('company') - @staticmethod - def default_period(): + @classmethod + def default_period(cls): Period = Pool().get('account.period') - return Period.find(Transaction().context.get('company'), - exception=False) + company = cls.default_company() + period = Period.find(company, exception=False) + return period.id if period else None @staticmethod def default_state(): @@ -385,10 +386,10 @@ class Move(ModelSQL, ModelView): move=self.rec_name)) with Transaction().set_context(company=self.company.id): date = Date.today() - period_id = Period.find(self.company.id, date=date) + period = Period.find(self.company, date=date) default.update({ 'date': date, - 'period': period_id, + 'period': period.id, }) if reversal: default['lines.debit'] = lambda data: data['credit'] @@ -1561,7 +1562,7 @@ class Line(MoveLineMixin, ModelSQL, ModelView): if not date: with Transaction().set_context(company=company.id): date = Date.today() - period_id = Period.find(reconcile_account.company.id, date=date) + period = Period.find(reconcile_account.company, date=date) account = None journal = None if writeoff: @@ -1574,7 +1575,7 @@ class Line(MoveLineMixin, ModelSQL, ModelView): move = Move() move.company = company move.journal = journal - move.period = period_id + move.period = period move.date = date move.description = description @@ -1665,11 +1666,13 @@ class OpenJournalAsk(ModelView): ('state', '!=', 'close'), ]) - @staticmethod - def default_period(): - Period = Pool().get('account.period') - return Period.find(Transaction().context.get('company'), - exception=False) + @classmethod + def default_period(cls): + pool = Pool() + Period = pool.get('account.period') + company = Transaction().context.get('company') + period = Period.find(company, exception=False) + return period.id if period else None class OpenJournal(Wizard): @@ -2359,7 +2362,7 @@ class GroupLines(Wizard): if not date: with Transaction().set_context(company=company.id): date = Date.today() - period = Period.find(company.id, date=date) + period = Period.find(company, date=date) move = Move() move.company = company @@ -2621,7 +2624,7 @@ class RescheduleLines(Wizard): if not date: with Transaction().set_context(company=company.id): date = Date.today() - period = Period.find(company.id, date=date) + period = Period.find(company, date=date) move = Move() move.company = company @@ -2818,7 +2821,7 @@ class DelegateLines(Wizard): if not date: with Transaction().set_context(company=company.id): date = Date.today() - period = Period.find(company.id, date=date) + period = Period.find(company, date=date) move = Move() move.company = company diff --git a/modules/account/move_template.py b/modules/account/move_template.py index d481d8cc48..979369d6f7 100644 --- a/modules/account/move_template.py +++ b/modules/account/move_template.py @@ -405,12 +405,13 @@ class CreateMoveTemplate(ModelView): Eval('context', {}).get('company', 0)), ]) - @staticmethod - def default_period(): + @classmethod + def default_period(cls): pool = Pool() Period = pool.get('account.period') company = Transaction().context.get('company') - return Period.find(company, exception=False) + period = Period.find(company, exception=False) + return period.id if period else None class CreateMoveKeywords(ModelView): diff --git a/modules/account/period.py b/modules/account/period.py index 3dd3618b00..73b227c8a6 100644 --- a/modules/account/period.py +++ b/modules/account/period.py @@ -1,6 +1,7 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +from trytond.cache import Cache from trytond.const import OPERATORS from trytond.i18n import gettext from trytond.model import Index, ModelSQL, ModelView, Workflow, fields @@ -50,6 +51,7 @@ class Period(Workflow, ModelSQL, ModelView): company = fields.Function(fields.Many2One('company.company', 'Company',), 'on_change_with_company', searcher='search_company') icon = fields.Function(fields.Char("Icon"), 'get_icon') + _find_cache = Cache(__name__ + '.find', context=False) @classmethod def __setup__(cls): @@ -180,39 +182,46 @@ class Period(Workflow, ModelSQL, ModelView): second=periods[0].rec_name)) @classmethod - def find(cls, company_id, date=None, exception=True, test_state=True): + def find(cls, company, date=None, exception=True, test_state=True): ''' - Return the period for the company_id - at the date or the current date. - If exception is set the function will raise an exception - if no period is found. - If test_state is true, it will search on non-closed periods + Return the period for the company at the date or the current date. + If exception is set it raises an exception if no period is found. + If test_state is true, it searches on non-closed periods ''' pool = Pool() Date = pool.get('ir.date') Lang = pool.get('ir.lang') + company_id = int(company) if company is not None else None if not date: with Transaction().set_context(company=company_id): date = Date.today() - clause = [ - ('start_date', '<=', date), - ('end_date', '>=', date), - ('fiscalyear.company', '=', company_id), - ('type', '=', 'standard'), - ] - if test_state: - clause.append(('state', '=', 'open')) - periods = cls.search(clause, order=[('start_date', 'DESC')], limit=1) - if not periods: - if exception: - lang = Lang.get() - raise PeriodNotFoundError( - gettext('account.msg_no_period_date', - date=lang.strftime(date))) + key = (company_id, date, test_state) + period = cls._find_cache.get(key, -1) + if period is not None and period < 0: + clause = [ + ('start_date', '<=', date), + ('end_date', '>=', date), + ('fiscalyear.company', '=', company_id), + ('type', '=', 'standard'), + ] + if test_state: + clause.append(('state', '=', 'open')) + periods = cls.search( + clause, order=[('start_date', 'DESC')], limit=1) + if periods: + period, = periods else: - return None - return periods[0].id + period = None + cls._find_cache.set(key, int(period) if period else None) + elif period is not None: + period = cls(period) + if not period and exception: + lang = Lang.get() + raise PeriodNotFoundError( + gettext('account.msg_no_period_date', + date=lang.strftime(date))) + return period @classmethod def _check(cls, periods): @@ -267,7 +276,9 @@ class Period(Workflow, ModelSQL, ModelView): if not vals.get('post_move_sequence'): vals['post_move_sequence'] = ( fiscalyear.post_move_sequence.id) - return super(Period, cls).create(vlist) + periods = super(Period, cls).create(vlist) + cls._find_cache.clear() + return periods @classmethod def write(cls, *args): @@ -307,11 +318,13 @@ class Period(Workflow, ModelSQL, ModelView): period=period.rec_name)) args.extend((periods, values)) super(Period, cls).write(*args) + cls._find_cache.clear() @classmethod def delete(cls, periods): cls._check(periods) super(Period, cls).delete(periods) + cls._find_cache.clear() @classmethod @ModelView.button diff --git a/modules/account/tax.py b/modules/account/tax.py index 29b6898995..1ba5840caf 100644 --- a/modules/account/tax.py +++ b/modules/account/tax.py @@ -557,8 +557,9 @@ class TaxCodeContext(ModelView): def default_period(cls): pool = Pool() Period = pool.get('account.period') - return Period.find( + period = Period.find( cls.default_company(), exception=False, test_state=False) + return period.id if period else None @fields.depends( 'method', 'company', diff --git a/modules/account_asset/asset.py b/modules/account_asset/asset.py index 9d3853a69b..d5b27f790e 100644 --- a/modules/account_asset/asset.py +++ b/modules/account_asset/asset.py @@ -534,7 +534,7 @@ class Asset(Workflow, ModelSQL, ModelView): Move = pool.get('account.move') MoveLine = pool.get('account.move.line') - period_id = Period.find(self.company.id, line.date) + period = Period.find(self.company, line.date) with Transaction().set_context(date=line.date): expense_line = MoveLine( credit=0, @@ -550,7 +550,7 @@ class Asset(Workflow, ModelSQL, ModelView): return Move( company=self.company, origin=line, - period=period_id, + period=period, journal=self.account_journal, date=line.date, lines=[expense_line, depreciation_line], @@ -596,7 +596,7 @@ class Asset(Workflow, ModelSQL, ModelView): if date is None: with Transaction().set_context(company=self.company.id): date = Date.today() - period_id = Period.find(self.company.id, date) + period = Period.find(self.company, date) if self.supplier_invoice_line: account_asset = self.supplier_invoice_line.account.current() else: @@ -626,7 +626,7 @@ class Asset(Workflow, ModelSQL, ModelView): return Move( company=self.company, origin=self, - period=period_id, + period=period, journal=self.account_journal, date=date, lines=lines, @@ -886,12 +886,12 @@ class UpdateAsset(Wizard): pool = Pool() Period = pool.get('account.period') Move = pool.get('account.move') - period_id = Period.find(asset.company.id, self.show_move.date) + period = Period.find(asset.company, self.show_move.date) return Move( company=asset.company, origin=asset, journal=asset.account_journal.id, - period=period_id, + period=period, date=self.show_move.date, ) diff --git a/modules/account_dunning_fee/dunning.py b/modules/account_dunning_fee/dunning.py index 11e7ca6035..48261f9712 100644 --- a/modules/account_dunning_fee/dunning.py +++ b/modules/account_dunning_fee/dunning.py @@ -140,7 +140,7 @@ class FeeDunningLevel(ModelSQL, ModelView): move.company = self.dunning.company move.journal = self.level.fee.journal move.date = today - move.period = Period.find(move.company.id, date=today) + move.period = Period.find(move.company, date=today) move.origin = self move.description = self.level.fee.name diff --git a/modules/account_invoice/invoice.py b/modules/account_invoice/invoice.py index d6d4ddc711..0a8b7a12ab 100644 --- a/modules/account_invoice/invoice.py +++ b/modules/account_invoice/invoice.py @@ -1093,11 +1093,11 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): remainder_total_currency accounting_date = self.accounting_date or self.invoice_date or today - period_id = Period.find(self.company.id, date=accounting_date) + period = Period.find(self.company, date=accounting_date) move = Move() move.journal = self.journal - move.period = period_id + move.period = period move.date = accounting_date move.origin = self move.company = self.company @@ -1174,11 +1174,10 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): pattern = pattern.copy() accounting_date = self.accounting_date or self.invoice_date - period_id = Period.find( - self.company.id, date=accounting_date, + period = Period.find( + self.company, date=accounting_date, test_state=self.type != 'in') - period = Period(period_id) fiscalyear = period.fiscalyear pattern.setdefault('company', self.company.id) pattern.setdefault('fiscalyear', fiscalyear.id) @@ -1443,10 +1442,10 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): if line.account.party_required: line.party = party - period_id = Period.find(self.company.id, date=date) + period = Period.find(self.company, date=date) move = Move( - journal=payment_method.journal, period=period_id, date=date, + journal=payment_method.journal, period=period, date=date, origin=self, description=description, company=self.company, lines=lines) move.save() diff --git a/modules/account_invoice_defer/account.py b/modules/account_invoice_defer/account.py index 08c70a6420..7d46446cbc 100644 --- a/modules/account_invoice_defer/account.py +++ b/modules/account_invoice_defer/account.py @@ -214,7 +214,7 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView): Period = pool.get('account.period') # Ensure it starts at an opened period for deferral in deferrals: - Period.find(deferral.company.id, deferral.start_date) + Period.find(deferral.company, deferral.start_date) # Set state before create moves and defer amount to pass assert cls.write(deferrals, {'state': 'running'}) cls.create_moves(deferrals) diff --git a/modules/account_payment_clearing/payment.py b/modules/account_payment_clearing/payment.py index 06f0e6f3e1..601e1eb048 100644 --- a/modules/account_payment_clearing/payment.py +++ b/modules/account_payment_clearing/payment.py @@ -246,7 +246,7 @@ class Payment(metaclass=PoolMeta): if date is None: with Transaction().set_context(company=self.company.id): date = Date.today() - period = Period.find(self.company.id, date=date) + period = Period.find(self.company, date=date) local_currency = self.journal.currency == self.company.currency if not local_currency: diff --git a/modules/account_statement/statement.py b/modules/account_statement/statement.py index 11a64b5444..ab0f69689b 100644 --- a/modules/account_statement/statement.py +++ b/modules/account_statement/statement.py @@ -576,9 +576,9 @@ class Statement(Workflow, ModelSQL, ModelView): Move = pool.get('account.move') Period = pool.get('account.period') - period_id = Period.find(self.company.id, date=key['date']) + period = Period.find(self.company, date=key['date']) return Move( - period=period_id, + period=period, journal=self.journal.journal, date=key['date'], origin=self, diff --git a/modules/account_stock_anglo_saxon/invoice.py b/modules/account_stock_anglo_saxon/invoice.py index 630a5e257b..ee6e2d23cf 100644 --- a/modules/account_stock_anglo_saxon/invoice.py +++ b/modules/account_stock_anglo_saxon/invoice.py @@ -70,8 +70,7 @@ class InvoiceLine(metaclass=PoolMeta): accounting_date = (self.invoice.accounting_date or self.invoice.invoice_date) - period_id = Period.find(self.invoice.company.id, date=accounting_date) - period = Period(period_id) + period = Period.find(self.invoice.company, date=accounting_date) if period.fiscalyear.account_stock_method != 'anglo_saxon': return result diff --git a/modules/account_stock_continental/product.py b/modules/account_stock_continental/product.py index 7bd126eea2..ccb29711e6 100644 --- a/modules/account_stock_continental/product.py +++ b/modules/account_stock_continental/product.py @@ -211,7 +211,7 @@ class Product(metaclass=PoolMeta): company = Company(context['company']) with Transaction().set_context(company=company.id): stock_date_end = Date.today() - period = Period(Period.find(company.id, date=stock_date_end)) + period = Period.find(company, date=stock_date_end) if period.fiscalyear.account_stock_method: moves = [] with Transaction().set_context(locations=[l.id for l in locations], diff --git a/modules/account_stock_continental/stock.py b/modules/account_stock_continental/stock.py index bf53f50590..6d62e7edce 100644 --- a/modules/account_stock_continental/stock.py +++ b/modules/account_stock_continental/stock.py @@ -105,20 +105,19 @@ class Move(metaclass=PoolMeta): with Transaction().set_context(company=self.company.id): today = Date.today() for date in [self.effective_date, today]: - period_id = Period.find( - self.company.id, date=date, exception=False, test_state=False) - if not period_id and date < today: + period = Period.find( + self.company, date=date, exception=False, test_state=False) + if not period and date < today: return - elif period_id: + elif period: break else: return - period = Period(period_id) if not period.fiscalyear.account_stock_method: return if period.state != 'open': date = today - period = Period(Period.find(self.company.id, date=date)) + period = Period.find(self.company, date=date) type_ = self._get_account_stock_move_type() if not type_: @@ -153,7 +152,7 @@ class Move(metaclass=PoolMeta): 'stock_journal', company=self.company.id) return AccountMove( journal=journal, - period=period_id, + period=period, date=date, origin=self, lines=account_move_lines, diff --git a/modules/account_tax_cash/account.py b/modules/account_tax_cash/account.py index 2ee6be0d35..fb9d6d6c9f 100644 --- a/modules/account_tax_cash/account.py +++ b/modules/account_tax_cash/account.py @@ -306,7 +306,7 @@ class Invoice(metaclass=PoolMeta): date = Transaction().context.get( 'payment_date', Date.today()) periods[invoice.company] = Period.find( - invoice.company.id, date=date) + invoice.company, date=date) period = periods[invoice.company] ratio = invoice.cash_paid_ratio for line in invoice.move.lines: @@ -331,8 +331,7 @@ class InvoiceTax(metaclass=PoolMeta): accounting_date = ( self.invoice.accounting_date or self.invoice.invoice_date) period = Period.find( - self.invoice.company.id, date=accounting_date) - period = Period(period) + self.invoice.company, date=accounting_date) return period.is_on_cash_basis(self.tax) diff --git a/modules/commission_waiting/commission.py b/modules/commission_waiting/commission.py index d62b1bdf7c..56220e4251 100644 --- a/modules/commission_waiting/commission.py +++ b/modules/commission_waiting/commission.py @@ -70,7 +70,7 @@ class Commission(metaclass=PoolMeta): if date is None: with Transaction().set_context(company=self.agent.company.id): date = Date.today() - period = Period.find(self.agent.company.id, date=date) + period = Period.find(self.agent.company, date=date) move = Move(journal=self.get_journal(), origin=self, date=date, period=period, company=self.agent.company) diff --git a/modules/sale_point/sale.py b/modules/sale_point/sale.py index 3fa8a0a7db..306902e66e 100644 --- a/modules/sale_point/sale.py +++ b/modules/sale_point/sale.py @@ -358,7 +358,7 @@ class POSSale(Workflow, ModelSQL, ModelView, TaxableMixin): Move = pool.get('account.move') Period = pool.get('account.period') - period_id = Period.find(self.company.id, date=self.date) + period = Period.find(self.company, date=self.date) lines = [] for line in self.lines: move_lines = line.get_account_move_lines() @@ -376,7 +376,7 @@ class POSSale(Workflow, ModelSQL, ModelView, TaxableMixin): move = Move() move.journal = self.point.journal - move.period = period_id + move.period = period move.date = self.date move.origin = self move.company = self.company @@ -1299,7 +1299,7 @@ class POSCashTransfer(Workflow, ModelSQL, ModelView): Move = pool.get('account.move') Period = pool.get('account.period') - period_id = Period.find(self.company.id, date=self.date) + period = Period.find(self.company, date=self.date) line = Line() if self.amount >= 0: line.debit, line.credit = self.amount, Decimal(0) @@ -1312,7 +1312,7 @@ class POSCashTransfer(Workflow, ModelSQL, ModelView): move = Move() move.journal = self.type.journal - move.period = period_id + move.period = period move.date = self.date move.origin = self move.company = self.company