[FIX] account_sequence: flush fields depending on sequence

Because we are flushing, and because the business code might be flushing
elsewhere (i.e. to validate constraints), the fields depending on the
sequence field might be protected by the ORM. This is not desired, so we
already reset them here.

The symptoms were that sometimes the related field `move_name` on
`account.move.line` would stay at `/` even after the new `name` was
given.

closes odoo/odoo#120488

X-original-commit: e7db87db57e4f98719602502d80948a5ed51adc2
Signed-off-by: Laurent Smet <las@odoo.com>
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
william-andre
2023-05-03 22:09:47 +02:00
parent 4523a250e0
commit 3d2f6a0c10
2 changed files with 31 additions and 0 deletions
+9
View File
@@ -243,6 +243,15 @@ class SequenceMixin(models.AbstractModel):
# is already flushed, otherwise we could lose non-sequence fields values, as the ORM believes
# them to be flushed.
self.flush_recordset()
# because we are flushing, and because the business code might be flushing elsewhere (i.e. to
# validate constraints), the fields depending on the sequence field might be protected by the
# ORM. This is not desired, so we already reset them here.
registry = self.env.registry
triggers = registry._field_triggers[self._fields[self._sequence_field]]
for inverse_field, triggered_fields in triggers.items():
for triggered_field in triggered_fields:
for field in registry.field_inverses[inverse_field[0]] if inverse_field else [None]:
self.env.add_to_compute(triggered_field, self[field.name] if field else self)
while True:
format_values['seq'] = format_values['seq'] + 1
sequence = format_string.format(**format_values)
@@ -11,6 +11,7 @@ from freezegun import freeze_time
from functools import reduce
import json
import psycopg2
from unittest.mock import patch
class TestSequenceMixinCommon(AccountTestInvoicingCommon):
@@ -383,6 +384,27 @@ class TestSequenceMixin(TestSequenceMixinCommon):
move_form.journal_id = journal
self.assertEqual(move.name, 'AJ/2021/10/0001')
def test_sequence_move_name_related_field_well_computed(self):
AccountMove = type(self.env['account.move'])
_compute_name = AccountMove._compute_name
def _flushing_compute_name(self):
self.env['account.move.line'].flush_model(fnames=['move_name'])
_compute_name(self)
payments = self.env['account.payment'].create([{
'payment_type': 'inbound',
'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id,
'partner_type': 'customer',
'partner_id': self.partner_a.id,
'amount': 500,
}] * 2)
with patch.object(AccountMove, '_compute_name', _flushing_compute_name):
payments.action_post()
for move in payments.move_id:
self.assertRecordValues(move.line_ids, [{'move_name': move.name}] * len(move.line_ids))
@tagged('post_install', '-at_install')
class TestSequenceMixinDeletion(TestSequenceMixinCommon):