mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-02-28 20:39:35 +00:00
Moved database queries from BarcodeRedirector to PartRepository
This commit is contained in:
parent
a9a1f1d265
commit
f77d201563
2 changed files with 81 additions and 41 deletions
|
|
@ -389,4 +389,69 @@ class PartRepository extends NamedDBElementRepository
|
||||||
return $baseIpn . '_' . ($maxSuffix + 1);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -132,13 +132,10 @@ final class BarcodeRedirector
|
||||||
// Try LCSC code (pc) as provider id if available
|
// Try LCSC code (pc) as provider id if available
|
||||||
$pc = $barcodeScan->lcscCode; // e.g. C138033
|
$pc = $barcodeScan->lcscCode; // e.g. C138033
|
||||||
if ($pc) {
|
if ($pc) {
|
||||||
$qb = $this->em->getRepository(Part::class)->createQueryBuilder('part');
|
$part = $this->em->getRepository(Part::class)->getPartByProviderInfo($pc);
|
||||||
$qb->where($qb->expr()->like('LOWER(part.providerReference.provider_id)', 'LOWER(:vendor_id)'));
|
if ($part !== null) {
|
||||||
$qb->setParameter('vendor_id', $pc);
|
return $part;
|
||||||
$results = $qb->getQuery()->getResult();
|
}
|
||||||
if ($results) {
|
|
||||||
return $results[0];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to MPN (pm)
|
// Fallback to MPN (pm)
|
||||||
|
|
@ -147,14 +144,10 @@ final class BarcodeRedirector
|
||||||
throw new EntityNotFoundException();
|
throw new EntityNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$mpnQb = $this->em->getRepository(Part::class)->createQueryBuilder('part');
|
$part = $this->em->getRepository(Part::class)->getPartByMPN($pm);
|
||||||
$mpnQb->where($mpnQb->expr()->like('LOWER(part.manufacturer_product_number)', 'LOWER(:mpn)'));
|
if ($part !== null) {
|
||||||
$mpnQb->setParameter('mpn', $pm);
|
return $part;
|
||||||
|
}
|
||||||
$results = $mpnQb->getQuery()->getResult();
|
|
||||||
if ($results) {
|
|
||||||
return $results[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new EntityNotFoundException();
|
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
|
// the info provider system or if the part was bought from a different vendor than the data was retrieved
|
||||||
// from.
|
// from.
|
||||||
if($barcodeScan->digikeyPartNumber) {
|
if($barcodeScan->digikeyPartNumber) {
|
||||||
$qb = $this->em->getRepository(Part::class)->createQueryBuilder('part');
|
|
||||||
//Lower() to be case insensitive
|
$part = $this->em->getRepository(Part::class)->getPartByProviderInfo($barcodeScan->digikeyPartNumber);
|
||||||
$qb->where($qb->expr()->like('LOWER(part.providerReference.provider_id)', 'LOWER(:vendor_id)'));
|
if ($part !== null) {
|
||||||
$qb->setParameter('vendor_id', $barcodeScan->digikeyPartNumber);
|
return $part;
|
||||||
$results = $qb->getQuery()->getResult();
|
|
||||||
if ($results) {
|
|
||||||
return $results[0];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$barcodeScan->supplierPartNumber){
|
if (!$barcodeScan->supplierPartNumber){
|
||||||
throw new EntityNotFoundException();
|
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
|
//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
|
//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
|
//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();
|
throw new EntityNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue