Understanding the Default Behavior:
In Odoo, the default behavior upon confirming a sale order is the automatic creation of a delivery order (picking) for the associated products. This process is facilitated by the stock rules mechanism, and the delivery is triggered during the execution of the action_confirm() method on the sale order. However, there are specific business scenarios where this automatic delivery creation may not be desirable—for instance, when dealing with special types of orders that shouldn’t trigger a delivery immediately, when deliveries are intended to be created manually at a later stage, or in cases where the sale order contains only service-type products that do not require physical shipment. To handle such exceptions elegantly, Odoo’s context mechanism can be leveraged.
When You Want to Skip Delivery Creation:
There are scenarios where you might want to confirm a sale order but not create outbound Delivery automatically:
- For any special order types which you have in your business where you dont want to create delivery upfront.
- When you want to manually create deliveries later
- For orders that don’t require physical shipment, other than service items.
Using Context to Control Delivery Creation:
The provided code shows an elegant solution using Odoo’s context mechanism to skip delivery creation:
1. Overriding action_confirm():
def action_confirm(self):
"""Confirm sale order and force delivery creation if skipped via context."""
res = super(
SaleOrder, self.with_context(prevent_delivery_creation=True)
).action_confirm()
- We override the standard action_confirm() method
- We call the parent method but with a modified context
- The prevent_delivery_creation=True context flag will be passed down
2. Modifying _action_launch_stock_rule()
def _action_launch_stock_rule(self, previous_product_uom_qty=False):
"""Override to prevent delivery creation."""
# Check if delivery creation should be skipped
if self.env.context.get("prevent_delivery_creation", False):
return # Skip the stock rule logic entirely
if self.env.context.get("prevent_stock_rule", False):
return
# Otherwise, execute the original behavior
return super(SaleOrderLine, self)._action_launch_stock_rule(
previous_product_uom_qty
)
This method normally triggers the stock rules that create deliveries
- If prevent_delivery_creation is in the context
- If prevent_stock_rule is in the context
- If none of these conditions are met, it falls back to the standard behavior
Conclusion:
This approach provides a clean and non-intrusive method to conditionally prevent the creation of delivery orders when confirming a sale order in Odoo. By utilizing Odoo’s robust context mechanism, developers can control the delivery workflow dynamically based on specific business needs.It allows you to maintain the default delivery creation process for the majority of sale orders, ensuring that standard operations remain unaffected. At the same time, it offers the flexibility to introduce exceptions for custom order types or specific business scenarios without altering the core logic.
Moreover, this technique helps avoid excessive customization in the stock or sales modules, thereby preserving the integrity of Odoo’s standard architecture. The use of context flags like prevent_delivery_creation keeps your business logic modular, easy to manage, and more resilient to future Odoo upgrades.