Files
myep-team/src/Repository/AssignmentRepository.php
T

243 lines
9.1 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Application;
use App\Entity\Assignment;
use App\Entity\Destination;
use App\Entity\Teamer;
use App\Model\AssignmentFilterDto;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Assignment>
*
* @method Assignment|null find($id, $lockMode = null, $lockVersion = null)
* @method Assignment|null findOneBy(array $criteria, array $orderBy = null)
* @method Assignment[] findAll()
* @method Assignment[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class AssignmentRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Assignment::class);
}
public function getListQuery(AssignmentFilterDto $filterDto): Query
{
$qb = $this->createQueryBuilder('assignment');
$qb
->select('assignment', 'destination', 'job_profile', 'application', 'disposition')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->where($qb->expr()->isNull('assignment.deletedAt'))
->leftJoin('assignment.applications', 'application', Join::WITH, $qb->expr()->neq('application.status', ':status'))
->leftJoin('assignment.dispositions', 'disposition')
->setParameter('status', Application::STATUS_REJECTED)
;
$this->applyFilterSettings($filterDto, $qb);
return $qb->getQuery();
}
public function getListQueryForTeamer(Teamer $teamer, AssignmentFilterDto $filterDto): Query
{
$qb = $this->createQueryBuilder('assignment');
$qb
->select('assignment', 'destination', 'job_profile', 'application', 'disposition')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->where($qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->gt('destination.dateFrom', ':now'),
$qb->expr()->andX(
$qb->expr()->isNotNull('assignment.dateFrom'),
$qb->expr()->gt('assignment.dateFrom', ':now')
)
),
$qb->expr()->isNull('assignment.deletedAt')
))
->leftJoin('assignment.applications', 'application', Join::WITH, $qb->expr()->eq('application.teamer', ':teamer'))
->leftJoin('assignment.dispositions', 'disposition', Join::WITH, $qb->expr()->eq('disposition.teamer', ':teamer'))
->setParameter('teamer', $teamer)
->setParameter('now', new \DateTimeImmutable())
;
$this->applyFilterSettings($filterDto, $qb);
return $qb->getQuery();
}
private function applyFilterSettings(AssignmentFilterDto $filterDto, QueryBuilder $qb): void
{
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 !== $jobProfile = $filterDto->getJobProfile()) {
$qb
->andWhere($qb->expr()->eq('assignment.jobProfile', ':jobProfile'))
->setParameter('jobProfile', $jobProfile)
;
}
if (null !== $destination = $filterDto->getDestination()) {
$qb
->andWhere($qb->expr()->eq('assignment.destination', ':destination'))
->setParameter('destination', $destination)
;
}
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)
;
}
}
}
public function getBookmarkQueryForTeamer(Teamer $teamer): Query
{
$qb = $this->createQueryBuilder('assignment');
$qb
->select('assignment', 'destination', 'job_profile', 'application', 'disposition')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->innerJoin('assignment.teamers', 'teamer', Join::WITH, $qb->expr()->eq('teamer', ':teamer'))
->leftJoin('assignment.applications', 'application', Join::WITH, $qb->expr()->eq('application.teamer', ':teamer'))
->leftJoin('assignment.dispositions', 'disposition', Join::WITH, $qb->expr()->eq('disposition.teamer', ':teamer'))
->where($qb->expr()->isNull('assignment.deletedAt'))
->setParameter('teamer', $teamer)
;
return $qb->getQuery();
}
public function getFilterOptions(): array
{
$options = [];
// Date range
$qb = $this->createQueryBuilder('assignment');
$result = $qb
->select('MIN(destination.dateFrom) minDate', 'MAX(destination.dateTo) maxDate')
->innerJoin('assignment.destination', 'destination')
->where($qb->expr()->isNull('assignment.deletedAt'))
->getQuery()
->getSingleResult()
;
$options['minDate'] = new \DateTimeImmutable($result['minDate']);
$options['maxDate'] = new \DateTimeImmutable($result['maxDate']);
// Job-profiles
$qb = $this->createQueryBuilder('assignment');
$result = $qb
->select('assignment', 'job_profile')
->innerJoin('assignment.jobProfile', 'job_profile')
->orderBy('job_profile.name', 'ASC')
->where($qb->expr()->isNull('assignment.deletedAt'))
->getQuery()
->getResult()
;
$options['jobProfiles'] = array_map(function (Assignment $assignment) {
return $assignment->getJobProfile();
}, $result);
// Destinations
$qb = $this->createQueryBuilder('assignment');
$result = $qb
->select('assignment', 'destination')
->innerJoin('assignment.destination', 'destination')
->where($qb->expr()->isNull('assignment.deletedAt'))
->orderBy('destination.product', 'ASC')
->groupBy('destination.id')
->getQuery()
->getResult()
;
$options['destinations'] = array_map(function (Assignment $assignment) {
return $assignment->getDestination();
}, $result);
return $options;
}
public function getNew(int $limit = 5): array
{
$qb = $this->createQueryBuilder('assignment');
return $qb
->select('assignment', 'destination', 'job_profile', 'owner')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->innerJoin('assignment.owner', 'owner')
->orderBy('assignment.createdAt', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
public function getNewMatchingTeamerProfile(Teamer $teamer, int $limit = 5): array
{
$qb = $this->createQueryBuilder('assignment');
$qb
->select('assignment', 'destination', 'job_profile')
->innerJoin('assignment.destination', 'destination')
->innerJoin('assignment.jobProfile', 'job_profile')
->leftJoin('assignment.applications', 'application', Join::WITH, $qb->expr()->eq('application.teamer', ':teamer'))
->leftJoin('assignment.dispositions', 'disposition', Join::WITH, $qb->expr()->eq('disposition.teamer', ':teamer'))
->where($qb->expr()->andX(
$qb->expr()->in('assignment.jobProfile', ':jobProfiles'),
$qb->expr()->isNull('application'),
$qb->expr()->isNull('disposition')
))
->setParameter('jobProfiles', $teamer->getJobProfiles())
->setParameter('teamer', $teamer)
;
return $qb
->orderBy('assignment.createdAt', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
}