Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Services/LabelSystem/BarcodeScanner/BarcodeScanHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public function scanBarcodeContent(string $input, ?BarcodeSourceType $type = nul
return new AmazonBarcodeScanResult($input);
}

if ($type === BarcodeSourceType::TME) {
return TMEBarcodeScanResult::parse($input);
}

//Null means auto and we try the different formats
$result = $this->parseInternalBarcode($input);

Expand Down Expand Up @@ -144,6 +148,11 @@ public function scanBarcodeContent(string $input, ?BarcodeSourceType $type = nul
return new AmazonBarcodeScanResult($input);
}

// Try TME barcode
if (TMEBarcodeScanResult::isTMEBarcode($input)) {
return TMEBarcodeScanResult::parse($input);
}

throw new InvalidArgumentException('Unknown barcode');
}

Expand All @@ -162,6 +171,7 @@ private function parseLCSCBarcode(string $input): LCSCBarcodeScanResult
return LCSCBarcodeScanResult::parse($input);
}


private function parseUserDefinedBarcode(string $input): ?LocalBarcodeScanResult
{
$lot_repo = $this->entityManager->getRepository(PartLot::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public function resolveEntity(BarcodeScanResultInterface $barcodeScan): Part|Par
?? $this->em->getRepository(Part::class)->getPartBySPN($barcodeScan->asin);
}

if ($barcodeScan instanceof TMEBarcodeScanResult) {
return $this->resolvePartFromTME($barcodeScan);
}

return null;
}

Expand Down Expand Up @@ -236,6 +240,26 @@ private function resolvePartFromLCSC(LCSCBarcodeScanResult $barcodeScan): ?Part
}


private function resolvePartFromTME(TMEBarcodeScanResult $barcodeScan): ?Part
{
$pn = $barcodeScan->tmePartNumber;
if ($pn) {
$part = $this->em->getRepository(Part::class)->getPartByProviderInfo($pn);
if ($part !== null) {
return $part;
}

//Try to find the part by SPN/SKU
$part = $this->em->getRepository(Part::class)->getPartBySPN($pn);
if ($part !== null) {
return $part;
}
}

// Fallback: search by MPN
return $this->em->getRepository(Part::class)->getPartByMPN($barcodeScan->mpn, $barcodeScan->manufacturer);
}

/**
* Tries to extract creation information for a part from the given barcode scan result. This can be used to
* automatically fill in the info provider reference of a part, when creating a new part based on the scan result.
Expand All @@ -247,6 +271,20 @@ private function resolvePartFromLCSC(LCSCBarcodeScanResult $barcodeScan): ?Part
*/
public function getCreateInfos(BarcodeScanResultInterface $scanResult): ?array
{
// TME
if ($scanResult instanceof TMEBarcodeScanResult) {
if ($scanResult->tmePartNumber === null) {
return null;
}
return [
'providerKey' => 'tme',
'providerId' => $scanResult->tmePartNumber,
'lotAmount' => $scanResult->quantity,
'lotName' => $scanResult->purchaseOrder,
'lotUserBarcode' => $scanResult->rawInput,
];
}

// LCSC
if ($scanResult instanceof LCSCBarcodeScanResult) {
return [
Expand Down
3 changes: 3 additions & 0 deletions src/Services/LabelSystem/BarcodeScanner/BarcodeSourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ enum BarcodeSourceType: string
case LCSC = 'lcsc';

case AMAZON = 'amazon';

/** For TME (tme.eu) formatted QR codes */
case TME = 'tme';
}
143 changes: 143 additions & 0 deletions src/Services/LabelSystem/BarcodeScanner/TMEBarcodeScanResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?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\BarcodeScanner;

use InvalidArgumentException;

/**
* This class represents the content of a tme.eu barcode label.
* The format is space-separated KEY:VALUE tokens, e.g.:
* QTY:1000 PN:SMD0603-5K1-1% PO:32723349/7 MFR:ROYALOHM MPN:0603SAF5101T5E CoO:TH RoHS https://www.tme.eu/details/...
*/
readonly class TMEBarcodeScanResult implements BarcodeScanResultInterface
{
/** @var int|null Quantity (QTY) */
public ?int $quantity;

/** @var string|null TME part number (PN) */
public ?string $tmePartNumber;

/** @var string|null Purchase order number (PO) */
public ?string $purchaseOrder;

/** @var string|null Manufacturer name (MFR) */
public ?string $manufacturer;

/** @var string|null Manufacturer part number (MPN) */
public ?string $mpn;

/** @var string|null Country of origin (CoO) */
public ?string $countryOfOrigin;

/** @var bool Whether the part is RoHS compliant */
public bool $rohs;

/** @var string|null The product URL */
public ?string $productUrl;

/**
* @param array<string, string> $fields Parsed key-value fields (keys uppercased)
* @param string $rawInput Original barcode string
*/
public function __construct(
public array $fields,
public string $rawInput,
) {
$this->quantity = isset($this->fields['QTY']) ? (int) $this->fields['QTY'] : null;
$this->tmePartNumber = $this->fields['PN'] ?? null;
$this->purchaseOrder = $this->fields['PO'] ?? null;
$this->manufacturer = $this->fields['MFR'] ?? null;
$this->mpn = $this->fields['MPN'] ?? null;
$this->countryOfOrigin = $this->fields['COO'] ?? null;
$this->rohs = isset($this->fields['ROHS']);
$this->productUrl = $this->fields['URL'] ?? null;
}

public function getSourceType(): BarcodeSourceType
{
return BarcodeSourceType::TME;
}

public function getDecodedForInfoMode(): array
{
return [
'Barcode type' => 'TME',
'TME Part No. (PN)' => $this->tmePartNumber ?? '',
'MPN' => $this->mpn ?? '',
'Manufacturer (MFR)' => $this->manufacturer ?? '',
'Qty' => $this->quantity !== null ? (string) $this->quantity : '',
'Purchase Order (PO)' => $this->purchaseOrder ?? '',
'Country of Origin (CoO)' => $this->countryOfOrigin ?? '',
'RoHS' => $this->rohs ? 'Yes' : 'No',
'URL' => $this->productUrl ?? '',
];
}

/**
* Returns true if the input looks like a TME barcode label (contains tme.eu URL).
*/
public static function isTMEBarcode(string $input): bool
{
return str_contains(strtolower($input), 'tme.eu');
}

/**
* Parse the TME barcode string into a TMEBarcodeScanResult.
*/
public static function parse(string $input): self
{
$raw = trim($input);

if (!self::isTMEBarcode($raw)) {
throw new InvalidArgumentException('Not a TME barcode');
}

$fields = [];

// Split on whitespace; each token is either KEY:VALUE, a bare keyword, or the URL
$tokens = preg_split('/\s+/', $raw);
foreach ($tokens as $token) {
if ($token === '') {
continue;
}

// The TME URL
if (str_starts_with(strtolower($token), 'http')) {
$fields['URL'] = $token;
continue;
}

$colonPos = strpos($token, ':');
if ($colonPos !== false) {
$key = strtoupper(substr($token, 0, $colonPos));
$value = substr($token, $colonPos + 1);
$fields[$key] = $value;
} else {
// Bare keyword like "RoHS"
$fields[strtoupper($token)] = '';
}
}

return new self($fields, $raw);
}
}
110 changes: 110 additions & 0 deletions tests/Services/LabelSystem/BarcodeScanner/TMEBarcodeScanResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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/>.
*/

namespace App\Tests\Services\LabelSystem\BarcodeScanner;

use App\Services\LabelSystem\BarcodeScanner\BarcodeSourceType;
use App\Services\LabelSystem\BarcodeScanner\TMEBarcodeScanResult;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class TMEBarcodeScanResultTest extends TestCase
{
private const EXAMPLE1 = 'QTY:1000 PN:SMD0603-5K1-1% PO:32723349/7 MFR:ROYALOHM MPN:0603SAF5101T5E CoO:TH RoHS https://www.tme.eu/details/SMD0603-5K1-1%25';
private const EXAMPLE2 = 'QTY:5 PN:ETQP3M6R8KVP PO:31199729/3 MFR:PANASONIC MPN:ETQP3M6R8KVP RoHS https://www.tme.eu/details/ETQP3M6R8KVP';

public function testIsTMEBarcode(): void
{
$this->assertFalse(TMEBarcodeScanResult::isTMEBarcode('invalid'));
$this->assertFalse(TMEBarcodeScanResult::isTMEBarcode('QTY:5 PN:ABC MPN:XYZ'));
$this->assertFalse(TMEBarcodeScanResult::isTMEBarcode(''));

$this->assertTrue(TMEBarcodeScanResult::isTMEBarcode(self::EXAMPLE1));
$this->assertTrue(TMEBarcodeScanResult::isTMEBarcode(self::EXAMPLE2));
}

public function testParseInvalidThrows(): void
{
$this->expectException(InvalidArgumentException::class);
TMEBarcodeScanResult::parse('not-a-tme-barcode');
}

public function testParseExample1(): void
{
$scan = TMEBarcodeScanResult::parse(self::EXAMPLE1);

$this->assertSame(1000, $scan->quantity);
$this->assertSame('SMD0603-5K1-1%', $scan->tmePartNumber);
$this->assertSame('32723349/7', $scan->purchaseOrder);
$this->assertSame('ROYALOHM', $scan->manufacturer);
$this->assertSame('0603SAF5101T5E', $scan->mpn);
$this->assertSame('TH', $scan->countryOfOrigin);
$this->assertTrue($scan->rohs);
$this->assertSame('https://www.tme.eu/details/SMD0603-5K1-1%25', $scan->productUrl);
$this->assertSame(self::EXAMPLE1, $scan->rawInput);
}

public function testParseExample2(): void
{
$scan = TMEBarcodeScanResult::parse(self::EXAMPLE2);

$this->assertSame(5, $scan->quantity);
$this->assertSame('ETQP3M6R8KVP', $scan->tmePartNumber);
$this->assertSame('31199729/3', $scan->purchaseOrder);
$this->assertSame('PANASONIC', $scan->manufacturer);
$this->assertSame('ETQP3M6R8KVP', $scan->mpn);
$this->assertNull($scan->countryOfOrigin);
$this->assertTrue($scan->rohs);
$this->assertSame('https://www.tme.eu/details/ETQP3M6R8KVP', $scan->productUrl);
}

public function testGetSourceType(): void
{
$scan = TMEBarcodeScanResult::parse(self::EXAMPLE2);
$this->assertSame(BarcodeSourceType::TME, $scan->getSourceType());
}

public function testParseUppercaseUrl(): void
{
$input = 'QTY:500 PN:M0.6W-10K MFR:ROYAL.OHM MPN:MF006FF1002A50 PO:7792659/8 HTTPS://WWW.TME.EU/DETAILS/M0.6W-10K';
$this->assertTrue(TMEBarcodeScanResult::isTMEBarcode($input));

$scan = TMEBarcodeScanResult::parse($input);
$this->assertSame(500, $scan->quantity);
$this->assertSame('M0.6W-10K', $scan->tmePartNumber);
$this->assertSame('ROYAL.OHM', $scan->manufacturer);
$this->assertSame('MF006FF1002A50', $scan->mpn);
$this->assertSame('7792659/8', $scan->purchaseOrder);
$this->assertSame('HTTPS://WWW.TME.EU/DETAILS/M0.6W-10K', $scan->productUrl);
}

public function testGetDecodedForInfoMode(): void
{
$scan = TMEBarcodeScanResult::parse(self::EXAMPLE1);
$decoded = $scan->getDecodedForInfoMode();

$this->assertSame('TME', $decoded['Barcode type']);
$this->assertSame('SMD0603-5K1-1%', $decoded['TME Part No. (PN)']);
$this->assertSame('0603SAF5101T5E', $decoded['MPN']);
$this->assertSame('ROYALOHM', $decoded['Manufacturer (MFR)']);
$this->assertSame('1000', $decoded['Qty']);
$this->assertSame('Yes', $decoded['RoHS']);
}
}
8 changes: 7 additions & 1 deletion translations/messages.de.xlf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en" trgLang="de">
<file id="messages.en">
<file id="messages.de">
<unit id="x_wTSQS" name="attachment_type.caption">
<segment state="translated">
<source>attachment_type.caption</source>
Expand Down Expand Up @@ -12861,6 +12861,12 @@ Buerklin-API-Authentication-Server:
<target>Amazon Barcode</target>
</segment>
</unit>
<unit id="d.V2Pid" name="scan_dialog.mode.tme">
<segment state="translated">
<source>scan_dialog.mode.tme</source>
<target>TME Barcode</target>
</segment>
</unit>
<unit id="BQWuR_G" name="settings.ips.canopy">
<segment state="translated">
<source>settings.ips.canopy</source>
Expand Down
6 changes: 6 additions & 0 deletions translations/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12863,6 +12863,12 @@ Buerklin-API Authentication server:
<target>Amazon barcode</target>
</segment>
</unit>
<unit id="d.V2Pid" name="scan_dialog.mode.tme">
<segment state="translated">
<source>scan_dialog.mode.tme</source>
<target>TME barcode</target>
</segment>
</unit>
<unit id="BQWuR_G" name="settings.ips.canopy">
<segment state="translated">
<source>settings.ips.canopy</source>
Expand Down
Loading