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 Mar 28, 2024
1 parent d3b41a5 commit 1294353
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 21 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)

Check warning on line 201 in l10n_it_asset_management/models/asset_depreciation.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_management/models/asset_depreciation.py#L200-L201

Added lines #L200 - L201 were not covered by tests

@api.onchange("first_dep_nr")
def onchange_normalize_first_dep_nr(self):
Expand Down
68 changes: 67 additions & 1 deletion l10n_it_asset_management/tests/test_assets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from odoo import fields
from odoo.exceptions import ValidationError
from odoo.fields import Command, first
from odoo.tests.common import TransactionCase
from odoo.tests.common import Form, TransactionCase
from odoo.tools.date_utils import relativedelta


Expand Down Expand Up @@ -642,6 +642,72 @@ def test_04_asset_partial_depreciate_from_purchase_invoice_increment(self):
sum(civ_dep_lines.mapped("amount")), 7000 * 0.6 + 9000 * 0.4
)

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

Check warning on line 648 in l10n_it_asset_management/tests/test_assets_management.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_management/tests/test_assets_management.py#L648

Added line #L648 was not covered by tests

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_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 1294353

Please sign in to comment.