diff --git a/l10n_it_asset_management/readme/CONTRIBUTORS.rst b/l10n_it_asset_management/readme/CONTRIBUTORS.rst index 7a4b8d745da9..8194c5b3dce2 100644 --- a/l10n_it_asset_management/readme/CONTRIBUTORS.rst +++ b/l10n_it_asset_management/readme/CONTRIBUTORS.rst @@ -2,6 +2,9 @@ * Silvio Gregorini * Stefano Pezzini * Lorenzo Battistini +* `TAKOBI `_: + + * Simone Rubino * `Aion Tech `_: * Simone Rubino diff --git a/l10n_it_asset_management/report/asset_journal.py b/l10n_it_asset_management/report/asset_journal.py index 02953df8e47a..475fc9947ce9 100644 --- a/l10n_it_asset_management/report/asset_journal.py +++ b/l10n_it_asset_management/report/asset_journal.py @@ -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). @@ -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", diff --git a/l10n_it_asset_management/report/asset_previsional.py b/l10n_it_asset_management/report/asset_previsional.py index 35f8515a0177..18f0336e096b 100644 --- a/l10n_it_asset_management/report/asset_previsional.py +++ b/l10n_it_asset_management/report/asset_previsional.py @@ -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). @@ -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", diff --git a/l10n_it_asset_management/tests/test_assets_management.py b/l10n_it_asset_management/tests/test_assets_management.py index 1ac68d123d35..ec3b70531cdf 100644 --- a/l10n_it_asset_management/tests/test_assets_management.py +++ b/l10n_it_asset_management/tests/test_assets_management.py @@ -1,9 +1,12 @@ # Copyright 2021 Sergio Corato +# 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 @@ -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)