diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index fb216f05347..8b8c99e164b 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -1889,8 +1889,13 @@ class AccountMove(models.Model): # Skip posted moves. for invoice in (x for x in container['records'] if x.state != 'posted'): - # Unlink tax lines if all tax tags have been removed. + # Unlink tax lines if all taxes have been removed. if not invoice.line_ids.tax_ids: + # if there isn't any tax but there remains a tax_line_id, it means we are currently in the process of + # removing the taxes from the entry. Thus, we want the automatic balancing to happen in order to have + # a smooth process for tax deletion + if not invoice.line_ids.filtered('tax_line_id'): + continue invoice.line_ids.filtered('tax_line_id').unlink() # Set the balancing line's balance and amount_currency to zero, diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py index 2ffacf12fb6..699e65572d9 100644 --- a/addons/account/tests/test_account_move_entry.py +++ b/addons/account/tests/test_account_move_entry.py @@ -2,7 +2,7 @@ from odoo.addons.account.tests.common import AccountTestInvoicingCommon from odoo.tests import tagged, new_test_user from odoo.tests.common import Form -from odoo import fields +from odoo import Command, fields from odoo.exceptions import UserError, RedirectWarning from dateutil.relativedelta import relativedelta @@ -805,3 +805,34 @@ class TestAccountMove(AccountTestInvoicingCommon): # You can remove journal items if the related journal entry is draft. self.test_move.button_draft() edit_tax_on_posted_moves() + + def test_misc_tax_autobalance(self): + # Saving an unbalanced entry isn't something desired but we need this piece of code to work in order to support + # the tax auto-calculation on miscellaneous move. Indeed, the JS class `AutosaveMany2ManyTagsField` triggers the + # saving of the record as soon as a tax base_line is modified. + move = self.env["account.move"].create({ + "move_type": "entry", + "line_ids": [ + Command.create({ + "name": "revenue line", + "account_id": self.company_data["default_account_revenue"].id, + 'tax_ids': [Command.set(self.company_data['default_tax_sale'].ids)], + "balance": -10.0, + }), + ] + }) + tax_line = move.line_ids.filtered("tax_ids") + tax_line.unlink() + + # But creating unbalanced misc entry shouldn't be allowed otherwise + with self.assertRaisesRegex(UserError, r"The move \(.*\) is not balanced\."): + self.env["account.move"].create({ + "move_type": "entry", + "line_ids": [ + Command.create({ + "name": "revenue line", + "account_id": self.company_data["default_account_revenue"].id, + "balance": -10.0, + }), + ] + })