From 2d9fb2b11073bb6295eae47ad7672266bb76a57c Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Tue, 16 Jul 2024 16:07:23 +0200 Subject: [PATCH] [IMP] l10n_it_asset_management: Generate Code from Sequence --- l10n_it_asset_management/models/asset.py | 6 ++++ .../models/asset_category.py | 5 +++ .../tests/test_assets_management.py | 19 +++++++++++ l10n_it_asset_management/views/asset.xml | 32 ++++++++++++++++++- .../views/asset_category.xml | 1 + .../wizard/account_move_manage_asset.py | 3 ++ .../wizard/account_move_manage_asset_view.xml | 28 +++++++++++++++- 7 files changed, 92 insertions(+), 2 deletions(-) diff --git a/l10n_it_asset_management/models/asset.py b/l10n_it_asset_management/models/asset.py index 949605621e62..f62bf4526b1f 100644 --- a/l10n_it_asset_management/models/asset.py +++ b/l10n_it_asset_management/models/asset.py @@ -31,6 +31,9 @@ def get_default_company_id(self): code = fields.Char( default="", ) + code_sequence_id = fields.Many2one( + related="category_id.code_sequence_id", + ) company_id = fields.Many2one( "res.company", @@ -114,6 +117,9 @@ def create(self, vals_list): asset = super().create(vals) if create_deps_from_categ: asset.onchange_category_id() + code_sequence = asset.code_sequence_id + if code_sequence and not asset.code: + asset.code = code_sequence.next_by_id() assets |= asset return assets diff --git a/l10n_it_asset_management/models/asset_category.py b/l10n_it_asset_management/models/asset_category.py index 603bcefd0467..49454e308c77 100644 --- a/l10n_it_asset_management/models/asset_category.py +++ b/l10n_it_asset_management/models/asset_category.py @@ -94,6 +94,11 @@ def get_default_type_ids(self): " printing assets' reports.", ) + code_sequence_id = fields.Many2one( + comodel_name="ir.sequence", + help="Sequence to generate the Code of new assets.", + ) + tag_ids = fields.Many2many( "asset.tag", string="Tag", diff --git a/l10n_it_asset_management/tests/test_assets_management.py b/l10n_it_asset_management/tests/test_assets_management.py index a9d880e606bc..beb8c9f5a943 100644 --- a/l10n_it_asset_management/tests/test_assets_management.py +++ b/l10n_it_asset_management/tests/test_assets_management.py @@ -816,3 +816,22 @@ def test_journal_prev_year(self): total = report.report_total_ids self.assertEqual(total.amount_depreciation_fund_curr_year, 1000) self.assertEqual(total.amount_depreciation_fund_prev_year, 1000) + + def test_create_category_code_sequence(self): + """If the category has a "Code Sequence", + it is used for created assets.""" + # Arrange + category = self.asset_category_1 + sequence = self.env["ir.sequence"].create( + { + "name": "Test Sequence", + } + ) + sequence_next = sequence.number_next + category.code_sequence_id = sequence + + # Act + asset = self._create_asset() + + # Assert + self.assertEqual(asset.code, str(sequence_next)) diff --git a/l10n_it_asset_management/views/asset.xml b/l10n_it_asset_management/views/asset.xml index 4749ec39dc52..446b176f3325 100644 --- a/l10n_it_asset_management/views/asset.xml +++ b/l10n_it_asset_management/views/asset.xml @@ -82,7 +82,37 @@ name="category_id" attrs="{'readonly': [('state', '!=', 'non_depreciated')]}" /> - + + +
+ The Code will be generated from the Category's Code Sequence +
+ diff --git a/l10n_it_asset_management/wizard/account_move_manage_asset.py b/l10n_it_asset_management/wizard/account_move_manage_asset.py index 1156aba16558..886994913593 100644 --- a/l10n_it_asset_management/wizard/account_move_manage_asset.py +++ b/l10n_it_asset_management/wizard/account_move_manage_asset.py @@ -33,6 +33,9 @@ def get_default_move_ids(self): code = fields.Char( default="", ) + code_sequence_id = fields.Many2one( + related="category_id.code_sequence_id", + ) company_id = fields.Many2one( "res.company", diff --git a/l10n_it_asset_management/wizard/account_move_manage_asset_view.xml b/l10n_it_asset_management/wizard/account_move_manage_asset_view.xml index 732600f9e4ee..3e32dafda4e3 100644 --- a/l10n_it_asset_management/wizard/account_move_manage_asset_view.xml +++ b/l10n_it_asset_management/wizard/account_move_manage_asset_view.xml @@ -73,7 +73,33 @@ options="{'no_create': True}" attrs="{'required': [('management_type', '=', 'create')]}" /> - + + +
+ The Code will be generated from the Category's Code Sequence +