fix: treat dates without price as unavailable
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user