[MIG] account_journal_restrict_mode: Migration to 17.0

This commit is contained in:
LauraCForgeFlow
2024-05-23 11:48:58 +02:00
parent 23417828e6
commit ae7e4aa3eb
4 changed files with 46 additions and 10 deletions

View File

@@ -4,7 +4,7 @@
{
"name": "Account Journal Restrict Mode",
"summary": "Lock All Posted Entries of Journals.",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"author": "ForgeFlow S.L., Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-financial-tools",
"category": "Accounting",

View File

@@ -1,12 +1,9 @@
# Copyright 2023 ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import SUPERUSER_ID, api
def post_init_hook(cr, registry):
def post_init_hook(env):
"""Enable restrict mode on all journals"""
env = api.Environment(cr, SUPERUSER_ID, {})
journals_to_update = env["account.journal"].search(
[("restrict_mode_hash_table", "=", False)]
)

View File

@@ -8,7 +8,7 @@ from odoo.exceptions import UserError
class AccountJournal(models.Model):
_inherit = "account.journal"
restrict_mode_hash_table = fields.Boolean(default=True)
restrict_mode_hash_table = fields.Boolean(default=True, readonly=True)
@api.constrains("restrict_mode_hash_table")
def _check_journal_restrict_mode(self):
@@ -17,3 +17,13 @@ class AccountJournal(models.Model):
raise UserError(
_("Journal %s must have Lock Posted Entries enabled.") % rec.name
)
@api.model_create_multi
def create(self, vals_list):
# Proposed fix to odoo https://github.com/odoo/odoo/pull/147738.
# But while they don't merge (as it's not an issue they will face in Odoo standard...)
journals = super().create(vals_list)
for journal in journals:
if journal.restrict_mode_hash_table and not journal.secure_sequence_id:
journal._create_secure_sequence(["secure_sequence_id"])
return journals

View File

@@ -2,14 +2,21 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.exceptions import UserError
from odoo.tests import common
from odoo.tests import tagged
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
class TestAccountJournalRestrictMode(common.TransactionCase):
@tagged("post_install", "-at_install")
class TestAccountJournalRestrictMode(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls):
super(TestAccountJournalRestrictMode, cls).setUpClass()
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
cls.account_journal_obj = cls.env["account.journal"]
cls.company_obj = cls.env["res.company"]
cls.currency_obj = cls.env["res.currency"]
cls.chart_template_obj = cls.env["account.chart.template"]
cls.country_be = cls.env.ref("base.be") # Refs
def test_journal_default_lock_entries(self):
journal = self.account_journal_obj.create(
@@ -18,3 +25,25 @@ class TestAccountJournalRestrictMode(common.TransactionCase):
self.assertTrue(journal.restrict_mode_hash_table)
with self.assertRaises(UserError):
journal.write({"restrict_mode_hash_table": False})
def test_journal_default_secure_sequence_new_company(self):
test_company = self.company_obj.create(
{
"name": "My Test Company",
"currency_id": self.currency_obj.search([("name", "=", "USD")]).id,
"country_id": self.country_be.id,
}
)
self.chart_template_obj.try_loading(
template_code="generic_coa", company=test_company, install_demo=False
)
journals = self.env["account.journal"].search(
[("company_id", "=", test_company.id)]
)
self.assertTrue(journals)
self.assertTrue(
all(
journal.restrict_mode_hash_table and journal.secure_sequence_id
for journal in journals
)
)