From 3b40999792d80786f4c973764e1f560934d6f6b0 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Wed, 18 Dec 2024 14:55:34 +0100 Subject: [PATCH] [IMP] l10n_it_vat_settlement_date: Prevent edit after posting Make the field `l10n_it_vat_settlement_date` follow the same readonly logic as `date`: it cannot be edited when the invoice is in posted. --- .../models/account_move.py | 5 ++ l10n_it_vat_settlement_date/tests/__init__.py | 1 + .../tests/test_account_move.py | 60 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 l10n_it_vat_settlement_date/tests/test_account_move.py diff --git a/l10n_it_vat_settlement_date/models/account_move.py b/l10n_it_vat_settlement_date/models/account_move.py index e4bbcfb01b0d..c6f9cf7dfad2 100644 --- a/l10n_it_vat_settlement_date/models/account_move.py +++ b/l10n_it_vat_settlement_date/models/account_move.py @@ -13,6 +13,11 @@ class AccountMove(models.Model): compute="_compute_l10n_it_vat_settlement_date", store=True, readonly=False, + states={ + "posted": [ + ("readonly", True), + ], + }, ) @api.depends( diff --git a/l10n_it_vat_settlement_date/tests/__init__.py b/l10n_it_vat_settlement_date/tests/__init__.py index 2d4ef90063e5..c44c3bed084a 100644 --- a/l10n_it_vat_settlement_date/tests/__init__.py +++ b/l10n_it_vat_settlement_date/tests/__init__.py @@ -1,4 +1,5 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import test_account_move from . import test_vat_period_end_statement from . import test_vat_registry diff --git a/l10n_it_vat_settlement_date/tests/test_account_move.py b/l10n_it_vat_settlement_date/tests/test_account_move.py new file mode 100644 index 000000000000..5197c7eee0fa --- /dev/null +++ b/l10n_it_vat_settlement_date/tests/test_account_move.py @@ -0,0 +1,60 @@ +# Copyright 2024 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import datetime + +from dateutil.relativedelta import relativedelta + +from odoo.tests import Form, tagged + +from odoo.addons.account.tests.common import AccountTestInvoicingCommon + + +@tagged("post_install", "-at_install") +class TestAccountMove(AccountTestInvoicingCommon): + def test_draft_not_readonly(self): + """VAT settlement date is not readonly when invoice is in draft.""" + # Arrange + bill = self.init_invoice( + "in_invoice", + invoice_date=datetime.date(2020, 1, 1), + amounts=[ + 100, + ], + ) + settlement_date = bill.invoice_date + relativedelta(days=1) + # pre-condition + self.assertEqual(bill.state, "draft") + + # Act + with Form(bill) as bill_form: + bill_form.l10n_it_vat_settlement_date = settlement_date + + # Assert + self.assertEqual(bill.l10n_it_vat_settlement_date, settlement_date) + + def test_posted_readonly(self): + """VAT settlement date is readonly when invoice is posted.""" + # Arrange + bill = self.init_invoice( + "in_invoice", + invoice_date=datetime.date(2020, 1, 1), + amounts=[ + 100, + ], + post=True, + ) + settlement_date = bill.invoice_date + relativedelta(days=1) + # pre-condition + self.assertEqual(bill.state, "posted") + + # Act + with self.assertRaises(AssertionError) as ae: + with Form(bill) as bill_form: + bill_form.l10n_it_vat_settlement_date = settlement_date + + # Assert + exc_message = ae.exception.args[0] + self.assertIn("readonly field", exc_message) + self.assertIn("l10n_it_vat_settlement_date", exc_message) + self.assertNotEqual(bill.l10n_it_vat_settlement_date, settlement_date)