Skip to content

Barcode Mixin

Barcode Plugins

InvenTree supports decoding of arbitrary barcode data via a Barcode Plugin interface. Barcode data POSTed to the /api/barcode/ endpoint will be supplied to all loaded barcode plugins, and the first plugin to successfully interpret the barcode data will return a response to the client.

InvenTree can generate native QR codes to represent database objects (e.g. a single StockItem). This barcode can then be used to perform quick lookup of a stock item or location in the database. A client application (for example the InvenTree mobile app) scans a barcode, and sends the barcode data to the InvenTree server. The server then uses the InvenTreeBarcodePlugin (found at src/backend/InvenTree/plugin/builtin/barcodes/inventree_barcode.py) to decode the supplied barcode data.

Any third-party barcodes can be decoded by writing a matching plugin to decode the barcode data. These plugins could then perform a server-side action or render a JSON response back to the client for further action.

Some examples of possible uses for barcode integration:

  • Stock lookup by scanning a barcode on a box of items
  • Receiving goods against a PurchaseOrder by scanning a supplier barcode
  • Perform a stock adjustment action (e.g. take 10 parts from stock whenever a barcode is scanned)

Barcode data are POSTed to the server as follows:

POST {
    barcode_data: "[(>someBarcodeDataWhichThePluginKnowsHowToDealWith"
}

Builtin Plugin

The InvenTree server includes a builtin barcode plugin which can decode QR codes generated by the server. This plugin is enabled by default.

Builtin BarcodePlugin for matching and generating internal barcodes.

Source code in src/backend/InvenTree/plugin/builtin/barcodes/inventree_barcode.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class InvenTreeInternalBarcodePlugin(BarcodeMixin, InvenTreePlugin):
    """Builtin BarcodePlugin for matching and generating internal barcodes."""

    NAME = 'InvenTreeBarcode'
    TITLE = _('InvenTree Barcodes')
    DESCRIPTION = _('Provides native support for barcodes')
    VERSION = '2.0.0'
    AUTHOR = _('InvenTree contributors')

    @staticmethod
    def get_supported_barcode_models():
        """Returns a list of database models which support barcode functionality."""
        return getModelsWithMixin(InvenTreeBarcodeMixin)

    def format_matched_response(self, label, model, instance):
        """Format a response for the scanned data."""
        return {label: instance.format_matched_response()}

    def scan(self, barcode_data):
        """Scan a barcode against this plugin.

        Here we are looking for a dict object which contains a reference to a particular InvenTree database object
        """
        # Attempt to coerce the barcode data into a dict object
        # This is the internal barcode representation that InvenTree uses
        barcode_dict = None

        if type(barcode_data) is dict:
            barcode_dict = barcode_data
        elif type(barcode_data) is str:
            try:
                barcode_dict = json.loads(barcode_data)
            except json.JSONDecodeError:
                pass

        supported_models = self.get_supported_barcode_models()

        if barcode_dict is not None and type(barcode_dict) is dict:
            # Look for various matches. First good match will be returned
            for model in supported_models:
                label = model.barcode_model_type()

                if label in barcode_dict:
                    try:
                        pk = int(barcode_dict[label])
                        instance = model.objects.get(pk=pk)
                        return self.format_matched_response(label, model, instance)
                    except (ValueError, model.DoesNotExist):
                        pass

        # Create hash from raw barcode data
        barcode_hash = hash_barcode(barcode_data)

        # If no "direct" hits are found, look for assigned third-party barcodes
        for model in supported_models:
            label = model.barcode_model_type()

            instance = model.lookup_barcode(barcode_hash)

            if instance is not None:
                return self.format_matched_response(label, model, instance)

Example Plugin

Please find below a very simple example that is executed each time a barcode is scanned.

from django.utils.translation import gettext_lazy as _

from InvenTree.models import InvenTreeBarcodeMixin
from plugin import InvenTreePlugin
from plugin.mixins import BarcodeMixin

class InvenTreeBarcodePlugin(BarcodeMixin, InvenTreePlugin):

    NAME = "MyBarcode"
    TITLE = "My Barcodes"
    DESCRIPTION = "support for barcodes"
    VERSION = "0.0.1"
    AUTHOR = "Michael"

    status = 0

    def scan(self, barcode_data):

        self.status = self.status+1
        print('Started barcode plugin', self.status)
        print(barcode_data)
        response = {}
        return response

To try it just copy the file to src/InvenTree/plugins and restart the server. Open the scan barcode window and start to scan codes or type in text manually. Each time the timeout is hit the plugin will execute and printout the result. The timeout can be changed in Settings->Barcode Support->Barcode Input Delay.