Files
myep-team/src/Repository/ApplicationRepository.php
T
2026-03-15 12:42:28 +01:00

212 lines
7.5 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Application;
use App\Entity\Assignment;
use App\Entity\Teamer;
use App\Model\ApplicationFilterDto;
use App\Model\AssignmentFilterDto;
use App\Repository\Traits\QueryHelperTrait;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Application>
*
* @method Application|null find($id, $lockMode = null, $lockVersion = null)
* @method Application|null findOneBy(array $criteria, array $orderBy = null)
* @method Application[] findAll()
* @method Application[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ApplicationRepository extends ServiceEntityRepository
{
use QueryHelperTrait;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Application::class);
}
public function getPendingQuery(ApplicationFilterDto $filterDto): Query
{
$qb = $this->createQueryBuilder('application');
$qb
->select('application', 'assignment', 'job_profile', 'destination', 'teamer', 'user')
->innerJoin('application.assignment', 'assignment')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->innerJoin('application.teamer', 'teamer')
->innerJoin('teamer.user', 'user')
->where($qb->expr()->neq('application.status', ':status'))
->setParameter('status', Application::STATUS_REJECTED)
;
if (false === $filterDto->isIncludePast()) {
$qb
->andWhere($qb->expr()->gte('destination.dateFrom', ':now'))
->setParameter('now', new \DateTimeImmutable())
;
}
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 ($jobProfiles = $filterDto->getJobProfiles()) {
$qb
->andWhere($qb->expr()->in('assignment.jobProfile', ':jobProfiles'))
->setParameter('jobProfiles', $jobProfiles)
;
}
if (0 < count($filterDto->getHotels())) {
$constraints = [];
foreach ($filterDto->getHotels() as $index => $hotel) {
$constraints[] = $qb->expr()->like('destination.hotel', ':hotel'.$index);
}
$qb->andWhere($qb->expr()->orX(...$constraints));
foreach ($filterDto->getHotels() as $index => $hotel) {
$qb->setParameter('hotel'.$index, '%'.$hotel.'%');
}
}
if (null !== $duration = $filterDto->getDuration()) {
$days = match ($duration) {
AssignmentFilterDto::DURATION_WEEKEND => [2, 4],
AssignmentFilterDto::DURATION_MID_WEEK => [5, 6],
AssignmentFilterDto::DURATION_FULL_WEEK => [7, 7],
default => [8, 0],
};
[$minDays, $maxDays] = $days;
$qb
->andWhere($qb->expr()->gte('DATEDIFF(destination.dateTo, destination.dateFrom)', ':minDays'))
->setParameter('minDays', $minDays)
;
if (0 < $maxDays) {
$qb
->andWhere($qb->expr()->lte('DATEDIFF(destination.dateTo, destination.dateFrom)', ':maxDays'))
->setParameter('maxDays', $maxDays)
;
}
}
if (null !== $country = $filterDto->getCountry()) {
$qb
->andWhere($qb->expr()->eq('destination.country', ':country'))
->setParameter('country', $country)
;
}
$teamerName = $filterDto->getTeamerName();
if (null !== $teamerName && '' !== trim($teamerName)) {
$qb
->andWhere($qb->expr()->orX(
$qb->expr()->like('user.firstName', ':teamerName'),
$qb->expr()->like('user.lastName', ':teamerName')
))
->setParameter('teamerName', '%'.$this->escapeLikeWildcards($teamerName).'%')
;
}
return $qb->getQuery();
}
public function getPendingForTeamerQuery(Teamer $teamer): Query
{
$qb = $this->createQueryBuilder('application');
return $qb
->select('application', 'assignment', 'destination', 'job_profile')
->innerJoin('application.assignment', 'assignment')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->where($qb->expr()->andX(
$qb->expr()->eq('application.teamer', ':teamer'),
$qb->expr()->orX(
$qb->expr()->neq('application.status', ':status'),
$qb->expr()->andX(
$qb->expr()->eq('application.status', ':status'),
$qb->expr()->gt('destination.dateFrom', ':today')
)
)
))
->setParameter('teamer', $teamer)
->setParameter('status', Application::STATUS_REJECTED)
->setParameter('today', new \DateTime())
->getQuery()
;
}
public function getPendingForTeamer(Teamer $teamer, int $limit = 5): array
{
return $this
->getPendingForTeamerQuery($teamer)
->setMaxResults($limit)
->getResult()
;
}
public function getCurrentByAssignment(Assignment $assignment): array
{
$qb = $this->createQueryBuilder('application');
return $qb
->select('application', 'teamer', 'feedback')
->innerJoin('application.teamer', 'teamer')
->leftJoin('teamer.feedback', 'feedback')
->where($qb->expr()->eq('application.assignment', ':assignment'))
->setParameter('assignment', $assignment)
->getQuery()
->getResult()
;
}
public function getNew(int $limit = 5): array
{
$qb = $this->createQueryBuilder('application');
return $qb
->select('application', 'assignment', 'destination', 'job_profile', 'teamer', 'user')
->innerJoin('application.assignment', 'assignment')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->innerJoin('application.teamer', 'teamer')
->innerJoin('teamer.user', 'user')
->where($qb->expr()->neq('application.status', ':status'))
->orderBy('application.createdAt', 'DESC')
->setParameter('status', Application::STATUS_REJECTED)
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
public function getCountByStatus(string $status): ?int
{
$qb = $this->createQueryBuilder('application');
return $qb
->select($qb->expr()->count('application'))
->where($qb->expr()->eq('application.status', ':status'))
->setParameter('status', $status)
->getQuery()
->getSingleScalarResult()
;
}
}