Skip to content

Commit

Permalink
[FIX] l10n_it_asset_management: Consider depreciation of all previous…
Browse files Browse the repository at this point in the history
… years

Co-authored-by: Simone Rubino <simone.rubino@aion-tech.it>
  • Loading branch information
SirTakobi and SirAionTech committed Dec 19, 2023
1 parent 7683a7c commit cf9c322
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
3 changes: 3 additions & 0 deletions l10n_it_asset_management/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Silvio Gregorini <silviogregorini@openforce.it>
* Stefano Pezzini <stefanopezzini@openforce.it>
* Lorenzo Battistini <lb@takobi.online>
* `TAKOBI <https://takobi.online>`_:

* Simone Rubino <sir@takobi.online>
* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <simone.rubino@aion-tech.it>
Expand Down
8 changes: 8 additions & 0 deletions l10n_it_asset_management/report/asset_journal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Author(s): Silvio Gregorini (silviogregorini@openforce.it)
# Copyright 2019 Openforce Srls Unipersonale (www.openforce.it)
# Copyright 2022 Simone Rubino - TAKOBI
# Copyright 2023 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

Expand Down Expand Up @@ -365,6 +366,13 @@ def generate_totals(self):
categ.report_id.company_id,
report_date,
)
elif fy_end < report_date:
totals_by_dep_type[dep_type][fname] += line_curr._convert(
last_line["amount_depreciation_fund_curr_year"],
curr,
categ.report_id.company_id,
report_date,
)
elif fname in (
"amount_in_total",
"amount_out_total",
Expand Down
8 changes: 8 additions & 0 deletions l10n_it_asset_management/report/asset_previsional.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Author(s): Silvio Gregorini (silviogregorini@openforce.it)
# Copyright 2019 Openforce Srls Unipersonale (www.openforce.it)
# Copyright 2022 Simone Rubino - TAKOBI
# Copyright 2023 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

Expand Down Expand Up @@ -397,6 +398,13 @@ def generate_totals(self):
categ.report_id.company_id,
report_date,
)
elif fy_end < report_date:
totals_by_dep_type[dep_type][fname] += line_curr._convert(
last_line["amount_depreciation_fund_curr_year"],
curr,
categ.report_id.company_id,
report_date,
)
elif fname in (
"amount_in_total",
"amount_out_total",
Expand Down
104 changes: 103 additions & 1 deletion l10n_it_asset_management/tests/test_assets_management.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Copyright 2021 Sergio Corato <https://github.com/sergiocorato>
# Copyright 2022 Simone Rubino - TAKOBI
# Copyright 2023 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import date

from odoo import fields
from odoo.exceptions import ValidationError
from odoo.fields import Command
from odoo.fields import Command, first
from odoo.tests.common import TransactionCase
from odoo.tools.date_utils import relativedelta

Expand Down Expand Up @@ -636,3 +639,102 @@ def test_04_asset_partial_depreciate_from_purchase_invoice_increment(self):
self.assertAlmostEqual(
sum(civ_dep_lines.mapped("amount")), 7000 * 0.6 + 9000 * 0.4
)

def _civil_depreciate_asset(self, asset):
# Keep only one civil depreciation
civil_depreciation_type = self.env.ref("assets_management.ad_type_civilistico")
civil_depreciation = first(
asset.depreciation_ids.filtered(
lambda d: d.type_id == civil_depreciation_type
)
)
(asset.depreciation_ids - civil_depreciation).unlink()

civil_depreciation.line_ids = [
(5, 0, 0),
(
0,
0,
{
"name": "2019",
"date": date(2019, 12, 31),
"move_type": "depreciated",
"amount": 500,
},
),
(
0,
0,
{
"name": "2020",
"date": date(2020, 12, 31),
"move_type": "depreciated",
"amount": 500,
},
),
]
return True

def _generate_fiscal_years(self, start_date, end_date):
fiscal_years = range(
start_date.year,
end_date.year,
)
fiscal_years_values = list()
for fiscal_year in fiscal_years:
fiscal_year_values = {
"name": "Fiscal Year %d" % fiscal_year,
"date_from": date(fiscal_year, 1, 1),
"date_to": date(fiscal_year, 12, 31),
}
fiscal_years_values.append(fiscal_year_values)
return self.env["account.fiscal.year"].create(fiscal_years_values)

def _get_report_values(self, report_type):
if report_type == "previsional":
wizard_model = "wizard.asset.previsional.report"
report_model = "report_asset_previsional"
export_method = "export_asset_previsional_report"
elif report_type == "journal":
wizard_model = "wizard.asset.journal.report"
report_model = "report_asset_journal"
export_method = "export_asset_journal_report"
else:
raise Exception("Report can only be 'journal' or 'previsional'")
return export_method, report_model, wizard_model

def _get_report(self, report_date, report_type):
export_method, report_model, wizard_model = self._get_report_values(report_type)

wiz = self.env[wizard_model].create(
{
"date": report_date,
}
)
report_result = getattr(wiz, export_method)()
report_ids = report_result["context"]["report_action"]["context"]["active_ids"]
report = self.env[report_model].browse(report_ids)
return report

def test_journal_prev_year(self):
"""
Previous year depreciation considers depreciation of all previous years
"""
# Arrange: Create an asset bought in 2019
# and totally depreciated in 2019 and 2020
purchase_date = date(2019, 1, 1)
asset = self._create_asset(purchase_date)
asset.purchase_date = purchase_date
self.assertEqual(asset.purchase_amount, 1000)
self._civil_depreciate_asset(asset)
self.assertEqual(asset.state, "totally_depreciated")

# Act: Generate the asset journal report for 2022
report_date = date(2022, 11, 7)
self._generate_fiscal_years(purchase_date, report_date)
report = self._get_report(report_date, "journal")

# Assert: The previous year depreciation counts.the depreciation of 2020
total = report.report_total_ids
self.assertEqual(total.amount_depreciation_fund_curr_year, 1000)
self.assertEqual(total.amount_depreciation_fund_prev_year, 1000)

0 comments on commit cf9c322

Please sign in to comment.