From 98038bc1808f55978ebc8f1e6fa5b6f5548323c4 Mon Sep 17 00:00:00 2001
From: "Guillaume (guva)" <guva@odoo.com>
Date: Thu, 25 May 2023 13:51:37 +0000
Subject: [PATCH] [FIX] account: change journal on journal entry

With this commit, we make sure that the name
of an entry is well computed when changing
the journal in the form view.

Steps:

- With fresh db
- Open a new journal entry form
- Change the journal
-> The name doesn't change, it should change
   regarding the code of the journal

Also add a test for the fix which introduced
the issue 46e6ae29411f07a55111da6672003fc8b88c1367.

opw-3332590

closes odoo/odoo#122505

Signed-off-by: Brice Bartoletti (bib) <bib@odoo.com>
---
 addons/account/models/account_move.py         |  2 +-
 .../account/tests/test_account_move_entry.py  | 19 ++++++++++++++++++
 .../tests/test_account_move_out_invoice.py    | 20 +++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index 1a0eca7d95fb..c6d48a3569c6 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -1630,7 +1630,7 @@ class AccountMove(models.Model):
 
     @api.onchange('journal_id')
     def _onchange_journal_id(self):
-        if not self.quick_edit_mode and self._get_last_sequence(lock=False):
+        if not self.quick_edit_mode:
             self.name = '/'
             self._compute_name()
 
diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py
index 059023d6646d..a614444cb9b6 100644
--- a/addons/account/tests/test_account_move_entry.py
+++ b/addons/account/tests/test_account_move_entry.py
@@ -1024,3 +1024,22 @@ class TestAccountMove(AccountTestInvoicingCommon):
             {'name': 'Purchase Tax 15%', 'credit':   0.0, 'debit':  15.0, 'account_id': self.company_data['default_account_tax_purchase'].id, 'tax_ids': [],               'tax_base_amount': 100, 'tax_tag_invert': False,  'tax_repartition_line_id': purchase_invoice_rep_line.id},
             {'name': 'debit',            'credit':   0.0, 'debit': 100.0, 'account_id': test_account.id,                                      'tax_ids': purchase_tax.ids, 'tax_base_amount': 0,   'tax_tag_invert': False,  'tax_repartition_line_id': False},
         ])
+
+    @freeze_time('2021-10-01 00:00:00')
+    def test_change_journal_account_move(self):
+        """Changing the journal should change the name of the move"""
+        journal = self.env['account.journal'].create({
+            'name': 'awesome journal',
+            'type': 'general',
+            'code': 'AJ',
+        })
+        move = self.env['account.move'].with_context(default_move_type='entry')
+        with Form(move) as move_form:
+            self.assertEqual(move_form.name, 'MISC/2021/10/0001')
+            move_form.journal_id, journal = journal, move_form.journal_id
+            self.assertEqual(move_form.name, 'AJ/2021/10/0001')
+            # ensure we aren't burning any sequence by switching journal
+            move_form.journal_id, journal = journal, move_form.journal_id
+            self.assertEqual(move_form.name, 'MISC/2021/10/0001')
+            move_form.journal_id, journal = journal, move_form.journal_id
+            self.assertEqual(move_form.name, 'AJ/2021/10/0001')
diff --git a/addons/account/tests/test_account_move_out_invoice.py b/addons/account/tests/test_account_move_out_invoice.py
index d26db0aecdb8..67abca2400fd 100644
--- a/addons/account/tests/test_account_move_out_invoice.py
+++ b/addons/account/tests/test_account_move_out_invoice.py
@@ -3433,3 +3433,23 @@ class TestAccountMoveOutInvoiceOnchanges(AccountTestInvoicingCommon):
         self.assertEqual(move.currency_id, self.company_data['currency'])
         move.journal_id = second_journal
         self.assertEqual(move.currency_id, self.currency_data['currency'])
+
+    @freeze_time('2023-01-01')
+    def test_change_first_journal_move_sequence(self):
+        """Invoice name should not be reset when posting the invoice"""
+        new_sale_journal = self.company_data['default_journal_sale'].copy()
+        invoice = self.env['account.move'].with_context(default_move_type='out_invoice').create({
+            'journal_id': new_sale_journal.id,
+            'partner_id': self.partner_a.id,
+            'name': 'INV1/2023/00010',
+            'invoice_line_ids': [
+                Command.create({
+                    'name': 'My super product.',
+                    'quantity': 1.0,
+                    'price_unit': 750.0,
+                    'account_id': self.company_data['default_account_revenue'].id,
+                })
+            ]
+        })
+        invoice.action_post()
+        self.assertEqual(invoice.name, 'INV1/2023/00010')
-- 
GitLab