diff --git a/addons/account/models/sequence_mixin.py b/addons/account/models/sequence_mixin.py index 9f3dcc23284..e2c032fd71f 100644 --- a/addons/account/models/sequence_mixin.py +++ b/addons/account/models/sequence_mixin.py @@ -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) diff --git a/addons/account/tests/test_sequence_mixin.py b/addons/account/tests/test_sequence_mixin.py index e975623460e..47ceddf30c4 100644 --- a/addons/account/tests/test_sequence_mixin.py +++ b/addons/account/tests/test_sequence_mixin.py @@ -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):