Added basic support for label barcodes (C39 and QR).

This commit is contained in:
Jan Böhmer 2020-04-24 22:10:49 +02:00
parent a7cfe7b42f
commit 6bd3ad6138
12 changed files with 620 additions and 46 deletions

View file

@ -0,0 +1,43 @@
<?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\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/scan")
* @package App\Controller
*/
class ScanController extends AbstractController
{
/**
* @Route("/{type}/{id}", name="scan_qr")
* @param string $type
* @param int $id
*/
public function scanQRCode(string $type, int $id)
{
$this->addFlash('success', 'QR Code scanned!');
return $this->redirectToRoute('homepage');
}
}

View file

@ -0,0 +1,74 @@
<?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;
use App\Entity\LabelSystem\LabelOptions;
use App\Services\LabelSystem\Barcodes\BarcodeContentGenerator;
use Com\Tecnick\Barcode\Barcode;
class BarcodeGenerator
{
protected $barcodeContentGenerator;
public function __construct(BarcodeContentGenerator $barcodeContentGenerator)
{
$this->barcodeContentGenerator = $barcodeContentGenerator;
}
public function generateSVG(LabelOptions $options, object $target): ?string
{
$barcode = new Barcode();
switch ($options->getBarcodeType()) {
case 'qr':
$type = 'QRCODE';
break;
case 'code39':
$type = 'C93';
break;
case 'none':
return null;
default:
throw new \InvalidArgumentException('Unknown label type!');
}
$bobj = $barcode->getBarcodeObj($type, $this->getContent($options, $target));
return $bobj->getSvgCode();
}
public function getContent(LabelOptions $options, object $target): ?string
{
switch ($options->getBarcodeType()) {
case 'qr':
return $this->barcodeContentGenerator->getURLContent($target);
case 'code39':
return $this->barcodeContentGenerator->get1DBarcodeContent($target);
case 'none':
return null;
default:
throw new \InvalidArgumentException('Unknown label type!');
}
}
}

View 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));
}
}

View file

@ -23,6 +23,7 @@ namespace App\Services\LabelSystem;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\LabelSystem\LabelOptions;
use App\Services\ElementTypeNameGenerator;
use App\Services\LabelSystem\Barcodes\BarcodeContentGenerator;
use Twig\Environment;
class LabelHTMLGenerator
@ -30,12 +31,15 @@ class LabelHTMLGenerator
protected $twig;
protected $elementTypeNameGenerator;
protected $replacer;
protected $barcodeGenerator;
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, LabelTextReplacer $replacer, Environment $twig)
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, LabelTextReplacer $replacer, Environment $twig,
BarcodeGenerator $barcodeGenerator)
{
$this->twig = $twig;
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
$this->replacer = $replacer;
$this->barcodeGenerator = $barcodeGenerator;
}
public function getLabelHTML(LabelOptions $options, array $elements): string
@ -48,13 +52,16 @@ class LabelHTMLGenerator
foreach ($elements as $element) {
$twig_elements[] = [
'element' => $element,
'lines' => $this->replacer->replace($options->getLines(), $element)
'lines' => $this->replacer->replace($options->getLines(), $element),
'barcode' => $this->barcodeGenerator->generateSVG($options, $element),
'barcode_content' => $this->barcodeGenerator->getContent($options, $element),
];
}
return $this->twig->render('LabelSystem/labels/base_label.html.twig', [
'meta_title' => $this->getPDFTitle($options, $elements[0]),
'elements' => $twig_elements,
'options' => $options,
]);
}