added capability to scan digikey barcodes and open the local part page based on the digikey part number or manufacturer part number

This commit is contained in:
jona 2024-12-20 11:05:22 +01:00 committed by Jan Böhmer
parent 9c99217dee
commit 18eb652cbc
9 changed files with 207 additions and 52 deletions

View file

@ -44,7 +44,7 @@ namespace App\Controller;
use App\Form\LabelSystem\ScanDialogType;
use App\Services\LabelSystem\Barcodes\BarcodeScanHelper;
use App\Services\LabelSystem\Barcodes\BarcodeRedirector;
use App\Services\LabelSystem\Barcodes\BarcodeScanResult;
use App\Services\LabelSystem\Barcodes\LocalBarcodeScanResult;
use App\Services\LabelSystem\Barcodes\BarcodeSourceType;
use Doctrine\ORM\EntityNotFoundException;
use InvalidArgumentException;
@ -109,7 +109,7 @@ class ScanController extends AbstractController
throw new InvalidArgumentException('Unknown type: '.$type);
}
//Construct the scan result manually, as we don't have a barcode here
$scan_result = new BarcodeScanResult(
$scan_result = new LocalBarcodeScanResult(
target_type: BarcodeScanHelper::QR_TYPE_MAP[$type],
target_id: $id,
//The routes are only used on the internal generated QR codes

View file

@ -71,7 +71,8 @@ class ScanDialogType extends AbstractType
null => 'scan_dialog.mode.auto',
BarcodeSourceType::INTERNAL => 'scan_dialog.mode.internal',
BarcodeSourceType::IPN => 'scan_dialog.mode.ipn',
BarcodeSourceType::VENDOR => 'scan_dialog.mode.vendor',
BarcodeSourceType::USER_DEFINED => 'scan_dialog.mode.vendor',
BarcodeSourceType::VENDOR => 'Interpret vendor (digikey for now)' //TODO:translate
},
]);

View file

@ -42,6 +42,7 @@ declare(strict_types=1);
namespace App\Services\LabelSystem\Barcodes;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;
@ -60,12 +61,22 @@ final class BarcodeRedirector
/**
* Determines the URL to which the user should be redirected, when scanning a QR code.
*
* @param BarcodeScanResult $barcodeScan The result of the barcode scan
* @param LocalBarcodeScanResult | VendorBarcodeScanResult $barcodeScan The result of the barcode scan
* @return string the URL to which should be redirected
*
* @throws EntityNotFoundException
*/
public function getRedirectURL(BarcodeScanResult $barcodeScan): string
public function getRedirectURL(LocalBarcodeScanResult | VendorBarcodeScanResult $barcodeScan): string
{
if($barcodeScan instanceof LocalBarcodeScanResult) {
return $this->getURLLocalBarcode($barcodeScan);
}
else{
return $this->getURLVendorBarcode($barcodeScan);
}
}
private function getURLLocalBarcode(LocalBarcodeScanResult $barcodeScan): string
{
switch ($barcodeScan->target_type) {
case LabelSupportedElement::PART:
@ -86,4 +97,46 @@ final class BarcodeRedirector
throw new InvalidArgumentException('Unknown $type: '.$barcodeScan->target_type->name);
}
}
/**
* Gets the URL to a part from a scan of a Vendor Barcode
*/
private function getURLVendorBarcode(VendorBarcodeScanResult $barcodeScan): string
{
$part = $this->getPartFromVendor($barcodeScan);
return $this->urlGenerator->generate('app_part_show', ['id' => $part->getID()]);
}
/**
* Gets a part from a scan of a Vendor Barcode by filtering for parts
* with the same Info Provider Id or, if that fails, by looking for parts with the
* same manufacturer product number. Only returns the first matching part.
*/
private function getPartFromVendor(VendorBarcodeScanResult $barcodeScan) : Part
{
// first check via the info provider ID (e.g. Vendor ID). This might fail if the part was not added via
// the info provider system or if the part was bought from a different vendor than the data was retrieved
// from.
$qb = $this->em->getRepository(Part::class)->createQueryBuilder('part');
//Lower() to be case insensitive
$qb->where($qb->expr()->like('LOWER(part.providerReference.provider_id)', 'LOWER(:vendor_id)'));
$qb->setParameter('vendor_id', $barcodeScan->vendor_part_number);
$results = $qb->getQuery()->getResult();
if($results){
return $results[0];
}
//Fallback to the manufacturer part number. This may return false positives, since it is common for
//multiple manufacturers to use the same part number for their version of a common product
//We assume the user is able to realize when this returns the wrong part
//If there's barcodes that contain the vendor we could make this more robust, but at least Digikey doesn't.
$qb = $this->em->getRepository(Part::class)->createQueryBuilder('part');
$qb->where($qb->expr()->like('LOWER(part.manufacturer_product_number)', 'LOWER(:mpn)'));
$qb->setParameter('mpn', $barcodeScan->manufacturer_part_number);
$results = $qb->getQuery()->getResult();
if($results){
return $results[0];
}
throw new EntityNotFoundException();
}
}

View file

@ -75,20 +75,23 @@ final class BarcodeScanHelper
* will try to guess the type.
* @param string $input
* @param BarcodeSourceType|null $type
* @return BarcodeScanResult
* @return LocalBarcodeScanResult
*/
public function scanBarcodeContent(string $input, ?BarcodeSourceType $type = null): BarcodeScanResult
public function scanBarcodeContent(string $input, ?BarcodeSourceType $type = null): LocalBarcodeScanResult | VendorBarcodeScanResult
{
//Do specific parsing
if ($type === BarcodeSourceType::INTERNAL) {
return $this->parseInternalBarcode($input) ?? throw new InvalidArgumentException('Could not parse barcode');
}
if ($type === BarcodeSourceType::VENDOR) {
return $this->parseVendorBarcode($input) ?? throw new InvalidArgumentException('Could not parse barcode');
if ($type === BarcodeSourceType::USER_DEFINED) {
return $this->parseUserDefinedBarcode($input) ?? throw new InvalidArgumentException('Could not parse barcode');
}
if ($type === BarcodeSourceType::IPN) {
return $this->parseIPNBarcode($input) ?? throw new InvalidArgumentException('Could not parse barcode');
}
if ($type === BarcodeSourceType::VENDOR) {
return $this->parseDigikeyBarcode($input) ?? throw new InvalidArgumentException('Could not parse barcode');
}
//Null means auto and we try the different formats
$result = $this->parseInternalBarcode($input);
@ -97,8 +100,8 @@ final class BarcodeScanHelper
return $result;
}
//Try to parse as vendor barcode
$result = $this->parseVendorBarcode($input);
//Try to parse as User defined barcode
$result = $this->parseUserDefinedBarcode($input);
if ($result !== null) {
return $result;
}
@ -109,10 +112,63 @@ final class BarcodeScanHelper
return $result;
}
$result = $this->parseDigikeyBarcode($input);
if ($result !== null) {
return $result;
}
throw new InvalidArgumentException('Unknown barcode');
}
private function parseVendorBarcode(string $input): ?BarcodeScanResult
private function parseDigikeyBarcode(string $input): ?VendorBarcodeScanResult
{
if(!str_starts_with($input, '[)>\u{1E}06\u{1D}')){
return null;
}
$barcodeParts = explode('\u{1D}',$input);
if (count($barcodeParts) !== 16){
return null;
}
$fieldIds = [
['id' => '', 'name' => ''],
['id' => 'P', 'name' => 'Customer Part Number'],
['id' => '1P', 'name' => 'Supplier Part Number'],
['id' => '30P','name' => 'Digikey Part Number'],
['id' => 'K', 'name' => 'Purchase Order Part Number'],
['id' => '1K', 'name' => 'Digikey Sales Order Number'],
['id' => '10K','name' => 'Digikey Invoice Number'],
['id' => '9D', 'name' => 'Date Code'],
['id' => '1T', 'name' => 'Lot Code'],
['id' => '11K','name' => 'Packing List Number'],
['id' => '4L', 'name' => 'Country of Origin'],
['id' => 'Q', 'name' => 'Quantity'],
['id' => '11Z','name' => 'Label Type'],
['id' => '12Z','name' => 'Part ID'],
['id' => '13Z','name' => 'NA'],
['id' => '20Z','name' => 'Padding']
];
$results = [];
foreach ($barcodeParts as $index => $part) {
$fieldProps = $fieldIds[$index];
if(!str_starts_with($part, $fieldProps['id'])){
return null;
}
$results[$fieldProps['name']] = substr($part, strlen($fieldProps['id']));
}
return new VendorBarcodeScanResult(
'Digikey',
$results['Supplier Part Number'],
$results['Digikey Part Number'],
$results['Date Code'],
$results['Quantity']
);
}
private function parseUserDefinedBarcode(string $input): ?LocalBarcodeScanResult
{
$lot_repo = $this->entityManager->getRepository(PartLot::class);
//Find only the first result
@ -124,14 +180,14 @@ final class BarcodeScanHelper
//We found a part, so use it to create the result
$lot = $results[0];
return new BarcodeScanResult(
return new LocalBarcodeScanResult(
target_type: LabelSupportedElement::PART_LOT,
target_id: $lot->getID(),
source_type: BarcodeSourceType::VENDOR
source_type: BarcodeSourceType::USER_DEFINED
);
}
private function parseIPNBarcode(string $input): ?BarcodeScanResult
private function parseIPNBarcode(string $input): ?LocalBarcodeScanResult
{
$part_repo = $this->entityManager->getRepository(Part::class);
//Find only the first result
@ -143,7 +199,7 @@ final class BarcodeScanHelper
//We found a part, so use it to create the result
$part = $results[0];
return new BarcodeScanResult(
return new LocalBarcodeScanResult(
target_type: LabelSupportedElement::PART,
target_id: $part->getID(),
source_type: BarcodeSourceType::IPN
@ -155,9 +211,9 @@ final class BarcodeScanHelper
* If the barcode could not be parsed at all, null is returned. If the barcode is a valid format, but could
* not be found in the database, an exception is thrown.
* @param string $input
* @return BarcodeScanResult|null
* @return LocalBarcodeScanResult|null
*/
private function parseInternalBarcode(string $input): ?BarcodeScanResult
private function parseInternalBarcode(string $input): ?LocalBarcodeScanResult
{
$input = trim($input);
$matches = [];
@ -167,7 +223,7 @@ final class BarcodeScanHelper
//Extract parts from QR code's URL
if (preg_match('#^https?://.*/scan/(\w+)/(\d+)/?$#', $input, $matches)) {
return new BarcodeScanResult(
return new LocalBarcodeScanResult(
target_type: self::QR_TYPE_MAP[strtolower($matches[1])],
target_id: (int) $matches[2],
source_type: BarcodeSourceType::INTERNAL
@ -183,7 +239,7 @@ final class BarcodeScanHelper
throw new InvalidArgumentException('Unknown prefix '.$prefix);
}
return new BarcodeScanResult(
return new LocalBarcodeScanResult(
target_type: self::PREFIX_TYPE_MAP[$prefix],
target_id: $id,
source_type: BarcodeSourceType::INTERNAL
@ -199,7 +255,7 @@ final class BarcodeScanHelper
throw new InvalidArgumentException('Unknown prefix '.$prefix);
}
return new BarcodeScanResult(
return new LocalBarcodeScanResult(
target_type: self::PREFIX_TYPE_MAP[$prefix],
target_id: $id,
source_type: BarcodeSourceType::INTERNAL
@ -208,7 +264,7 @@ final class BarcodeScanHelper
//Legacy Part-DB location labels used $L00336 format
if (preg_match('#^\$L(\d{5,})$#', $input, $matches)) {
return new BarcodeScanResult(
return new LocalBarcodeScanResult(
target_type: LabelSupportedElement::STORELOCATION,
target_id: (int) $matches[1],
source_type: BarcodeSourceType::INTERNAL
@ -217,7 +273,7 @@ final class BarcodeScanHelper
//Legacy Part-DB used EAN8 barcodes for part labels. Format 0000001(2) (note the optional 8th digit => checksum)
if (preg_match('#^(\d{7})\d?$#', $input, $matches)) {
return new BarcodeScanResult(
return new LocalBarcodeScanResult(
target_type: LabelSupportedElement::PART,
target_id: (int) $matches[1],
source_type: BarcodeSourceType::INTERNAL

View file

@ -36,5 +36,7 @@ enum BarcodeSourceType
* This barcode is a custom barcode from a third party like a vendor, which was set via the vendor_barcode
* field of a part lot.
*/
case USER_DEFINED;
case VENDOR;
}

View file

@ -26,9 +26,10 @@ namespace App\Services\LabelSystem\Barcodes;
use App\Entity\LabelSystem\LabelSupportedElement;
/**
* This class represents the result of a barcode scan, with the target type and the ID of the element
* This class represents the result of a barcode scan of a barcode that uniquely identifies a local entity,
* like an internally generated barcode or a barcode that was added manually to the system by a user
*/
class BarcodeScanResult
class LocalBarcodeScanResult
{
public function __construct(
public readonly LabelSupportedElement $target_type,

View file

@ -0,0 +1,42 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Services\LabelSystem\Barcodes;
/**
* This class represents the result of a scan of a barcode that was printed by a third party
* and contains useful information about an item, like a vendor id or the order quantity
*/
class VendorBarcodeScanResult
{
public function __construct(
public readonly string $vendor,
public readonly ?string $manufacturer_part_number = null,
public readonly ?string $vendor_part_number = null,
public readonly ?string $date_code = null,
public readonly ?string $quantity = null
)
{
}
}

View file

@ -43,7 +43,7 @@ namespace App\Tests\Services\LabelSystem\Barcodes;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Services\LabelSystem\Barcodes\BarcodeRedirector;
use App\Services\LabelSystem\Barcodes\BarcodeScanResult;
use App\Services\LabelSystem\Barcodes\LocalBarcodeScanResult;
use App\Services\LabelSystem\Barcodes\BarcodeSourceType;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@ -60,17 +60,17 @@ final class BarcodeRedirectorTest extends KernelTestCase
public static function urlDataProvider(): \Iterator
{
yield [new BarcodeScanResult(LabelSupportedElement::PART, 1, BarcodeSourceType::INTERNAL), '/en/part/1'];
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 1, BarcodeSourceType::INTERNAL), '/en/part/1'];
//Part lot redirects to Part info page (Part lot 1 is associated with part 3)
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 1, BarcodeSourceType::INTERNAL), '/en/part/3'];
yield [new BarcodeScanResult(LabelSupportedElement::STORELOCATION, 1, BarcodeSourceType::INTERNAL), '/en/store_location/1/parts'];
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 1, BarcodeSourceType::INTERNAL), '/en/part/3'];
yield [new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 1, BarcodeSourceType::INTERNAL), '/en/store_location/1/parts'];
}
/**
* @dataProvider urlDataProvider
* @group DB
*/
public function testGetRedirectURL(BarcodeScanResult $scanResult, string $url): void
public function testGetRedirectURL(LocalBarcodeScanResult $scanResult, string $url): void
{
$this->assertSame($url, $this->service->getRedirectURL($scanResult));
}
@ -79,7 +79,7 @@ final class BarcodeRedirectorTest extends KernelTestCase
{
$this->expectException(EntityNotFoundException::class);
//If we encounter an invalid lot, we must throw an exception
$this->service->getRedirectURL(new BarcodeScanResult(LabelSupportedElement::PART_LOT,
$this->service->getRedirectURL(new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT,
12_345_678, BarcodeSourceType::INTERNAL));
}
}

View file

@ -43,7 +43,7 @@ namespace App\Tests\Services\LabelSystem\Barcodes;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Services\LabelSystem\Barcodes\BarcodeScanHelper;
use App\Services\LabelSystem\Barcodes\BarcodeScanResult;
use App\Services\LabelSystem\Barcodes\LocalBarcodeScanResult;
use App\Services\LabelSystem\Barcodes\BarcodeSourceType;
use Com\Tecnick\Barcode\Barcode;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -61,55 +61,55 @@ class BarcodeScanHelperTest extends WebTestCase
public static function dataProvider(): \Iterator
{
//QR URL content:
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 1, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 1, BarcodeSourceType::INTERNAL),
'https://localhost:8000/scan/lot/1'];
yield [new BarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
'https://localhost:8000/scan/part/123'];
yield [new BarcodeScanResult(LabelSupportedElement::STORELOCATION, 4, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 4, BarcodeSourceType::INTERNAL),
'http://foo.bar/part-db/scan/location/4'];
//Current Code39 format:
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 10, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 10, BarcodeSourceType::INTERNAL),
'L0010'];
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 123, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 123, BarcodeSourceType::INTERNAL),
'L0123'];
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 123456, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 123456, BarcodeSourceType::INTERNAL),
'L123456'];
yield [new BarcodeScanResult(LabelSupportedElement::PART, 2, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 2, BarcodeSourceType::INTERNAL),
'P0002'];
//Development phase Code39 barcodes:
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 10, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 10, BarcodeSourceType::INTERNAL),
'L-000010'];
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 10, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 10, BarcodeSourceType::INTERNAL),
'Lß000010'];
yield [new BarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
'P-000123'];
yield [new BarcodeScanResult(LabelSupportedElement::STORELOCATION, 123, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 123, BarcodeSourceType::INTERNAL),
'S-000123'];
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 12_345_678, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 12_345_678, BarcodeSourceType::INTERNAL),
'L-12345678'];
//Legacy storelocation format
yield [new BarcodeScanResult(LabelSupportedElement::STORELOCATION, 336, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 336, BarcodeSourceType::INTERNAL),
'$L00336'];
yield [new BarcodeScanResult(LabelSupportedElement::STORELOCATION, 12_345_678, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::STORELOCATION, 12_345_678, BarcodeSourceType::INTERNAL),
'$L12345678'];
//Legacy Part format
yield [new BarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
'0000123'];
yield [new BarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 123, BarcodeSourceType::INTERNAL),
'00001236'];
yield [new BarcodeScanResult(LabelSupportedElement::PART, 1_234_567, BarcodeSourceType::INTERNAL),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 1_234_567, BarcodeSourceType::INTERNAL),
'12345678'];
//Test IPN barcode
yield [new BarcodeScanResult(LabelSupportedElement::PART, 2, BarcodeSourceType::IPN),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART, 2, BarcodeSourceType::IPN),
'IPN123'];
//Test vendor barcode
yield [new BarcodeScanResult(LabelSupportedElement::PART_LOT, 2,BarcodeSourceType::VENDOR),
yield [new LocalBarcodeScanResult(LabelSupportedElement::PART_LOT, 2,BarcodeSourceType::USER_DEFINED),
'lot2_vendor_barcode'];
}
@ -131,7 +131,7 @@ class BarcodeScanHelperTest extends WebTestCase
/**
* @dataProvider dataProvider
*/
public function testNormalizeBarcodeContent(BarcodeScanResult $expected, string $input): void
public function testNormalizeBarcodeContent(LocalBarcodeScanResult $expected, string $input): void
{
$this->assertEquals($expected, $this->service->scanBarcodeContent($input));
}