Skip to content

Commit 7a749e0

Browse files
[FIX] product_secondary_unit: UoM conversion in secondary unit mixin
Replace _get_factor_line() with uom._compute_quantity() to fix incorrect conversion when line UoM differs from product's base UoM. The previous method failed for certain UoM combinations (e.g., line in L, product in mL). Co-authored-by: Yoshi Tashiro <[email protected]>
1 parent e80a17c commit 7a749e0

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

product_secondary_unit/models/product_secondary_unit_mixin.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def _get_default_secondary_uom(self):
5555
def _get_uom_line(self):
5656
return self[self._secondary_unit_fields["uom_field"]]
5757

58+
# TODO: This method is now not used in this module. Deprecate it in future.
5859
def _get_factor_line(self):
5960
uom_line = self._get_uom_line()
6061
return self.secondary_uom_id.factor * (
@@ -72,6 +73,17 @@ def _get_secondary_uom_qty_depends(self):
7273
return []
7374
return [self._secondary_unit_fields["qty_field"]]
7475

76+
@api.model
77+
def _convert_qty_to_secondary_uom(self, record, qty):
78+
# Intended to be called from other operations if needed.
79+
uom_line = record._get_uom_line()
80+
uom_product = record.product_id[record._product_uom_field]
81+
qty_base = uom_line._compute_quantity(qty, uom_product)
82+
return float_round(
83+
qty_base / (record.secondary_uom_id.factor or 1.0),
84+
precision_rounding=record.secondary_uom_id.uom_id.rounding,
85+
)
86+
7587
@api.depends(lambda x: x._get_secondary_uom_qty_depends())
7688
def _compute_secondary_uom_qty(self):
7789
for line in self:
@@ -80,13 +92,8 @@ def _compute_secondary_uom_qty(self):
8092
continue
8193
elif line.secondary_uom_id.dependency_type == "independent":
8294
continue
83-
factor = line._get_factor_line()
8495
qty_line = line._get_quantity_from_line()
85-
qty = float_round(
86-
qty_line / (factor or 1.0),
87-
precision_rounding=line.secondary_uom_id.uom_id.rounding,
88-
)
89-
line.secondary_uom_qty = qty
96+
line.secondary_uom_qty = self._convert_qty_to_secondary_uom(line, qty_line)
9097

9198
def _get_default_value_for_qty_field(self):
9299
return self.default_get([self._secondary_unit_fields["qty_field"]]).get(
@@ -114,12 +121,11 @@ def _compute_helper_target_field_qty(self):
114121
rec.env.remove_to_compute(
115122
field=rec._fields["secondary_uom_qty"], records=rec
116123
)
117-
factor = rec._get_factor_line()
118-
qty = float_round(
119-
rec.secondary_uom_qty * factor,
120-
precision_rounding=rec._get_uom_line().rounding,
121-
)
122-
rec[rec._secondary_unit_fields["qty_field"]] = qty
124+
qty_base = rec.secondary_uom_qty * rec.secondary_uom_id.factor
125+
uom_line = rec._get_uom_line()
126+
uom_product = rec.product_id[rec._product_uom_field]
127+
qty_line = uom_product._compute_quantity(qty_base, uom_line)
128+
rec[rec._secondary_unit_fields["qty_field"]] = qty_line
123129

124130
def _onchange_helper_product_uom_for_secondary(self):
125131
"""Helper method to be called from onchange method of uom field in
@@ -130,13 +136,8 @@ def _onchange_helper_product_uom_for_secondary(self):
130136
return
131137
elif self.secondary_uom_id.dependency_type == "independent":
132138
return
133-
factor = self._get_factor_line()
134-
line_qty = self._get_quantity_from_line()
135-
qty = float_round(
136-
line_qty / (factor or 1.0),
137-
precision_rounding=self.secondary_uom_id.uom_id.rounding,
138-
)
139-
self.secondary_uom_qty = qty
139+
qty_line = self._get_quantity_from_line()
140+
self.secondary_uom_qty = self._convert_qty_to_secondary_uom(self, qty_line)
140141

141142
@api.model
142143
def default_get(self, fields_list):

product_secondary_unit/tests/test_secondary_unit_mixin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ def setUpClass(cls):
1515
from .models import SecondaryUnitFake
1616

1717
cls.loader.update_registry((SecondaryUnitFake,))
18-
cls.product_uom_kg = cls.env.ref("uom.product_uom_kgm")
1918
cls.product_uom_unit = cls.env.ref("uom.product_uom_unit")
2019
cls.product_uom_dozen = cls.env.ref("uom.product_uom_dozen")
2120
cls.product_template = cls.env["product.template"].create(
2221
{
2322
"name": "test",
24-
"uom_id": cls.product_uom_kg.id,
25-
"uom_po_id": cls.product_uom_kg.id,
23+
"uom_id": cls.product_uom_unit.id,
24+
"uom_po_id": cls.product_uom_unit.id,
2625
"secondary_uom_ids": [
2726
Command.create(
2827
{

0 commit comments

Comments
 (0)