From ab68f4b37f71bafd23e7ccee689ae727cd9f56f7 Mon Sep 17 00:00:00 2001 From: jona Date: Thu, 19 Dec 2024 18:56:35 +0100 Subject: [PATCH] added button to show existing part with same manufacturer and mpn in provider list --- src/Controller/InfoProviderController.php | 38 +++++++++++++- .../search/part_search.html.twig | 52 ++++++++++++------- 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/Controller/InfoProviderController.php b/src/Controller/InfoProviderController.php index 46c5f041..1236dba3 100644 --- a/src/Controller/InfoProviderController.php +++ b/src/Controller/InfoProviderController.php @@ -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', [ diff --git a/templates/info_providers/search/part_search.html.twig b/templates/info_providers/search/part_search.html.twig index 0de72587..5e090c66 100644 --- a/templates/info_providers/search/part_search.html.twig +++ b/templates/info_providers/search/part_search.html.twig @@ -52,59 +52,71 @@ {% trans %}part.table.manufacturingStatus{% endtrans %} {% trans %}info_providers.table.provider.label{% endtrans %} + {% for result in results %} + {% set dto = result["dto"] %} - - {% if result.provider_url is not null %} - {{ result.name }} + {% if dto.provider_url is not null %} + {{ dto.name }} {% else %} - {{ result.name }} + {{ dto.name }} {% endif %} - {% if result.mpn is not null %} + {% if dto.mpn is not null %}
- {{ result.mpn }} + {{ dto.mpn }} {% endif %} - {{ result.description }} - {% if result.category is not null %} + {{ dto.description }} + {% if dto.category is not null %}
- {{ result.category }} + {{ dto.category }} {% endif %} - {{ result.manufacturer ?? '' }} - {% if result.footprint is not null %} + {{ dto.manufacturer ?? '' }} + {% if dto.footprint is not null %}
- {{ result.footprint }} + {{ dto.footprint }} {% endif %} - {{ helper.m_status_to_badge(result.manufacturing_status) }} + {{ helper.m_status_to_badge(dto.manufacturing_status) }} - {% if result.provider_url %} - - {{ info_provider_label(result.provider_key)|default(result.provider_key) }} + {% if dto.provider_url %} + + {{ info_provider_label(dto.provider_key)|default(dto.provider_key) }} {% else %} - {{ info_provider_label(result.provider_key)|default(result.provider_key) }} + {{ info_provider_label(dto.provider_key)|default(dto.provider_key) }} {% endif %}
- {{ result.provider_id }} + {{ dto.provider_id }} + + {% if result["localPart"] is not null %} + {# TODO: Needs translation #} + + + {% endif %} + + + {% if update_target %} {# We update an existing part #} {% set href = path('info_providers_update_part', - {'providerKey': result.provider_key, 'providerId': result.provider_id, 'id': update_target.iD}) %} + {'providerKey': dto.provider_key, 'providerId': dto.provider_id, 'id': update_target.iD}) %} {% else %} {# Create a fresh part #} {% set href = path('info_providers_create_part', - {'providerKey': result.provider_key, 'providerId': result.provider_id}) %} + {'providerKey': dto.provider_key, 'providerId': dto.provider_id}) %} {% endif %}