wip: refactored price overrides

This commit is contained in:
Björn Fromme committed 2026-07-16 09:03:42 +02:00
1 parent bb7ae1adcc
commit 41424de151
13 files changed
+668 -118

No files matched your search

+68 -18
View File
@@ -7,7 +7,10 @@ namespace App\Controller\Api;
use App\BpnConnect\ContingentsClient;
use App\BpnConnect\Exception\BpnConnectException;
use App\Entity\Groups\AccommodationPrice;
use App\Enum\Groups\PriceType;
use App\Repository\Groups\AccommodationPriceRepository;
use App\Repository\Groups\AccommodationRepository;
use App\Service\PriceTimelineBuilder;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -24,11 +27,44 @@ class ContingentController extends AbstractController
{
public function __construct(
private readonly ContingentsClient $contingentsClient,
private readonly AccommodationRepository $accommodationRepository,
private readonly AccommodationPriceRepository $priceRepository,
private readonly CacheInterface $cache,
private readonly PriceTimelineBuilder $priceTimelineBuilder,
) {
}
#[Route(path: '/contingents/prices', name: 'api_contingents_prices', methods: ['GET'])]
public function prices(Request $request): JsonResponse
{
$hotelCode = $request->query->get('hotelCode');
$yearRaw = $request->query->get('year');
if (!$hotelCode || !$yearRaw) {
return $this->json(['error' => 'hotelCode and year are required.'], Response::HTTP_BAD_REQUEST);
}
if (!preg_match('/^\d{4}$/', $yearRaw)) {
return $this->json(['error' => 'year must be a 4-digit integer.'], Response::HTTP_BAD_REQUEST);
}
$accommodation = $this->accommodationRepository->findOneBy(['calendarCode' => $hotelCode]);
if (null === $accommodation) {
return $this->json(['error' => 'hotel not found for hotelCode.'], Response::HTTP_BAD_REQUEST);
}
$year = (int) $yearRaw;
$yearStart = new \DateTimeImmutable("{$year}-01-01");
$yearEnd = new \DateTimeImmutable("{$year}-12-31");
$prices = $this->priceRepository->findByHotelCodeAndDateRange($hotelCode, $yearStart, $yearEnd);
$currency = $accommodation->getCurrency();
return $this->json($this->priceTimelineBuilder->buildTimeline($prices, $yearStart, $yearEnd, $currency));
}
#[Route(path: '/contingents/calendar', name: 'api_contingents_calendar', methods: ['GET'])]
public function calendar(Request $request): JsonResponse
{
@@ -47,6 +83,12 @@ class ContingentController extends AbstractController
return $this->json(['error' => 'dateFrom and dateTo must be in Y-m-d format.'], Response::HTTP_BAD_REQUEST);
}
$accommodation = $this->accommodationRepository->findOneBy(['calendarCode' => $hotelCode]);
if (null === $accommodation) {
return $this->json(['error' => 'hotel not found for hotelCode.'], Response::HTTP_BAD_REQUEST);
}
try {
$cacheKey = sprintf('contingents_calendar_%s_%s_%s', $hotelCode, $dateFromRaw, $dateToRaw);
$calendar = $this->cache->get($cacheKey, function (ItemInterface $item) use ($hotelCode, $dateFromRaw, $dateToRaw) {
@@ -60,8 +102,10 @@ class ContingentController extends AbstractController
$prices = $this->priceRepository->findByHotelCodeAndDateRange($hotelCode, $dateFrom, $dateTo);
$currency = $accommodation->getCurrency();
$data = array_map(
fn ($entry) => $this->enrichEntry($entry->date, $entry->status->value, $prices),
fn ($entry) => $this->enrichEntry($entry->date, $entry->status->value, $prices, $currency),
$calendar->data,
);
@@ -70,33 +114,39 @@ class ContingentController extends AbstractController
/**
* @param AccommodationPrice[] $prices
* @return array{date: string, status: string, pricePerNight: float|null, pricePerNightOverride: float|null, includedPax: int|null}
* @return array{date: string, status: string, type: string|null, pricePerNight: float|null, defaultPricePerNight: float|null, priceAdditionalPerson: float|null, defaultPriceAdditionalPerson: float|null, includedPax: int|null, minPax: int|null, minNights: int|null}
*/
private function enrichEntry(string $date, string $status, array $prices): array
private function enrichEntry(string $date, string $status, array $prices, string $currency): array
{
$day = (new \DateTimeImmutable($date))->setTime(0, 0);
$price = null;
foreach ($prices as $candidate) {
if ($candidate->getDateFrom() > $day || $candidate->getDateTo() < $day) {
continue;
}
// dateFrom and dateTo are both inclusive (last night, not checkout day)
$candidates = array_filter(
$prices,
fn($p) => $p->getDateFrom() <= $day && $p->getDateTo() >= $day,
);
if (null === $price || $candidate->getDateFrom() > $price->getDateFrom()) {
$price = $candidate;
}
$winner = $this->priceTimelineBuilder->resolveWinner($candidates);
$defaultPrice = null;
if ($winner?->getType() === PriceType::DISCOUNT) {
$defaults = array_filter($candidates, fn($p) => $p->getType() === null);
$defaultPrice = $this->priceTimelineBuilder->resolveWinner($defaults);
}
return [
'date' => $date,
'status' => $status,
'pricePerNight' => null !== $price ? round($price->getPricePerNight() / 100, 2) : null,
'pricePerNightOverride' => null !== $price?->getPricePerNightOverride() ? round($price->getPricePerNightOverride() / 100, 2) : null,
'priceAdditionalPerson' => null !== $price ? round($price->getPriceAdditionalPerson() / 100, 2) : null,
'priceAdditionalPersonOverride' => null !== $price?->getPriceAdditionalPersonOverride() ? round($price->getPriceAdditionalPersonOverride() / 100, 2) : null,
'includedPax' => $price?->getIncludedPax(),
'minPax' => $price?->getAccommodation()->getMinPaxSummer(),
'minNights' => $price?->getMinNights(),
'type' => $winner?->getType()?->value,
'pricePerNight' => $winner !== null ? round($winner->getPricePerNight() / 100, 2) : null,
'defaultPricePerNight' => $defaultPrice !== null ? round($defaultPrice->getPricePerNight() / 100, 2) : null,
'priceAdditionalPerson' => $winner !== null ? round($winner->getPriceAdditionalPerson() / 100, 2) : null,
'defaultPriceAdditionalPerson' => $defaultPrice !== null ? round($defaultPrice->getPriceAdditionalPerson() / 100, 2) : null,
'currency' => $currency,
'includedPax' => $winner?->getIncludedPax(),
'minPax' => $winner?->getAccommodation()->getMinPaxSummer(),
'minNights' => $winner?->getMinNights(),
];
}
}