|
| 1 | +# Copyright 2026 Camptocamp SA |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | +import math |
| 4 | + |
| 5 | +from odoo import api, fields, models |
| 6 | + |
| 7 | + |
| 8 | +class SaleOrderLine(models.Model): |
| 9 | + _inherit = "sale.order.line" |
| 10 | + |
| 11 | + def _sale_multiple_uom_has_unit_reference(self): |
| 12 | + """Return whether both UoMs share ``Units`` as common reference. |
| 13 | +
|
| 14 | + This is used to distinguish quantity-based UoMs from dimensional UoMs. |
| 15 | +
|
| 16 | + When both the order line UoM and the sales multiple UoM share the |
| 17 | + ``uom.product_uom_unit`` reference, the sellable quantity in the order |
| 18 | + line UoM should remain an integer count of items or packs. |
| 19 | +
|
| 20 | + Examples: |
| 21 | + - Pack of 5 and Pack of 100 -> True |
| 22 | + - Pack of 6 and Pack of 100 -> True |
| 23 | + - 400 g and 1 kg -> False |
| 24 | + """ |
| 25 | + self.ensure_one() |
| 26 | + unit_uom = self.env.ref("uom.product_uom_unit", raise_if_not_found=False) |
| 27 | + if not unit_uom: |
| 28 | + return False |
| 29 | + multiple_uom = self.product_id.sale_multiple_uom_id |
| 30 | + return self.product_uom_id._has_common_reference( |
| 31 | + unit_uom |
| 32 | + ) and multiple_uom._has_common_reference(unit_uom) |
| 33 | + |
| 34 | + def _get_sale_multiple_step_qty(self) -> float: |
| 35 | + """Return the effective quantity step in the order line UoM. |
| 36 | +
|
| 37 | + The sales multiple is stored as a UoM on the product. This method |
| 38 | + expresses one unit of that sales multiple UoM in the order line UoM. |
| 39 | +
|
| 40 | + For UoMs sharing ``Units`` as common reference, the result is ceiled to |
| 41 | + keep an integer count of items or packs. |
| 42 | +
|
| 43 | + Examples: |
| 44 | + - sales multiple UoM: Pack of 100 |
| 45 | + order line UoM: Pack of 5 |
| 46 | + step quantity: 20 |
| 47 | +
|
| 48 | + - sales multiple UoM: Pack of 100 |
| 49 | + order line UoM: Pack of 6 |
| 50 | + raw step: 16.666... |
| 51 | + effective step quantity: 17 |
| 52 | +
|
| 53 | + - sales multiple UoM: 1 kg |
| 54 | + order line UoM: 400 g |
| 55 | + step quantity: 2.5 |
| 56 | + """ |
| 57 | + self.ensure_one() |
| 58 | + multiple_uom = self.product_id.sale_multiple_uom_id |
| 59 | + step_qty = multiple_uom._compute_quantity(1.0, self.product_uom_id) |
| 60 | + if self._sale_multiple_uom_has_unit_reference(): |
| 61 | + step_qty = math.ceil(step_qty) |
| 62 | + return step_qty |
| 63 | + |
| 64 | + def _round_sale_qty_to_multiple(self, qty_to_order: float) -> float: |
| 65 | + """Round the order line quantity to a multiple of the sales multiple UoM. |
| 66 | +
|
| 67 | + :param qty_to_order: quantity expressed in the order line UoM (product_uom_id). |
| 68 | + :return: rounded quantity expressed in the order line UoM (product_uom_id). |
| 69 | +
|
| 70 | + This method is inspired by |
| 71 | + ``stock.warehouse.orderpoint::_get_multiple_rounded_qty``. |
| 72 | + Round using "UP" strategy to the nearest multiple of |
| 73 | + product_id.sale_multiple_uom_id: |
| 74 | +
|
| 75 | + - Convert qty_to_order from the product_uom_id to the |
| 76 | + product_id.sale_multiple_uom_id |
| 77 | + - Round the quantity using "UP" strategy |
| 78 | + - Convert back to the product_uom_id quantity |
| 79 | +
|
| 80 | + Being said, for the UoMs which share common reference unit (e.g. Units) |
| 81 | + the sales multiple UoM is divisible by the order line UoM |
| 82 | + and the division result is usually an integer. |
| 83 | + But for dimensional UoMs like (e.g. Kg(s), Meter(s)) the rounding result |
| 84 | + can be a floating point number which is perfectly acceptable. |
| 85 | +
|
| 86 | + For example (compatible UoMs: 100 is divisible by 5): |
| 87 | + - order line UoM: Pack of 5 (5 units) |
| 88 | + - sales multiple UoM: Pack of 100 (100 units) |
| 89 | + - qty_to_order = 15 packs are rounded to 20 packs (100 units): |
| 90 | + 15 packs of 5 units = 75 units |
| 91 | + -> 1 pack of 100 = 100 units -> 20 packs of 5 units |
| 92 | + - qty_to_order = 55 packs are rounded to 60 packs (300 units): |
| 93 | + 55 packs of 5 units = 275 units |
| 94 | + -> 3 packs of 100 = 300 units -> 60 packs of 5 units |
| 95 | +
|
| 96 | + For example (compatible UoMs: 100 is not divisible by 6): |
| 97 | + - order line UoM: Pack of 6 (6 units) |
| 98 | + - sales multiple UoM: Pack of 100 (100 units) |
| 99 | + - qty_to_order = 13 packs are rounded to 16.67 packs (100.02 units): |
| 100 | + 13 packs of 6 units = 78 units -> 1.0 pack of 100 = 100 units |
| 101 | + -> 16.6667 packs of 6 units => ceiled to 17 |
| 102 | +
|
| 103 | + For example (dimensional UoMs: 1 kg is not divisible by 400g): |
| 104 | + - order line UoM: 400g (0.4 kg) |
| 105 | + - sales multiple UoM: 1 kg |
| 106 | + - qty_to_order = 2 packs are rounded to 2.5 packs (1 kg): |
| 107 | + 2 packs of 400g = 0.8 kg -> 1.0 pack of 1 kg = 1 kg |
| 108 | + -> 2.5 packs of 400g = 1 kg (fractional, must NOT be ceiled to 3) |
| 109 | + """ |
| 110 | + self.ensure_one() |
| 111 | + multiple_uom = self.product_id.sale_multiple_uom_id |
| 112 | + # For UoMs sharing ``Units`` as common reference, keep quantities that |
| 113 | + # are already multiples of the effective step unchanged. |
| 114 | + if self._sale_multiple_uom_has_unit_reference(): |
| 115 | + step_qty = self._get_sale_multiple_step_qty() |
| 116 | + multiple = qty_to_order / step_qty |
| 117 | + rounded_multiple = fields.Float.round( |
| 118 | + multiple, precision_digits=0, rounding_method="UP" |
| 119 | + ) |
| 120 | + if multiple_uom.compare(rounded_multiple, multiple) == 0: |
| 121 | + return qty_to_order |
| 122 | + |
| 123 | + packs = self.product_uom_id._compute_quantity(qty_to_order, multiple_uom) |
| 124 | + packs = fields.Float.round(packs, precision_digits=0, rounding_method="UP") |
| 125 | + qty_rounded = multiple_uom._compute_quantity(packs, self.product_uom_id) |
| 126 | + # For UoMs sharing ``Units`` as common reference, |
| 127 | + # we ceil the result to keep an integer count of items or packs. |
| 128 | + if self._sale_multiple_uom_has_unit_reference(): |
| 129 | + qty_rounded = math.ceil(qty_rounded) |
| 130 | + return qty_rounded |
| 131 | + |
| 132 | + @api.onchange("product_id", "product_uom_qty", "product_uom_id") |
| 133 | + def _onchange_product_uom_qty_round_multiple(self): |
| 134 | + """Round product_uom_qty to a multiple of the sales multiple UoM. |
| 135 | +
|
| 136 | + If sales multiple UoM is set on the product, this onchange rounds |
| 137 | + the order line quantity to the nearest multiple of that UoM. |
| 138 | + """ |
| 139 | + for line in self: |
| 140 | + multiple_uom = line.product_id.sale_multiple_uom_id |
| 141 | + if not multiple_uom: |
| 142 | + continue |
| 143 | + |
| 144 | + qty_to_order = line.product_uom_qty or 0.0 |
| 145 | + if qty_to_order <= 0: |
| 146 | + continue |
| 147 | + rounded_qty = line._round_sale_qty_to_multiple(qty_to_order) |
| 148 | + # ``uom.uom.compare`` returns -1, 0 or 1 depending on whether |
| 149 | + # ``rounded_qty`` is lower than, equal to, or greater than |
| 150 | + # ``qty_to_order`` (within UoM rounding precision). |
| 151 | + if line.product_uom_id.compare(rounded_qty, qty_to_order) != 0: |
| 152 | + line.product_uom_qty = rounded_qty |
0 commit comments