From 958bee9cf9f770351fbc346a081cfe059a911b3f Mon Sep 17 00:00:00 2001
From: oco-odoo <oco@odoo.com>
Date: Thu, 6 Feb 2020 13:26:33 +0000
Subject: [PATCH] [FIX] account: correctly round amounts to compute matched
 percentage

Before that, depending on rounding errors on floats, it was possible to end up with a matched percentage of 0.999999999 instead of 1.0 on an account move in case of full reconciliation. This could in turn cause problems with cash basis taxes, when checking what proportion of the move is reconciled.

closes odoo/odoo#44845

X-original-commit: 57c33971316396aad1f72d53aa9d7efb4a29310f
Signed-off-by: oco-odoo <oco-odoo@users.noreply.github.com>
---
 addons/account/models/account_move.py | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index 6dfabab5a90c..3db76347c9fe 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -1833,10 +1833,11 @@ class AccountMove(models.Model):
         params = [self.id, self.id]
         self._cr.execute(query, params)
         total_amount, total_reconciled = self._cr.fetchone()
-        if float_is_zero(total_amount, precision_rounding=self.company_id.currency_id.rounding):
+        currency = self.company_id.currency_id
+        if float_is_zero(total_amount, precision_rounding=currency.rounding):
             return 1.0
         else:
-            return abs(total_reconciled / total_amount)
+            return abs(currency.round(total_reconciled) / currency.round(total_amount))
 
     def _get_reconciled_payments(self):
         """Helper used to retrieve the reconciled payments on this journal entry"""
@@ -3816,7 +3817,9 @@ class AccountMoveLine(models.Model):
                     if total_amount_currency == 0.0:
                         matched_percentage_per_move[line.move_id.id] = 1.0
                     else:
-                        matched_percentage_per_move[line.move_id.id] = total_reconciled_currency / total_amount_currency
+                        # lines_to_consider is always non-empty when total_amount_currency is 0
+                        currency = lines_to_consider[0].currency_id or lines_to_consider[0].company_id.currency_id
+                        matched_percentage_per_move[line.move_id.id] = currency.round(total_reconciled_currency) / currency.round(total_amount_currency)
         return matched_percentage_per_move
 
     def _get_analytic_tag_ids(self):
-- 
GitLab