*/ final class EmployeeContractPeriodRepository extends ServiceEntityRepository implements EmployeeContractPeriodReadRepositoryInterface { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, EmployeeContractPeriod::class); } /** * @param list $employees * * @return list */ public function findByEmployeesAndDateRange(array $employees, DateTimeImmutable $from, DateTimeImmutable $to): array { if ([] === $employees) { return []; } return $this->createQueryBuilder('p') ->andWhere('p.employee IN (:employees)') ->andWhere('p.startDate <= :to') ->andWhere('p.endDate IS NULL OR p.endDate >= :from') ->setParameter('employees', $employees) ->setParameter('from', $from) ->setParameter('to', $to) ->orderBy('p.startDate', 'ASC') ->getQuery() ->getResult() ; } public function findOneCoveringDate(Employee $employee, DateTimeImmutable $date): ?EmployeeContractPeriod { return $this->createQueryBuilder('p') ->andWhere('p.employee = :employee') ->andWhere('p.startDate <= :date') ->andWhere('p.endDate IS NULL OR p.endDate >= :date') ->setParameter('employee', $employee) ->setParameter('date', $date) ->orderBy('p.startDate', 'DESC') ->setMaxResults(1) ->getQuery() ->getOneOrNullResult() ; } public function closeOpenPeriods(Employee $employee, DateTimeImmutable $endDate): int { return $this->createQueryBuilder('p') ->update() ->set('p.endDate', ':endDate') ->andWhere('p.employee = :employee') ->andWhere('p.endDate IS NULL') ->setParameter('employee', $employee) ->setParameter('endDate', $endDate) ->getQuery() ->execute() ; } public function hasPaidLeaveSettledClosureBetween( Employee $employee, DateTimeImmutable $from, DateTimeImmutable $to ): bool { $count = $this->createQueryBuilder('p') ->select('COUNT(p.id)') ->andWhere('p.employee = :employee') ->andWhere('p.paidLeaveSettled = :paidLeaveSettled') ->andWhere('p.endDate IS NOT NULL') ->andWhere('p.endDate >= :from') ->andWhere('p.endDate <= :to') ->setParameter('employee', $employee) ->setParameter('paidLeaveSettled', true) ->setParameter('from', $from) ->setParameter('to', $to) ->getQuery() ->getSingleScalarResult() ; return (int) $count > 0; } public function findLatestPaidLeaveSettledClosureDateBetween( Employee $employee, DateTimeImmutable $from, DateTimeImmutable $to ): ?DateTimeImmutable { $result = $this->createQueryBuilder('p') ->select('p.endDate AS endDate') ->andWhere('p.employee = :employee') ->andWhere('p.paidLeaveSettled = :paidLeaveSettled') ->andWhere('p.endDate IS NOT NULL') ->andWhere('p.endDate >= :from') ->andWhere('p.endDate <= :to') ->setParameter('employee', $employee) ->setParameter('paidLeaveSettled', true) ->setParameter('from', $from) ->setParameter('to', $to) ->orderBy('p.endDate', 'DESC') ->setMaxResults(1) ->getQuery() ->getOneOrNullResult() ; if (!is_array($result) || !isset($result['endDate']) || !$result['endDate'] instanceof DateTimeImmutable) { return null; } return $result['endDate']; } }