Skip to content

Commit

Permalink
[IMP] l10n_it_asset_management: Max depreciable amount
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Jul 10, 2024
1 parent 79f19bb commit fc237d2
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 20 deletions.
4 changes: 2 additions & 2 deletions l10n_it_asset_management/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Author(s): Silvio Gregorini ([email protected])
# Copyright 2019 Openforce Srls Unipersonale (www.openforce.it)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import asset_compute_depreciable_amount

from . import account_account
from . import account_fiscal_year
from . import account_journal
Expand Down
4 changes: 3 additions & 1 deletion l10n_it_asset_management/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def onchange_company_currency(self):
def onchange_purchase_amount(self):
if self.purchase_amount:
for dep in self.depreciation_ids:
dep.amount_depreciable = self.purchase_amount * dep.base_coeff
dep.amount_depreciable = dep._get_depreciable_amount(
self.purchase_amount
)
if self.depreciation_ids.mapped("line_ids").filtered(
lambda line: line.move_type == "depreciated"
):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# Author(s): Silvio Gregorini ([email protected])
# Copyright 2019 Openforce Srls Unipersonale (www.openforce.it)
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AssetCategoryDepreciationType(models.Model):
_name = "asset.category.depreciation.type"
_inherit = "l10n_it_asset_management.compute.depreciable_amount"
_description = "Asset Category - Depreciation Type"

base_coeff = fields.Float(
default=1,
help="Coeff to compute depreciable amount from purchase amount",
string="Dep Base Coeff",
)

category_id = fields.Many2one(
"asset.category",
ondelete="cascade",
Expand Down Expand Up @@ -46,8 +42,9 @@ class AssetCategoryDepreciationType(models.Model):
def get_depreciation_vals(self, amount_depreciable=0):
self.ensure_one()
return {
"amount_depreciable": amount_depreciable * self.base_coeff,
"amount_depreciable": self._get_depreciable_amount(amount_depreciable),
"base_coeff": self.base_coeff,
"base_max_amount": self.base_max_amount,
"mode_id": self.mode_id.id,
"percentage": self.percentage,
"pro_rata_temporis": self.pro_rata_temporis,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AssetComputeDepreciableAmount(models.AbstractModel):
_name = "l10n_it_asset_management.compute.depreciable_amount"
_description = "Compute depreciable amount"

base_coeff = fields.Float(
default=1,
help="Coeff to compute depreciable amount from purchase amount",
string="Dep Base Coeff",
)
base_max_amount = fields.Float(
string="Maximum depreciable amount",
)

def _get_depreciable_amount(self, base_amount):
"""Compute how much of `base_amount` can be depreciated."""
self.ensure_one()
depreciable_amount = base_amount
if self.base_coeff:
depreciable_amount = base_amount * self.base_coeff
if self.base_max_amount:
depreciable_amount = min(depreciable_amount, self.base_max_amount)
return depreciable_amount
17 changes: 8 additions & 9 deletions l10n_it_asset_management/models/asset_depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class AssetDepreciation(models.Model):
_name = "asset.depreciation"
_inherit = "l10n_it_asset_management.compute.depreciable_amount"
_description = "Assets Depreciations"

amount_depreciable = fields.Monetary(string="Initial Depreciable Amount")
Expand Down Expand Up @@ -71,12 +72,6 @@ class AssetDepreciation(models.Model):
string="Asset",
)

base_coeff = fields.Float(
default=1,
help="Coeff to compute amount depreciable from purchase amount",
string="Depreciable Base Coeff",
)

company_id = fields.Many2one(
"res.company", readonly=True, related="asset_id.company_id", string="Company"
)
Expand Down Expand Up @@ -196,10 +191,14 @@ def _compute_state(self):
for dep in self:
dep.state = dep.get_depreciation_state()

@api.onchange("asset_id", "base_coeff")
def onchange_base_coeff(self):
@api.onchange(
"asset_id",
"base_coeff",
"base_max_amount",
)
def onchange_depreciable_amount_computation(self):
purchase_amount = self.asset_id.purchase_amount
self.amount_depreciable = self.base_coeff * purchase_amount
self.amount_depreciable = self._get_depreciable_amount(purchase_amount)

@api.onchange("first_dep_nr")
def onchange_normalize_first_dep_nr(self):
Expand Down
66 changes: 66 additions & 0 deletions l10n_it_asset_management/tests/test_assets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,72 @@ def test_entry_out_update_asset(self):
depreciation_info.amount_residual, asset.purchase_amount - removed_amount
)

def _create_asset_wizard(self, move, wizard_values=None):
"""Create the asset management wizard on `move`."""
if wizard_values is None:
wizard_values = dict()

wizard_action = move.open_wizard_manage_asset()
wizard_model_name = wizard_action["res_model"]
wizard_context = wizard_action["context"]
wizard_model = self.env[wizard_model_name].with_context(**wizard_context)

wizard_form = Form(wizard_model)
for field, value in wizard_values.items():
setattr(wizard_form, field, value)
return wizard_form.save()

def _run_asset_wizard(self, move, wizard_values=None):
"""Execute the asset management wizard on `move`."""
wizard = self._create_asset_wizard(move, wizard_values=wizard_values)
return wizard.link_asset()

def test_max_amount_depreciable(self):
"""
Set max amount depreciable in category line,
if the asset has a higher amount, the max amount is set as depreciable instead.
"""
# Arrange
purchase_amount = 1000
max_depreciable_amount = 120
category = self.asset_category_1
civ_type = self.env.ref("l10n_it_asset_management.ad_type_civilistico")
category_civ_depreciation_type = category.type_ids.filtered(
lambda x: x.depreciation_type_id == civ_type
)
category_civ_depreciation_type.update(
{
"base_max_amount": max_depreciable_amount,
}
)
purchase_invoice = self._create_purchase_invoice(
fields.Date.today(), amount=purchase_amount
)
# pre-condition
self.assertEqual(purchase_invoice.amount_untaxed, purchase_amount)
self.assertEqual(
category_civ_depreciation_type.base_max_amount, max_depreciable_amount
)
self.assertGreater(purchase_amount, max_depreciable_amount)

# Act
asset = self._run_asset_wizard(
purchase_invoice,
wizard_values={
"management_type": "create",
"name": "Test asset",
"category_id": category,
},
)

# Assert
self.assertEqual(asset.category_id, category)
civ_depreciation = asset.depreciation_ids.filtered(
lambda x: x.type_id == civ_type
)
self.assertEqual(civ_depreciation.base_max_amount, max_depreciable_amount)
self.assertEqual(civ_depreciation.amount_depreciable, max_depreciable_amount)

def _civil_depreciate_asset(self, asset):
# Keep only one civil depreciation
civil_depreciation_type = self.env.ref(
Expand Down
1 change: 1 addition & 0 deletions l10n_it_asset_management/views/asset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<field name="date_start" />
<field name="percentage" />
<field name="base_coeff" />
<field name="base_max_amount" />
<field name="amount_depreciable" />
<field name="amount_depreciable_updated" />
<field name="amount_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 @@ -40,6 +40,7 @@
<field name="depreciation_type_id" />
<field name="pro_rata_temporis" />
<field name="base_coeff" />
<field name="base_max_amount" />
<field name="percentage" />
<field name="mode_id" />
</tree>
Expand Down
14 changes: 13 additions & 1 deletion l10n_it_asset_management/views/asset_depreciation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@
/>
<field
name="base_coeff"
attrs="{'readonly': [('state', '!=', 'non_depreciated')]}"
attrs="{
'readonly': [
('state', '!=', 'non_depreciated'),
],
}"
/>
<field
name="base_max_amount"
attrs="{
'readonly': [
('state', '!=', 'non_depreciated'),
],
}"
/>
</group>
<group>
Expand Down

0 comments on commit fc237d2

Please sign in to comment.