Skip to content
Closed
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
4 changes: 4 additions & 0 deletions product_secondary_unit/models/product_secondary_unit_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions product_secondary_unit/tests/test_secondary_unit_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading