Skip to content

Commit

Permalink
[FIX] l10n_it_asset_management: Gain/Loss proportional to depreciation
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Oct 18, 2024
1 parent e6dd81a commit 337c38f
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 3 deletions.
191 changes: 191 additions & 0 deletions l10n_it_asset_management/tests/test_asset_depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,194 @@ def test_line_accounts(self):
# 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)

def test_depreciate_sale_loss_coefficient(self):
"""Depreciate and sale an asset with a sale invoice.
Loss is proportional to the depreciation coefficient."""
# Arrange
asset_dep_type = self.civilistico_asset_dep_type
asset_category = self.asset_category_1

purchase_date = date(2020, month=1, day=1)
depreciation_base_coeff = 0.2
depreciation_percentage = 20.0
depreciation_date = date(2020, month=12, day=31)
depreciated_amount = 160
sale_price = 500

category_depreciation_type = asset_category.type_ids.filtered(
lambda x, dep_type=asset_dep_type: x.depreciation_type_id == dep_type
)
category_depreciation_type.base_coeff = depreciation_base_coeff
category_depreciation_type.percentage = depreciation_percentage
category_depreciation_type.mode_id.line_ids.unlink()

asset = self._create_asset(purchase_date)
self.assertEqual(asset.category_id, asset_category)
asset_depreciation = asset.depreciation_ids.filtered(
lambda x, dep_type=asset_dep_type: x.type_id == dep_type
)
# pre-condition
self.assertEqual(asset_depreciation.base_coeff, depreciation_base_coeff)
self.assertEqual(asset_depreciation.percentage, depreciation_percentage)
self.assertEqual(asset_depreciation.amount_residual, 200)

# Act: Depreciate and dismiss with sale
self._depreciate_asset(asset, depreciation_date)
self.assertEqual(asset_depreciation.amount_residual, depreciated_amount)
depreciation_lines = asset_depreciation.line_ids
sale_invoice = self._create_sale_invoice(asset, amount=sale_price)
self._link_asset_move(
sale_invoice,
"dismiss",
wiz_values={
"asset_id": asset,
},
)

# Assert: Loss is proportional to `depreciation_base_coeff`
depreciation_lines = asset_depreciation.line_ids - depreciation_lines
self.assertRecordValues(
depreciation_lines.sorted("move_type"),
[
{
"move_type": "loss",
"amount": abs(
sale_price * depreciation_base_coeff - depreciated_amount
),
},
{
"move_type": "out",
"amount": sale_price * depreciation_base_coeff,
},
],
)

def test_depreciate_update_loss_coefficient(self):
"""Depreciate and update an asset with a purchase invoice.
'In' depreciation line is proportional to the depreciation coefficient."""
# Arrange
asset_dep_type = self.civilistico_asset_dep_type
asset_category = self.asset_category_1

purchase_date = date(2020, month=1, day=1)
depreciation_base_coeff = 0.2
depreciation_percentage = 20.0
depreciation_date = date(2020, month=12, day=31)
depreciated_amount = 160
update_date = date(2021, month=6, day=6)
update_price = 500

category_depreciation_type = asset_category.type_ids.filtered(
lambda x, dep_type=asset_dep_type: x.depreciation_type_id == dep_type
)
category_depreciation_type.base_coeff = depreciation_base_coeff
category_depreciation_type.percentage = depreciation_percentage
category_depreciation_type.mode_id.line_ids.unlink()

asset = self._create_asset(purchase_date)
self.assertEqual(asset.category_id, asset_category)
asset_depreciation = asset.depreciation_ids.filtered(
lambda x, dep_type=asset_dep_type: x.type_id == dep_type
)
# pre-condition
self.assertEqual(asset_depreciation.base_coeff, depreciation_base_coeff)
self.assertEqual(asset_depreciation.percentage, depreciation_percentage)
self.assertEqual(asset_depreciation.amount_residual, 200)

# Act: Depreciate and update with purchase
self._depreciate_asset(asset, depreciation_date)
self.assertEqual(asset_depreciation.amount_residual, depreciated_amount)
depreciation_lines = asset_depreciation.line_ids
purchase_invoice = self._create_purchase_invoice(
update_date, amount=update_price
)
self._link_asset_move(
purchase_invoice,
"update",
wiz_values={
"asset_id": asset,
},
)

# Assert: 'In' is proportional to `depreciation_base_coeff`
depreciation_lines = asset_depreciation.line_ids - depreciation_lines
self.assertRecordValues(
depreciation_lines,
[
{
"move_type": "in",
"amount": update_price * depreciation_base_coeff,
},
],
)

def test_depreciate_partial_sale_loss_coefficient(self):
"""Depreciate and partial depreciate an asset with a sale invoice.
Loss is proportional to the depreciation coefficient."""
# Arrange
asset_dep_type = self.civilistico_asset_dep_type
asset_category = self.asset_category_1

purchase_date = date(2020, month=1, day=1)
depreciation_base_coeff = 0.2
depreciation_percentage = 20.0
depreciation_date = date(2020, month=12, day=31)
depreciated_amount = 160
sale_price = 500
depreciated_fund_amount = 100
asset_purchase_amount = 200

category_depreciation_type = asset_category.type_ids.filtered(
lambda x, dep_type=asset_dep_type: x.depreciation_type_id == dep_type
)
category_depreciation_type.base_coeff = depreciation_base_coeff
category_depreciation_type.percentage = depreciation_percentage
category_depreciation_type.mode_id.line_ids.unlink()

asset = self._create_asset(purchase_date)
self.assertEqual(asset.category_id, asset_category)
asset_depreciation = asset.depreciation_ids.filtered(
lambda x, dep_type=asset_dep_type: x.type_id == dep_type
)
# pre-condition
self.assertEqual(asset_depreciation.base_coeff, depreciation_base_coeff)
self.assertEqual(asset_depreciation.percentage, depreciation_percentage)
self.assertEqual(asset_depreciation.amount_residual, 200)

# Act: Depreciate and update with purchase
self._depreciate_asset(asset, depreciation_date)
self.assertEqual(asset_depreciation.amount_residual, depreciated_amount)
depreciation_lines = asset_depreciation.line_ids
sale_invoice = self._create_sale_invoice(asset, amount=sale_price)
self._link_asset_move(
sale_invoice,
"partial_dismiss",
wiz_values={
"asset_id": asset,
"depreciated_fund_amount": depreciated_fund_amount,
"asset_purchase_amount": asset_purchase_amount,
},
)

# Assert: Create lines are proportional to `depreciation_base_coeff`
depreciation_lines = asset_depreciation.line_ids - depreciation_lines
self.assertRecordValues(
depreciation_lines.sorted("move_type"),
[
{
"move_type": "depreciated",
"amount": -1 * depreciated_fund_amount * depreciation_base_coeff,
},
{
"move_type": "gain",
"amount": sale_price * depreciation_base_coeff
- (asset_purchase_amount - depreciated_fund_amount)
* depreciation_base_coeff,
},
{
"move_type": "out",
"amount": asset_purchase_amount * depreciation_base_coeff,
},
],
)
21 changes: 18 additions & 3 deletions l10n_it_asset_management/wizard/account_move_manage_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ def get_dismiss_asset_vals(self):
residual = dep.amount_residual
dep_vals = {"line_ids": []}
dep_writeoff = writeoff
base_coeff = dep.base_coeff
if base_coeff:
dep_writeoff *= base_coeff

if self.dismiss_asset_without_sale and not self.move_line_ids:
asset_accounting_info_ids = [
Expand Down Expand Up @@ -549,6 +552,14 @@ def get_partial_dismiss_asset_vals(self):
else:
dep_writeoff = writeoff

dep_fund_amount = fund_amt
dep_purchase_amt = purchase_amt
base_coeff = dep.base_coeff
if base_coeff:
dep_fund_amount *= base_coeff
dep_purchase_amt *= base_coeff
dep_writeoff *= base_coeff

name = _(
"Partial dismissal from move(s) %(move_nums)s",
move_nums=move_nums,
Expand All @@ -564,7 +575,7 @@ def get_partial_dismiss_asset_vals(self):
)
for line in self.move_line_ids
],
"amount": purchase_amt,
"amount": dep_purchase_amt,
"date": dismiss_date,
"move_type": "out",
"name": name,
Expand All @@ -580,7 +591,7 @@ def get_partial_dismiss_asset_vals(self):
)
for line in self.move_line_ids
],
"amount": -fund_amt,
"amount": -dep_fund_amount,
"date": dismiss_date,
"move_type": "depreciated",
"name": name,
Expand All @@ -594,7 +605,7 @@ def get_partial_dismiss_asset_vals(self):
]
}

balance = (fund_amt + dep_writeoff) - purchase_amt
balance = (dep_fund_amount + dep_writeoff) - dep_purchase_amt
if not float_is_zero(balance, digits):
loss_gain_vals = {
"asset_accounting_info_ids": [
Expand Down Expand Up @@ -658,6 +669,10 @@ def get_update_asset_vals(self):
)
for line in lines
)
base_coeff = dep.base_coeff
if base_coeff:
amount *= base_coeff

sign = 1 if float_compare(amount, 0, digits) > 0 else -1
# Block updates if the amount to be written off is higher than
# the residual amount
Expand Down

0 comments on commit 337c38f

Please sign in to comment.