From d22cd5473326b3b5af4ae44b0d121349a1a90850 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#81983

Issue: 2718413
Signed-off-by: Laurent Smet <las@odoo.com>
---
 addons/account/models/account_move.py       | 21 ++++++++++++---------
 addons/account/tests/test_reconciliation.py |  7 ++-----
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index c1bb61ef2f20..734d611712c1 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -2129,15 +2129,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_reconciliation.py b/addons/account/tests/test_reconciliation.py
index 77bac475d32b..36f6fa85c328 100644
--- a/addons/account/tests/test_reconciliation.py
+++ b/addons/account/tests/test_reconciliation.py
@@ -1913,15 +1913,12 @@ class TestReconciliationExec(TestReconciliation):
     def test_reconciliation_cash_basis_revert(self):
         company = self.env.ref('base.main_company')
         company.tax_cash_basis_journal_id = self.cash_basis_journal
+        tax_cash_basis10percent = self.tax_cash_basis.copy({'amount': 10})
+        self.tax_waiting_account.reconcile = True
         tax_waiting_account10 = self.tax_waiting_account.copy({
             'name': 'TAX WAIT 10',
             'code': 'TWAIT1',
         })
-        tax_cash_basis10percent = self.tax_cash_basis.copy({
-            'amount': 10,
-            'cash_basis_transition_account_id': tax_waiting_account10.id,
-        })
-        self.tax_waiting_account.reconcile = True
 
         # Purchase
         purchase_move = self.env['account.move'].create({
-- 
GitLab