From b1082348e7a0875349bea3d8ccdc3556a5bc0fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Snauwaert?= Date: Mon, 3 Jul 2017 11:22:32 +0200 Subject: [PATCH] [FIX] account: reconciliation, fix query that fetch perfect line match (#17969) When opening the reconciliation widget, there are two queries that are done, The first one is to find a move line that perfectly match the statement line and is done in SQL for performance issue. The second one is a search with a domain to find all the lines that could be used in that reconciliation. 1) There was a mismatch between the two. The domain fetches lines that have a payment_id while the SQL query does not. Which is inconsistent 2) In some case, we did not find a perfect match on the amount using the SQL query because of a rounding error. Example is for amount 3,3. In python: 3,3 is represented like this 3,3000000000003 and doing a float_round of that value still returns 3,3000000000003 Since we want to match on an exact amount, using float_repr is needed to fix that error --- addons/account/models/account_bank_statement.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/addons/account/models/account_bank_statement.py b/addons/account/models/account_bank_statement.py index 2320643e5f8..d5e9ab8bb56 100644 --- a/addons/account/models/account_bank_statement.py +++ b/addons/account/models/account_bank_statement.py @@ -3,7 +3,7 @@ from openerp import api, fields, models, _ from openerp.osv import expression from openerp.tools import float_is_zero -from openerp.tools import float_compare, float_round +from openerp.tools import float_compare, float_round, float_repr from openerp.tools.misc import formatLang from openerp.exceptions import UserError, ValidationError @@ -606,7 +606,8 @@ class AccountBankStatementLine(models.Model): from_clause = "FROM account_move_line aml JOIN account_account acc ON acc.id = aml.account_id " where_clause = """WHERE aml.company_id = %(company_id)s AND ( - (aml.statement_id IS NULL AND aml.account_id IN %(account_payable_receivable)s) + (aml.statement_id IS NULL AND aml.account_id IN %(account_payable_receivable)s + AND aml.payment_id IS NOT NULL) OR ("""+acc_type+""" AND aml.reconciled = false) )""" @@ -630,7 +631,7 @@ class AccountBankStatementLine(models.Model): precision = st_line_currency and st_line_currency.decimal_places or company_currency.decimal_places params = {'company_id': self.env.user.company_id.id, 'account_payable_receivable': (self.journal_id.default_credit_account_id.id, self.journal_id.default_debit_account_id.id), - 'amount': float_round(amount, precision_digits=precision), + 'amount': float_repr(float_round(amount, precision_digits=precision), precision_digits=precision), 'partner_id': self.partner_id.id, 'excluded_ids': tuple(excluded_ids), 'ref': self.name, @@ -651,9 +652,9 @@ class AccountBankStatementLine(models.Model): # Look for a single move line with the same amount field = currency and 'amount_residual_currency' or 'amount_residual' liquidity_field = currency and 'amount_currency' or amount > 0 and 'debit' or 'credit' - liquidity_amt_clause = currency and '%(amount)s' or 'abs(%(amount)s)' + liquidity_amt_clause = currency and '%(amount)s::numeric' or 'abs(%(amount)s::numeric)' sql_query = self._get_common_sql_query(excluded_ids=excluded_ids) + \ - " AND ("+field+" = %(amount)s OR (acc.internal_type = 'liquidity' AND "+liquidity_field+" = " + liquidity_amt_clause + ")) \ + " AND ("+field+" = %(amount)s::numeric OR (acc.internal_type = 'liquidity' AND "+liquidity_field+" = " + liquidity_amt_clause + ")) \ ORDER BY date_maturity asc, aml.id asc LIMIT 1" self.env.cr.execute(sql_query, params) results = self.env.cr.fetchone()