Odoo – UoM Rounding Alert for Sale Order Quantity Input
UoM Rounding Alert for Sale Order Quantity Input
To alert or prevent users from entering a quantity that doesn’t comply with the Unit of Measure (UoM) rounding precision (e.g., whole numbers for UoMs with a 1.00 rounding), you can implement a constraint or an onchange
warning on the Sale Order Line.
Scenario
Suppose you have a product “ABC” with a UoM of Roll, and:
- Rounding precision is set to 1.00
- Ratio is set to 1.00
This means the quantity should be a whole number (e.g., 12
, 13
).
If a user inputs 12.6, this should trigger a warning or error, because fractional quantities are not allowed.
Implementation Steps
Step 1: Create the Module Skeleton
Use the scaffold command to generate the module structure:
./odoo-bin scaffold uom_alert addons
This will create a folder structure like:
uom_alert/
├── __init__.py
├── __manifest__.py
├── models/
│ └── sale_order.py ← Your custom logic here
Step 2: Add Logic to sale_order.py
from odoo import models, api, _
from odoo.exceptions import UserError
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
@api.onchange('product_uom_qty', 'product_uom', 'product_id')
def _onchange_uom_rounding(self):
for line in self:
if not line.product_uom or not line.product_id or not line.product_uom_qty:
continue
try:
qty = float(line.product_uom_qty)
rounding = float(line.product_uom.rounding)
except (ValueError, TypeError):
continue # Skip invalid input
if rounding > 0 and not self._is_multiple_of(qty, rounding):
raise UserError(_(
"Quantity %.2f for product '%s' must be a multiple of the unit of measure precision (%.2f)."
) % (qty, line.product_id.display_name, rounding))
def _is_multiple_of(self, qty, rounding):
return abs(round(qty / rounding) - (qty / rounding)) < 1e-6
Step 3: __init__.py
in models/
Folder
from . import sale_order
Step 4: __manifest__.py
in Root Module Folder
{
'name': "UOM Alert",
'summary': "Alerts when product quantity input in a sale order does not match UoM rounding precision.",
'author': "My Company",
'website': "https://genkiware.com",
'category': 'Sales',
'version': '1.0',
'depends': ['sale'],
'data': [
# 'security/ir.model.access.csv',
],
'installable': True,
}
Related Post
Happy Chinese New Year 2021! 牛年新年快樂!
願君喜迎新春牛,勤力敬業似耕牛,身強體壯賽公牛,心平氣和不鬥牛;愛情美滿勝牽牛,財運亨通賺金牛,春風得意莫吹牛,一年更比一年牛。 May you be prosperous! Wishing you prosperity and wealth!…
There are no reviews yet.