Skip to content

Commit c1e63df

Browse files
committed
[17.0][FIX] product_secondary_unit: unnecessary sec_uom_qty set.
1 parent e80e148 commit c1e63df

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎product_secondary_unit/models/product_secondary_unit_mixin.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2021 Tecnativa - Sergio Teruel
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33
from odoo import api, fields, models
4-
from odoo.tools.float_utils import float_round
4+
from odoo.tools.float_utils import float_is_zero, float_round
55

66

77
class ProductSecondaryUnitMixin(models.AbstractModel):
@@ -76,7 +76,11 @@ def _get_secondary_uom_qty_depends(self):
7676
def _compute_secondary_uom_qty(self):
7777
for line in self:
7878
if not line.secondary_uom_id:
79-
line.secondary_uom_qty = 0.0
79+
if not float_is_zero(
80+
line.secondary_uom_qty, precision_digits=line._get_uom_precision()
81+
):
82+
# Only set to 0.0 if it's not already 0.0
83+
self.secondary_uom_qty = 0.0
8084
continue
8185
elif line.secondary_uom_id.dependency_type == "independent":
8286
continue
@@ -88,6 +92,9 @@ def _compute_secondary_uom_qty(self):
8892
)
8993
line.secondary_uom_qty = qty
9094

95+
def _get_uom_precision(self):
96+
return self.env["decimal.precision"].precision_get("Product Unit of Measure")
97+
9198
def _get_default_value_for_qty_field(self):
9299
return self.default_get([self._secondary_unit_fields["qty_field"]]).get(
93100
self._secondary_unit_fields["qty_field"]
@@ -126,7 +133,11 @@ def _onchange_helper_product_uom_for_secondary(self):
126133
target model.
127134
"""
128135
if not self.secondary_uom_id:
129-
self.secondary_uom_qty = 0.0
136+
if not float_is_zero(
137+
self.secondary_uom_qty, precision_digits=self._get_uom_precision()
138+
):
139+
# Only set to 0.0 if it's not already 0.0
140+
self.secondary_uom_qty = 0.0
130141
return
131142
elif self.secondary_uom_id.dependency_type == "independent":
132143
return

‎product_secondary_unit/tests/test_secondary_unit_mixin.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ def test_product_secondary_unit_mixin_no_uom_onchange(self):
102102
fake_model._onchange_helper_product_uom_for_secondary()
103103
self.assertEqual(fake_model.secondary_uom_qty, 0)
104104

105+
def test_product_secondary_unit_mixin_remove_sec_uom(self):
106+
fake_model = self.secondary_unit_fake
107+
fake_model.write(
108+
{"secondary_uom_qty": 5, "secondary_uom_id": self.secondary_unit_box_5.id}
109+
)
110+
fake_model.secondary_uom_id = False
111+
self.assertEqual(fake_model.product_uom_qty, 0)
112+
self.assertFalse(fake_model.secondary_uom_id)
113+
self.assertEqual(fake_model.secondary_uom_qty, 0)
114+
115+
def test_product_secondary_unit_mixin_wo_sec_uom(self):
116+
fake_model = self.secondary_unit_fake
117+
fake_model.write({"product_uom_qty": 5})
118+
self.assertEqual(fake_model.product_uom_qty, 5)
119+
self.assertFalse(fake_model.secondary_uom_id)
120+
self.assertEqual(fake_model.secondary_uom_qty, 0)
121+
105122
def test_chained_compute_field(self):
106123
"""Secondary_uom_qty has not been computed when secondary_uom_id changes"""
107124
fake_model = self.secondary_unit_fake

0 commit comments

Comments
 (0)