Allow to read amazon labels for part retrieval and creation

This commit is contained in:
Jan Böhmer 2026-02-22 23:16:39 +01:00
parent 87919eb445
commit 0b9b2cbf58
7 changed files with 103 additions and 9 deletions

View file

@ -72,12 +72,7 @@ class ScanDialogType extends AbstractType
'placeholder' => 'scan_dialog.mode.auto',
'choice_label' => fn (?BarcodeSourceType $enum) => match($enum) {
null => 'scan_dialog.mode.auto',
BarcodeSourceType::INTERNAL => 'scan_dialog.mode.internal',
BarcodeSourceType::IPN => 'scan_dialog.mode.ipn',
BarcodeSourceType::USER_DEFINED => 'scan_dialog.mode.user',
BarcodeSourceType::EIGP114 => 'scan_dialog.mode.eigp',
BarcodeSourceType::GTIN => 'scan_dialog.mode.gtin',
BarcodeSourceType::LCSC => 'scan_dialog.mode.lcsc',
default => 'scan_dialog.mode.' . $enum->value,
},
]);

View file

@ -454,4 +454,28 @@ class PartRepository extends NamedDBElementRepository
return $qb->getQuery()->getOneOrNullResult();
}
/**
* Finds a part based on the provided SPN (Supplier Part Number), with an option for case sensitivity.
* If no part is found with the given SPN, null is returned.
* @param string $spn
* @param bool $caseInsensitive
* @return Part|null
*/
public function getPartBySPN(string $spn, bool $caseInsensitive = true): ?Part
{
$qb = $this->createQueryBuilder('part');
$qb->select('part');
$qb->leftJoin('part.orderdetails', 'o');
if ($caseInsensitive) {
$qb->where("LOWER(o.supplierpartnr) = LOWER(:spn)");
} else {
$qb->where("o.supplierpartnr = :spn");
}
$qb->setParameter('spn', $spn);
return $qb->getQuery()->getOneOrNullResult();
}
}

View file

@ -0,0 +1,46 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 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\BarcodeScanner;
final readonly class AmazonBarcodeScanResult implements BarcodeScanResultInterface
{
public function __construct(public string $asin) {
if (!self::isAmazonBarcode($asin)) {
throw new \InvalidArgumentException("The provided input '$asin' is not a valid Amazon barcode (ASIN)");
}
}
public static function isAmazonBarcode(string $input): bool
{
//Amazon barcodes are 10 alphanumeric characters
return preg_match('/^[A-Z0-9]{10}$/i', $input) === 1;
}
public function getDecodedForInfoMode(): array
{
return [
'ASIN' => $this->asin,
];
}
}

View file

@ -101,6 +101,10 @@ final class BarcodeScanHelper
return $this->parseLCSCBarcode($input);
}
if ($type === BarcodeSourceType::AMAZON) {
return new AmazonBarcodeScanResult($input);
}
//Null means auto and we try the different formats
$result = $this->parseInternalBarcode($input);
@ -135,6 +139,11 @@ final class BarcodeScanHelper
return $this->parseLCSCBarcode($input);
}
//Try amazon barcode
if (AmazonBarcodeScanResult::isAmazonBarcode($input)) {
return new AmazonBarcodeScanResult($input);
}
throw new InvalidArgumentException('Unknown barcode');
}

View file

@ -144,7 +144,12 @@ final readonly class BarcodeScanResultHandler
return $this->resolvePartFromLCSC($barcodeScan);
}
throw new \InvalidArgumentException("Barcode does not support resolving to a local entity: ".get_class($barcodeScan));
if ($barcodeScan instanceof AmazonBarcodeScanResult) {
return $this->em->getRepository(Part::class)->getPartByProviderInfo($barcodeScan->asin)
?? $this->em->getRepository(Part::class)->getPartBySPN($barcodeScan->asin);
}
return null;
}
/**
@ -258,6 +263,13 @@ final readonly class BarcodeScanResultHandler
return $this->getCreationInfoForEIGP114($scanResult);
}
if ($scanResult instanceof AmazonBarcodeScanResult) {
return [
'providerKey' => 'canopy',
'providerId' => $scanResult->asin,
];
}
return null;
}

View file

@ -36,12 +36,12 @@ enum BarcodeSourceType: string
/**
* This barcode is a user defined barcode defined on a part lot
*/
case USER_DEFINED = 'user_defined';
case USER_DEFINED = 'user';
/**
* EIGP114 formatted barcodes like used by digikey, mouser, etc.
*/
case EIGP114 = 'eigp114';
case EIGP114 = 'eigp';
/**
* GTIN /EAN barcodes, which are used on most products in the world. These are checked with the GTIN field of a part.
@ -50,4 +50,6 @@ enum BarcodeSourceType: string
/** For LCSC.com formatted QR codes */
case LCSC = 'lcsc';
case AMAZON = 'amazon';
}

View file

@ -12563,5 +12563,11 @@ Buerklin-API Authentication server:
<target>Create [part] from barcode</target>
</segment>
</unit>
<unit id="8WZYwRJ" name="scan_dialog.mode.amazon">
<segment>
<source>scan_dialog.mode.amazon</source>
<target>Amazon barcode</target>
</segment>
</unit>
</file>
</xliff>