Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Registrazione contabile per cespite crea sempre rettifica negativa #4018

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion assets_management/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ITA - Gestione Cespiti
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:93205b17d62b5a84274c015b11036ed38fb43141d50ec244673f1f0c03f9da43
!! source digest: sha256:11430ad54191b2a10124159348728f931698a5f729cf69ee5fbb7f56ff2aa595
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -84,6 +84,9 @@ Contributors
* `TAKOBI <https://takobi.online>`_:

* Simone Rubino <[email protected]>
* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>

Base icon made by `surang <https://www.flaticon.com/authors/surang>`_ from `www.flaticon.com <https://www.flaticon.com/>`_.

Expand Down
1 change: 1 addition & 0 deletions assets_management/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 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).

{
Expand Down
3 changes: 3 additions & 0 deletions assets_management/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
* `TAKOBI <https://takobi.online>`_:

* Simone Rubino <[email protected]>
* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>

Base icon made by `surang <https://www.flaticon.com/authors/surang>`_ from `www.flaticon.com <https://www.flaticon.com/>`_.
6 changes: 5 additions & 1 deletion assets_management/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ <h1 class="title">ITA - Gestione Cespiti</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:93205b17d62b5a84274c015b11036ed38fb43141d50ec244673f1f0c03f9da43
!! source digest: sha256:11430ad54191b2a10124159348728f931698a5f729cf69ee5fbb7f56ff2aa595
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/l10n-italy/tree/14.0/assets_management"><img alt="OCA/l10n-italy" src="https://img.shields.io/badge/github-OCA%2Fl10n--italy-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/l10n-italy-14-0/l10n-italy-14-0-assets_management"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/l10n-italy&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This modules allows account management of companies’ assets.</p>
Expand Down Expand Up @@ -422,6 +422,10 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<li>Simone Rubino &lt;<a class="reference external" href="mailto:sir&#64;takobi.online">sir&#64;takobi.online</a>&gt;</li>
</ul>
</li>
<li><a class="reference external" href="https://aiontech.company/">Aion Tech</a>:<ul>
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;aion-tech.it">simone.rubino&#64;aion-tech.it</a>&gt;</li>
</ul>
</li>
</ul>
<p>Base icon made by <a class="reference external" href="https://www.flaticon.com/authors/surang">surang</a> from <a class="reference external" href="https://www.flaticon.com/">www.flaticon.com</a>.</p>
</div>
Expand Down
41 changes: 39 additions & 2 deletions assets_management/tests/test_assets_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright 2021 Sergio Corato <https://github.com/sergiocorato>
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests import new_test_user
from odoo.tests.common import SavepointCase
from odoo.tests.common import Form, SavepointCase


class TestAssets(SavepointCase):
Expand Down Expand Up @@ -119,6 +120,13 @@ def setUpClass(cls):
cls.loss_account_company_2 = cls.loss_account_company_1.copy(
{"company_id": cls.company_2.id}
)
cls.bank_account = cls.env["account.account"].create(
{
"code": "TBA",
"name": "Test Bank Account",
"user_type_id": cls.env.ref("account.data_account_type_liquidity").id,
}
)
# Journals
cls.journal_company_1 = cls.env["account.journal"].search(
[("type", "=", "general"), ("company_id", "=", cls.company_1.id)],
Expand Down Expand Up @@ -251,7 +259,7 @@ def setUpClass(cls):
}
)

def _create_asset(self, asset_date):
def _create_asset(self, asset_date=None):
asset = self.env["asset.asset"].create(
{
"name": "Test asset",
Expand Down Expand Up @@ -308,3 +316,32 @@ def _create_purchase_invoice(self, invoice_date, tax_ids=False, amount=7000):
purchase_invoice.action_post()
self.assertEqual(purchase_invoice.state, "posted")
return purchase_invoice

def _create_entry(self, account, amount, post=True):
"""Create an entry that adds `amount` to `account`."""
entry_form = Form(self.env["account.move"])
with entry_form.line_ids.new() as asset_line:
asset_line.account_id = account
asset_line.debit = amount
with entry_form.line_ids.new() as bank_line:
bank_line.account_id = self.bank_account
entry = entry_form.save()

if post:
entry.action_post()

self.assertEqual(entry.move_type, "entry")
return entry

def _update_asset(self, entry, asset):
"""Execute the wizard on `entry` to update `asset`."""
wizard_action = entry.open_wizard_manage_asset()
wizard_model = self.env[wizard_action["res_model"]]
wizard_context = wizard_action["context"]

wizard_form = Form(wizard_model.with_context(**wizard_context))
wizard_form.management_type = "update"
wizard_form.asset_id = asset
wizard = wizard_form.save()

return wizard.link_asset()
41 changes: 41 additions & 0 deletions assets_management/tests/test_assets_management.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2021 Sergio Corato <https://github.com/sergiocorato>
# Copyright 2022 Simone Rubino - TAKOBI
# Copyright 2023 Nextev Srl <[email protected]>
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import date

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

def test_entry_in_update_asset(self):
"""An entry adding to the asset account
creates a positive accounting info."""
asset = self._create_asset()
added_amount = 100
entry = self._create_entry(asset.category_id.asset_account_id, added_amount)
# pre-condition
self.assertFalse(asset.asset_accounting_info_ids)

# Act
self._update_asset(entry, asset)

# Assert
accounting_info = asset.asset_accounting_info_ids
self.assertEqual(accounting_info.move_type, "in")
depreciation_info = asset.depreciation_ids
self.assertEqual(
depreciation_info.amount_residual, asset.purchase_amount + added_amount
)

def test_entry_out_update_asset(self):
"""An entry removing from the asset account
creates a negative accounting info."""
asset = self._create_asset()
removed_amount = 100
entry = self._create_entry(asset.category_id.asset_account_id, -removed_amount)
# pre-condition
self.assertFalse(asset.asset_accounting_info_ids)

# Act
self._update_asset(entry, asset)

# Assert
accounting_info = asset.asset_accounting_info_ids
self.assertEqual(accounting_info.move_type, "out")
depreciation_info = asset.depreciation_ids
self.assertEqual(
depreciation_info.amount_residual, asset.purchase_amount - removed_amount
)

def _civil_depreciate_asset(self, asset):
# Keep only one civil depreciation
civil_depreciation_type = self.env.ref("assets_management.ad_type_civilistico")
Expand Down
39 changes: 14 additions & 25 deletions assets_management/wizard/account_move_manage_asset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 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 _, api, fields, models
Expand Down Expand Up @@ -649,39 +650,26 @@ def get_update_asset_vals(self):
for move, lines in grouped_move_lines.items():
move_num = move.name

move_type = "in" if move.is_outbound() else "out"
if not move_type:
raise ValidationError(
_(
"Could not retrieve depreciation line type from"
" move `{}` (type `{}`)."
).format(move_num, move_type)
)

# Compute amount and sign to preview how much the line
# balance will be: if it's going to write off the
# whole residual amount and more, making it become lower
# than zero, raise error
# todo probabilmente si può evitare questo calcolo
amount = 0
if lines:
amount = abs(
sum(
line.currency_id._convert(
line.debit - line.credit,
dep.currency_id,
line.company_id,
line.date,
)
for line in lines
amount = sum(
line.currency_id._convert(
line.debit - line.credit,
dep.currency_id,
line.company_id,
line.date,
)
for line in lines
)
sign = 1
if move_type == "out":
sign = -1
sign = 1 if float_compare(amount, 0, digits) > 0 else -1
# Block updates if the amount to be written off is higher than
# the residual amount
if sign < 0 and float_compare(residual, amount, digits) < 0:
if sign < 0 and float_compare(residual, abs(amount), digits) < 0:
raise ValidationError(
_(
"Could not update `{}`: not enough residual amount"
Expand All @@ -691,9 +679,10 @@ def get_update_asset_vals(self):
" instead?"
).format(asset_name, move_num, -amount, residual)
)
balances += sign * amount
balances += amount
# end todo

dep_type = "in" if sign > 0 else "out"
dep_line_vals = {
"asset_accounting_info_ids": [
(
Expand All @@ -706,9 +695,9 @@ def get_update_asset_vals(self):
)
for line in lines
],
"amount": amount,
"amount": abs(amount),
"date": move.date,
"move_type": move_type,
"move_type": dep_type,
"name": _("From move(s) ") + move_num,
}
dep_vals["line_ids"].append((0, 0, dep_line_vals))
Expand Down
Loading