Skip to content

Allocate Mixin

AllocateMixin

The AllocateMixin class enables plugins to customize how stock items are automatically allocated against build orders and sales orders.

When a user triggers "auto allocation" of stock against an order, InvenTree first determines a list of candidate stock items for each line, using the default allocation rules (e.g. in-stock, matching part / variant / substitute, location, serialization, etc). Before this candidate list is used to actually create the stock allocations, it is passed through any active plugins which implement the AllocateMixin class - allowing a plugin to filter, reorder, or otherwise adjust which stock items are used.

Multi Plugin Support

If multiple plugins are active which implement the AllocateMixin methods, they are called in turn - each plugin receives the (possibly already adjusted) output of the previous plugin.

Default Behavior

Neither method needs to be implemented by a plugin. If a method is not overridden - or if it returns None - the provided list of stock items is passed through unmodified.

Build Order Allocation

The filter_build_allocation method is called when automatically allocating stock against a build order - for both "tracked" and "untracked" stock items.

Note that this method is called once per candidate list - once for each BuildLine (untracked stock), and once for each tracked build output.

Filter the stock items available for auto-allocation against a build order.

Parameters:

Name Type Description Default
build_line Model

The BuildLine object which is being allocated against

required
stock_items list

A list of candidate StockItem objects, which have already been filtered against the default allocation rules (e.g. in-stock, matching part / variant / substitute, location, etc)

required

Returns:

Type Description
list

A list of StockItem objects to be used for auto-allocation.

The default implementation simply returns the provided list of stock items, unmodified.

Source code in src/backend/InvenTree/plugin/base/integration/AllocateMixin.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def filter_build_allocation(
    self, build_line: Model, stock_items: list, **kwargs
) -> list:
    """Filter the stock items available for auto-allocation against a build order.

    Arguments:
        build_line: The BuildLine object which is being allocated against
        stock_items: A list of candidate StockItem objects, which have already
            been filtered against the default allocation rules (e.g. in-stock,
            matching part / variant / substitute, location, etc)

    Returns:
        A list of StockItem objects to be used for auto-allocation.

    The default implementation simply returns the provided list of stock items,
    unmodified.
    """
    return stock_items

Sales Order Allocation

The filter_sales_order_allocation method is called when automatically allocating stock against a sales order.

Filter the stock items available for auto-allocation against a sales order.

Parameters:

Name Type Description Default
order_line Model

The SalesOrderLineItem object which is being allocated against

required
stock_items list

A list of candidate StockItem objects, which have already been filtered against the default allocation rules (e.g. in-stock, matching part, location, serialization, etc)

required

Returns:

Type Description
list

A list of StockItem objects to be used for auto-allocation.

The default implementation simply returns the provided list of stock items, unmodified.

Source code in src/backend/InvenTree/plugin/base/integration/AllocateMixin.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def filter_sales_order_allocation(
    self, order_line: Model, stock_items: list, **kwargs
) -> list:
    """Filter the stock items available for auto-allocation against a sales order.

    Arguments:
        order_line: The SalesOrderLineItem object which is being allocated against
        stock_items: A list of candidate StockItem objects, which have already
            been filtered against the default allocation rules (e.g. in-stock,
            matching part, location, serialization, etc)

    Returns:
        A list of StockItem objects to be used for auto-allocation.

    The default implementation simply returns the provided list of stock items,
    unmodified.
    """
    return stock_items

Sample Plugin

A sample plugin which implements custom allocation filtering is provided in the InvenTree source code. It excludes any stock item with a batch code of REJECT from being automatically allocated:

A sample plugin for demonstrating custom auto-allocation behavior.

Any stock item with a batch code of 'REJECT' is excluded from auto-allocation, for both build orders and sales orders.

Source code in src/backend/InvenTree/plugin/samples/integration/allocate_sample.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class SampleAllocatePlugin(AllocateMixin, InvenTreePlugin):
    """A sample plugin for demonstrating custom auto-allocation behavior.

    Any stock item with a batch code of 'REJECT' is excluded from
    auto-allocation, for both build orders and sales orders.
    """

    NAME = 'SampleAllocate'
    SLUG = 'sampleallocate'
    TITLE = 'Sample Allocate Plugin'
    DESCRIPTION = (
        'A sample plugin for demonstrating custom stock allocation functionality'
    )
    VERSION = '0.1.0'

    def filter_build_allocation(self, build_line, stock_items, **kwargs):
        """Exclude any stock item with a 'REJECT' batch code."""
        return [item for item in stock_items if item.batch != REJECT_BATCH_CODE]

    def filter_sales_order_allocation(self, order_line, stock_items, **kwargs):
        """Exclude any stock item with a 'REJECT' batch code."""
        return [item for item in stock_items if item.batch != REJECT_BATCH_CODE]