added button to show existing part with same manufacturer and mpn in provider list

This commit is contained in:
jona 2024-12-19 18:56:35 +01:00 committed by Jan Böhmer
parent e9efbff912
commit ab68f4b37f
2 changed files with 69 additions and 21 deletions

View file

@ -23,10 +23,13 @@ declare(strict_types=1);
namespace App\Controller;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Part;
use App\Form\InfoProviderSystem\PartSearchType;
use App\Services\InfoProviderSystem\PartInfoRetriever;
use App\Services\InfoProviderSystem\ProviderRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -42,7 +45,8 @@ class InfoProviderController extends AbstractController
{
public function __construct(private readonly ProviderRegistry $providerRegistry,
private readonly PartInfoRetriever $infoRetriever)
private readonly PartInfoRetriever $infoRetriever,
private readonly EntityManagerInterface $em)
{
}
@ -58,6 +62,34 @@ class InfoProviderController extends AbstractController
]);
}
private function matchResultsToKnownParts(array $partsList): array
{
$manufacturerQb = $this->em->getRepository(Manufacturer::class)->createQueryBuilder("manufacturer");
$manufacturerQb->where($manufacturerQb->expr()->like("LOWER(manufacturer.name)", "LOWER(:manufacturer_name)"));
$mpnQb = $this->em->getRepository(Part::class)->createQueryBuilder("part");
$mpnQb->where($mpnQb->expr()->like("LOWER(part.manufacturer_product_number)", "LOWER(:mpn)"));
$mpnQb->andWhere($mpnQb->expr()->eq("part.manufacturer", ":manufacturer"));
foreach ($partsList as $index => $part) {
$manufacturerQb->setParameter("manufacturer_name", $part["dto"]->manufacturer);
$manufacturers = $manufacturerQb->getQuery()->getResult();
if(!$manufacturers) {
continue;
}
$mpnQb->setParameter("manufacturer", $manufacturers);
$mpnQb->setParameter("mpn", $part["dto"]->mpn);
$localParts = $mpnQb->getQuery()->getResult();
if(!$localParts) {
continue;
}
$partsList[$index]["localPart"] = $localParts[0];
}
return $partsList;
}
#[Route('/search', name: 'info_providers_search')]
#[Route('/update/{target}', name: 'info_providers_update_part_search')]
public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger): Response
@ -87,6 +119,10 @@ class InfoProviderController extends AbstractController
//Log the exception
$exceptionLogger->error('Error during info provider search: ' . $e->getMessage(), ['exception' => $e]);
}
$results = array_map(function ($result) {return ["dto" => $result,"localPart" => null];}, $results);
if(!$update_target) {
$results = $this->matchResultsToKnownParts($results);
}
}
return $this->render('info_providers/search/part_search.html.twig', [