Skip to Content
Menu
This question has been flagged
2 Replies
397 Views

In Odoo Studio, I want to create an automatic action that prevents a Manufacturing Order (MO) from being produced if the components are not available in stock. Can you help me with the Python code for this?

Avatar
Discard
Best Answer

Setup in Odoo

  1. Go to Developer Mode (activate it if not already).
  2. Navigate to Technical > Automation > Automated Actions.
  3. Create a new Automated Action:
    • Model: Manufacturing Order (mrp.production)
    • Trigger: On Update or On Creation & Update (depending on use case)
    • Filter Domain (optional): You may filter on state = 'confirmed' or similar.
    • Action To Do: Execute Python Code

Python Code Example

Here’s the Python code that can be used in the automated action to prevent production if components are missing:

if record.state == 'confirmed':
    for move in record.move_raw_ids:
        if move.product_id.type == 'product' and move.product_uom_qty > move.reserved_availability:
            raise UserError(f"Not enough stock for component '{move.product_id.display_name}'.\n"
                            f"Required: {move.product_uom_qty}, Reserved: {move.reserved_availability}")

Avatar
Discard
Best Answer

There is no need for custom Python code to achieve this.

You set this up on the Bill of Material. In the 'Miscellaneous' tab, under 'Manufacturing Readiness', set this to 'When all components are available'. This should also be the default setting on a BoM.

Avatar
Discard