Skip to content

Commit 5f081b8

Browse files
committed
[IMP] l10n_it_asset_management: Compute depreciated fund
1 parent 4fec0ad commit 5f081b8

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

l10n_it_asset_management/tests/test_assets_management.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from odoo import fields
88
from odoo.exceptions import ValidationError
9-
from odoo.fields import Command
9+
from odoo.fields import Command, first
10+
from odoo.tests import Form
1011
from odoo.tools.date_utils import relativedelta
1112

1213
from .common import Common
@@ -731,3 +732,64 @@ def test_same_asset_report_residual_partial_depreciation(self):
731732
self.assertEqual(
732733
asset_report_depreciation_line.amount_residual, expected_residual_amount
733734
)
735+
736+
def test_wizard_compute_depreciated_fund(self):
737+
"""
738+
When partially dismissing an asset,
739+
the fund amount in wizard is computed based on
740+
the purchased amount and civil depreciation amounts.
741+
"""
742+
# Arrange
743+
civil_depreciation_type = self.env.ref(
744+
"l10n_it_asset_management.ad_type_civilistico"
745+
)
746+
purchase_date = date(2019, 1, 1)
747+
depreciation_date = date(2020, 1, 1)
748+
civil_depreciable_amount = 1000
749+
civil_depreciated_amount = 250
750+
purchase_amount = 300
751+
752+
asset = self._create_asset(purchase_date)
753+
civil_depreciation = first(
754+
asset.depreciation_ids.filtered(
755+
lambda d: d.type_id == civil_depreciation_type
756+
)
757+
)
758+
civil_depreciation.mode_id.line_ids.unlink()
759+
civil_depreciation.percentage = 25.0
760+
self._depreciate_asset(asset, depreciation_date)
761+
762+
invoice_form = Form(
763+
self.env["account.move"].with_context(default_move_type="out_invoice")
764+
)
765+
invoice_form.partner_id = self.env.ref("base.partner_demo")
766+
with invoice_form.invoice_line_ids.new() as line:
767+
line.account_id = asset.category_id.asset_account_id
768+
line.price_unit = 100
769+
invoice = invoice_form.save()
770+
invoice.action_post()
771+
772+
self.assertEqual(
773+
civil_depreciation.amount_depreciable, civil_depreciable_amount
774+
)
775+
self.assertEqual(
776+
civil_depreciation.amount_depreciated, civil_depreciated_amount
777+
)
778+
779+
# Act
780+
wizard_action = invoice.open_wizard_manage_asset()
781+
wizard_form = Form(
782+
self.env[wizard_action["res_model"]].with_context(
783+
**wizard_action["context"]
784+
)
785+
)
786+
wizard_form.management_type = "partial_dismiss"
787+
wizard_form.asset_id = asset
788+
wizard_form.asset_purchase_amount = purchase_amount
789+
wizard = wizard_form.save()
790+
791+
# Assert
792+
self.assertEqual(
793+
wizard.depreciated_fund_amount,
794+
purchase_amount * civil_depreciated_amount / civil_depreciable_amount,
795+
)

l10n_it_asset_management/wizard/account_move_manage_asset.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ def get_default_move_ids(self):
4747
string="Currency",
4848
)
4949

50-
depreciated_fund_amount = fields.Monetary()
50+
depreciated_fund_amount = fields.Monetary(
51+
compute="_compute_depreciated_fund_amount",
52+
store=True,
53+
readonly=False,
54+
)
5155

5256
depreciation_type_ids = fields.Many2many(
5357
"asset.depreciation.type", string="Depreciation Types"
@@ -122,6 +126,35 @@ def get_default_move_ids(self):
122126
"update": lambda w: w.update_asset(),
123127
}
124128

129+
@api.depends(
130+
"asset_id",
131+
"asset_purchase_amount",
132+
"management_type",
133+
)
134+
def _compute_depreciated_fund_amount(self):
135+
civil_depreciation_type = self.env.ref(
136+
"l10n_it_asset_management.ad_type_civilistico",
137+
raise_if_not_found=False,
138+
)
139+
for wizard in self:
140+
depreciated_fund_amount = 0
141+
if civil_depreciation_type and wizard.management_type == "partial_dismiss":
142+
asset = wizard.asset_id
143+
civil_depreciation = asset.depreciation_ids.filtered(
144+
lambda dep: dep.type_id == civil_depreciation_type
145+
)
146+
civil_depreciable_amount = civil_depreciation.amount_depreciable
147+
if civil_depreciation and civil_depreciable_amount:
148+
# Resolve
149+
# depreciated : depreciable = fund_amount : purchase_amount
150+
# for fund_amount
151+
depreciated_fund_amount = (
152+
wizard.asset_purchase_amount
153+
* civil_depreciation.amount_depreciated
154+
/ civil_depreciable_amount
155+
)
156+
wizard.depreciated_fund_amount = depreciated_fund_amount
157+
125158
@api.onchange("asset_id", "management_type")
126159
def onchange_depreciation_type_ids(self):
127160
if self.management_type == "update":

0 commit comments

Comments
 (0)