Skip to content

Commit 45f6bc3

Browse files
committed
Merge PR #4249 into 16.0
Signed-off-by SirAionTech
2 parents c780b71 + 4d37b8e commit 45f6bc3

File tree

2 files changed

+92
-28
lines changed

2 files changed

+92
-28
lines changed

l10n_it_asset_management/tests/test_assets_management.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from odoo import fields
88
from odoo.exceptions import ValidationError
99
from odoo.fields import Command, first
10+
from odoo.tests import Form
1011
from odoo.tests.common import TransactionCase
1112
from odoo.tools.date_utils import relativedelta
1213

@@ -153,8 +154,16 @@ def setUpClass(cls):
153154
],
154155
}
155156
)
157+
cls.bank_account = cls.env["account.account"].create(
158+
{
159+
"code": "TBA",
160+
"name": "Test Bank Account",
161+
"account_type": "asset_cash",
162+
}
163+
)
164+
cls.env.user.groups_id += cls.env.ref("account.group_account_readonly")
156165

157-
def _create_asset(self, asset_date):
166+
def _create_asset(self, asset_date=None):
158167
asset = self.env["asset.asset"].create(
159168
{
160169
"name": "Test asset",
@@ -210,6 +219,35 @@ def _create_purchase_invoice(self, invoice_date, tax_ids=False, amount=7000):
210219
self.assertEqual(purchase_invoice.state, "posted")
211220
return purchase_invoice
212221

222+
def _create_entry(self, account, amount, post=True):
223+
"""Create an entry that adds `amount` to `account`."""
224+
entry_form = Form(self.env["account.move"])
225+
with entry_form.line_ids.new() as asset_line:
226+
asset_line.account_id = account
227+
asset_line.debit = amount
228+
with entry_form.line_ids.new() as bank_line:
229+
bank_line.account_id = self.bank_account
230+
entry = entry_form.save()
231+
232+
if post:
233+
entry.action_post()
234+
235+
self.assertEqual(entry.move_type, "entry")
236+
return entry
237+
238+
def _update_asset(self, entry, asset):
239+
"""Execute the wizard on `entry` to update `asset`."""
240+
wizard_action = entry.open_wizard_manage_asset()
241+
wizard_model = self.env[wizard_action["res_model"]]
242+
wizard_context = wizard_action["context"]
243+
244+
wizard_form = Form(wizard_model.with_context(**wizard_context))
245+
wizard_form.management_type = "update"
246+
wizard_form.asset_id = asset
247+
wizard = wizard_form.save()
248+
249+
return wizard.link_asset()
250+
213251
def test_00_create_asset_depreciate_and_sale(self):
214252
today = fields.Date.today()
215253
first_depreciation_date = today.replace(month=12, day=31) + relativedelta(
@@ -642,6 +680,46 @@ def test_04_asset_partial_depreciate_from_purchase_invoice_increment(self):
642680
sum(civ_dep_lines.mapped("amount")), 7000 * 0.6 + 9000 * 0.4
643681
)
644682

683+
def test_entry_in_update_asset(self):
684+
"""An entry adding to the asset account
685+
creates a positive accounting info."""
686+
asset = self._create_asset()
687+
added_amount = 100
688+
entry = self._create_entry(asset.category_id.asset_account_id, added_amount)
689+
# pre-condition
690+
self.assertFalse(asset.asset_accounting_info_ids)
691+
692+
# Act
693+
self._update_asset(entry, asset)
694+
695+
# Assert
696+
accounting_info = asset.asset_accounting_info_ids
697+
self.assertEqual(accounting_info.move_type, "in")
698+
depreciation_info = asset.depreciation_ids
699+
self.assertEqual(
700+
depreciation_info.amount_residual, asset.purchase_amount + added_amount
701+
)
702+
703+
def test_entry_out_update_asset(self):
704+
"""An entry removing from the asset account
705+
creates a negative accounting info."""
706+
asset = self._create_asset()
707+
removed_amount = 100
708+
entry = self._create_entry(asset.category_id.asset_account_id, -removed_amount)
709+
# pre-condition
710+
self.assertFalse(asset.asset_accounting_info_ids)
711+
712+
# Act
713+
self._update_asset(entry, asset)
714+
715+
# Assert
716+
accounting_info = asset.asset_accounting_info_ids
717+
self.assertEqual(accounting_info.move_type, "out")
718+
depreciation_info = asset.depreciation_ids
719+
self.assertEqual(
720+
depreciation_info.amount_residual, asset.purchase_amount - removed_amount
721+
)
722+
645723
def _civil_depreciate_asset(self, asset):
646724
# Keep only one civil depreciation
647725
civil_depreciation_type = self.env.ref(

l10n_it_asset_management/wizard/account_move_manage_asset.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -642,41 +642,26 @@ def get_update_asset_vals(self):
642642
for move, lines in grouped_move_lines.items():
643643
move_num = move.name
644644

645-
move_type = "in" if move.is_outbound() else "out"
646-
if not move_type:
647-
raise ValidationError(
648-
_(
649-
"Could not retrieve depreciation line type from"
650-
" move `%(move_num)s` (type `%(move_type)s`).",
651-
move_num=move_num,
652-
move_type=move_type,
653-
)
654-
)
655-
656645
# Compute amount and sign to preview how much the line
657646
# balance will be: if it's going to write off the
658647
# whole residual amount and more, making it become lower
659648
# than zero, raise error
660649
# todo probabilmente si può evitare questo calcolo
661650
amount = 0
662651
if lines:
663-
amount = abs(
664-
sum(
665-
line.currency_id._convert(
666-
line.debit - line.credit,
667-
dep.currency_id,
668-
line.company_id,
669-
line.date,
670-
)
671-
for line in lines
652+
amount = sum(
653+
line.currency_id._convert(
654+
line.debit - line.credit,
655+
dep.currency_id,
656+
line.company_id,
657+
line.date,
672658
)
659+
for line in lines
673660
)
674-
sign = 1
675-
if move_type == "out":
676-
sign = -1
661+
sign = 1 if float_compare(amount, 0, digits) > 0 else -1
677662
# Block updates if the amount to be written off is higher than
678663
# the residual amount
679-
if sign < 0 and float_compare(residual, amount, digits) < 0:
664+
if sign < 0 and float_compare(residual, abs(amount), digits) < 0:
680665
raise ValidationError(
681666
_(
682667
"Could not update `%(asset_name)s`:"
@@ -692,9 +677,10 @@ def get_update_asset_vals(self):
692677
residual=residual,
693678
)
694679
)
695-
balances += sign * amount
680+
balances += amount
696681
# end todo
697682

683+
dep_type = "in" if sign > 0 else "out"
698684
dep_line_vals = {
699685
"asset_accounting_info_ids": [
700686
Command.create(
@@ -705,9 +691,9 @@ def get_update_asset_vals(self):
705691
)
706692
for line in lines
707693
],
708-
"amount": amount,
694+
"amount": abs(amount),
709695
"date": move.date,
710-
"move_type": move_type,
696+
"move_type": dep_type,
711697
"name": _("From move(s) ") + move_num,
712698
}
713699
dep_vals["line_ids"].append(Command.create(dep_line_vals))

0 commit comments

Comments
 (0)