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,))