changeset 4b8495cb0efd in modules/stock:default
details: https://hg.tryton.org/modules/stock?cmd=changeset&node=4b8495cb0efd
description:
        Round quantity computing in search clause

        The internal quantity is stored without precision constraint and so the 
sum
        result of many rows cumulate the rounding issue.
        So the quantity must be rounded to return correct result when operator 
is
        equality.
        But as getting the digits from the product default uom would require to 
make
        many joins, the simplest and more efficient solution is to use the 
highest
        digits of the units.

        issue11235
        review390001002
diffstat:

 move.py |  12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diffs (37 lines):

diff -r 7db58c00d232 -r 4b8495cb0efd move.py
--- a/move.py   Tue Feb 15 00:08:47 2022 +0100
+++ b/move.py   Thu Feb 17 00:37:39 2022 +0100
@@ -8,8 +8,9 @@
 from itertools import groupby
 
 from sql import Column, For, Literal, Null, Union
-from sql.aggregate import Sum
+from sql.aggregate import Max, Sum
 from sql.conditionals import Case, Coalesce
+from sql.functions import Round
 
 from trytond.i18n import gettext
 from trytond.model import Check, Model, ModelSQL, ModelView, Workflow, fields
@@ -118,6 +119,8 @@
         pool = Pool()
         Product = pool.get('product.product')
         Move = pool.get('stock.move')
+        Uom = pool.get('product.uom')
+        uom = Uom.__table__()
 
         if not location_ids or not domain:
             return []
@@ -135,7 +138,12 @@
                 query = Move.compute_quantities_query(
                     location_ids, with_childs, grouping=grouping)
                 col_id = Column(query, grouping[position])
-                quantity = Sum(query.quantity)
+                # We need to round the result to have same result as
+                # products_by_location but as we do not have the unit, we use
+                # the biggest digits of all unit as best approximation.
+                quantity = Round(
+                    fields.Numeric('quantity').sql_cast(Sum(query.quantity)),
+                    uom.select(Max(uom.digits)))
                 group_by = [Column(query, key).as_(key) for key in grouping]
                 return [('id', 'in', query.select(
                             col_id,

Reply via email to