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

292 lines
11 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Application;
use App\Entity\Assignment;
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', 'teamer')
->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', ':applicationStatus'))
->leftJoin('assignment.dispositions', 'disposition')
->leftJoin('disposition.teamer', 'teamer')
->setParameter('applicationStatus', Application::STATUS_REJECTED)
;
$this->applyFilterSettings($filterDto, $qb);
return $qb->getQuery();
}
public function getListQueryForTeamer(Teamer $teamer, AssignmentFilterDto $filterDto): Query
{
// Find ids of assignments with available slots first
$availableAssignmentsIds = $this->getAvailableAssignmentsIds();
$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'),
$qb->expr()->notIn('assignment.status', ':status'),
$qb->expr()->in('assignment.id', $availableAssignmentsIds)
))
->leftJoin('assignment.applications', 'application', Join::WITH, $qb->expr()->eq('application.teamer', ':teamer'))
->leftJoin('assignment.dispositions', 'disposition', Join::WITH, $qb->expr()->eq('disposition.teamer', ':teamer'))
->groupBy('assignment')
->setParameter('teamer', $teamer)
->setParameter('status', [Assignment::STATUS_DRAFT, Assignment::STATUS_CALLED_OFF])
->setParameter('now', new \DateTimeImmutable())
;
$this->applyFilterSettings($filterDto, $qb);
return $qb->getQuery();
}
private function applyFilterSettings(AssignmentFilterDto $filterDto, QueryBuilder $qb): void
{
if (false === $filterDto->isIncludePast()) {
$qb
->andWhere($qb->expr()->gte('destination.dateFrom', ':now'))
->setParameter('now', new \DateTimeImmutable())
;
}
if (0 < count($filterDto->getStatus())) {
$qb
->andWhere($qb->expr()->in('assignment.staffingStatus', ':status'))
->setParameter('status', $filterDto->getStatus())
;
}
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)
;
}
foreach ($filterDto->getHotels() as $hotel) {
$qb
->andWhere($qb->expr()->like('destination.product', ':hotel'))
->setParameter('hotel', '%'.$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)
;
}
}
}
public function getBookmarkQueryForTeamer(Teamer $teamer): Query
{
// Find ids of assignments with available slots first
$availableAssignmentsIds = $this->getAvailableAssignmentsIds();
$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()->andX(
$qb->expr()->isNull('assignment.deletedAt'),
$qb->expr()->in('assignment.id', $availableAssignmentsIds)
))
->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);
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()->eq('assignment.status', ':status'),
$qb->expr()->in('assignment.jobProfile', ':jobProfiles'),
$qb->expr()->isNull('application'),
$qb->expr()->isNull('disposition'),
$qb->expr()->orX(
$qb->expr()->gt('assignment.dateFrom', ':dateFrom'),
$qb->expr()->gt('destination.dateFrom', ':dateFrom')
)
))
->setParameter('status', Assignment::STATUS_PUBLISHED)
->setParameter('jobProfiles', $teamer->getJobProfiles())
->setParameter('teamer', $teamer)
->setParameter('dateFrom', new \DateTimeImmutable())
;
return $qb
->orderBy('assignment.createdAt', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
private function getAvailableAssignmentsIds(): array
{
$qb = $this->createQueryBuilder('assignment');
$availableAssignments = $qb
->select('assignment.id')
->leftJoin('assignment.dispositions', 'disposition')
->innerJoin('assignment.destination', 'destination')
->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()->notIn('assignment.status', ':status'),
$qb->expr()->isNull('assignment.deletedAt')
))
->groupBy('assignment')
->having($qb->expr()->gt('assignment.availableDispositions', $qb->expr()->count('disposition.id')))
->setParameter('status', [Assignment::STATUS_DRAFT, Assignment::STATUS_CALLED_OFF])
->setParameter('now', new \DateTimeImmutable())
->getQuery()
->getArrayResult()
;
return array_column($availableAssignments, 'id');
}
}