fix: treat dates without price as unavailable

This commit is contained in:
Björn Fromme
2026-08-04 11:29:58 +02:00
parent 331c88c38e
commit 8ff13614d2
8 changed files with 407 additions and 23 deletions
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\Groups\AccommodationPrice;
use App\Repository\Groups\AccommodationPriceRepository;
/**
* Determines which days of a date range are covered by an AccommodationPrice.
*
* A day without a covering price is not sold and must not be offered as available,
* regardless of what the external contingent calendar reports.
*/
final readonly class AccommodationPriceCoverage
{
public function __construct(
private AccommodationPriceRepository $priceRepository,
) {
}
/**
* @return array<string, true> keyed by Y-m-d, only covered days present
*/
public function coveredDates(string $hotelCode, \DateTimeImmutable $dateFrom, \DateTimeImmutable $dateTo): array
{
return $this->coveredDatesFor(
$this->priceRepository->findByHotelCodeAndDateRange($hotelCode, $dateFrom, $dateTo),
$dateFrom,
$dateTo,
);
}
/**
* @param AccommodationPrice[] $prices
*
* @return array<string, true> keyed by Y-m-d, only covered days present
*/
public function coveredDatesFor(array $prices, \DateTimeImmutable $dateFrom, \DateTimeImmutable $dateTo): array
{
$start = $dateFrom->setTime(0, 0);
$end = $dateTo->setTime(0, 0);
$covered = [];
// dateFrom and dateTo are both inclusive (last night, not checkout day)
for ($day = $start; $day <= $end; $day = $day->modify('+1 day')) {
foreach ($prices as $price) {
if ($price->getDateFrom() <= $day && $price->getDateTo() >= $day) {
$covered[$day->format('Y-m-d')] = true;
break;
}
}
}
return $covered;
}
}