|
| 1 | +# Copyright 2026 Camptocamp SA |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | +from odoo import api, fields, models |
| 4 | +from odoo.exceptions import ValidationError |
| 5 | + |
| 6 | + |
| 7 | +class SaleOrderLine(models.Model): |
| 8 | + _inherit = "sale.order.line" |
| 9 | + |
| 10 | + def _round_sale_qty_to_multiple(self, qty_to_order: float) -> float: |
| 11 | + """Round the order line quantity to a multiple of the sales multiple UoM. |
| 12 | +
|
| 13 | + :param qty_to_order: quantity expressed in the order line UoM (product_uom_id). |
| 14 | + :return: rounded quantity expressed in the order line UoM (product_uom_id). |
| 15 | +
|
| 16 | + This method is inspired by |
| 17 | + ``stock.warehouse.orderpoint::_get_multiple_rounded_qty``. |
| 18 | + Round using "UP" strategy to the nearest multiple of |
| 19 | + product_id.sale_multiple_uom_id: |
| 20 | +
|
| 21 | + - Convert qty_to_order from the product_uom_id to the |
| 22 | + product_id.sale_multiple_uom_id |
| 23 | + - Round the quantity using "UP" strategy |
| 24 | + - Convert back to the product_uom_id quantity |
| 25 | +
|
| 26 | + Being said, if sales multiple UoM is divisible by the order line UoM, |
| 27 | + the result will be an integer. |
| 28 | + Otherwise, we expect rounding issues. |
| 29 | + It's up to the user to choose compatible UoMs. |
| 30 | +
|
| 31 | + For example (compatible UoMs: 100 is divisible by 5): |
| 32 | + - order line UoM: Pack of 5 (5 units) |
| 33 | + - sales multiple UoM: Pack of 100 (100 units) |
| 34 | + - qty_to_order = 15 packs is rounded to 20 packs (100 units): |
| 35 | + 15 packs of 5 = 75 units -> 1 pack of 100 = 100 units -> 20 packs of 5 |
| 36 | + - qty_to_order = 55 packs is rounded to 60 packs (300 units): |
| 37 | + 55 packs of 5 = 275 units -> 3 packs of 100 = 300 units -> 60 packs of 5 |
| 38 | +
|
| 39 | + For example (fractional result: 100 is not divisible by 6): |
| 40 | + - order line UoM: Pack of 6 (6 units) |
| 41 | + - sales multiple UoM: Pack of 100 (100 units) |
| 42 | + - qty_to_order = 13 packs is rounded to 17 packs (100.02 units): |
| 43 | + 13 packs of 6 = 78 units -> 1.0 pack of 100 = 100 units |
| 44 | + -> 16.6667 packs of 6 |
| 45 | + """ |
| 46 | + self.ensure_one() |
| 47 | + packs = self.product_uom_id._compute_quantity( |
| 48 | + qty_to_order, self.product_id.sale_multiple_uom_id |
| 49 | + ) |
| 50 | + packs = fields.Float.round(packs, precision_digits=0, rounding_method="UP") |
| 51 | + qty_rounded = self.product_id.sale_multiple_uom_id._compute_quantity( |
| 52 | + packs, self.product_uom_id |
| 53 | + ) |
| 54 | + return qty_rounded |
| 55 | + |
| 56 | + def _nearest_valid_integer_qty(self, entered_qty: float) -> float: |
| 57 | + """Return the nearest valid integer qty bigger than entered (UP). |
| 58 | +
|
| 59 | + The idea is simply to try integer quantities bigger than the entered |
| 60 | + quantity until we find one that, when rounded to the sales multiple UoM, |
| 61 | + gives an integer result. |
| 62 | + """ |
| 63 | + self.ensure_one() |
| 64 | + qty_to_check = int(entered_qty) + 1 |
| 65 | + while True: |
| 66 | + rounded_qty = self._round_sale_qty_to_multiple(qty_to_check) |
| 67 | + if rounded_qty.is_integer(): |
| 68 | + return rounded_qty |
| 69 | + qty_to_check += 1 |
| 70 | + |
| 71 | + def _prepare_incompatible_uom_warning_msg( |
| 72 | + self, |
| 73 | + entered_qty: float, |
| 74 | + rounded_qty: float, |
| 75 | + ) -> str: |
| 76 | + """Prepare the warning message for incompatible UoMs. |
| 77 | +
|
| 78 | + :param entered_qty: quantity entered by the user. |
| 79 | + :param rounded_qty: rounded quantity to the sales multiple UoM. |
| 80 | + :return: warning message string. |
| 81 | + """ |
| 82 | + self.ensure_one() |
| 83 | + multiple_uom = self.product_id.sale_multiple_uom_id |
| 84 | + valid_qty = self._nearest_valid_integer_qty(entered_qty) |
| 85 | + return self.env._( |
| 86 | + "Incompatible UoMs.\n" |
| 87 | + "The entered qty %(entered_qty).2f is rounded to " |
| 88 | + "%(rounded_qty).2f which is not valid " |
| 89 | + "for product '%(product)s'.\n" |
| 90 | + "It should be a multiple of %(multiple)s.\n" |
| 91 | + "The nearest valid qty is %(valid_qty).2f.", |
| 92 | + entered_qty=entered_qty, |
| 93 | + rounded_qty=rounded_qty, |
| 94 | + product=self.product_id.display_name, |
| 95 | + multiple=multiple_uom.display_name, |
| 96 | + valid_qty=valid_qty, |
| 97 | + ) |
| 98 | + |
| 99 | + @api.onchange("product_id", "product_uom_qty", "product_uom_id") |
| 100 | + def _onchange_product_uom_qty(self): |
| 101 | + """Round product_uom_qty to a multiple of the sales multiple UoM. |
| 102 | +
|
| 103 | + If sales multiple UoM is set on the product, this onchange rounds |
| 104 | + the order line quantity to the nearest multiple of that UoM using |
| 105 | + the "_round_sale_qty_to_multiple" method. |
| 106 | +
|
| 107 | + If the rounded quantity is a floating point number, it means that the |
| 108 | + selected UoM is not compatible with the sales multiple UoM. |
| 109 | + See an example in the "_round_sale_qty_to_multiple" method. |
| 110 | +
|
| 111 | + For such cases, a warning is displayed to inform the user |
| 112 | + about the incompatibility of UoMs. |
| 113 | + """ |
| 114 | + for line in self: |
| 115 | + multiple_uom = line.product_id.sale_multiple_uom_id |
| 116 | + if not multiple_uom: |
| 117 | + continue |
| 118 | + |
| 119 | + qty_to_order = line.product_uom_qty or 0.0 |
| 120 | + if qty_to_order <= 0: |
| 121 | + continue |
| 122 | + |
| 123 | + rounded_qty = line._round_sale_qty_to_multiple(qty_to_order) |
| 124 | + if not rounded_qty.is_integer(): |
| 125 | + msg = line._prepare_incompatible_uom_warning_msg( |
| 126 | + entered_qty=qty_to_order, |
| 127 | + rounded_qty=rounded_qty, |
| 128 | + ) |
| 129 | + return {"warning": {"title": self.env._("Warning"), "message": msg}} |
| 130 | + equal_qty = line.product_uom_id.compare(rounded_qty, qty_to_order) == 0 |
| 131 | + if not equal_qty: |
| 132 | + line.product_uom_qty = rounded_qty |
| 133 | + |
| 134 | + @api.constrains( |
| 135 | + "product_id", |
| 136 | + "product_uom_qty", |
| 137 | + "product_uom_id", |
| 138 | + ) |
| 139 | + def _check_rounded_sale_multiple_qty(self): |
| 140 | + """Ensure product_uom_qty is a multiple of the sales multiple UoM.""" |
| 141 | + for line in self: |
| 142 | + multiple_uom = line.product_id.sale_multiple_uom_id |
| 143 | + if not multiple_uom: |
| 144 | + continue |
| 145 | + |
| 146 | + qty_to_order = line.product_uom_qty or 0.0 |
| 147 | + if qty_to_order <= 0: |
| 148 | + continue |
| 149 | + |
| 150 | + rounded_qty = line._round_sale_qty_to_multiple(qty_to_order) |
| 151 | + if not rounded_qty.is_integer(): |
| 152 | + msg = line._prepare_incompatible_uom_warning_msg( |
| 153 | + entered_qty=qty_to_order, |
| 154 | + rounded_qty=rounded_qty, |
| 155 | + ) |
| 156 | + raise ValidationError(msg) |
0 commit comments