mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Added basic support for label barcodes (C39 and QR).
This commit is contained in:
parent
a7cfe7b42f
commit
6bd3ad6138
12 changed files with 620 additions and 46 deletions
101
src/Services/LabelSystem/Barcodes/BarcodeContentGenerator.php
Normal file
101
src/Services/LabelSystem/Barcodes/BarcodeContentGenerator.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2020 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\Services\LabelSystem\Barcodes;
|
||||
|
||||
|
||||
use App\Entity\Base\AbstractDBElement;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Entity\Parts\PartLot;
|
||||
use App\Entity\Parts\Storelocation;
|
||||
use App\Entity\Parts\Supplier;
|
||||
use App\Exceptions\EntityNotSupportedException;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class BarcodeContentGenerator
|
||||
{
|
||||
protected $urlGenerator;
|
||||
|
||||
public const PREFIX_MAP = [
|
||||
Part::class => 'P',
|
||||
PartLot::class => 'L',
|
||||
Storelocation::class => 'S'
|
||||
];
|
||||
|
||||
protected const URL_MAP = [
|
||||
Part::class => 'part',
|
||||
PartLot::class => 'lot',
|
||||
Storelocation::class => 'location',
|
||||
];
|
||||
|
||||
public function __construct(UrlGeneratorInterface $urlGenerator)
|
||||
{
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a fixed URL to the given Element that can be embedded in a 2D code (e.g. QR code).
|
||||
* @param AbstractDBElement $target
|
||||
* @return string
|
||||
*/
|
||||
public function getURLContent(AbstractDBElement $target): string
|
||||
{
|
||||
if ($target instanceof Part) {
|
||||
$type = 'part';
|
||||
} else {
|
||||
throw new EntityNotSupportedException();
|
||||
}
|
||||
|
||||
return $this->urlGenerator->generate('scan_qr', [
|
||||
'type' => $type,
|
||||
'id' => $target->getID(),
|
||||
], UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Code that can be used in a 1D barcode.
|
||||
* The return value has a format of "L-000123"
|
||||
* @param AbstractDBElement $target
|
||||
* @return string
|
||||
*/
|
||||
public function get1DBarcodeContent(AbstractDBElement $target): string
|
||||
{
|
||||
$prefix = $this->classToString(self::PREFIX_MAP, $target);
|
||||
$id = sprintf('%06d', $target->getID());
|
||||
return $prefix . '-' . $id;
|
||||
}
|
||||
|
||||
protected function classToString(array $map, object $target): string
|
||||
{
|
||||
$class = get_class($target);
|
||||
if (isset($map[$class])) {
|
||||
return $map[$class];
|
||||
}
|
||||
|
||||
foreach($map as $class => $string) {
|
||||
if (is_a($target, $class)) {
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException('Unknown object class ' . get_class($target));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue