Skip to content

Commit

Permalink
[IMP] l10n_it_asset_management: Simulate entries on other journal
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Apr 12, 2024
1 parent 1e36df4 commit f45f82a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
6 changes: 5 additions & 1 deletion l10n_it_asset_management/models/asset_depreciation_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 68 additions & 3 deletions l10n_it_asset_management/tests/test_assets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down Expand Up @@ -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)
24 changes: 21 additions & 3 deletions l10n_it_asset_management/wizard/asset_generate_depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -102,16 +108,28 @@ 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
assets.
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
options="{'no_create': True}"
widget="many2many_tags"
/>
<field name="journal_id" />
</group>
<group name="filters" string="Filters">
<field
Expand Down

0 comments on commit f45f82a

Please sign in to comment.