Skip to main content

Overview

The Stock Request framework extends the standard Odoo procurement.group model to ensure that procurements generated from stock requests maintain proper origin traceability. When a procurement is triggered by a stock request that belongs to an order, the extension updates the procurement’s origin to reference the stock request order name.

Model Information

_inherit
string
procurement.group
_name
string
procurement.group

Methods

run
method
Extends the procurement run method to update the origin of procurements that are linked to stock request orders.Parameters:
  • procurements (list): List of procurement namedtuples to process
  • raise_user_error (bool, default=True): Whether to raise user-facing errors
Returns: Result from parent run methodProcess Flow:
  1. Iterates through all procurements in the list
  2. Checks if each procurement has a stock_request_id in its values
  3. If the stock request belongs to an order, creates a new procurement with updated origin
  4. Replaces original procurements with updated ones
  5. Calls the parent run method with the modified procurement list
@api.model
def run(self, procurements, raise_user_error=True):
    indexes_to_pop = []
    new_procs = []
    for i, procurement in enumerate(procurements):
        if "stock_request_id" in procurement.values and procurement.values.get(
            "stock_request_id"
        ):
            req = self.env["stock.request"].browse(
                procurement.values.get("stock_request_id")
            )
            if req.order_id:
                new_procs.append(procurement._replace(origin=req.order_id.name))
                indexes_to_pop.append(i)
    if new_procs:
        indexes_to_pop.reverse()
        for index in indexes_to_pop:
            procurements.pop(index)
        procurements.extend(new_procs)
    return super().run(procurements, raise_user_error=raise_user_error)

Purpose and Behavior

Origin Traceability

The extension ensures proper traceability by updating procurement origins when they’re generated from stock requests:

Processing Logic

# Procurement with stock_request_id in values
procurement = Procurement(
    product_id=product,
    product_qty=10.0,
    values={
        'stock_request_id': 123,  # Stock request ID
        # ... other values
    }
)

# The stock request has an order
stock_request = env['stock.request'].browse(123)
stock_request.order_id  # Returns stock.request.order record

# After run() method:
# - Original procurement is replaced
# - New procurement has origin = stock_request.order_id.name

Technical Details

Index Management

The method uses a reverse-indexed approach to safely modify the procurements list:
  1. Forward iteration: Identifies procurements to replace and stores their indexes
  2. Reverse pop: Removes original procurements starting from the highest index to avoid index shifting issues
  3. Extension: Adds new procurements with updated origins to the end of the list
Reversing the index list before popping is crucial to prevent index shifting errors when removing multiple items from a list.

Procurement Values

The extension looks for a specific key in the procurement values dictionary:
stock_request_id
integer
ID of the stock request that triggered this procurement. This value is added by the Stock Request framework when creating procurements.

Usage Examples

Standard Procurement Flow

# Running procurements with stock requests
ProcurementGroup = self.env['procurement.group']

procurements = [
    Procurement(
        product_id=product1,
        product_qty=10.0,
        product_uom=uom,
        location_id=location,
        name='Stock Request Line',
        origin='SR001',
        company_id=company,
        values={
            'stock_request_id': 123,
            'group_id': group,
            # ... other values
        }
    ),
    # ... more procurements
]

# Run procurements - origins will be updated automatically
ProcurementGroup.run(procurements)

Checking Procurement Origin

# After procurement.group.run() is called:
# Procurements linked to stock request orders will have
# their origin updated to the order name

stock_request = self.env['stock.request'].browse(123)
if stock_request.order_id:
    expected_origin = stock_request.order_id.name
    # The generated purchase order, manufacturing order, etc.
    # will show this origin for traceability

Integration Points

Stock Request Procurement

When stock requests generate procurements:
  1. The stock_request_id is added to procurement values
  2. The procurement group run method is called
  3. This extension updates origins for better traceability

Downstream Documents

The updated origin flows to downstream documents:
  • Purchase Orders: Origin shows stock request order name
  • Manufacturing Orders: Origin shows stock request order name
  • Stock Moves: Origin maintains traceability to the order
This extension improves the audit trail by ensuring all documents generated from a stock request order reference the order name rather than individual request names.

Source Code References

  • Stock Request extensions: stock_request/models/procurement_group.py

Build docs developers (and LLMs) love