From 241aa3e5633a1d1e97e6710929466a8e0eb43fda Mon Sep 17 00:00:00 2001 From: mreficent Date: Mon, 19 Apr 2021 13:04:19 +0200 Subject: [PATCH] [FIX] account_asset_management: method_time migration was incomplete --- .../migrations/12.0.1.0.0/post-migration.py | 38 +++++++++++++++---- .../models/account_asset.py | 10 ++--- .../models/account_asset_profile.py | 6 ++- .../tests/test_account_asset_management.py | 29 ++++++++++++++ 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/account_asset_management/migrations/12.0.1.0.0/post-migration.py b/account_asset_management/migrations/12.0.1.0.0/post-migration.py index cfc028abd..b81d8bbea 100644 --- a/account_asset_management/migrations/12.0.1.0.0/post-migration.py +++ b/account_asset_management/migrations/12.0.1.0.0/post-migration.py @@ -18,20 +18,44 @@ def adjust_asset_values(env): FROm account_asset_profile aap WHERE aa.profile_id = aap.id""", ) - # Adjust method_time, method_number and method_period - number = sql.Identifier(openupgrade.get_legacy_name('method_number')) - period = sql.Identifier(openupgrade.get_legacy_name('method_period')) + # Adjust method_time, method_end, method_number and method_period + method_number = sql.Identifier(openupgrade.get_legacy_name('method_number')) + method_period = sql.Identifier(openupgrade.get_legacy_name('method_period')) + method_time = sql.Identifier(openupgrade.get_legacy_name('method_time')) for table in ['account_asset_profile', 'account_asset']: table = sql.Identifier(table) openupgrade.logged_query( env.cr, sql.SQL(""" UPDATE {table} SET method_time = 'year', + method_end = NULL, method_number = ({number} * {period}) / 12 - WHERE MOD({number} * {period}, 12) = 0 + WHERE MOD({number} * {period}, 12) = 0 AND {time} != 'end' """).format( - number=number, - period=period, + number=method_number, + period=method_period, + time=method_time, + table=table, + ), + ) + openupgrade.logged_query( + env.cr, sql.SQL(""" + UPDATE {table} + SET method_time = 'year', + method_number = 0 + WHERE {time} = 'end' + """).format( + time=method_time, + table=table, + ), + ) + openupgrade.logged_query( + env.cr, sql.SQL(""" + UPDATE {table} + SET method_end = NULL + WHERE {time} = 'number' + """).format( + time=method_time, table=table, ), ) @@ -45,7 +69,7 @@ def adjust_asset_values(env): END) WHERE {period} IN (1, 3, 12) """).format( - period=period, + period=method_period, table=table, ), ) diff --git a/account_asset_management/models/account_asset.py b/account_asset_management/models/account_asset.py index 32ea1f96d..0a13fb915 100644 --- a/account_asset_management/models/account_asset.py +++ b/account_asset_management/models/account_asset.py @@ -157,10 +157,8 @@ class AccountAsset(models.Model): "number of depreciation lines.\n" " * Number of Years: Specify the number of years " "for the depreciation.\n" - # " * Number of Depreciations: Fix the number of " - # "depreciation lines and the time between 2 depreciations.\n" - # " * Ending Date: Choose the time between 2 depreciations " - # "and the date the depreciations won't go beyond." + " * Number of Depreciations: Fix the number of " + "depreciation lines and the time between 2 depreciations.\n" ) days_calc = fields.Boolean( string='Calculate by days', @@ -258,10 +256,10 @@ class AccountAsset(models.Model): "Year.")) @api.multi - @api.constrains('date_start', 'method_end', 'method_time') + @api.constrains('date_start', 'method_end', 'method_number', 'method_time') def _check_dates(self): for asset in self: - if asset.method_time == 'end': + if asset.method_time == 'year' and not asset.method_number: if asset.method_end <= asset.date_start: raise UserError( _("The Start Date must precede the Ending Date.")) diff --git a/account_asset_management/models/account_asset_profile.py b/account_asset_management/models/account_asset_profile.py index dcc069dc9..548b8dee6 100644 --- a/account_asset_management/models/account_asset_profile.py +++ b/account_asset_management/models/account_asset_profile.py @@ -99,7 +99,10 @@ class AccountAssetProfile(models.Model): help="Choose the method to use to compute the dates and " "number of depreciation lines.\n" " * Number of Years: Specify the number of years " - "for the depreciation.\n") + "for the depreciation.\n" + " * Number of Depreciations: Fix the number of " + "depreciation lines and the time between 2 depreciations.\n" + ) days_calc = fields.Boolean( string='Calculate by days', default=False, @@ -164,6 +167,7 @@ class AccountAssetProfile(models.Model): """ return [ ('year', _('Number of Years or end date')), + ('number', _('Number of Depreciations')), ] @api.multi diff --git a/account_asset_management/tests/test_account_asset_management.py b/account_asset_management/tests/test_account_asset_management.py index f640b07be..eefe6e9dc 100644 --- a/account_asset_management/tests/test_account_asset_management.py +++ b/account_asset_management/tests/test_account_asset_management.py @@ -679,3 +679,32 @@ class TestAssetManagement(SavepointCase): [(group_tfa.id, 'Tangible Fixed A...')]) self.assertFalse( self.env['account.asset.group']._name_search('stessA dexiF')) + + def test_16_use_number_of_depreciations(self): + # When you run a depreciation with method = 'number' + profile = self.env.ref("account_asset_management.account_asset_profile_car_5Y") + profile.method_time = "number" + asset = self.asset_model.create( + { + "name": "test asset", + "profile_id": profile.id, + "purchase_value": 10000, + "salvage_value": 0, + "date_start": time.strftime("2019-01-01"), + "method_time": "year", + "method_number": 5, + "method_period": "month", + "prorata": False, + "days_calc": False, + "use_leap_years": False, + } + ) + asset.compute_depreciation_board() + asset.refresh() + for _i in range(1, 11): + self.assertAlmostEqual( + asset.depreciation_line_ids[1].amount, 166.67, places=2 + ) + # 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)