From 5414bb78dfc416257bcd26aedcab532080015e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 16 Dec 2014 18:57:46 +0100 Subject: [PATCH 1/4] [IMP] account_asset_management: more tests (test_4 failing) test_4 is failing as it is a scenario of having an init_entry depreciation line in an active fiscal year. Next commits intend to fix this. --- .../tests/test_account_asset_management.py | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/account_asset_management/tests/test_account_asset_management.py b/account_asset_management/tests/test_account_asset_management.py index 264061055..aca637862 100644 --- a/account_asset_management/tests/test_account_asset_management.py +++ b/account_asset_management/tests/test_account_asset_management.py @@ -23,12 +23,15 @@ import openerp.tests.common as common +import time + class TestAssetManagement(common.TransactionCase): def setUp(self): super(TestAssetManagement, self).setUp() self.asset_model = self.registry('account.asset.asset') + self.dl_model = self.registry('account.asset.depreciation.line') def test_1(self): """ Compute depreciation boards and post assets for first year, @@ -116,3 +119,90 @@ class TestAssetManagement(common.TransactionCase): self.assertEquals(vehicle.value_residual, 9000) self.assertEquals(fa.value_depreciated, 2500) self.assertEquals(fa.value_residual, 9000) + + def test_2(self): + """ prorata temporis depreciation """ + asset_id = self.asset_model.create(self.cr, self.uid, { + 'name': 'test asset', + 'category_id': self.ref('account_asset_management.' + 'account_asset_category_car_5Y'), + 'purchase_value': 3333, + 'salvage_value': 0, + 'date_start': time.strftime('%Y-07-07'), + 'method_number': 5, + 'method_period': 'month', + 'prorata': True, + }) + asset = self.asset_model.browse(self.cr, self.uid, asset_id) + self.asset_model.compute_depreciation_board( + self.cr, self.uid, [asset.id]) + asset.refresh() + self.assertEquals(asset.depreciation_line_ids[1].amount, 47.33) + self.assertEquals(asset.depreciation_line_ids[2].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[3].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[4].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[5].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[6].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[-1].amount, 8.22) + + def test_3(self): + """ prorata temporis depreciation with initial value in + previous year """ + asset_id = self.asset_model.create(self.cr, self.uid, { + 'name': 'test asset', + 'category_id': self.ref('account_asset_management.' + 'account_asset_category_car_5Y'), + 'purchase_value': 3333, + 'salvage_value': 0, + 'date_start': time.strftime('2013-07-07'), + 'method_number': 5, + 'method_period': 'month', + 'prorata': True, + }) + self.dl_model.create(self.cr, self.uid, { + 'asset_id': asset_id, + 'amount': 325.08, + 'line_date': time.strftime('2013-12-31'), + 'type': 'depreciate', + 'init_entry': True, + }) + asset = self.asset_model.browse(self.cr, self.uid, asset_id) + self.assertEquals(len(asset.depreciation_line_ids), 2) + self.asset_model.compute_depreciation_board( + self.cr, self.uid, [asset.id]) + asset.refresh() + self.assertEquals(asset.value_depreciated, 325.08) + self.assertEquals(asset.depreciation_line_ids[2].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[3].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[-1].amount, 8.22) + + def test_4(self): + """ prorata temporis depreciation with initial value in + curent year """ + asset_id = self.asset_model.create(self.cr, self.uid, { + 'name': 'test asset', + 'category_id': self.ref('account_asset_management.' + 'account_asset_category_car_5Y'), + 'purchase_value': 3333, + 'salvage_value': 0, + 'date_start': time.strftime('%Y-07-07'), + 'method_number': 5, + 'method_period': 'month', + 'prorata': True, + }) + self.dl_model.create(self.cr, self.uid, { + 'asset_id': asset_id, + 'amount': 279.44, + 'line_date': time.strftime('%Y-11-30'), + 'type': 'depreciate', + 'init_entry': True, + }) + asset = self.asset_model.browse(self.cr, self.uid, asset_id) + self.assertEquals(len(asset.depreciation_line_ids), 2) + self.asset_model.compute_depreciation_board( + self.cr, self.uid, [asset.id]) + asset.refresh() + self.assertEquals(asset.value_depreciated, 279.44) + self.assertEquals(asset.depreciation_line_ids[2].amount, 45.64) + self.assertEquals(asset.depreciation_line_ids[3].amount, 55.55) + self.assertEquals(asset.depreciation_line_ids[-1].amount, 8.22) From 176ee25f1ed8084ccfb9bd26ca4cffd4c39110f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 16 Dec 2014 19:01:07 +0100 Subject: [PATCH 2/4] [IMP] account_asset_management: support moves with more than one asset depreciaion in the same move Such moves are not generated by the module itself but may exist when importing existing moves from another system. This patch should otherwise not change the behaviour of the module. --- account_asset_management/account_asset.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/account_asset_management/account_asset.py b/account_asset_management/account_asset.py index f2f585689..53a4a7f18 100644 --- a/account_asset_management/account_asset.py +++ b/account_asset_management/account_asset.py @@ -715,7 +715,7 @@ class account_asset_asset(orm.Model): CASE WHEN (aadl.type = 'depreciate' AND aadl.init_entry = TRUE) THEN aadl.amount - WHEN aml.account_id=%s + WHEN aml.account_id=%s AND aml.asset_id=%s THEN (COALESCE(aml.debit,0.0) - COALESCE(aml.credit,0.0)) ELSE 0.0 END AS amount @@ -725,7 +725,8 @@ class account_asset_asset(orm.Model): ON aml.move_id=am.id WHERE aadl.id IN %s) sq """, - (exp_acc_id, tuple(posted_depreciation_line_ids))) + (exp_acc_id, asset.id, + tuple(posted_depreciation_line_ids))) res = cr.fetchone() depreciated_value = res[0] residual_amount = asset.asset_value - depreciated_value @@ -741,10 +742,13 @@ class account_asset_asset(orm.Model): INNER JOIN account_move am ON aadl.move_id=am.id INNER JOIN account_move_line aml ON aml.move_id=am.id INNER JOIN account_period ap ON am.period_id=ap.id - WHERE aadl.id in %s AND aml.account_id=%s AND ap.fiscalyear_id=%s + WHERE aadl.id in %s + AND aml.account_id=%s AND aml.asset_id=%s + AND ap.fiscalyear_id=%s """, (tuple(posted_depreciation_line_ids), - exp_acc_id, entry['fy_id'])) + exp_acc_id, asset.id, + entry['fy_id'])) res = cr.fetchone() fy_amount_check = res[0] else: From fe8f976aa882077223952a74eba0eaaeb4539bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sun, 21 Dec 2014 22:11:47 +0100 Subject: [PATCH 3/4] [FIX] account_asset_management: support init entries in current fy Fixes test_4. Handle situations where we have depreciation init entries in the same fiscal year as real depreciation lines. --- account_asset_management/account_asset.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/account_asset_management/account_asset.py b/account_asset_management/account_asset.py index 53a4a7f18..344bc983e 100644 --- a/account_asset_management/account_asset.py +++ b/account_asset_management/account_asset.py @@ -735,6 +735,7 @@ class account_asset_asset(orm.Model): if amount_diff: entry = table[table_i_start] if entry['fy_id']: + # check account moves cr.execute( """ SELECT COALESCE(SUM(aml.debit) -SUM(aml.credit), 0.0) AS depreciated_value @@ -751,6 +752,21 @@ class account_asset_asset(orm.Model): entry['fy_id'])) res = cr.fetchone() fy_amount_check = res[0] + # check init entries with no corresponding account move + cr.execute( + """ + SELECT COALESCE(SUM(amount), 0.0) AS depreciated_value + FROM account_asset_depreciation_line + WHERE id in %s + AND init_entry + AND move_id is null + AND line_date >= %s and line_date <= %s + """, + (tuple(posted_depreciation_line_ids), + entry['date_start'], + entry['date_stop'])) + res = cr.fetchone() + fy_amount_check += res[0] else: fy_amount_check = 0.0 lines = entry['lines'] From 0d943321f148469ed589a7db2ac7691cb420aacd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 22 Dec 2014 21:31:57 +0100 Subject: [PATCH 4/4] [IMP] account_asset_management: simplify adjustment queries Use depreciation lines instead of account moves since there are now checks that ensure both are synchronised. --- account_asset_management/account_asset.py | 65 ++++------------------- 1 file changed, 10 insertions(+), 55 deletions(-) diff --git a/account_asset_management/account_asset.py b/account_asset_management/account_asset.py index 344bc983e..84f8edb85 100644 --- a/account_asset_management/account_asset.py +++ b/account_asset_management/account_asset.py @@ -697,36 +697,13 @@ class account_asset_asset(orm.Model): table_i_start = table_i line_i_start = line_i - # check via initial_balance and accounting entries - # if residual value corresponds with table + # check if residual value corresponds with table # and adjust table when needed - # we base the calculation on accounting entries - # in stead of depreciation lines since this is - # the most reliable source of information - # (in previous versions of the account_asset module - # there was no constraint to prevent inconsistencies - # between depreciation lines and accounting entries). - exp_acc_id = \ - asset.category_id.account_expense_depreciation_id.id cr.execute( - """ - SELECT SUM(sq.amount) AS depreciated_value FROM - (SELECT - CASE - WHEN (aadl.type = 'depreciate' AND aadl.init_entry = TRUE) - THEN aadl.amount - WHEN aml.account_id=%s AND aml.asset_id=%s - THEN (COALESCE(aml.debit,0.0) - COALESCE(aml.credit,0.0)) - ELSE 0.0 - END AS amount - FROM account_asset_depreciation_line aadl - LEFT OUTER JOIN account_move am ON aadl.move_id=am.id - LEFT OUTER JOIN account_move_line aml - ON aml.move_id=am.id - WHERE aadl.id IN %s) sq - """, - (exp_acc_id, asset.id, - tuple(posted_depreciation_line_ids))) + "SELECT COALESCE(SUM(amount), 0.0) " + "FROM account_asset_depreciation_line " + "WHERE id IN %s", + (tuple(posted_depreciation_line_ids),)) res = cr.fetchone() depreciated_value = res[0] residual_amount = asset.asset_value - depreciated_value @@ -735,38 +712,16 @@ class account_asset_asset(orm.Model): if amount_diff: entry = table[table_i_start] if entry['fy_id']: - # check account moves cr.execute( - """ - SELECT COALESCE(SUM(aml.debit) -SUM(aml.credit), 0.0) AS depreciated_value - FROM account_asset_depreciation_line aadl - INNER JOIN account_move am ON aadl.move_id=am.id - INNER JOIN account_move_line aml ON aml.move_id=am.id - INNER JOIN account_period ap ON am.period_id=ap.id - WHERE aadl.id in %s - AND aml.account_id=%s AND aml.asset_id=%s - AND ap.fiscalyear_id=%s - """, - (tuple(posted_depreciation_line_ids), - exp_acc_id, asset.id, - entry['fy_id'])) - res = cr.fetchone() - fy_amount_check = res[0] - # check init entries with no corresponding account move - cr.execute( - """ - SELECT COALESCE(SUM(amount), 0.0) AS depreciated_value - FROM account_asset_depreciation_line - WHERE id in %s - AND init_entry - AND move_id is null - AND line_date >= %s and line_date <= %s - """, + "SELECT COALESCE(SUM(amount), 0.0) " + "FROM account_asset_depreciation_line " + "WHERE id in %s " + " AND line_date >= %s and line_date <= %s", (tuple(posted_depreciation_line_ids), entry['date_start'], entry['date_stop'])) res = cr.fetchone() - fy_amount_check += res[0] + fy_amount_check = res[0] else: fy_amount_check = 0.0 lines = entry['lines']