diff --git a/addons/sale/models/sale_order_line.py b/addons/sale/models/sale_order_line.py index d8c3e0dacc8..69ca835c773 100644 --- a/addons/sale/models/sale_order_line.py +++ b/addons/sale/models/sale_order_line.py @@ -941,16 +941,16 @@ class SaleOrderLine(models.Model): #=== CRUD METHODS ===# def _add_precomputed_values(self, vals_list): - """ In the specific case where the discount is provided in the create values + """ In case an editable precomputed field is provided in the create values without being rounded, we have to 'manually' round it otherwise it won't be, - because editable precomputed field values are kept 'as is'. + because those field values are kept 'as is'. This is a temporary fix until the problem is fixed in the ORM. """ - precision = self.env['decimal.precision'].precision_get('Discount') for vals in vals_list: - if vals.get('discount'): - vals['discount'] = float_round(vals['discount'], precision_digits=precision) + for fname in ('discount', 'product_uom_qty'): + if fname in vals: + vals[fname] = self._fields[fname].convert_to_cache(vals[fname], self) return super()._add_precomputed_values(vals_list) @api.model_create_multi diff --git a/addons/sale/tests/test_sale_prices.py b/addons/sale/tests/test_sale_prices.py index a5d11f8bb42..5e7b10e8b2b 100644 --- a/addons/sale/tests/test_sale_prices.py +++ b/addons/sale/tests/test_sale_prices.py @@ -7,7 +7,7 @@ from datetime import timedelta from odoo import fields from odoo.fields import Command from odoo.tests import Form, tagged -from odoo.tools import float_compare, mute_logger +from odoo.tools import float_compare, mute_logger, float_round from odoo.addons.sale.tests.common import SaleCommon @@ -978,3 +978,21 @@ class TestSalePrices(SaleCommon): # 136.36 price tax excluded with discount applied self.assertEqual(order.amount_undiscounted, 272.72) self.assertEqual(line.price_subtotal, 136.36) + + def test_product_quantity_rounding(self): + """When adding a sale order line, product quantity should be rounded + according to decimal precision""" + order = self.empty_order + + product_uom_qty = 0.333333 + order.order_line = [Command.create({ + 'product_id': self.product.id, + 'product_uom_qty': product_uom_qty, + 'price_unit': 75.0, + })] + order.action_confirm() + line = order.order_line + quantity_precision = self.env['decimal.precision'].precision_get('Product Unit of Measure') + expected_price_subtotal = line.price_unit * float_round(product_uom_qty, precision_digits=quantity_precision) + self.assertAlmostEqual(line.price_subtotal, expected_price_subtotal) + self.assertEqual(order.amount_total, order.tax_totals.get('amount_total'))