From d03a66c0c3976573defb0ff0bbb4d0a9f910a795 Mon Sep 17 00:00:00 2001 From: Brice bib Bartoletti Date: Fri, 13 Jan 2023 13:42:18 +0000 Subject: [PATCH] [FIX] account: prevent saving unbalanced misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The aim of this commit is to prevent the user to save unbalanced misc entry that wouldn't be created by adding a tax. Context: Since commit d8d47f9 we allow user to create unbalanced entries by automatically balancing them on a suspense account. This isn't an issue but we also allow to post those entries. As the flow is smooth and fast, a quick user could just post this entries which could be problematic. Resetting to draft could be an option but that wouldn't be convenient if some lines would have been reconciled. (which could have side effects in case of there would be a cash basis tax on the line) It's also impossible in case of a journal that would be hashed. It also means that the problem would be delayed until the end of year accounting processes, when accountants are tied to dead-lines. Before this commit: An unbalanced misc entry created manually would automatically be balanced with a suspense account. After this commit: - Unbalanced misc entry can't be saved if there isn't any tax on it - Unbalanced misc entry with a tax are balanced automatically. - Unbalanced misc entry with a tax we are about to remove are balanced automatically closes odoo/odoo#110063 Community-pr: https://github.com/odoo/odoo/pull/109878 Task-id: 3129172 X-original-commit: c86786676b86381fbeeac9897508d3fe0ce77151 Signed-off-by: William André (wan) Signed-off-by: Brice Bartoletti (bib) --- addons/account/models/account_move.py | 7 +++- .../account/tests/test_account_move_entry.py | 33 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) 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, + }), + ] + })