Skip to content

Commit

Permalink
[IMP] l10n_it_asset_management: Specific asset depreciation account
Browse files Browse the repository at this point in the history
Allow to use a different account in the asset depreciation than the asset category account
  • Loading branch information
SirAionTech committed Oct 18, 2024
1 parent c2677e5 commit e6dd81a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
45 changes: 45 additions & 0 deletions l10n_it_asset_management/models/asset_depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,51 @@ class AssetDepreciation(models.Model):

zero_depreciation_until = fields.Date(string="Zero Depreciation Up To")

depreciation_account_id = fields.Many2one(
comodel_name="account.account",
compute="_compute_depreciation_account_id",
readonly=False,
store=True,
string="Depreciation Account",
)
gain_account_id = fields.Many2one(
comodel_name="account.account",
compute="_compute_gain_account_id",
readonly=False,
store=True,
string="Capital Gain Account",
)
loss_account_id = fields.Many2one(
comodel_name="account.account",
compute="_compute_loss_account_id",
readonly=False,
store=True,
string="Capital Loss Account",
)

@api.depends(
"asset_id.category_id",
)
def _compute_depreciation_account_id(self):
for dep in self:
dep.depreciation_account_id = (
dep.asset_id.category_id.depreciation_account_id
)

@api.depends(
"asset_id.category_id",
)
def _compute_gain_account_id(self):
for dep in self:
dep.gain_account_id = dep.asset_id.category_id.gain_account_id

@api.depends(
"asset_id.category_id",
)
def _compute_loss_account_id(self):
for dep in self:
dep.loss_account_id = dep.asset_id.category_id.loss_account_id

@api.model_create_multi
def create(self, vals_list):
depreciations = self.browse()
Expand Down
6 changes: 3 additions & 3 deletions l10n_it_asset_management/models/asset_depreciation_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def get_depreciated_account_move_line_vals(self):
# Asset depreciation
if not self.partial_dismissal:
credit_account_id = self.asset_id.category_id.fund_account_id.id
debit_account_id = self.asset_id.category_id.depreciation_account_id.id
debit_account_id = self.depreciation_id.depreciation_account_id.id

# Asset partial dismissal
else:
Expand All @@ -427,7 +427,7 @@ def get_depreciated_account_move_line_vals(self):
def get_gain_account_move_line_vals(self):
self.ensure_one()
credit_line_vals = {
"account_id": self.asset_id.category_id.gain_account_id.id,
"account_id": self.depreciation_id.gain_account_id.id,
"credit": self.amount,
"debit": 0.0,
"currency_id": self.currency_id.id,
Expand Down Expand Up @@ -462,7 +462,7 @@ def get_loss_account_move_line_vals(self):
"name": " - ".join((self.asset_id.make_name(), self.name)),
}
debit_line_vals = {
"account_id": self.asset_id.category_id.loss_account_id.id,
"account_id": self.depreciation_id.loss_account_id.id,
"credit": 0.0,
"debit": self.amount,
"currency_id": self.currency_id.id,
Expand Down
1 change: 1 addition & 0 deletions l10n_it_asset_management/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import test_assets_management
from . import test_asset_depreciation
42 changes: 42 additions & 0 deletions l10n_it_asset_management/tests/test_asset_depreciation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import date

from .common import Common


class TestAssetDepreciation(Common):
def test_line_accounts(self):
"""Accounts can be overridden in asset depreciation.
Overridden accounts are used in generated moves.
"""
new_depreciation_account = self.expense_account.copy()

purchase_date = date(2020, month=1, day=1)
asset = self._create_asset(purchase_date)

depreciation_date = date(2020, month=12, day=31)
asset_depreciation = asset.depreciation_ids.filtered(
lambda x, dev_type=self.civilistico_asset_dep_type: x.type_id == dev_type
)
asset_depreciation.percentage = 20.0
# pre-condition: depreciation accounts default to category accounts
self.assertEqual(
asset_depreciation.depreciation_account_id,
asset.category_id.depreciation_account_id,
)
self.assertEqual(
asset_depreciation.gain_account_id, asset.category_id.gain_account_id
)
self.assertEqual(
asset_depreciation.loss_account_id, asset.category_id.loss_account_id
)

# Act: change depreciation account and generate move
asset_depreciation.depreciation_account_id = new_depreciation_account
self._depreciate_asset(asset, depreciation_date)

# Assert: new account is used in generated move
depreciation_move = asset_depreciation.line_ids.move_id
self.assertIn(new_depreciation_account, depreciation_move.line_ids.account_id)
12 changes: 12 additions & 0 deletions l10n_it_asset_management/views/asset_depreciation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@
name="base_coeff"
attrs="{'readonly': [('state', '!=', 'non_depreciated')]}"
/>
<field
name="depreciation_account_id"
attrs="{'readonly': [('state', '!=', 'non_depreciated')]}"
/>
<field
name="gain_account_id"
attrs="{'readonly': [('state', '!=', 'non_depreciated')]}"
/>
<field
name="loss_account_id"
attrs="{'readonly': [('state', '!=', 'non_depreciated')]}"
/>
</group>
<group>
<field
Expand Down

0 comments on commit e6dd81a

Please sign in to comment.