diff --git a/product_secondary_unit/models/product_secondary_unit_mixin.py b/product_secondary_unit/models/product_secondary_unit_mixin.py index e8daa80b635..bd53ca71bef 100644 --- a/product_secondary_unit/models/product_secondary_unit_mixin.py +++ b/product_secondary_unit/models/product_secondary_unit_mixin.py @@ -126,6 +126,10 @@ def _compute_helper_target_field_qty(self): rec.secondary_uom_qty * factor, precision_rounding=rec._get_uom_line().rounding, ) + # To avoid resetting the primary quantity to 0.0 + if not rec.secondary_uom_qty: + # Don't modify the primary quantity - preserve it + continue rec[rec._secondary_unit_fields["qty_field"]] = qty def _onchange_helper_product_uom_for_secondary(self): diff --git a/product_secondary_unit/tests/test_secondary_unit_mixin.py b/product_secondary_unit/tests/test_secondary_unit_mixin.py index 2a1d1efa293..bbd181f5a7a 100644 --- a/product_secondary_unit/tests/test_secondary_unit_mixin.py +++ b/product_secondary_unit/tests/test_secondary_unit_mixin.py @@ -146,3 +146,21 @@ def test_independent_type(self): fake_model.write({"secondary_uom_qty": 4}) self.assertEqual(fake_model.product_uom_qty, 17) self.assertEqual(fake_model.secondary_uom_qty, 4) + + def test_independent_type_with_existing_qty(self): + """Test assigning secondary_uom_id to a record with existing qty. + + This should not reset product_uom_qty to 0.0, + then switching to independent type, the original qty should be preserved. + """ + fake_model = self.secondary_unit_fake + fake_model.product_uom_qty = 10.0 + fake_model.secondary_uom_id = self.secondary_unit_box_5 + fake_model.secondary_uom_id.write({"dependency_type": "independent"}) + previous_product_uom_qty = fake_model.product_uom_qty + + self.assertEqual(previous_product_uom_qty, 10.0) + fake_model.write({"secondary_uom_qty": 2}) + + self.assertEqual(fake_model.product_uom_qty, previous_product_uom_qty) + self.assertEqual(fake_model.secondary_uom_qty, 2)