From 8deeb10ef8071d13cb088a3a7e10464737e9a421 Mon Sep 17 00:00:00 2001 From: "Andrea Grazioso (agr-odoo)" Date: Thu, 23 Mar 2023 14:39:08 +0000 Subject: [PATCH] [FIX] account: lock posted entry issue with cash rounding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Enable Cash Rounding in settings. - Create the cash rounding as - Rounding precision: 5.00 - Rounding Strategy: Add a rounding line - Profit Account: Any - Loss Account: Any - Rounding Method: Half-up - Enable "Lock Posted Entries with Hash" on Customer Invoices Journal. - Create a draft invoice and set the cash rounding on it. - Confirm Error will raise You cannot edit the following fields: Account, Label, Partner. The following entries are already hashed This occurs because cash rounding is recomputed during post, after hash was written opw-3235377 closes odoo/odoo#119126 X-original-commit: 5cb044dd8a2385c9a3c09520ed97eb4faa39216d Signed-off-by: William André (wan) --- addons/account/models/account_move.py | 6 +++++ .../tests/test_account_inalterable_hash.py | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index d61c6fddd40..afa67c22993 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -1990,6 +1990,12 @@ class AccountMove(models.Model): # self.line_ids -= existing_cash_rounding_line return + # No update needed + if existing_cash_rounding_line \ + and existing_cash_rounding_line.balance == diff_balance \ + and existing_cash_rounding_line.amount_currency == diff_amount_currency: + return + _apply_cash_rounding(self, diff_balance, diff_amount_currency, existing_cash_rounding_line) @contextmanager diff --git a/addons/account/tests/test_account_inalterable_hash.py b/addons/account/tests/test_account_inalterable_hash.py index f79209bdf19..602edca76e3 100644 --- a/addons/account/tests/test_account_inalterable_hash.py +++ b/addons/account/tests/test_account_inalterable_hash.py @@ -1,6 +1,7 @@ from odoo.addons.account.tests.common import AccountTestInvoicingCommon from odoo.models import Model from odoo.tests import tagged +from odoo.tests.common import Form from odoo import fields from odoo.exceptions import UserError from odoo.tools import format_date @@ -243,3 +244,26 @@ class TestAccountMoveInalterableHash(AccountTestInvoicingCommon): Model.write(moves[1], {'date': fields.Date.from_string('2023-01-07')}) integrity_check = moves.company_id._check_hash_integrity()['results'][0] self.assertEqual(integrity_check['msg_cover'], f'Corrupted data on journal entry with id {moves[1].id}.') + + def test_account_move_hash_with_cash_rounding(self): + # Enable inalterable hash + self.company_data['default_journal_sale'].restrict_mode_hash_table = True + # Required for `invoice_cash_rounding_id` to be visible in the view + self.env.user.groups_id += self.env.ref('account.group_cash_rounding') + # Test 'add_invoice_line' rounding + invoice = self.init_invoice('out_invoice', products=self.product_a+self.product_b) + move_form = Form(invoice) + # Add a cash rounding having 'add_invoice_line'. + move_form.invoice_cash_rounding_id = self.cash_rounding_a + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 999.99 + move_form.save() + + # Should not raise + invoice.action_post() + + self.assertEqual(invoice.amount_total, 1410.0) + self.assertEqual(invoice.amount_untaxed, 1200.0) + self.assertEqual(invoice.amount_tax, 210) + self.assertEqual(len(invoice.invoice_line_ids), 2) + self.assertEqual(len(invoice.line_ids), 6)