2019-02-23 22:52:48 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2019-03-05 14:37:41 +01:00
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2019-02-23 22:52:48 +01:00
|
|
|
namespace App\Repository;
|
|
|
|
|
|
2025-04-01 16:10:10 +02:00
|
|
|
use App\Entity\Parts\Category;
|
2023-06-18 00:00:58 +02:00
|
|
|
use App\Entity\Parts\Part;
|
2020-02-10 23:26:45 +01:00
|
|
|
use App\Entity\Parts\PartLot;
|
2022-08-14 19:32:53 +02:00
|
|
|
use Doctrine\ORM\NonUniqueResultException;
|
|
|
|
|
use Doctrine\ORM\NoResultException;
|
2020-02-10 23:26:45 +01:00
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2025-04-01 16:10:10 +02:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-02-23 22:52:48 +01:00
|
|
|
|
2023-06-18 00:00:58 +02:00
|
|
|
/**
|
|
|
|
|
* @extends NamedDBElementRepository<Part>
|
|
|
|
|
*/
|
2020-03-01 19:46:48 +01:00
|
|
|
class PartRepository extends NamedDBElementRepository
|
2019-02-23 22:52:48 +01:00
|
|
|
{
|
2025-04-01 16:10:10 +02:00
|
|
|
private TranslatorInterface $translator;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
EntityManagerInterface $em,
|
|
|
|
|
TranslatorInterface $translator
|
|
|
|
|
) {
|
|
|
|
|
parent::__construct($em, $em->getClassMetadata(Part::class));
|
|
|
|
|
|
|
|
|
|
$this->translator = $translator;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 23:26:45 +01:00
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* Gets the summed up instock of all parts (only parts without a measurement unit).
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2022-08-14 19:32:53 +02:00
|
|
|
* @throws NoResultException
|
|
|
|
|
* @throws NonUniqueResultException
|
2020-02-10 23:26:45 +01:00
|
|
|
*/
|
2020-04-08 16:41:04 +02:00
|
|
|
public function getPartsInstockSum(): float
|
2020-02-10 23:26:45 +01:00
|
|
|
{
|
|
|
|
|
$qb = new QueryBuilder($this->getEntityManager());
|
|
|
|
|
$qb->select('SUM(part_lot.amount)')
|
|
|
|
|
->from(PartLot::class, 'part_lot')
|
|
|
|
|
->leftJoin('part_lot.part', 'part')
|
|
|
|
|
->where('part.partUnit IS NULL');
|
|
|
|
|
|
|
|
|
|
$query = $qb->getQuery();
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-04-08 16:41:04 +02:00
|
|
|
return (float) ($query->getSingleScalarResult() ?? 0.0);
|
2020-02-10 23:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* Gets the number of parts that has price information.
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2022-08-14 19:32:53 +02:00
|
|
|
* @throws NoResultException
|
|
|
|
|
* @throws NonUniqueResultException
|
2020-02-10 23:26:45 +01:00
|
|
|
*/
|
|
|
|
|
public function getPartsCountWithPrice(): int
|
|
|
|
|
{
|
|
|
|
|
$qb = $this->createQueryBuilder('part');
|
2020-08-30 00:44:55 +02:00
|
|
|
$qb->select('COUNT(DISTINCT part)')
|
2020-02-10 23:26:45 +01:00
|
|
|
->innerJoin('part.orderdetails', 'orderdetail')
|
|
|
|
|
->innerJoin('orderdetail.pricedetails', 'pricedetail')
|
|
|
|
|
->where('pricedetail.price > 0.0');
|
|
|
|
|
|
|
|
|
|
$query = $qb->getQuery();
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-04-08 16:41:04 +02:00
|
|
|
return (int) ($query->getSingleScalarResult() ?? 0);
|
2020-02-10 23:26:45 +01:00
|
|
|
}
|
2022-12-24 14:04:46 +01:00
|
|
|
|
2023-06-18 00:00:58 +02:00
|
|
|
/**
|
|
|
|
|
* @return Part[]
|
|
|
|
|
*/
|
2022-12-24 14:04:46 +01:00
|
|
|
public function autocompleteSearch(string $query, int $max_limits = 50): array
|
|
|
|
|
{
|
|
|
|
|
$qb = $this->createQueryBuilder('part');
|
|
|
|
|
$qb->select('part')
|
|
|
|
|
->leftJoin('part.category', 'category')
|
2022-12-24 16:25:29 +01:00
|
|
|
->leftJoin('part.footprint', 'footprint')
|
2022-12-24 14:04:46 +01:00
|
|
|
|
2024-12-02 00:17:54 +01:00
|
|
|
->where('ILIKE(part.name, :query) = TRUE')
|
|
|
|
|
->orWhere('ILIKE(part.description, :query) = TRUE')
|
|
|
|
|
->orWhere('ILIKE(category.name, :query) = TRUE')
|
|
|
|
|
->orWhere('ILIKE(footprint.name, :query) = TRUE')
|
2022-12-24 14:04:46 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
$qb->setParameter('query', '%'.$query.'%');
|
|
|
|
|
|
|
|
|
|
$qb->setMaxResults($max_limits);
|
2024-06-17 22:33:40 +02:00
|
|
|
$qb->orderBy('NATSORT(part.name)', 'ASC');
|
2022-12-24 14:04:46 +01:00
|
|
|
|
|
|
|
|
return $qb->getQuery()->getResult();
|
|
|
|
|
}
|
2025-04-01 16:10:10 +02:00
|
|
|
|
|
|
|
|
public function autoCompleteIpn(Part $part, int $autocompletePartDigits): array
|
|
|
|
|
{
|
|
|
|
|
$category = $part->getCategory();
|
|
|
|
|
$ipnSuggestions = ['commonPrefixes' => [], 'prefixesPartIncrement' => []];
|
|
|
|
|
|
|
|
|
|
// Validate the category and ensure it's an instance of Category
|
|
|
|
|
if ($category instanceof Category) {
|
|
|
|
|
$currentPath = $category->getPartIpnPrefix();
|
|
|
|
|
$directIpnPrefixEmpty = $category->getPartIpnPrefix() === '';
|
|
|
|
|
$currentPath = $currentPath === '' ? 'n.a.' : $currentPath;
|
|
|
|
|
|
|
|
|
|
$increment = $this->generateNextPossiblePartIncrement($currentPath, $part, $autocompletePartDigits);
|
|
|
|
|
|
|
|
|
|
$ipnSuggestions['commonPrefixes'][] = [
|
|
|
|
|
'title' => $currentPath . '-',
|
|
|
|
|
'description' => $directIpnPrefixEmpty ? $this->translator->trans('part.edit.tab.advanced.ipn.prefix_empty.direct_category', ['%name%' => $category->getName()]) : $this->translator->trans('part.edit.tab.advanced.ipn.prefix.direct_category')
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$ipnSuggestions['prefixesPartIncrement'][] = [
|
|
|
|
|
'title' => $currentPath . '-' . $increment,
|
|
|
|
|
'description' => $directIpnPrefixEmpty ? $this->translator->trans('part.edit.tab.advanced.ipn.prefix_empty.direct_category', ['%name%' => $category->getName()]) : $this->translator->trans('part.edit.tab.advanced.ipn.prefix.direct_category.increment')
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Process parent categories
|
|
|
|
|
$parentCategory = $category->getParent();
|
|
|
|
|
|
|
|
|
|
while ($parentCategory instanceof Category) {
|
|
|
|
|
// Prepend the parent category's prefix to the current path
|
|
|
|
|
$currentPath = $parentCategory->getPartIpnPrefix() . '-' . $currentPath;
|
|
|
|
|
$currentPath = $parentCategory->getPartIpnPrefix() === '' ? 'n.a.-' . $currentPath : $currentPath;
|
|
|
|
|
|
|
|
|
|
$ipnSuggestions['commonPrefixes'][] = [
|
|
|
|
|
'title' => $currentPath . '-',
|
|
|
|
|
'description' => $this->translator->trans('part.edit.tab.advanced.ipn.prefix.hierarchical.no_increment')
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$increment = $this->generateNextPossiblePartIncrement($currentPath, $part, $autocompletePartDigits);
|
|
|
|
|
|
|
|
|
|
$ipnSuggestions['prefixesPartIncrement'][] = [
|
|
|
|
|
'title' => $currentPath . '-' . $increment,
|
|
|
|
|
'description' => $this->translator->trans('part.edit.tab.advanced.ipn.prefix.hierarchical.increment')
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Move to the next parent category
|
|
|
|
|
$parentCategory = $parentCategory->getParent();
|
|
|
|
|
}
|
|
|
|
|
} elseif ($part->getID() === null) {
|
|
|
|
|
$ipnSuggestions['commonPrefixes'][] = [
|
|
|
|
|
'title' => 'n.a.',
|
|
|
|
|
'description' => $this->translator->trans('part.edit.tab.advanced.ipn.prefix.not_saved')
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ipnSuggestions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function generateNextPossiblePartIncrement(string $currentPath, Part $currentPart, int $autocompletePartDigits): string
|
|
|
|
|
{
|
|
|
|
|
$qb = $this->createQueryBuilder('part');
|
|
|
|
|
|
|
|
|
|
$expectedLength = strlen($currentPath) + 1 + $autocompletePartDigits; // Path + '-' + $autocompletePartDigits digits
|
|
|
|
|
|
|
|
|
|
// Fetch all parts in the given category, sorted by their ID in ascending order
|
|
|
|
|
$qb->select('part')
|
|
|
|
|
->where('part.ipn LIKE :ipnPattern')
|
|
|
|
|
->andWhere('LENGTH(part.ipn) = :expectedLength')
|
|
|
|
|
->setParameter('ipnPattern', $currentPath . '%')
|
|
|
|
|
->setParameter('expectedLength', $expectedLength)
|
|
|
|
|
->orderBy('part.id', 'ASC');
|
|
|
|
|
|
|
|
|
|
$parts = $qb->getQuery()->getResult();
|
|
|
|
|
|
|
|
|
|
// Collect all used increments in the category
|
|
|
|
|
$usedIncrements = [];
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
if ($part->getIpn() === null || $part->getIpn() === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($part->getId() === $currentPart->getId()) {
|
|
|
|
|
// Extract and return the current part's increment directly
|
|
|
|
|
$incrementPart = substr($part->getIpn(), -$autocompletePartDigits);
|
|
|
|
|
if (is_numeric($incrementPart)) {
|
|
|
|
|
return str_pad((string) $incrementPart, $autocompletePartDigits, '0', STR_PAD_LEFT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract last $autocompletePartDigits digits for possible available part increment
|
|
|
|
|
$incrementPart = substr($part->getIpn(), -$autocompletePartDigits);
|
|
|
|
|
if (is_numeric($incrementPart)) {
|
|
|
|
|
$usedIncrements[] = (int) $incrementPart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate the next free $autocompletePartDigits-digit increment
|
|
|
|
|
$nextIncrement = 1; // Start at the beginning
|
|
|
|
|
|
|
|
|
|
while (in_array($nextIncrement, $usedIncrements)) {
|
|
|
|
|
$nextIncrement++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return str_pad((string) $nextIncrement, $autocompletePartDigits, '0', STR_PAD_LEFT);
|
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|