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', [

View file

@ -52,59 +52,71 @@
<th>{% trans %}part.table.manufacturingStatus{% endtrans %}</th>
<th>{% trans %}info_providers.table.provider.label{% endtrans %}</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for result in results %}
{% set dto = result["dto"] %}
<tr>
<td>
<img src="{{ result.preview_image_url }}" data-thumbnail="{{ result.preview_image_url }}"
<img src="{{ dto.preview_image_url }}" data-thumbnail="{{ dto.preview_image_url }}"
class="hoverpic" style="max-width: 45px;" {{ stimulus_controller('elements/hoverpic') }}>
</td>
<td>
{% if result.provider_url is not null %}
<a href="{{ result.provider_url }}" target="_blank" rel="noopener">{{ result.name }}</a>
{% if dto.provider_url is not null %}
<a href="{{ dto.provider_url }}" target="_blank" rel="noopener">{{ dto.name }}</a>
{% else %}
{{ result.name }}
{{ dto.name }}
{% endif %}
{% if result.mpn is not null %}
{% if dto.mpn is not null %}
<br>
<small class="text-muted" title="{% trans %}part.table.mpn{% endtrans %}">{{ result.mpn }}</small>
<small class="text-muted" title="{% trans %}part.table.mpn{% endtrans %}">{{ dto.mpn }}</small>
{% endif %}
</td>
<td>
{{ result.description }}
{% if result.category is not null %}
{{ dto.description }}
{% if dto.category is not null %}
<br>
<small class="text-muted">{{ result.category }}</small>
<small class="text-muted">{{ dto.category }}</small>
{% endif %}
</td>
<td>
{{ result.manufacturer ?? '' }}
{% if result.footprint is not null %}
{{ dto.manufacturer ?? '' }}
{% if dto.footprint is not null %}
<br>
<small class="text-muted">{{ result.footprint }}</small>
<small class="text-muted">{{ dto.footprint }}</small>
{% endif %}
</td>
<td>{{ helper.m_status_to_badge(result.manufacturing_status) }}</td>
<td>{{ helper.m_status_to_badge(dto.manufacturing_status) }}</td>
<td>
{% if result.provider_url %}
<a href="{{ result.provider_url }}" target="_blank" rel="noopener">
{{ info_provider_label(result.provider_key)|default(result.provider_key) }}
{% if dto.provider_url %}
<a href="{{ dto.provider_url }}" target="_blank" rel="noopener">
{{ info_provider_label(dto.provider_key)|default(dto.provider_key) }}
</a>
{% else %}
{{ info_provider_label(result.provider_key)|default(result.provider_key) }}
{{ info_provider_label(dto.provider_key)|default(dto.provider_key) }}
{% endif %}
<br>
<small class="text-muted">{{ result.provider_id }}</small>
<small class="text-muted">{{ dto.provider_id }}</small>
</td>
<td>
{% if result["localPart"] is not null %}
<a class="btn btn-primary" href="{{ path('part_edit', {'id': result['localPart'].id}) }}"
target="_blank" title="Edit Existing Part"> {# TODO: Needs translation #}
<i class="fa-solid fa-pencil"></i>
</a>
{% endif %}
</td>
<td>
{% 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 %}
<a class="btn btn-primary" href="{{ href }}"