diff --git a/src/Repository/PartRepository.php b/src/Repository/PartRepository.php index 9d5fee5e..03d6a8cc 100644 --- a/src/Repository/PartRepository.php +++ b/src/Repository/PartRepository.php @@ -389,4 +389,69 @@ class PartRepository extends NamedDBElementRepository return $baseIpn . '_' . ($maxSuffix + 1); } + /** + * Finds a part based on the provided info provider key and ID, with an option for case sensitivity. + * If no part is found with the given provider key and ID, null is returned. + * @param string $providerID + * @param string|null $providerKey If null, the provider key will not be included in the search criteria, and only the provider ID will be used for matching. + * @param bool $caseInsensitive If true, the provider ID comparison will be case-insensitive. Default is true. + * @return Part|null + */ + public function getPartByProviderInfo(string $providerID, ?string $providerKey = null, bool $caseInsensitive = true): ?Part + { + $qb = $this->createQueryBuilder('part'); + $qb->select('part'); + + if ($providerKey) { + $qb->where("part.providerReference.provider_key = :providerKey"); + $qb->setParameter('providerKey', $providerKey); + } + + + if ($caseInsensitive) { + $qb->andWhere("LOWER(part.providerReference.provider_id) = LOWER(:providerID)"); + } else { + $qb->andWhere("part.providerReference.provider_id = :providerID"); + } + + $qb->setParameter('providerID', $providerID); + + return $qb->getQuery()->getOneOrNullResult(); + } + + /** + * Finds a part based on the provided MPN (Manufacturer Part Number), with an option for case sensitivity. + * If no part is found with the given MPN, null is returned. + * @param string $mpn + * @param string|null $manufacturerName If provided, the search will also include a match for the manufacturer's name. If null, the manufacturer name will not be included in the search criteria. + * @param bool $caseInsensitive If true, the MPN comparison will be case-insensitive. Default is true (case-insensitive). + * @return Part|null + */ + public function getPartByMPN(string $mpn, ?string $manufacturerName = null, bool $caseInsensitive = true): ?Part + { + $qb = $this->createQueryBuilder('part'); + $qb->select('part'); + + if ($caseInsensitive) { + $qb->where("LOWER(part.mpn) = LOWER(:mpn)"); + } else { + $qb->where("part.mpn = :mpn"); + } + + if ($manufacturerName !== null) { + $qb->leftJoin('part.manufacturer', 'manufacturer'); + + if ($caseInsensitive) { + $qb->andWhere("LOWER(part.manufacturer.name) = LOWER(:manufacturerName)"); + } else { + $qb->andWhere("manufacturer.name = :manufacturerName"); + } + $qb->setParameter('manufacturerName', $manufacturerName); + } + + $qb->setParameter('mpn', $mpn); + + return $qb->getQuery()->getOneOrNullResult(); + } + } diff --git a/src/Services/LabelSystem/BarcodeScanner/BarcodeRedirector.php b/src/Services/LabelSystem/BarcodeScanner/BarcodeRedirector.php index d4b7bed9..32e5fb77 100644 --- a/src/Services/LabelSystem/BarcodeScanner/BarcodeRedirector.php +++ b/src/Services/LabelSystem/BarcodeScanner/BarcodeRedirector.php @@ -132,13 +132,10 @@ final class BarcodeRedirector // Try LCSC code (pc) as provider id if available $pc = $barcodeScan->lcscCode; // e.g. C138033 if ($pc) { - $qb = $this->em->getRepository(Part::class)->createQueryBuilder('part'); - $qb->where($qb->expr()->like('LOWER(part.providerReference.provider_id)', 'LOWER(:vendor_id)')); - $qb->setParameter('vendor_id', $pc); - $results = $qb->getQuery()->getResult(); - if ($results) { - return $results[0]; - } + $part = $this->em->getRepository(Part::class)->getPartByProviderInfo($pc); + if ($part !== null) { + return $part; + } } // Fallback to MPN (pm) @@ -147,14 +144,10 @@ final class BarcodeRedirector throw new EntityNotFoundException(); } - $mpnQb = $this->em->getRepository(Part::class)->createQueryBuilder('part'); - $mpnQb->where($mpnQb->expr()->like('LOWER(part.manufacturer_product_number)', 'LOWER(:mpn)')); - $mpnQb->setParameter('mpn', $pm); - - $results = $mpnQb->getQuery()->getResult(); - if ($results) { - return $results[0]; - } + $part = $this->em->getRepository(Part::class)->getPartByMPN($pm); + if ($part !== null) { + return $part; + } throw new EntityNotFoundException(); } @@ -189,17 +182,14 @@ final class BarcodeRedirector // the info provider system or if the part was bought from a different vendor than the data was retrieved // from. if($barcodeScan->digikeyPartNumber) { - $qb = $this->em->getRepository(Part::class)->createQueryBuilder('part'); - //Lower() to be case insensitive - $qb->where($qb->expr()->like('LOWER(part.providerReference.provider_id)', 'LOWER(:vendor_id)')); - $qb->setParameter('vendor_id', $barcodeScan->digikeyPartNumber); - $results = $qb->getQuery()->getResult(); - if ($results) { - return $results[0]; + + $part = $this->em->getRepository(Part::class)->getPartByProviderInfo($barcodeScan->digikeyPartNumber); + if ($part !== null) { + return $part; } } - if(!$barcodeScan->supplierPartNumber){ + if (!$barcodeScan->supplierPartNumber){ throw new EntityNotFoundException(); } @@ -207,27 +197,12 @@ final class BarcodeRedirector //multiple manufacturers to use the same part number for their version of a common product //We assume the user is able to realize when this returns the wrong part //If the barcode specifies the manufacturer we try to use that as well - $mpnQb = $this->em->getRepository(Part::class)->createQueryBuilder('part'); - $mpnQb->where($mpnQb->expr()->like('LOWER(part.manufacturer_product_number)', 'LOWER(:mpn)')); - $mpnQb->setParameter('mpn', $barcodeScan->supplierPartNumber); - - if($barcodeScan->mouserManufacturer){ - $manufacturerQb = $this->em->getRepository(Manufacturer::class)->createQueryBuilder("manufacturer"); - $manufacturerQb->where($manufacturerQb->expr()->like("LOWER(manufacturer.name)", "LOWER(:manufacturer_name)")); - $manufacturerQb->setParameter("manufacturer_name", $barcodeScan->mouserManufacturer); - $manufacturers = $manufacturerQb->getQuery()->getResult(); - - if($manufacturers) { - $mpnQb->andWhere($mpnQb->expr()->eq("part.manufacturer", ":manufacturer")); - $mpnQb->setParameter("manufacturer", $manufacturers); - } + $part = $this->em->getRepository(Part::class)->getPartByMPN($barcodeScan->supplierPartNumber, $barcodeScan->mouserManufacturer); + if($part !== null) { + return $part; } - $results = $mpnQb->getQuery()->getResult(); - if($results){ - return $results[0]; - } throw new EntityNotFoundException(); }