diff --git a/addons/sale_stock/models/sale_order.py b/addons/sale_stock/models/sale_order.py index 75607d09e0a..be166972818 100644 --- a/addons/sale_stock/models/sale_order.py +++ b/addons/sale_stock/models/sale_order.py @@ -7,7 +7,7 @@ from datetime import datetime, timedelta from collections import defaultdict from odoo import api, fields, models, _ -from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, float_compare +from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, float_compare, float_round from odoo.exceptions import UserError _logger = logging.getLogger(__name__) @@ -570,7 +570,18 @@ class SaleOrderLine(models.Model): pack = self.product_packaging qty = self.product_uom_qty q = default_uom._compute_quantity(pack.qty, self.product_uom) - if qty and q and round(qty % q, 2): + # We do not use the modulo operator to check if qty is a mltiple of q. Indeed the quantity + # per package might be a float, leading to incorrect results. For example: + # 8 % 1.6 = 1.5999999999999996 + # 5.4 % 1.8 = 2.220446049250313e-16 + if ( + qty + and q + and float_compare( + qty / q, float_round(qty / q, precision_rounding=1.0), precision_rounding=0.001 + ) + != 0 + ): newqty = qty - (qty % q) + q return { 'warning': {