From 570f8ee71e2c35537299ffcd3e592ff0b76e9dbb Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Mon, 11 Mar 2019 09:42:13 +0000 Subject: [PATCH] [FIX] account: bank statement confirm performance improvement `fast_counterpart_creation` creates new move lines. The ORM doesn't know if these new move lines changed or not the value of the field `journal_entry_ids` of each of the bank statement lines, and for that reason this field got invalidated from the cache at each iteration, and the value was prefetched as soon as `st_line.journal_entry_ids.ids` was asked. Storing the value of this field for each line in the dict `journal_entries` overcome this problem as we get the value from this dict during the loop. Of course, this relies on the fact the value of `journal_entry_ids` doesn't change during the loop --- addons/account/models/account_bank_statement.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/addons/account/models/account_bank_statement.py b/addons/account/models/account_bank_statement.py index 3446479ba18..9e9228947ce 100644 --- a/addons/account/models/account_bank_statement.py +++ b/addons/account/models/account_bank_statement.py @@ -234,13 +234,17 @@ class AccountBankStatement(models.Model): statements = self.filtered(lambda r: r.state == 'open') for statement in statements: moves = self.env['account.move'] + # `line.journal_entry_ids` gets invalidated from the cache during the loop + # because new move lines are being created at each iteration. + # The below dict is to prevent the ORM to permanently refetch `line.journal_entry_ids` + line_journal_entries = {line: line.journal_entry_ids for line in statement.line_ids} for st_line in statement.line_ids: - if st_line.account_id and not st_line.journal_entry_ids.ids: + journal_entries = line_journal_entries[st_line] + if st_line.account_id and not journal_entries.ids: st_line.fast_counterpart_creation() - elif not st_line.journal_entry_ids.ids and not statement.currency_id.is_zero(st_line.amount): + elif not journal_entries.ids and not statement.currency_id.is_zero(st_line.amount): raise UserError(_('All the account entries lines must be processed in order to close the statement.')) - for aml in st_line.journal_entry_ids: - moves |= aml.move_id + moves = statement.mapped('line_ids.journal_entry_ids.move_id') if moves: moves.filtered(lambda m: m.state != 'posted').post() statement.message_post(body=_('Statement %s confirmed, journal items were created.') % (statement.name,))