From 9ff14512da99b9930d9452a097a15b3038c71cf4 Mon Sep 17 00:00:00 2001 From: Maxence Groine Date: Fri, 24 May 2019 08:48:58 +0200 Subject: [PATCH] Included PR #828 Fix computation of depreciation lines when having multiple assets in invoice --- .../models/account_asset_line.py | 33 +++++++++++-------- .../readme/CONTRIBUTORS.rst | 3 +- .../tests/test_account_asset_management.py | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/account_asset_management/models/account_asset_line.py b/account_asset_management/models/account_asset_line.py index b85ef0c7..defea02b 100644 --- a/account_asset_management/models/account_asset_line.py +++ b/account_asset_management/models/account_asset_line.py @@ -72,19 +72,26 @@ class AccountAssetLine(models.Model): dlines = dlines.filtered(lambda l: l.type == 'depreciate') dlines = dlines.sorted(key=lambda l: l.line_date) - for i, dl in enumerate(dlines): - if i == 0: - depreciation_base = dl.depreciation_base - depreciated_value = dl.previous_id \ - and (depreciation_base - dl.previous_id.remaining_value) \ - or 0.0 - remaining_value = \ - depreciation_base - depreciated_value - dl.amount - else: - depreciated_value += dl.previous_id.amount - remaining_value -= dl.amount - dl.depreciated_value = depreciated_value - dl.remaining_value = remaining_value + # Group depreciation lines per asset + asset_ids = dlines.mapped('asset_id') + grouped_dlines = [] + for asset in asset_ids: + grouped_dlines.append( + dlines.filtered(lambda l: l.asset_id.id == asset.id)) + + for dlines in grouped_dlines: + for i, dl in enumerate(dlines): + if i == 0: + depreciation_base = dl.depreciation_base + tmp = depreciation_base - dl.previous_id.remaining_value + depreciated_value = dl.previous_id and tmp or 0.0 + remaining_value = \ + depreciation_base - depreciated_value - dl.amount + else: + depreciated_value += dl.previous_id.amount + remaining_value -= dl.amount + dl.depreciated_value = depreciated_value + dl.remaining_value = remaining_value @api.depends('move_id') @api.multi diff --git a/account_asset_management/readme/CONTRIBUTORS.rst b/account_asset_management/readme/CONTRIBUTORS.rst index b2cacd6d..bf16c0bb 100644 --- a/account_asset_management/readme/CONTRIBUTORS.rst +++ b/account_asset_management/readme/CONTRIBUTORS.rst @@ -5,4 +5,5 @@ - Stéphane Bidoul (Acsone) - Adrien Peiffer (Acsone) - Akim Juillerat -- Henrik Norlin (Apps2GROW) \ No newline at end of file +- Henrik Norlin (Apps2GROW) +- Maxence Groine diff --git a/account_asset_management/tests/test_account_asset_management.py b/account_asset_management/tests/test_account_asset_management.py index bb8c549b..2cc5622b 100644 --- a/account_asset_management/tests/test_account_asset_management.py +++ b/account_asset_management/tests/test_account_asset_management.py @@ -101,7 +101,7 @@ class TestAssetManagement(SavepointCase): 'account_id': cls.account_payable.id, 'price_unit': 20000.00, 'quantity': 1, - 'product_id': cls.product_id}) + 'product_id': cls.product.id}) cls.invoice_2 = cls.account_invoice.create({ 'partner_id': cls.partner.id,