Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions product_secondary_unit/models/product_secondary_unit_mixin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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"]
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions product_secondary_unit/tests/test_secondary_unit_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading