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] [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']