From 8507f43b1bc96fed32bae8726cf5327ceb6dd0bc Mon Sep 17 00:00:00 2001 From: Maxence Groine Date: Fri, 24 May 2019 08:48:58 +0200 Subject: [PATCH] [FIX] account_asset_management: Fix lines computation in invoice with multiple assets --- .../models/account_asset_line.py | 33 +++++++++++-------- .../readme/CONTRIBUTORS.rst | 1 + .../tests/test_account_asset_management.py | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/account_asset_management/models/account_asset_line.py b/account_asset_management/models/account_asset_line.py index 47faa5381..4356c4692 100644 --- a/account_asset_management/models/account_asset_line.py +++ b/account_asset_management/models/account_asset_line.py @@ -74,19 +74,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 fb458b390..44b850039 100644 --- a/account_asset_management/readme/CONTRIBUTORS.rst +++ b/account_asset_management/readme/CONTRIBUTORS.rst @@ -5,3 +5,4 @@ - Stéphane Bidoul (Acsone) - Adrien Peiffer (Acsone) - Akim Juillerat +- 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 bb8c549b3..2cc5622b0 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,