From f45f82a8b724d5125a75ee708484d1c616a68335 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Wed, 3 Apr 2024 14:48:26 +0200 Subject: [PATCH] [IMP] l10n_it_asset_management: Simulate entries on other journal --- .../models/asset_depreciation_line.py | 6 +- .../tests/test_assets_management.py | 71 ++++++++++++++++++- .../wizard/asset_generate_depreciation.py | 24 ++++++- .../asset_generate_depreciation_view.xml | 1 + 4 files changed, 95 insertions(+), 7 deletions(-) diff --git a/l10n_it_asset_management/models/asset_depreciation_line.py b/l10n_it_asset_management/models/asset_depreciation_line.py index 40296df2b197..5c7634561d55 100644 --- a/l10n_it_asset_management/models/asset_depreciation_line.py +++ b/l10n_it_asset_management/models/asset_depreciation_line.py @@ -362,10 +362,14 @@ def generate_account_move_single(self): def get_account_move_vals(self): self.ensure_one() + journal = self.env.context.get( + "l10n_it_asset_override_journal", + self.asset_id.category_id.journal_id, + ) return { "company_id": self.company_id.id, "date": self.date, - "journal_id": self.asset_id.category_id.journal_id.id, + "journal_id": journal.id, "line_ids": [], "ref": _("Asset: ") + self.asset_id.make_name(), "move_type": "entry", diff --git a/l10n_it_asset_management/tests/test_assets_management.py b/l10n_it_asset_management/tests/test_assets_management.py index b08acd41cbaf..5296a9919a08 100644 --- a/l10n_it_asset_management/tests/test_assets_management.py +++ b/l10n_it_asset_management/tests/test_assets_management.py @@ -167,7 +167,15 @@ def _create_asset(self, asset_date): ) return asset - def _depreciate_asset_wizard(self, asset, date_dep, period="year"): + def _depreciate_asset_wizard( + self, + asset, + date_dep, + period="year", + override_journal=None, + ): + if override_journal is None: + override_journal = self.env["account.journal"].browse() wiz_vals = asset.with_context( **{"allow_reload_window": True} ).launch_wizard_generate_depreciations() @@ -178,13 +186,25 @@ def _depreciate_asset_wizard(self, asset, date_dep, period="year"): { "date_dep": date_dep, "period": period, + "journal_id": override_journal.id, } ) ) return wiz - def _depreciate_asset(self, asset, date_dep, period="year"): - wiz = self._depreciate_asset_wizard(asset, date_dep, period=period) + def _depreciate_asset( + self, + asset, + date_dep, + period="year", + override_journal=None, + ): + wiz = self._depreciate_asset_wizard( + asset, + date_dep, + period=period, + override_journal=override_journal, + ) wiz.do_generate() def _create_purchase_invoice(self, invoice_date, tax_ids=False, amount=7000): @@ -858,3 +878,48 @@ def test_missing_fiscal_year_warning(self): # Assert 2: no fiscal years are missing depreciate_wizard = self._depreciate_asset_wizard(asset, depreciation_date) self.assertFalse(depreciate_wizard.missing_fiscal_year_warning) + + def test_override_journal(self): + """ + Set an "Override Journal" in the depreciation wizard, + the journal entries are created in the selected journal. + """ + # Arrange + override_journal = self.env["account.journal"].create( + { + "name": "Test override journal", + "code": "TOJ", + "type": "general", + } + ) + purchase_date = date(2019, 1, 1) + asset = self._create_asset(purchase_date) + depreciation_date = date(2019, 1, 31) + self._generate_fiscal_years( + asset.purchase_date, + depreciation_date, + ) + civ_depreciation_type = self.env.ref( + "l10n_it_asset_management.ad_type_civilistico" + ) + civ_depreciation = asset.depreciation_ids.filtered( + lambda x: x.type_id == civ_depreciation_type + ) + civ_depreciation.percentage = 12.0 + depreciate_asset_wizard = self._depreciate_asset_wizard( + asset, + depreciation_date, + period="month", + override_journal=override_journal, + ) + # pre-condition + self.assertNotEqual( + depreciate_asset_wizard.journal_id, asset.category_id.journal_id + ) + + # Act + depreciate_asset_wizard.do_generate() + + # Assert + account_move = asset.depreciation_ids.line_ids.move_id + self.assertEqual(account_move.journal_id, depreciate_asset_wizard.journal_id) diff --git a/l10n_it_asset_management/wizard/asset_generate_depreciation.py b/l10n_it_asset_management/wizard/asset_generate_depreciation.py index d8402721e5f0..09b93d845444 100644 --- a/l10n_it_asset_management/wizard/asset_generate_depreciation.py +++ b/l10n_it_asset_management/wizard/asset_generate_depreciation.py @@ -58,6 +58,12 @@ def get_default_type_ids(self): string="Depreciation Types", ) + journal_id = fields.Many2one( + comodel_name="account.journal", + string="Override journal", + help="Create move entries in this journal " + "instead of the category's journal.", + ) period = fields.Selection( selection=[ ("year", "Year"), @@ -102,6 +108,16 @@ def _compute_missing_fiscal_year_warning(self): missing_fiscal_year_warning ) + def _get_depreciation_context(self): + # Add depreciation date in context just in case + depreciation_context = dict( + dep_date=self.date_dep, + ) + override_journal = self.journal_id + if override_journal: + depreciation_context["l10n_it_asset_override_journal"] = self.journal_id + return depreciation_context + def do_generate(self): """ Launches the generation of new depreciation lines for the retrieved @@ -109,9 +125,11 @@ def do_generate(self): Reloads the current window if necessary. """ self.ensure_one() - # Add depreciation date in context just in case - deps = self.env["asset.depreciation"] - all_deps = self.with_context(dep_date=self.date_dep).get_depreciations() + self_with_depreciation_context = self.with_context( + **self._get_depreciation_context() + ) + deps = self_with_depreciation_context.env["asset.depreciation"] + all_deps = self_with_depreciation_context.get_depreciations() for dep in all_deps: if ( not dep.last_depreciation_date diff --git a/l10n_it_asset_management/wizard/asset_generate_depreciation_view.xml b/l10n_it_asset_management/wizard/asset_generate_depreciation_view.xml index e034f51e382d..9f43da87805b 100644 --- a/l10n_it_asset_management/wizard/asset_generate_depreciation_view.xml +++ b/l10n_it_asset_management/wizard/asset_generate_depreciation_view.xml @@ -27,6 +27,7 @@ options="{'no_create': True}" widget="many2many_tags" /> +