From c6c9e97d78667a2a0355f85a6f77097301211702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Tue, 9 Nov 2021 09:49:42 +0100 Subject: [PATCH] [FIX] account_asset_management: Avoid the error when clicking on "Delete" button if there is a residual value and all the depreciation lines are posted. TT32861 --- .../models/account_asset.py | 20 ++++++++++- .../tests/test_account_asset_management.py | 33 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/account_asset_management/models/account_asset.py b/account_asset_management/models/account_asset.py index d6b92de7..519c6571 100644 --- a/account_asset_management/models/account_asset.py +++ b/account_asset_management/models/account_asset.py @@ -680,9 +680,16 @@ class AccountAsset(models.Model): # recompute in case of deviation depreciated_value_posted = depreciated_value = 0.0 if posted_lines: + total_table_lines = sum([len(entry["lines"]) for entry in table]) + move_check_lines = asset.depreciation_line_ids.filtered("move_check") last_depreciation_date = last_line.line_date last_date_in_table = table[-1]["lines"][-1]["date"] - if last_date_in_table <= last_depreciation_date: + # If the number of lines in the table is the same as the depreciation + # lines, we will not show an error even if the dates are the same. + if (last_date_in_table < last_depreciation_date) or ( + last_date_in_table == last_depreciation_date + and total_table_lines != len(move_check_lines) + ): raise UserError( _( "The duration of the asset conflicts with the " @@ -723,6 +730,17 @@ class AccountAsset(models.Model): residual_amount = asset.depreciation_base - depreciated_value amount_diff = round(residual_amount_table - residual_amount, digits) if amount_diff: + # We will auto-create a new line because the number of lines in + # the tables are the same as the posted depreciations and there + # is still a residual value. Only in this case we will need to + # add a new line to the table with the amount of the difference. + if len(move_check_lines) == total_table_lines: + table[table_i_start]["lines"].append( + table[table_i_start]["lines"][line_i_start - 1] + ) + line = table[table_i_start]["lines"][line_i_start] + line["days"] = 0 + line["amount"] = amount_diff # compensate in first depreciation entry # after last posting line = table[table_i_start]["lines"][line_i_start] diff --git a/account_asset_management/tests/test_account_asset_management.py b/account_asset_management/tests/test_account_asset_management.py index c754e3cf..639f4067 100644 --- a/account_asset_management/tests/test_account_asset_management.py +++ b/account_asset_management/tests/test_account_asset_management.py @@ -760,3 +760,36 @@ class TestAssetManagement(AccountTestInvoicingCommon): # In the last month of the fiscal year we compensate for the small # deviations if that is necessary. self.assertAlmostEqual(asset.depreciation_line_ids[12].amount, 166.63, places=2) + + def test_20_asset_removal_with_value_residual(self): + """Asset removal with value residual""" + asset = self.asset_model.create( + { + "name": "test asset removal", + "profile_id": self.car5y.id, + "purchase_value": 1000, + "salvage_value": 0, + "date_start": "2019-01-01", + "method_time": "number", + "method_number": 10, + "method_period": "month", + "prorata": False, + } + ) + asset.compute_depreciation_board() + asset.validate() + lines = asset.depreciation_line_ids.filtered(lambda x: not x.init_entry) + self.assertEqual(len(lines), 10) + last_line = lines[-1] + last_line["amount"] = last_line["amount"] - 0.10 + for asset_line in lines: + asset_line.create_move() + self.assertEqual(asset.value_residual, 0.10) + asset.compute_depreciation_board() + lines = asset.depreciation_line_ids.filtered(lambda x: not x.init_entry) + self.assertEqual(len(lines), 11) + last_line = lines[-1] + self.assertEqual(last_line.amount, 0.10) + last_line.create_move() + self.assertEqual(asset.value_residual, 0) + self.assertEqual(asset.state, "close")