103 lines
3.0 KiB
PHP
103 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repository\Groups;
|
|
|
|
use App\Entity\Groups\Accommodation;
|
|
use App\Entity\Groups\ContingentDay;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<ContingentDay>
|
|
*/
|
|
class ContingentDayRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, ContingentDay::class);
|
|
}
|
|
|
|
/**
|
|
* Returns the snapshot days within [dateFrom, dateTo] for the accommodation
|
|
* identified by the given hotel code, keyed by Y-m-d.
|
|
*
|
|
* @return array<string, ContingentDay>
|
|
*/
|
|
public function findByHotelCodeAndDateRange(
|
|
string $hotelCode,
|
|
\DateTimeImmutable $dateFrom,
|
|
\DateTimeImmutable $dateTo,
|
|
): array {
|
|
/** @var ContingentDay[] $days */
|
|
$days = $this->createQueryBuilder('cd')
|
|
->join('cd.accommodation', 'a')
|
|
->where('a.calendarCode = :hotelCode')
|
|
->andWhere('cd.date >= :dateFrom')
|
|
->andWhere('cd.date <= :dateTo')
|
|
->setParameter('hotelCode', $hotelCode)
|
|
->setParameter('dateFrom', $dateFrom)
|
|
->setParameter('dateTo', $dateTo)
|
|
->orderBy('cd.date', 'ASC')
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
return $this->indexByDate($days);
|
|
}
|
|
|
|
/**
|
|
* Same as above but for a known accommodation; used by the sync to diff against.
|
|
*
|
|
* @return array<string, ContingentDay>
|
|
*/
|
|
public function findByAccommodationAndDateRange(
|
|
Accommodation $accommodation,
|
|
\DateTimeImmutable $dateFrom,
|
|
\DateTimeImmutable $dateTo,
|
|
): array {
|
|
/** @var ContingentDay[] $days */
|
|
$days = $this->createQueryBuilder('cd')
|
|
->where('cd.accommodation = :accommodation')
|
|
->andWhere('cd.date >= :dateFrom')
|
|
->andWhere('cd.date <= :dateTo')
|
|
->setParameter('accommodation', $accommodation)
|
|
->setParameter('dateFrom', $dateFrom)
|
|
->setParameter('dateTo', $dateTo)
|
|
->orderBy('cd.date', 'ASC')
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
return $this->indexByDate($days);
|
|
}
|
|
|
|
/**
|
|
* Retention: drops snapshot days that are in the past and can no longer be requested.
|
|
*/
|
|
public function deleteBefore(\DateTimeImmutable $date): int
|
|
{
|
|
return (int) $this->createQueryBuilder('cd')
|
|
->delete()
|
|
->where('cd.date < :date')
|
|
->setParameter('date', $date)
|
|
->getQuery()
|
|
->execute();
|
|
}
|
|
|
|
/**
|
|
* @param ContingentDay[] $days
|
|
*
|
|
* @return array<string, ContingentDay>
|
|
*/
|
|
private function indexByDate(array $days): array
|
|
{
|
|
$indexed = [];
|
|
|
|
foreach ($days as $day) {
|
|
$indexed[$day->getDate()->format('Y-m-d')] = $day;
|
|
}
|
|
|
|
return $indexed;
|
|
}
|
|
}
|