Skip to content

Commit

Permalink
[IMP] l10n_it_asset_management: Generate Code from Sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Oct 18, 2024
1 parent 4fec0ad commit 0de6329
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
6 changes: 6 additions & 0 deletions l10n_it_asset_management/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions l10n_it_asset_management/models/asset_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions l10n_it_asset_management/tests/test_assets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,22 @@ def test_same_asset_report_residual_partial_depreciation(self):
self.assertEqual(
asset_report_depreciation_line.amount_residual, expected_residual_amount
)

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))
32 changes: 31 additions & 1 deletion l10n_it_asset_management/views/asset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,37 @@
name="category_id"
attrs="{'readonly': [('state', '!=', 'non_depreciated')]}"
/>
<field name="code" />
<field
name="code"
attrs="{
'invisible': [
'&amp;',
('code_sequence_id', '!=', False),
('id', '=', False),
],
}"
/>
<!-- Invisibility domain of `code_sequence_details` could be simpler,
but like this it is easier to maintain and more obvious that it is
the opposite of `code`'s invisibility domain. -->
<div
name="code_sequence_details"
class="text-muted"
colspan="2"
attrs="{
'invisible': [
'!',
'&amp;',
('code_sequence_id', '!=', False),
('id', '=', False),
],
}"
>
The Code will be generated from the Category's Code Sequence <field
name="code_sequence_id"
class="d-inline"
/>
</div>
<field
name="used"
attrs="{'readonly': [('state', '!=', 'non_depreciated')]}"
Expand Down
1 change: 1 addition & 0 deletions l10n_it_asset_management/views/asset_category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<group>
<group>
<field name="tag_ids" widget="many2many_tags" />
<field name="code_sequence_id" />
</group>
<group>
<field name="print_by_default" />
Expand Down
3 changes: 3 additions & 0 deletions l10n_it_asset_management/wizard/account_move_manage_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,33 @@
options="{'no_create': True}"
attrs="{'required': [('management_type', '=', 'create')]}"
/>
<field name="code" />
<field
name="code"
attrs="{
'invisible': [
('code_sequence_id', '!=', False),
],
}"
/>
<!-- Invisibility domain of `code_sequence_details` could be simpler,
but like this it is easier to maintain and more obvious that it is
the opposite of `code`'s invisibility domain. -->
<div
name="code_sequence_details"
class="text-muted"
colspan="2"
attrs="{
'invisible': [
'!',
('code_sequence_id', '!=', False),
],
}"
>
The Code will be generated from the Category's Code Sequence <field
name="code_sequence_id"
class="d-inline"
/>
</div>
<field name="used" />
</group>
<group>
Expand Down

0 comments on commit 0de6329

Please sign in to comment.