diff --git a/l10n_it_asset_management/tests/test_asset_depreciation.py b/l10n_it_asset_management/tests/test_asset_depreciation.py index b3b355fc95db..c52bc22ae68e 100644 --- a/l10n_it_asset_management/tests/test_asset_depreciation.py +++ b/l10n_it_asset_management/tests/test_asset_depreciation.py @@ -205,8 +205,8 @@ def test_depreciate_partial_sale_loss_coefficient(self): "partial_dismiss", wiz_values={ "asset_id": asset, - "depreciated_fund_amount": depreciated_fund_amount, "asset_purchase_amount": asset_purchase_amount, + "depreciated_fund_amount": depreciated_fund_amount, }, ) diff --git a/l10n_it_asset_management/tests/test_assets_management.py b/l10n_it_asset_management/tests/test_assets_management.py index a13f4c60a59f..1408f2d30759 100644 --- a/l10n_it_asset_management/tests/test_assets_management.py +++ b/l10n_it_asset_management/tests/test_assets_management.py @@ -6,7 +6,8 @@ from odoo import fields from odoo.exceptions import AccessError, ValidationError -from odoo.fields import Command +from odoo.fields import Command, first +from odoo.tests import Form from odoo.tools.date_utils import relativedelta from .common import Common @@ -816,3 +817,64 @@ def test_open_manage_asset_wiz(self): asset_wiz.with_user(forbidden_user).link_asset() asset_wiz.with_user(manager_user).link_asset() asset_wiz.with_user(account_user).link_asset() + + def test_wizard_compute_depreciated_fund(self): + """ + When partially dismissing an asset, + the fund amount in wizard is computed based on + the purchased amount and civil depreciation amounts. + """ + # Arrange + civil_depreciation_type = self.env.ref( + "l10n_it_asset_management.ad_type_civilistico" + ) + purchase_date = date(2019, 1, 1) + depreciation_date = date(2020, 1, 1) + civil_depreciable_amount = 1000 + civil_depreciated_amount = 250 + purchase_amount = 300 + + asset = self._create_asset(purchase_date) + civil_depreciation = first( + asset.depreciation_ids.filtered( + lambda d: d.type_id == civil_depreciation_type + ) + ) + civil_depreciation.mode_id.line_ids.unlink() + civil_depreciation.percentage = 25.0 + self._depreciate_asset(asset, depreciation_date) + + invoice_form = Form( + self.env["account.move"].with_context(default_move_type="out_invoice") + ) + invoice_form.partner_id = self.env.ref("base.partner_demo") + with invoice_form.invoice_line_ids.new() as line: + line.account_id = asset.category_id.asset_account_id + line.price_unit = 100 + invoice = invoice_form.save() + invoice.action_post() + + self.assertEqual( + civil_depreciation.amount_depreciable, civil_depreciable_amount + ) + self.assertEqual( + civil_depreciation.amount_depreciated, civil_depreciated_amount + ) + + # Act + wizard_action = invoice.open_wizard_manage_asset() + wizard_form = Form( + self.env[wizard_action["res_model"]].with_context( + **wizard_action["context"] + ) + ) + wizard_form.management_type = "partial_dismiss" + wizard_form.asset_id = asset + wizard_form.asset_purchase_amount = purchase_amount + wizard = wizard_form.save() + + # Assert + self.assertEqual( + wizard.depreciated_fund_amount, + purchase_amount * civil_depreciated_amount / civil_depreciable_amount, + ) 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 7f0635015f04..590eba628ef5 100644 --- a/l10n_it_asset_management/wizard/account_move_manage_asset.py +++ b/l10n_it_asset_management/wizard/account_move_manage_asset.py @@ -47,7 +47,11 @@ def get_default_move_ids(self): string="Currency", ) - depreciated_fund_amount = fields.Monetary() + depreciated_fund_amount = fields.Monetary( + compute="_compute_depreciated_fund_amount", + store=True, + readonly=False, + ) depreciation_type_ids = fields.Many2many( "asset.depreciation.type", string="Depreciation Types" @@ -122,6 +126,35 @@ def get_default_move_ids(self): "update": lambda w: w.update_asset(), } + @api.depends( + "asset_id", + "asset_purchase_amount", + "management_type", + ) + def _compute_depreciated_fund_amount(self): + civil_depreciation_type = self.env.ref( + "l10n_it_asset_management.ad_type_civilistico", + raise_if_not_found=False, + ) + for wizard in self: + depreciated_fund_amount = 0 + if civil_depreciation_type and wizard.management_type == "partial_dismiss": + asset = wizard.asset_id + civil_depreciation = asset.depreciation_ids.filtered( + lambda dep: dep.type_id == civil_depreciation_type + ) + civil_depreciable_amount = civil_depreciation.amount_depreciable + if civil_depreciation and civil_depreciable_amount: + # Resolve + # depreciated : depreciable = fund_amount : purchase_amount + # for fund_amount + depreciated_fund_amount = ( + wizard.asset_purchase_amount + * civil_depreciation.amount_depreciated + / civil_depreciable_amount + ) + wizard.depreciated_fund_amount = depreciated_fund_amount + @api.onchange("asset_id", "management_type") def onchange_depreciation_type_ids(self): if self.management_type == "update":