[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
This commit is contained in:
Denis Ledoux
2019-03-11 17:22:09 +00:00
parent 4231103cc9
commit 570f8ee71e
@@ -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,))