71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
"""The BarcodePlugin is meant to integrate the Barcodes into InvenTree.
|
|
|
|
This plugin can currently only match barcodes to supplier parts.
|
|
"""
|
|
|
|
import re
|
|
import json
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from plugin import InvenTreePlugin
|
|
from plugin.mixins import SettingsMixin, SupplierBarcodeMixin
|
|
|
|
|
|
class ReicheltPlugin(SupplierBarcodeMixin, SettingsMixin, InvenTreePlugin):
|
|
"""Plugin to integrate the Barcodes into InvenTree."""
|
|
|
|
NAME = 'ReicheltBarcodePlugin'
|
|
SLUG = 'Reicheltplugin'
|
|
TITLE = _('Supplier Integration - Reichelt')
|
|
DESCRIPTION = _('Provides support for scanning Reichelt barcodes')
|
|
VERSION = '1.0.0'
|
|
AUTHOR = _('BlubbFish')
|
|
|
|
DEFAULT_SUPPLIER_NAME = 'Reichelt'
|
|
SETTINGS = {
|
|
'SUPPLIER_ID': {
|
|
'name': _('Supplier'),
|
|
'description': _("The Supplier which acts as 'Reichelt'"),
|
|
'model': 'company.company',
|
|
'model_filters': {'is_supplier': True},
|
|
}
|
|
}
|
|
|
|
# Custom field mapping for LCSC barcodes
|
|
BARCODE_FIELDS = {
|
|
'K': SupplierBarcodeMixin.CUSTOMER_ORDER_NUMBER,
|
|
'1K': SupplierBarcodeMixin.SUPPLIER_ORDER_NUMBER,
|
|
'11K': SupplierBarcodeMixin.PACKING_LIST_NUMBER,
|
|
'6D': SupplierBarcodeMixin.SHIP_DATE,
|
|
'9D': SupplierBarcodeMixin.DATE_CODE,
|
|
'10D': SupplierBarcodeMixin.DATE_CODE,
|
|
'4K': SupplierBarcodeMixin.PURCHASE_ORDER_LINE,
|
|
'14K': SupplierBarcodeMixin.PURCHASE_ORDER_LINE,
|
|
'P': SupplierBarcodeMixin.SUPPLIER_PART_NUMBER,
|
|
'1P': SupplierBarcodeMixin.MANUFACTURER_PART_NUMBER,
|
|
'30P': SupplierBarcodeMixin.SUPPLIER_PART_NUMBER,
|
|
'1T': SupplierBarcodeMixin.LOT_CODE,
|
|
'4L': SupplierBarcodeMixin.COUNTRY_OF_ORIGIN,
|
|
'1V': SupplierBarcodeMixin.MANUFACTURER,
|
|
'Q': SupplierBarcodeMixin.QUANTITY,
|
|
}
|
|
|
|
def extract_barcode_fields(self, barcode_data: str) -> dict[str, str]:
|
|
"""Get supplier_part and barcode_fields from LCSC QR-Code.
|
|
|
|
Example QR-Code: {"K":"KA-0077","1K":"028-3720301-6481114","6D":"20260219","P":"B0977HLT4P","1P":"SDCZ48-064G-G46T","4K":"1","Q":0.0,"10D":"2601","1T":"1","4L":"DE"}
|
|
"""
|
|
try:
|
|
jsonData = json.loads(barcode_data)
|
|
except ValueError as err:
|
|
return {}
|
|
|
|
barcode_fields = {}
|
|
|
|
# Map from LCSC field names to standard field names
|
|
for key, field in self.BARCODE_FIELDS.items():
|
|
if key in jsonData.keys():
|
|
barcode_fields[field] = jsonData[key]
|
|
|
|
return barcode_fields |