From c1e63df602df48ccb48b973291990579009bc085 Mon Sep 17 00:00:00 2001 From: Qlasta Date: Tue, 29 Apr 2025 11:12:16 +0300 Subject: [PATCH] [17.0][FIX] product_secondary_unit: unnecessary sec_uom_qty set. --- .../models/product_secondary_unit_mixin.py | 17 ++++++++++++++--- .../tests/test_secondary_unit_mixin.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/product_secondary_unit/models/product_secondary_unit_mixin.py b/product_secondary_unit/models/product_secondary_unit_mixin.py index 527f187da21..e8daa80b635 100644 --- a/product_secondary_unit/models/product_secondary_unit_mixin.py +++ b/product_secondary_unit/models/product_secondary_unit_mixin.py @@ -1,7 +1,7 @@ # Copyright 2021 Tecnativa - Sergio Teruel # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import api, fields, models -from odoo.tools.float_utils import float_round +from odoo.tools.float_utils import float_is_zero, float_round class ProductSecondaryUnitMixin(models.AbstractModel): @@ -76,7 +76,11 @@ def _get_secondary_uom_qty_depends(self): def _compute_secondary_uom_qty(self): for line in self: if not line.secondary_uom_id: - line.secondary_uom_qty = 0.0 + if not float_is_zero( + line.secondary_uom_qty, precision_digits=line._get_uom_precision() + ): + # Only set to 0.0 if it's not already 0.0 + self.secondary_uom_qty = 0.0 continue elif line.secondary_uom_id.dependency_type == "independent": continue @@ -88,6 +92,9 @@ def _compute_secondary_uom_qty(self): ) line.secondary_uom_qty = qty + def _get_uom_precision(self): + return self.env["decimal.precision"].precision_get("Product Unit of Measure") + def _get_default_value_for_qty_field(self): return self.default_get([self._secondary_unit_fields["qty_field"]]).get( self._secondary_unit_fields["qty_field"] @@ -126,7 +133,11 @@ def _onchange_helper_product_uom_for_secondary(self): target model. """ if not self.secondary_uom_id: - self.secondary_uom_qty = 0.0 + if not float_is_zero( + self.secondary_uom_qty, precision_digits=self._get_uom_precision() + ): + # Only set to 0.0 if it's not already 0.0 + self.secondary_uom_qty = 0.0 return elif self.secondary_uom_id.dependency_type == "independent": return diff --git a/product_secondary_unit/tests/test_secondary_unit_mixin.py b/product_secondary_unit/tests/test_secondary_unit_mixin.py index df27d26ad54..2a1d1efa293 100644 --- a/product_secondary_unit/tests/test_secondary_unit_mixin.py +++ b/product_secondary_unit/tests/test_secondary_unit_mixin.py @@ -102,6 +102,23 @@ def test_product_secondary_unit_mixin_no_uom_onchange(self): fake_model._onchange_helper_product_uom_for_secondary() self.assertEqual(fake_model.secondary_uom_qty, 0) + def test_product_secondary_unit_mixin_remove_sec_uom(self): + fake_model = self.secondary_unit_fake + fake_model.write( + {"secondary_uom_qty": 5, "secondary_uom_id": self.secondary_unit_box_5.id} + ) + fake_model.secondary_uom_id = False + self.assertEqual(fake_model.product_uom_qty, 0) + self.assertFalse(fake_model.secondary_uom_id) + self.assertEqual(fake_model.secondary_uom_qty, 0) + + def test_product_secondary_unit_mixin_wo_sec_uom(self): + fake_model = self.secondary_unit_fake + fake_model.write({"product_uom_qty": 5}) + self.assertEqual(fake_model.product_uom_qty, 5) + self.assertFalse(fake_model.secondary_uom_id) + self.assertEqual(fake_model.secondary_uom_qty, 0) + def test_chained_compute_field(self): """Secondary_uom_qty has not been computed when secondary_uom_id changes""" fake_model = self.secondary_unit_fake