Files
myep-team/src/Repository/UploadRepository.php
T
2026-09-09 11:14:00 +02:00

235 lines
7.9 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Upload;
use App\Model\DocumentFilterDto;
use App\Repository\Traits\QueryHelperTrait;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Upload>
*
* @method Upload|null find($id, $lockMode = null, $lockVersion = null)
* @method Upload|null findOneBy(array $criteria, array $orderBy = null)
* @method Upload[] findAll()
* @method Upload[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UploadRepository extends ServiceEntityRepository
{
use QueryHelperTrait;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Upload::class);
}
public function getGlobalListQuery(): Query
{
$qb = $this->createQueryBuilder('upload');
return $qb
->where($qb->expr()->eq('upload.type', ':type'))
->setParameter('type', Upload::TYPE_DOCUMENT)
->getQuery()
;
}
public function getGlobal(): array
{
return $this
->getGlobalListQuery()
->getResult()
;
}
public function getAutocompletionData(string $search): array
{
$qb = $this->createQueryBuilder('upload');
$documents = $qb
->where($qb->expr()->orX(
$qb->expr()->like('upload.originalFilename', ':filename'),
$qb->expr()->like('upload.displayName', ':filename')
))
->setParameter('filename', '%'.$this->escapeLikeWildcards($search).'%')
->orderBy('upload.originalFilename', \SortDirection::Ascending)
->getQuery()
->getResult()
;
$data = [
[
'value' => '',
'text' => '...',
],
];
foreach ($documents as $document) {
/* @var Upload $document */
$data[] = [
'value' => $document->getId(),
'text' => $document->getDisplayName() ?? $document->getOriginalFilename(),
];
}
return $data;
}
public function getBatchDownloadListQuery(
string $type = Upload::TYPE_INVOICE,
string $status = Upload::STATUS_PAID,
): Query {
$qb = $this->createQueryBuilder('upload');
return $qb
->select('upload', 'disposition', 'assignment', 'job_profile', 'destination', 'teamer', 'user')
->innerJoin('upload.disposition', 'disposition')
->innerJoin('disposition.assignment', 'assignment')
->innerJoin('assignment.jobProfile', 'job_profile')
->innerJoin('assignment.destination', 'destination')
->innerJoin('disposition.teamer', 'teamer')
->innerJoin('teamer.user', 'user')
->where($qb->expr()->andX(
$qb->expr()->eq('upload.type', ':type'),
$qb->expr()->eq('upload.status', ':status'),
$qb->expr()->isNull('upload.downloadedAt'),
))
->orderBy('upload.createdAt', \SortDirection::Descending)
->setParameter('type', $type)
->setParameter('status', $status)
->getQuery()
;
}
public function getBatchDownloadList(
string $type = Upload::TYPE_INVOICE,
string $status = Upload::STATUS_PAID,
): array {
return $this
->getBatchDownloadListQuery($type, $status)
->getResult()
;
}
public function getUploadListQuery(DocumentFilterDto $filterDto): Query
{
$qb = $this->createQueryBuilder('upload');
$qb
->select('upload', 'disposition', 'assignment', 'job_profile', 'destination', 'teamer', 'user')
->innerJoin('upload.disposition', 'disposition')
->innerJoin('disposition.assignment', 'assignment')
->innerJoin('assignment.jobProfile', 'job_profile')
->innerJoin('assignment.destination', 'destination')
->innerJoin('disposition.teamer', 'teamer')
->innerJoin('teamer.user', 'user')
->addOrderBy('FIELD(upload.status, :checked, :new)', \SortDirection::Descending)
->setParameter('new', Upload::STATUS_NEW)
->setParameter('checked', Upload::STATUS_CHECKED)
;
if (null !== $teamer = $filterDto->getTeamer()) {
$qb
->andWhere($qb->expr()->eq('disposition.teamer', ':teamer'))
->setParameter('teamer', $teamer)
;
}
if (null !== $type = $filterDto->getType()) {
$qb
->andWhere($qb->expr()->eq('upload.type', ':type'))
->setParameter('type', $type)
;
} else {
// Belege are evidence for a Honorarnote, never checked in their own right - they have
// no status and are reachable from the check modal of their invoice. This is the only
// upload query that is not already scoped to a type, so it is the only one that would
// otherwise pad the list with them.
$qb
->andWhere($qb->expr()->neq('upload.type', ':excludedType'))
->setParameter('excludedType', Upload::TYPE_RECEIPT)
;
}
if (null !== $dateFrom = $filterDto->getDateFrom()) {
$qb
->andWhere($qb->expr()->gte('destination.dateFrom', ':dateFrom'))
->setParameter('dateFrom', $dateFrom)
;
}
if (null !== $dateTo = $filterDto->getDateTo()) {
$qb
->andWhere($qb->expr()->lte('destination.dateTo', ':dateTo'))
->setParameter('dateTo', $dateTo)
;
}
if (null !== $status = $filterDto->getStatus()) {
$qb
->andWhere($qb->expr()->eq('upload.status', ':status'))
->setParameter('status', $status)
;
}
if ($jobProfiles = $filterDto->getJobProfiles()) {
$qb
->andWhere($qb->expr()->in('assignment.jobProfile', ':jobProfiles'))
->setParameter('jobProfiles', $jobProfiles)
;
}
foreach ($filterDto->getHotels() as $hotel) {
$qb
->andWhere($qb->expr()->like('destination.hotel', ':hotel'))
->setParameter('hotel', '%'.$hotel.'%')
;
}
return $qb->getQuery();
}
public function getNewByType(string $type): array
{
$qb = $this->createQueryBuilder('upload');
return $qb
->select('upload', 'owner', 'teamer', 'user', 'disposition', 'assignment')
->innerJoin('upload.owner', 'owner')
->innerJoin('owner.teamer', 'teamer')
->innerJoin('teamer.user', 'user')
->leftJoin('upload.disposition', 'disposition')
->leftJoin('disposition.assignment', 'assignment')
->where($qb->expr()->andX(
$qb->expr()->eq('upload.type', ':type'),
$qb->expr()->in('upload.status', ':status')
))
->orderBy('upload.createdAt', \SortDirection::Descending)
->setParameter('type', $type)
->setParameter('status', [Upload::STATUS_NEW, Upload::STATUS_PENDING])
->getQuery()
->getResult()
;
}
public function getCountByStatusAndType(string $status, string $type): ?int
{
$qb = $this->createQueryBuilder('upload');
return $qb
->select($qb->expr()->count('upload'))
->where($qb->expr()->andX(
$qb->expr()->eq('upload.type', ':type'),
$qb->expr()->eq('upload.status', ':status')
))
->setParameter('type', $type)
->setParameter('status', $status)
->getQuery()
->getSingleScalarResult()
;
}
}