From 258da73ae51e90c9e883ad1ea107683aebb27454 Mon Sep 17 00:00:00 2001
From: Laurent Smet <las@odoo.com>
Date: Mon, 18 Sep 2023 11:09:18 +0200
Subject: [PATCH] [FIX] account: Allow custom tax tags on misc journal entry

- Create a move
- Add a line and set a custom tax tag on it
- Add a new line to balance the move
=> The tax tag is gone.

This is because the code is recomputing taxes when adding a new line.

closes odoo/odoo#136047

Opw: 3487465
X-original-commit: 1cad85f77e6e7ca9bcbbd2545e53ad94eb9ad6ce
Signed-off-by: Brice Bartoletti (bib) <bib@odoo.com>
Signed-off-by: Laurent Smet (las) <las@odoo.com>
---
 addons/account/models/account_move.py         | 15 +++++++-----
 .../account/tests/test_account_move_entry.py  | 23 +++++++++++++++++++
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index 90ce939f426d..99754757674b 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -1144,6 +1144,7 @@ class AccountMove(models.Model):
             expected_tax_rep_lines = set()
             current_tax_rep_lines = set()
             inv_recompute_all_taxes = recompute_all_taxes
+            has_taxes = False
             for line in invoice.line_ids:
                 if line.recompute_tax_line:
                     inv_recompute_all_taxes = True
@@ -1151,6 +1152,7 @@ class AccountMove(models.Model):
                 if line.tax_repartition_line_id:
                     current_tax_rep_lines.add(line.tax_repartition_line_id._origin)
                 elif line.tax_ids:
+                    has_taxes = True
                     if invoice.is_invoice(include_receipts=True):
                         is_refund = invoice.move_type in ('out_refund', 'in_refund')
                     else:
@@ -1171,12 +1173,13 @@ class AccountMove(models.Model):
             delta_tax_rep_lines = expected_tax_rep_lines - current_tax_rep_lines
 
             # Compute taxes.
-            if inv_recompute_all_taxes:
-                invoice._recompute_tax_lines()
-            elif recompute_tax_base_amount:
-                invoice._recompute_tax_lines(recompute_tax_base_amount=True)
-            elif delta_tax_rep_lines and not self._context.get('move_reverse_cancel'):
-                invoice._recompute_tax_lines(tax_rep_lines_to_recompute=delta_tax_rep_lines)
+            if has_taxes or current_tax_rep_lines:
+                if inv_recompute_all_taxes:
+                    invoice._recompute_tax_lines()
+                elif recompute_tax_base_amount:
+                    invoice._recompute_tax_lines(recompute_tax_base_amount=True)
+                elif delta_tax_rep_lines and not self._context.get('move_reverse_cancel'):
+                    invoice._recompute_tax_lines(tax_rep_lines_to_recompute=delta_tax_rep_lines)
 
             if invoice.is_invoice(include_receipts=True):
 
diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py
index ec10204316d7..ce3628cbf6fd 100644
--- a/addons/account/tests/test_account_move_entry.py
+++ b/addons/account/tests/test_account_move_entry.py
@@ -486,6 +486,29 @@ class TestAccountMove(AccountTestInvoicingCommon):
             {'name': 'credit_line_1',            'debit': 0.0,       'credit': 1200.0,   'tax_ids': [],                                  'tax_line_id': False},
         ])
 
+    def test_misc_custom_tags(self):
+        tag = self.env['account.account.tag'].create({
+            'name': "test_misc_custom_tags",
+            'applicability': 'taxes',
+            'country_id': self.env.ref('base.us').id,
+        })
+        move_form = Form(self.env['account.move'].with_context(default_move_type='entry'))
+        with move_form.line_ids.new() as debit_line:
+            debit_line.name = 'debit_line'
+            debit_line.account_id = self.company_data['default_account_revenue']
+            debit_line.debit = 1000
+            debit_line.tax_tag_ids.add(tag)
+        with move_form.line_ids.new() as credit_line:
+            credit_line.name = 'credit_line'
+            credit_line.account_id = self.company_data['default_account_revenue']
+            credit_line.credit = 1000
+        move = move_form.save()
+        self.assertRecordValues(move.line_ids, [
+            # pylint: disable=bad-whitespace
+            {'debit': 1000.0,   'credit': 0.0,      'tax_tag_ids': tag.ids},
+            {'debit': 0.0,      'credit': 1000.0,   'tax_tag_ids': []},
+        ])
+
     def test_misc_prevent_unlink_posted_items(self):
         # You cannot remove journal items if the related journal entry is posted.
         self.test_move.action_post()
-- 
GitLab