From 480510327493d0819a99feed3deda0fbeccc2eb2 Mon Sep 17 00:00:00 2001 From: "Guillaume (guva)" Date: Wed, 27 Oct 2021 12:47:20 +0000 Subject: [PATCH] [FIX] account: change journal on draft invoice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Step to reproduce: - Create a SO, save and confirm it - Create an invoice, on edit mode, change the journal and click on save Issue: - If the invoice is the first one in the current month, a sequence number is automatically assigned and an error is displaid. forwardport of https://github.com/odoo/odoo/commit/b8328e45d526d276b5781da3523a0d5874da64bd opw-2660571 opw-2879716 closes odoo/odoo#79084 closes odoo/odoo#95386 X-original-commit: 7b296982139518460405fa63eca6774bd82ba7b1 Signed-off-by: William André (wan) Signed-off-by: Minet Adrien (admi) --- addons/account/models/account_move.py | 2 +- addons/account/tests/test_sequence_mixin.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 4d7eaae350f..763e6aaf28a 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -2319,7 +2319,7 @@ class AccountMove(models.Model): raise UserError(_('You cannot overwrite the values ensuring the inalterability of the accounting.')) if (move.posted_before and 'journal_id' in vals and move.journal_id.id != vals['journal_id']): raise UserError(_('You cannot edit the journal of an account move if it has been posted once.')) - if (move.name and move.name != '/' and 'journal_id' in vals and move.journal_id.id != vals['journal_id']): + if (move.name and move.name != '/' and move.sequence_number not in (0, 1) and 'journal_id' in vals and move.journal_id.id != vals['journal_id']): raise UserError(_('You cannot edit the journal of an account move if it already has a sequence number assigned.')) # You can't change the date of a move being inside a locked period. diff --git a/addons/account/tests/test_sequence_mixin.py b/addons/account/tests/test_sequence_mixin.py index a42d1381811..714706e47ff 100644 --- a/addons/account/tests/test_sequence_mixin.py +++ b/addons/account/tests/test_sequence_mixin.py @@ -7,6 +7,7 @@ from odoo.exceptions import ValidationError, UserError from odoo.tools import mute_logger from dateutil.relativedelta import relativedelta +from freezegun import freeze_time from functools import reduce import json import psycopg2 @@ -474,3 +475,17 @@ class TestSequenceMixinDeletion(TestSequenceMixinCommon): all_moves.button_draft() with self.assertRaises(UserError): all_moves.unlink() + + @freeze_time('2021-10-01 00:00:00') + def test_change_journal_on_first_account_move(self): + """Changing the journal on the first move is allowed""" + journal = self.env['account.journal'].create({ + 'name': 'awesome journal', + 'type': 'general', + 'code': 'AJ', + }) + move = self.env['account.move'].create({}) + self.assertEqual(move.name, 'MISC/2021/10/0001') + with Form(move) as move_form: + move_form.journal_id = journal + self.assertEqual(move.name, 'AJ/2021/10/0001')