|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +/** |
| 24 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 25 | + * |
| 26 | + * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
| 27 | + * |
| 28 | + * This program is free software: you can redistribute it and/or modify |
| 29 | + * it under the terms of the GNU Affero General Public License as published |
| 30 | + * by the Free Software Foundation, either version 3 of the License, or |
| 31 | + * (at your option) any later version. |
| 32 | + * |
| 33 | + * This program is distributed in the hope that it will be useful, |
| 34 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 35 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 36 | + * GNU Affero General Public License for more details. |
| 37 | + * |
| 38 | + * You should have received a copy of the GNU Affero General Public License |
| 39 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 40 | + */ |
| 41 | + |
| 42 | +namespace App\Services\LabelSystem\BarcodeScanner; |
| 43 | + |
| 44 | +use App\Entity\LabelSystem\LabelSupportedElement; |
| 45 | +use App\Entity\Parts\Manufacturer; |
| 46 | +use App\Entity\Parts\Part; |
| 47 | +use App\Entity\Parts\PartLot; |
| 48 | +use Doctrine\ORM\EntityManagerInterface; |
| 49 | +use Doctrine\ORM\EntityNotFoundException; |
| 50 | +use InvalidArgumentException; |
| 51 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 52 | + |
| 53 | +/** |
| 54 | + * @see \App\Tests\Services\LabelSystem\Barcodes\BarcodeRedirectorTest |
| 55 | + */ |
| 56 | +final class BarcodeRedirector |
| 57 | +{ |
| 58 | + public function __construct(private readonly UrlGeneratorInterface $urlGenerator, private readonly EntityManagerInterface $em) |
| 59 | + { |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Determines the URL to which the user should be redirected, when scanning a QR code. |
| 64 | + * |
| 65 | + * @param BarcodeScanResultInterface $barcodeScan The result of the barcode scan |
| 66 | + * @return string the URL to which should be redirected |
| 67 | + * |
| 68 | + * @throws EntityNotFoundException |
| 69 | + */ |
| 70 | + public function getRedirectURL(BarcodeScanResultInterface $barcodeScan): string |
| 71 | + { |
| 72 | + if($barcodeScan instanceof LocalBarcodeScanResult) { |
| 73 | + return $this->getURLLocalBarcode($barcodeScan); |
| 74 | + } |
| 75 | + |
| 76 | + if ($barcodeScan instanceof EIGP114BarcodeScanResult) { |
| 77 | + return $this->getURLVendorBarcode($barcodeScan); |
| 78 | + } |
| 79 | + |
| 80 | + throw new InvalidArgumentException('Unknown $barcodeScan type: '.get_class($barcodeScan)); |
| 81 | + } |
| 82 | + |
| 83 | + private function getURLLocalBarcode(LocalBarcodeScanResult $barcodeScan): string |
| 84 | + { |
| 85 | + switch ($barcodeScan->target_type) { |
| 86 | + case LabelSupportedElement::PART: |
| 87 | + return $this->urlGenerator->generate('app_part_show', ['id' => $barcodeScan->target_id]); |
| 88 | + case LabelSupportedElement::PART_LOT: |
| 89 | + //Try to determine the part to the given lot |
| 90 | + $lot = $this->em->find(PartLot::class, $barcodeScan->target_id); |
| 91 | + if (!$lot instanceof PartLot) { |
| 92 | + throw new EntityNotFoundException(); |
| 93 | + } |
| 94 | + |
| 95 | + return $this->urlGenerator->generate('app_part_show', ['id' => $lot->getPart()->getID()]); |
| 96 | + |
| 97 | + case LabelSupportedElement::STORELOCATION: |
| 98 | + return $this->urlGenerator->generate('part_list_store_location', ['id' => $barcodeScan->target_id]); |
| 99 | + |
| 100 | + default: |
| 101 | + throw new InvalidArgumentException('Unknown $type: '.$barcodeScan->target_type->name); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Gets the URL to a part from a scan of a Vendor Barcode |
| 107 | + */ |
| 108 | + private function getURLVendorBarcode(EIGP114BarcodeScanResult $barcodeScan): string |
| 109 | + { |
| 110 | + $part = $this->getPartFromVendor($barcodeScan); |
| 111 | + return $this->urlGenerator->generate('app_part_show', ['id' => $part->getID()]); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Gets a part from a scan of a Vendor Barcode by filtering for parts |
| 116 | + * with the same Info Provider Id or, if that fails, by looking for parts with a |
| 117 | + * matching manufacturer product number. Only returns the first matching part. |
| 118 | + */ |
| 119 | + private function getPartFromVendor(EIGP114BarcodeScanResult $barcodeScan) : Part |
| 120 | + { |
| 121 | + // first check via the info provider ID (e.g. Vendor ID). This might fail if the part was not added via |
| 122 | + // the info provider system or if the part was bought from a different vendor than the data was retrieved |
| 123 | + // from. |
| 124 | + if($barcodeScan->digikeyPartNumber) { |
| 125 | + $qb = $this->em->getRepository(Part::class)->createQueryBuilder('part'); |
| 126 | + //Lower() to be case insensitive |
| 127 | + $qb->where($qb->expr()->like('LOWER(part.providerReference.provider_id)', 'LOWER(:vendor_id)')); |
| 128 | + $qb->setParameter('vendor_id', $barcodeScan->digikeyPartNumber); |
| 129 | + $results = $qb->getQuery()->getResult(); |
| 130 | + if ($results) { |
| 131 | + return $results[0]; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if(!$barcodeScan->supplierPartNumber){ |
| 136 | + throw new EntityNotFoundException(); |
| 137 | + } |
| 138 | + |
| 139 | + //Fallback to the manufacturer part number. This may return false positives, since it is common for |
| 140 | + //multiple manufacturers to use the same part number for their version of a common product |
| 141 | + //We assume the user is able to realize when this returns the wrong part |
| 142 | + //If the barcode specifies the manufacturer we try to use that as well |
| 143 | + $mpnQb = $this->em->getRepository(Part::class)->createQueryBuilder('part'); |
| 144 | + $mpnQb->where($mpnQb->expr()->like('LOWER(part.manufacturer_product_number)', 'LOWER(:mpn)')); |
| 145 | + $mpnQb->setParameter('mpn', $barcodeScan->supplierPartNumber); |
| 146 | + |
| 147 | + if($barcodeScan->mouserManufacturer){ |
| 148 | + $manufacturerQb = $this->em->getRepository(Manufacturer::class)->createQueryBuilder("manufacturer"); |
| 149 | + $manufacturerQb->where($manufacturerQb->expr()->like("LOWER(manufacturer.name)", "LOWER(:manufacturer_name)")); |
| 150 | + $manufacturerQb->setParameter("manufacturer_name", $barcodeScan->mouserManufacturer); |
| 151 | + $manufacturers = $manufacturerQb->getQuery()->getResult(); |
| 152 | + |
| 153 | + if($manufacturers) { |
| 154 | + $mpnQb->andWhere($mpnQb->expr()->eq("part.manufacturer", ":manufacturer")); |
| 155 | + $mpnQb->setParameter("manufacturer", $manufacturers); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + $results = $mpnQb->getQuery()->getResult(); |
| 161 | + if($results){ |
| 162 | + return $results[0]; |
| 163 | + } |
| 164 | + throw new EntityNotFoundException(); |
| 165 | + } |
| 166 | +} |
0 commit comments