2019-03-05 23:52:45 +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/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-03-05 23:52:45 +01:00
|
|
|
namespace App\DataTables;
|
|
|
|
|
2023-07-22 23:51:06 +02:00
|
|
|
use App\DataTables\Adapters\FetchResultsAtOnceORMAdapter;
|
2023-07-23 21:48:38 +02:00
|
|
|
use App\DataTables\Adapters\TwoStepORMAdapater;
|
2023-07-08 23:49:47 +02:00
|
|
|
use App\DataTables\Column\EnumColumn;
|
2023-07-29 16:42:27 +02:00
|
|
|
use App\Doctrine\Helpers\FieldHelper;
|
2023-07-08 23:49:47 +02:00
|
|
|
use App\Entity\Parts\ManufacturingStatus;
|
2023-07-22 23:51:06 +02:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
|
use Doctrine\ORM\Query;
|
2023-07-23 21:48:38 +02:00
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator;
|
2023-07-22 23:51:06 +02:00
|
|
|
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
|
|
|
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
|
2023-10-04 21:14:57 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2023-06-11 14:55:06 +02:00
|
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
|
|
use App\Entity\Parts\Storelocation;
|
2019-09-07 15:23:03 +02:00
|
|
|
use App\DataTables\Column\EntityColumn;
|
2020-05-22 22:31:19 +02:00
|
|
|
use App\DataTables\Column\IconLinkColumn;
|
2019-09-07 12:48:49 +02:00
|
|
|
use App\DataTables\Column\LocaleDateTimeColumn;
|
2019-11-10 19:43:55 +01:00
|
|
|
use App\DataTables\Column\MarkdownColumn;
|
2019-11-10 19:16:39 +01:00
|
|
|
use App\DataTables\Column\PartAttachmentsColumn;
|
2022-09-09 00:10:29 +02:00
|
|
|
use App\DataTables\Column\PrettyBoolColumn;
|
2022-12-17 01:19:52 +01:00
|
|
|
use App\DataTables\Column\RowClassColumn;
|
2022-09-10 01:26:09 +02:00
|
|
|
use App\DataTables\Column\SelectColumn;
|
2022-09-10 00:37:47 +02:00
|
|
|
use App\DataTables\Column\SIUnitNumberColumn;
|
2019-11-10 19:38:36 +01:00
|
|
|
use App\DataTables\Column\TagsColumn;
|
2022-08-15 01:01:27 +02:00
|
|
|
use App\DataTables\Filters\PartFilter;
|
2022-09-09 23:33:49 +02:00
|
|
|
use App\DataTables\Filters\PartSearchFilter;
|
2022-12-18 21:58:21 +01:00
|
|
|
use App\DataTables\Helpers\PartDataTableHelper;
|
2019-08-12 18:04:53 +02:00
|
|
|
use App\Entity\Parts\Part;
|
2022-09-04 03:37:54 +02:00
|
|
|
use App\Entity\Parts\PartLot;
|
2022-12-18 17:28:42 +01:00
|
|
|
use App\Services\Formatters\AmountFormatter;
|
2019-03-05 23:52:45 +01:00
|
|
|
use App\Services\EntityURLGenerator;
|
|
|
|
use Doctrine\ORM\QueryBuilder;
|
|
|
|
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
|
|
|
|
use Omines\DataTablesBundle\Column\TextColumn;
|
|
|
|
use Omines\DataTablesBundle\DataTable;
|
|
|
|
use Omines\DataTablesBundle\DataTableTypeInterface;
|
2020-03-08 13:05:53 +01:00
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
2019-09-06 18:25:24 +02:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2019-03-05 23:52:45 +01:00
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
final class PartsDataTable implements DataTableTypeInterface
|
2019-03-05 23:52:45 +01:00
|
|
|
{
|
2023-10-04 21:14:57 +02:00
|
|
|
public function __construct(private readonly EntityURLGenerator $urlGenerator, private readonly TranslatorInterface $translator, private readonly AmountFormatter $amountFormatter, private readonly PartDataTableHelper $partDataTableHelper, private readonly Security $security, private readonly string $default_part_columns, protected LoggerInterface $logger)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function configureOptions(OptionsResolver $optionsResolver): void
|
|
|
|
{
|
|
|
|
$optionsResolver->setDefaults([
|
|
|
|
'filter' => null,
|
|
|
|
'search' => null
|
|
|
|
]);
|
|
|
|
|
|
|
|
$optionsResolver->setAllowedTypes('filter', [PartFilter::class, 'null']);
|
|
|
|
$optionsResolver->setAllowedTypes('search', [PartSearchFilter::class, 'null']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function configure(DataTable $dataTable, array $options): void
|
|
|
|
{
|
|
|
|
$resolver = new OptionsResolver();
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
$options = $resolver->resolve($options);
|
|
|
|
|
|
|
|
$dataTable
|
|
|
|
//Color the table rows depending on the review and favorite status
|
|
|
|
->add('dont_matter', RowClassColumn::class, [
|
|
|
|
'render' => function ($value, Part $context): string {
|
|
|
|
if ($context->isNeedsReview()) {
|
|
|
|
return 'table-secondary';
|
|
|
|
}
|
|
|
|
if ($context->isFavorite()) {
|
|
|
|
return 'table-info';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ''; //Default coloring otherwise
|
|
|
|
},
|
|
|
|
])
|
|
|
|
|
|
|
|
->add('select', SelectColumn::class)
|
|
|
|
->add('picture', TextColumn::class, [
|
|
|
|
'label' => '',
|
|
|
|
'className' => 'no-colvis',
|
|
|
|
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderPicture($context),
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
// internal array of $dataTable->add(...) parameters. Parameters will be later passed to the method
|
|
|
|
// after sorting columns according to TABLE_PART_DEFAULT_COLUMNS option.
|
|
|
|
$columns = [];
|
|
|
|
|
|
|
|
$columns['name'] = [
|
|
|
|
'name', TextColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.name'),
|
|
|
|
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderName($context),
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['id'] = [
|
|
|
|
'id', TextColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.id'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['ipn'] = [
|
|
|
|
'ipn', TextColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.ipn'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['description'] = [
|
|
|
|
'description', MarkdownColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.description'),
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->security->isGranted('@categories.read')) {
|
|
|
|
$columns['category'] = [
|
|
|
|
'category', EntityColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.category'),
|
|
|
|
'property' => 'category',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->security->isGranted('@footprints.read')) {
|
|
|
|
$columns['footprint'] = [
|
|
|
|
'footprint', EntityColumn::class,
|
|
|
|
[
|
|
|
|
'property' => 'footprint',
|
|
|
|
'label' => $this->translator->trans('part.table.footprint'),
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($this->security->isGranted('@manufacturers.read')) {
|
|
|
|
$columns['manufacturer'] = [
|
|
|
|
'manufacturer', EntityColumn::class,
|
|
|
|
[
|
|
|
|
'property' => 'manufacturer',
|
|
|
|
'label' => $this->translator->trans('part.table.manufacturer'),
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($this->security->isGranted('@storelocations.read')) {
|
|
|
|
$columns['storelocation'] = [
|
|
|
|
'storelocation', TextColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.storeLocations'),
|
|
|
|
'orderField' => 'storelocations.name',
|
|
|
|
'render' => function ($value, Part $context): string {
|
|
|
|
$tmp = [];
|
|
|
|
foreach ($context->getPartLots() as $lot) {
|
|
|
|
//Ignore lots without storelocation
|
|
|
|
if (!$lot->getStorageLocation() instanceof Storelocation) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$tmp[] = sprintf(
|
|
|
|
'<a href="%s" title="%s">%s</a>',
|
|
|
|
$this->urlGenerator->listPartsURL($lot->getStorageLocation()),
|
|
|
|
htmlspecialchars($lot->getStorageLocation()->getFullPath()),
|
|
|
|
htmlspecialchars($lot->getStorageLocation()->getName())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('<br>', $tmp);
|
|
|
|
},
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$columns['amount'] = [
|
|
|
|
'amount', TextColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.amount'),
|
|
|
|
'render' => function ($value, Part $context) {
|
|
|
|
$amount = $context->getAmountSum();
|
|
|
|
$expiredAmount = $context->getExpiredAmountSum();
|
|
|
|
|
|
|
|
$ret = '';
|
|
|
|
|
|
|
|
if ($context->isAmountUnknown()) {
|
|
|
|
//When all amounts are unknown, we show a question mark
|
|
|
|
if ($amount === 0.0) {
|
|
|
|
$ret .= sprintf('<b class="text-primary" title="%s">?</b>',
|
|
|
|
$this->translator->trans('part_lots.instock_unknown'));
|
|
|
|
} else { //Otherwise mark it with greater equal and the (known) amount
|
|
|
|
$ret .= sprintf('<b class="text-primary" title="%s">≥</b>',
|
|
|
|
$this->translator->trans('part_lots.instock_unknown')
|
|
|
|
);
|
|
|
|
$ret .= htmlspecialchars($this->amountFormatter->format($amount, $context->getPartUnit()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ret .= htmlspecialchars($this->amountFormatter->format($amount, $context->getPartUnit()));
|
|
|
|
}
|
2022-11-28 23:59:01 +01:00
|
|
|
|
|
|
|
//If we have expired lots, we show them in parentheses behind
|
|
|
|
if ($expiredAmount > 0) {
|
|
|
|
$ret .= sprintf(' <span title="%s" class="text-muted">(+%s)</span>',
|
|
|
|
$this->translator->trans('part_lots.is_expired'),
|
2023-02-26 01:23:36 +01:00
|
|
|
htmlspecialchars($this->amountFormatter->format($expiredAmount, $context->getPartUnit())));
|
2022-11-28 23:59:01 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 23:15:29 +02:00
|
|
|
//When the amount is below the minimum amount, we highlight the number red
|
|
|
|
if ($context->isNotEnoughInstock()) {
|
|
|
|
$ret = sprintf('<b class="text-danger" title="%s">%s</b>',
|
|
|
|
$this->translator->trans('part.info.amount.less_than_desired'),
|
|
|
|
$ret);
|
|
|
|
}
|
2022-11-28 23:59:01 +01:00
|
|
|
|
2023-10-04 21:14:57 +02:00
|
|
|
return $ret;
|
|
|
|
},
|
|
|
|
'orderField' => 'amountSum'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['minamount'] = [
|
|
|
|
'minamount', TextColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.minamount'),
|
|
|
|
'visible' => false,
|
|
|
|
'render' => fn($value, Part $context): string => htmlspecialchars($this->amountFormatter->format($value, $context->getPartUnit())),
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->security->isGranted('@footprints.read')) {
|
|
|
|
$columns['partUnit'] = [
|
|
|
|
'partUnit', TextColumn::class,
|
|
|
|
[
|
|
|
|
'field' => 'partUnit.name',
|
|
|
|
'label' => $this->translator->trans('part.table.partUnit'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$columns['addedDate'] = [
|
|
|
|
'addedDate', LocaleDateTimeColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.addedDate'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['lastModified'] = [
|
|
|
|
'lastModified', LocaleDateTimeColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.lastModified'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['needs_review'] = [
|
|
|
|
'needs_review', PrettyBoolColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.needsReview'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['favorite'] = [
|
|
|
|
'favorite', PrettyBoolColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.favorite'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['manufacturing_status'] = [
|
|
|
|
'manufacturing_status', EnumColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.manufacturingStatus'),
|
|
|
|
'visible' => false,
|
|
|
|
'class' => ManufacturingStatus::class,
|
|
|
|
'render' => function(?ManufacturingStatus $status, Part $context): string {
|
|
|
|
if (!$status) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->translator->trans($status->toTranslationKey());
|
|
|
|
} ,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['manufacturer_product_number'] = [
|
|
|
|
'manufacturer_product_number', TextColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.mpn'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['mass'] = [
|
|
|
|
'mass', SIUnitNumberColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.mass'),
|
|
|
|
'visible' => false,
|
|
|
|
'unit' => 'g'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['tags'] = [
|
|
|
|
'tags', TagsColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.tags'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['attachments'] = [
|
|
|
|
'attachments', PartAttachmentsColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.attachments'),
|
|
|
|
'visible' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$columns['edit'] = [
|
|
|
|
'edit', IconLinkColumn::class,
|
|
|
|
[
|
|
|
|
'label' => $this->translator->trans('part.table.edit'),
|
|
|
|
'visible' => false,
|
|
|
|
'href' => fn($value, Part $context) => $this->urlGenerator->editURL($context),
|
|
|
|
'disabled' => fn($value, Part $context) => !$this->security->isGranted('edit', $context),
|
|
|
|
'title' => $this->translator->trans('part.table.edit.title'),
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$visible_columns_ids = array_map("trim", explode(",", $this->default_part_columns));
|
|
|
|
$allowed_configurable_columns_ids = ["name", "id", "ipn", "description", "category", "footprint", "manufacturer",
|
|
|
|
"storelocation", "amount", "minamount", "partUnit", "addedDate", "lastModified", "needs_review", "favorite",
|
|
|
|
"manufacturing_status", "manufacturer_product_number", "mass", "tags", "attachments", "edit"
|
|
|
|
];
|
|
|
|
$processed_columns = [];
|
|
|
|
|
|
|
|
foreach ($visible_columns_ids as $col_id) {
|
|
|
|
if (!in_array($col_id, $allowed_configurable_columns_ids) || !isset($columns[$col_id])) {
|
|
|
|
$this->logger->warning("Configuration option TABLE_PART_DEFAULT_COLUMNS specify invalid column '$col_id'. Collumn is skipped.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($col_id, $processed_columns)) {
|
|
|
|
$this->logger->warning("Configuration option TABLE_PART_DEFAULT_COLUMNS specify column '$col_id' multiple time. Only first occurence is used.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = [];
|
|
|
|
if (count($columns[$col_id]) >= 3) {
|
|
|
|
$options = $columns[$col_id][2];
|
|
|
|
}
|
|
|
|
$options["visible"] = true;
|
|
|
|
$dataTable->add($col_id, $columns[$col_id][1], $options);
|
|
|
|
|
|
|
|
$processed_columns[] = $col_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add remaining non-visible columns
|
|
|
|
foreach ($allowed_configurable_columns_ids as $col_id) {
|
|
|
|
if (in_array($col_id, $processed_columns)) {
|
|
|
|
// column already processed
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = [];
|
|
|
|
if (count($columns[$col_id]) >= 3) {
|
|
|
|
$options = $columns[$col_id][2];
|
|
|
|
}
|
|
|
|
$options["visible"] = false;
|
|
|
|
$dataTable->add($col_id, $columns[$col_id][1], $options);
|
|
|
|
|
|
|
|
$processed_columns[] = $col_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataTable->addOrderBy('name')
|
|
|
|
->createAdapter(TwoStepORMAdapater::class, [
|
|
|
|
'filter_query' => $this->getFilterQuery(...),
|
|
|
|
'detail_query' => $this->getDetailQuery(...),
|
|
|
|
'entity' => Part::class,
|
|
|
|
'hydrate' => Query::HYDRATE_OBJECT,
|
|
|
|
'criteria' => [
|
|
|
|
function (QueryBuilder $builder) use ($options): void {
|
|
|
|
$this->buildCriteria($builder, $options);
|
|
|
|
},
|
|
|
|
new SearchCriteriaProvider(),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function getFilterQuery(QueryBuilder $builder): void
|
|
|
|
{
|
|
|
|
/* In the filter query we only select the IDs. The fetching of the full entities is done in the detail query.
|
|
|
|
* We only need to join the entities here, so we can filter by them.
|
|
|
|
* The filter conditions are added to this QB in the buildCriteria method.
|
|
|
|
*/
|
|
|
|
$builder
|
|
|
|
->select('part.id')
|
|
|
|
->addSelect('part.minamount AS HIDDEN minamount')
|
|
|
|
//Calculate amount sum using a subquery, so we can filter and sort by it
|
|
|
|
->addSelect(
|
|
|
|
'(
|
2023-07-23 21:48:38 +02:00
|
|
|
SELECT IFNULL(SUM(partLot.amount), 0.0)
|
|
|
|
FROM '. PartLot::class. ' partLot
|
|
|
|
WHERE partLot.part = part.id
|
|
|
|
AND partLot.instock_unknown = false
|
|
|
|
AND (partLot.expiration_date IS NULL OR partLot.expiration_date > CURRENT_DATE())
|
|
|
|
) AS HIDDEN amountSum'
|
2023-10-04 21:14:57 +02:00
|
|
|
)
|
|
|
|
->from(Part::class, 'part')
|
|
|
|
->leftJoin('part.category', 'category')
|
|
|
|
->leftJoin('part.master_picture_attachment', 'master_picture_attachment')
|
|
|
|
->leftJoin('part.partLots', 'partLots')
|
|
|
|
->leftJoin('partLots.storage_location', 'storelocations')
|
|
|
|
->leftJoin('part.footprint', 'footprint')
|
|
|
|
->leftJoin('footprint.master_picture_attachment', 'footprint_attachment')
|
|
|
|
->leftJoin('part.manufacturer', 'manufacturer')
|
|
|
|
->leftJoin('part.orderdetails', 'orderdetails')
|
|
|
|
->leftJoin('orderdetails.supplier', 'suppliers')
|
|
|
|
->leftJoin('part.attachments', 'attachments')
|
|
|
|
->leftJoin('part.partUnit', 'partUnit')
|
|
|
|
->leftJoin('part.parameters', 'parameters')
|
|
|
|
|
|
|
|
//This must be the only group by, or the paginator will not work correctly
|
|
|
|
->addGroupBy('part.id')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getDetailQuery(QueryBuilder $builder, array $filter_results): void
|
|
|
|
{
|
|
|
|
$ids = array_map(fn($row) => $row['id'], $filter_results);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In this query we take the IDs which were filtered, paginated and sorted in the filter query, and fetch the
|
|
|
|
* full entities.
|
|
|
|
* We can do complex fetch joins, as we do not need to filter or sort here (which would kill the performance).
|
|
|
|
* The only condition should be for the IDs.
|
|
|
|
* It is important that elements are ordered the same way, as the IDs are passed, or ordering will be wrong.
|
|
|
|
*/
|
|
|
|
$builder
|
|
|
|
->select('part')
|
|
|
|
->addSelect('category')
|
|
|
|
->addSelect('footprint')
|
|
|
|
->addSelect('manufacturer')
|
|
|
|
->addSelect('partUnit')
|
|
|
|
->addSelect('master_picture_attachment')
|
|
|
|
->addSelect('footprint_attachment')
|
|
|
|
->addSelect('partLots')
|
|
|
|
->addSelect('orderdetails')
|
|
|
|
->addSelect('attachments')
|
|
|
|
->addSelect('storelocations')
|
|
|
|
//Calculate amount sum using a subquery, so we can filter and sort by it
|
|
|
|
->addSelect(
|
|
|
|
'(
|
2022-09-04 03:37:54 +02:00
|
|
|
SELECT IFNULL(SUM(partLot.amount), 0.0)
|
|
|
|
FROM '. PartLot::class. ' partLot
|
|
|
|
WHERE partLot.part = part.id
|
|
|
|
AND partLot.instock_unknown = false
|
|
|
|
AND (partLot.expiration_date IS NULL OR partLot.expiration_date > CURRENT_DATE())
|
|
|
|
) AS HIDDEN amountSum'
|
2023-10-04 21:14:57 +02:00
|
|
|
)
|
|
|
|
->from(Part::class, 'part')
|
|
|
|
->leftJoin('part.category', 'category')
|
|
|
|
->leftJoin('part.master_picture_attachment', 'master_picture_attachment')
|
|
|
|
->leftJoin('part.partLots', 'partLots')
|
|
|
|
->leftJoin('partLots.storage_location', 'storelocations')
|
|
|
|
->leftJoin('part.footprint', 'footprint')
|
|
|
|
->leftJoin('footprint.master_picture_attachment', 'footprint_attachment')
|
|
|
|
->leftJoin('part.manufacturer', 'manufacturer')
|
|
|
|
->leftJoin('part.orderdetails', 'orderdetails')
|
|
|
|
->leftJoin('orderdetails.supplier', 'suppliers')
|
|
|
|
->leftJoin('part.attachments', 'attachments')
|
|
|
|
->leftJoin('part.partUnit', 'partUnit')
|
|
|
|
->leftJoin('part.parameters', 'parameters')
|
|
|
|
|
|
|
|
->where('part.id IN (:ids)')
|
|
|
|
->setParameter('ids', $ids)
|
|
|
|
|
|
|
|
//We have to group by all elements, or only the first sub elements of an association is fetched! (caused issue #190)
|
|
|
|
->addGroupBy('part')
|
|
|
|
->addGroupBy('partLots')
|
|
|
|
->addGroupBy('category')
|
|
|
|
->addGroupBy('master_picture_attachment')
|
|
|
|
->addGroupBy('storelocations')
|
|
|
|
->addGroupBy('footprint')
|
|
|
|
->addGroupBy('footprint_attachment')
|
|
|
|
->addGroupBy('manufacturer')
|
|
|
|
->addGroupBy('orderdetails')
|
|
|
|
->addGroupBy('suppliers')
|
|
|
|
->addGroupBy('attachments')
|
|
|
|
->addGroupBy('partUnit')
|
|
|
|
->addGroupBy('parameters')
|
|
|
|
;
|
|
|
|
|
|
|
|
//Get the results in the same order as the IDs were passed
|
|
|
|
FieldHelper::addOrderByFieldParam($builder, 'part.id', 'ids');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildCriteria(QueryBuilder $builder, array $options): void
|
|
|
|
{
|
|
|
|
//Apply the search criterias first
|
|
|
|
if ($options['search'] instanceof PartSearchFilter) {
|
|
|
|
$search = $options['search'];
|
|
|
|
$search->apply($builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
//We do the most stuff here in the filter class
|
|
|
|
if ($options['filter'] instanceof PartFilter) {
|
|
|
|
$filter = $options['filter'];
|
|
|
|
$filter->apply($builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|