From 2f6a35eb73978ce0638765fd0981c7e95d9df696 Mon Sep 17 00:00:00 2001
From: Laurent Smet <las@odoo.com>
Date: Tue, 28 Dec 2021 13:01:44 +0000
Subject: [PATCH] [FIX] account: Keep CABA tax account when reversing

When reversing the CABA entry, the CABA tax account was replaced by the transition tax account if not set on the repartition line.
To reproduce:
Set up a CABA tax with ACC1 as transition tax account and ACC2 as tax account.
When creating the CABA entry, there is a line on ACC1 and another on ACC2.
When reversing the CABA entry, both lines are now on ACC1.

Introduced by https://github.com/odoo/odoo/pull/79556

closes odoo/odoo#81986

Issue: 2718413
X-original-commit: d22cd5473326b3b5af4ae44b0d121349a1a90850
Signed-off-by: Laurent Smet <las@odoo.com>
---
 addons/account/models/account_move.py         | 21 +++++++++++--------
 .../tests/test_account_move_reconcile.py      |  1 -
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index 21113d38fee8..483af967944f 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -2401,15 +2401,18 @@ class AccountMove(models.Model):
                 refund_repartition_line = tax_repartition_lines_mapping[invoice_repartition_line]
 
                 # Find the right account.
-                account_id = self.env['account.move.line']._get_default_tax_account(refund_repartition_line).id
-                if not account_id:
-                    if not invoice_repartition_line.account_id:
-                        # Keep the current account as the current one comes from the base line.
-                        account_id = line_vals['account_id']
-                    else:
-                        tax = invoice_repartition_line.invoice_tax_id
-                        base_line = self.line_ids.filtered(lambda line: tax in line.tax_ids.flatten_taxes_hierarchy())[0]
-                        account_id = base_line.account_id.id
+                if cancel:
+                    account_id = line_vals['account_id']
+                else:
+                    account_id = self.env['account.move.line']._get_default_tax_account(refund_repartition_line).id
+                    if not account_id:
+                        if not invoice_repartition_line.account_id:
+                            # Keep the current account as the current one comes from the base line.
+                            account_id = line_vals['account_id']
+                        else:
+                            tax = invoice_repartition_line.invoice_tax_id
+                            base_line = self.line_ids.filtered(lambda line: tax in line.tax_ids.flatten_taxes_hierarchy())[0]
+                            account_id = base_line.account_id.id
 
                 tags = refund_repartition_line.tag_ids
                 if line_vals.get('tax_ids'):
diff --git a/addons/account/tests/test_account_move_reconcile.py b/addons/account/tests/test_account_move_reconcile.py
index bb58e3bc700a..2e2d83252cc0 100644
--- a/addons/account/tests/test_account_move_reconcile.py
+++ b/addons/account/tests/test_account_move_reconcile.py
@@ -2060,7 +2060,6 @@ class TestAccountMoveReconcile(AccountTestInvoicingCommon):
     def test_reconcile_cash_basis_revert(self):
         ''' Ensure the cash basis journal entry can be reverted. '''
         self.cash_basis_transfer_account.reconcile = True
-        self.cash_basis_tax_a_third_amount.cash_basis_transition_account_id = self.tax_account_1
 
         invoice_move = self.env['account.move'].create({
             'move_type': 'entry',
-- 
GitLab