* * @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() ; } /** * Finds all applications of a teamer whose assignment period overlaps the given period. * * The assignment period is the date range of its destination. Boundaries count as an * overlap: a teamer cannot end one assignment on the same day another one begins. * * @return array */ public function findOverlappingForTeamer(Teamer $teamer, CarbonPeriodImmutable $period): array { return $this ->getOverlappingForTeamerQuery($teamer, $period) ->getResult() ; } public function getOverlappingForTeamerQuery(Teamer $teamer, CarbonPeriodImmutable $period): Query { $qb = $this->createQueryBuilder('application'); return $qb ->innerJoin('application.assignment', 'assignment') ->innerJoin('assignment.destination', 'destination') ->where($qb->expr()->andX( $qb->expr()->eq('application.teamer', ':teamer'), $qb->expr()->neq('application.status', ':application_rejected'), $qb->expr()->neq('assignment.status', ':assignment_called_off'), $qb->expr()->isNull('assignment.deletedAt'), $qb->expr()->isNull('destination.deletedAt'), $qb->expr()->lte('destination.dateFrom', ':dateTo'), $qb->expr()->gte('destination.dateTo', ':dateFrom') )) ->setParameter('teamer', $teamer) ->setParameter('application_rejected', Application::STATUS_REJECTED) ->setParameter('assignment_called_off', Assignment::STATUS_CALLED_OFF) ->setParameter('dateFrom', $period->start->toDateTimeImmutable()) ->setParameter('dateTo', $period->end->toDateTimeImmutable()) ->getQuery() ; } 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', \SortDirection::Descending) ->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() ; } /** * The raw application tally grouped by normalized hotel code. * * Mirrors DispositionRepository::getFeedbackStatisticsByNormalizedHotelCodeQuery() - the * "Bewerbungen pro Destination" screen sits next to "Feedback pro Destination" and means the * same thing by a destination and a season, so the normalization and the date filter are the * shared ones. * * Status is deliberately not broken out: an application's status moves over its lifetime, so a * snapshot of the current status is not a meaningful figure for a past period. */ public function getApplicationsByDestinationQuery( ?\DateTimeImmutable $dateFrom = null, ?\DateTimeImmutable $dateTo = null, ): Query { $qb = $this->createQueryBuilder('application'); // Normalize hotel code: strip SER prefix to get base 3-char code $normalizedHotelCode = "CASE WHEN destination.hotelCode LIKE 'SER%' THEN SUBSTRING(destination.hotelCode, 4, 3) ELSE SUBSTRING(destination.hotelCode, 1, 3) END"; $qb ->select( $normalizedHotelCode.' AS hotelCode', 'COUNT(application.id) AS totalCount' ) ->innerJoin('application.assignment', 'assignment') ->innerJoin('assignment.destination', 'destination') ->groupBy('hotelCode') ->orderBy('totalCount', \SortDirection::Descending) ->addOrderBy('hotelCode', \SortDirection::Ascending) ; SeasonPeriodFilter::apply($qb, $dateFrom, $dateTo); return $qb->getQuery(); } /** * Returns the application count grouped by normalized hotel code (base 3-char code). * * @return array */ public function getApplicationsByDestination( ?\DateTimeImmutable $dateFrom = null, ?\DateTimeImmutable $dateTo = null, ): array { return array_map(static fn (array $row): array => [ 'hotelCode' => $row['hotelCode'], 'total' => (int) $row['totalCount'], ], $this->getApplicationsByDestinationQuery($dateFrom, $dateTo)->getResult()); } }