<?php
namespace App\Controller\Front\Product;
use App\Config\FaqRouteEnum;
use App\Config\ModuleEnum;
use App\Entity\ApiLog;
use App\Entity\CustomerMoral;
use App\Entity\HotelAvailabilityRequest;
use App\Module\Attribute\RequiresModule;
use App\Repository\CityRepository;
use App\Repository\FrontThemeRepository;
use App\Repository\HotelXmlPriceRepository;
use App\Repository\HotelXmlRepository;
use App\Repository\OrderLineRepository;
use App\Repository\OrderRepository;
use App\Repository\UserRepository;
use App\Service\Api3TAuthenticationService;
use App\Service\ApiLogService;
use App\Service\CartService;
use App\Service\CurrencyService;
use App\Service\FrontRecentSearchService;
use App\Service\FrontService;
use App\Service\Helpers;
use App\Service\HotelApiService;
use App\Service\HotelOfferDetailsService;
use App\Service\HotelSearchStreamService;
use App\Service\ParameterService;
use DateTime;
use Exception;
use Gedmo\Translatable\TranslatableListener;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Service\CacheResolverService;
#[Route('/hotel')]
#[RequiresModule(ModuleEnum::hotel)]
class HotelController extends AbstractController
{
private const UNNORMALIZED_CACHE_PREFIX = 'hub_unnormalized_rooms_';
private const UNNORMALIZED_CACHE_TTL = 600; // 10 min
private const PAGES_COUNT_TTL = 604800; // 7 jours
private const PAGES_COUNT_REFRESH_AFTER = 3600; // rafraîchir en fin de stream si > 1h
public function __construct(
private readonly Api3TAuthenticationService $apiAuthenticationService,
private readonly HotelApiService $hotelApiService,
private readonly CurrencyService $currencyService,
private readonly FrontService $frontService,
private readonly ParameterService $parameterService,
private readonly Helpers $helpers,
private readonly LoggerInterface $logger,
private readonly CacheItemPoolInterface $searchCachePool,
private readonly translatorInterface $translator,
private readonly CacheResolverService $cacheResolverService,
private readonly ApiLogService $apiLogService,
private readonly UserRepository $userRepository,
)
{
}
#[Route('/hotelcity/{name}', name: 'app_front_hotel_city')]
public function hotelByCity(
string $name,
CityRepository $cityRepository,
HotelXmlPriceRepository $hotelXmlPriceRepository,
FrontService $frontService
): Response
{
$hotels = [];
$city = $cityRepository->findOneBy(['name' => $name]);
$description = "";
$country = "";
if ($city) {
$description = $city->getDescription();
$country = $city->getCountry()->getName();
}
$hotelXmlPrices = $hotelXmlPriceRepository->getHotelXmlPriceByCriterias(['cities' => [$name]]);
foreach ($hotelXmlPrices as $HotelXmlPrice) {
$hotels[] = $frontService->getItemHotel($HotelXmlPrice);
}
return $this->render('front/hotel/hotel_by_city.html.twig', [
'hotels' => $hotels,
'city' => $city,
'name' => $name,
'country' => $country,
'society' => $this->parameterService->getSocietyParameters(),
'social_networks' => $this->frontService->getSocialNetworks(),
'agencies' => $this->frontService->getAgencies(),
'currencies' => $this->frontService->getCurrencies(),
'currencySwitcher' => true,
'tracking_tools' => $this->frontService->getTrackingTools(),
'faqs' => $this->frontService->getFaqs(FaqRouteEnum::APP_FRONT_HOTEL_CITY, strtolower($name)),
]);
}
#[Route('/hoteltheme/{name}', name: 'app_front_hotel_theme')]
public function hotelByTheme(
string $name,
FrontThemeRepository $frontThemeRepository,
HotelXmlPriceRepository $hotelXmlPriceRepository,
FrontService $frontService
): Response
{
$hotels = [];
$theme = $frontThemeRepository->findOneBy(['name' => $name]);
$description = "";
if ($theme) {
$description = $theme->getDescription();
}
$hotelXmlPrices = $hotelXmlPriceRepository->getHotelXmlPriceByCriterias(['themes' => $name]);
foreach ($hotelXmlPrices as $HotelXmlPrice) {
$hotels[] = $frontService->getItemHotel($HotelXmlPrice);
}
return $this->render('front/hotel/hotel_by_theme.html.twig', [
'hotels' => $hotels,
'description' => $description,
'name' => $name,
'society' => $this->parameterService->getSocietyParameters(),
'social_networks' => $this->frontService->getSocialNetworks(),
'agencies' => $this->frontService->getAgencies(),
'currencies' => $this->frontService->getCurrencies(),
'tracking_tools' => $this->frontService->getTrackingTools()
]);
}
#[Route('/search', name: 'hotel_search', methods: ['GET', 'POST'])]
public function hotelSearch(Request $request, FrontRecentSearchService $frontRecentSearchService): Response
{
$data = $request->get("data"); // data : JSON request
$requestData = json_decode($data, true);
$date = new DateTime();
// Output the date in default format
$currentDate = $date->format('Y-m-d'); // Example: 2025-01-02 15:30:45
if ($requestData['checkIn'] < $currentDate || $requestData['checkOut'] < $currentDate) {
return $this->redirectToRoute('app_main');
}
$user = $this->getUser();
$customer = $this->apiAuthenticationService->getCustomer($this->getUser());
if (is_null($customer)) {
$this->addFlash('info', $this->translator->trans(
'Pages.Common.Messages.SELECT_CUSTOMER_TO_CONTINUE_BOOKING',
[],
'messages_front'
));
return $this->redirectToRoute('front_info_message');
}
$auth_data = $this->apiAuthenticationService->getAuthenticationArray($user);
if (is_null($auth_data)) {
$this->addFlash('info', $this->translator->trans(
'Pages.Common.Messages.SELECT_CUSTOMER_TO_CONTINUE_BOOKING',
[],
'messages_front'
));
return $this->redirectToRoute('front_info_message');
}
$hotel_search_history = $frontRecentSearchService->updateHotelSearchHistory($requestData, $data);
$isSearchByMap = $this->parameterService->isSearchMap();
return $this->render('front/hotel/search.html.twig', [
'requestData' => $requestData,
'customer' => $customer,
'society' => $this->parameterService->getSocietyParameters(),
'social_networks' => $this->frontService->getSocialNetworks(),
'agencies' => $this->frontService->getAgencies(),
'currencies' => $this->frontService->getCurrencies(),
'hotel_search_history' => $hotel_search_history,
'sections' => $this->frontService->getSections(),
'currencySwitcher' => true,
'tracking_tools' => $this->frontService->getTrackingTools(),
'isSearchByMap' => $isSearchByMap,
]);
}
#[Route('/formsearch/{city_label}/{hotel_label}/{hotel_id}', name: 'hotel_form_search', defaults: ['hotel_id' => null], methods: ['GET', 'POST'])]
public function hotelFormSearch(Request $request, string $city_label, string $hotel_label,?string $hotel_id): Response
{
$requestData = json_decode($request->get("data"), true);
return $this->render('front/hotel/form_search.html.twig', [
'requestData' => $requestData,
'hotel_label' => $hotel_label,
'city_label' => $city_label,
'hotel_id' => $hotel_id
/* 'society' => $this->parameterService->getSocietyParameters(),
'social_networks' => $this->frontService->getSocialNetworks(),
'agencies' => $this->frontService->getAgencies(),
'currencies' => $this->frontService->getCurrencies(),*/
]);
}
#[Route('/search_json', name: 'hotel_search_json', methods: ['GET', 'POST'])]
public function hotelSearchJson(Request $request, HotelApiService $hotelApiService): JsonResponse
{
// ----- PREPARE the HttpRequestCall ----- //
// Get auth_data : user, timestamp, signature
$user = $this->getUser();
$auth_data = $this->apiAuthenticationService->getAuthenticationArray($user);
$customer = $this->apiAuthenticationService->getCustomer($user);
$showResellingRates = false;
if ($customer instanceof CustomerMoral) {
if ($customer->getResellerMargin() > 0 && (in_array('ROLE_B2B_ADMIN', $user->getRoles()) || in_array('ROLE_B2B_AGENT', $user->getRoles()))) {
$showResellingRates = true;
}
}
$requestJson = json_decode($request->getContent(), true);
$xmlSourceId = $request->headers->get('xmlSourceId') ?? 0;
$base_uri = $request->getSchemeAndHttpHost();
// ----- PERFORM the HttpRequestCall ----- //
$data = $hotelApiService->query_ApiHotel_Availability($auth_data, $requestJson, $xmlSourceId, $base_uri, true);
$today = DateTime::createFromFormat("Y-m-d", date("Y-m-d"));
if (count($data['content']) > 0) { // Re-format data to fit the FRONT-B2C :::: VIEW_BY_BOARD ::::
$nbRooms = count($requestJson['occupancies']);
$content_front = []; // prepare data to fit the front design
foreach ($data['content'] as $hotel) {
$hotel_key = strtoupper($hotel['hotel']['name']); // HOTEL_ITEM_IDENTIFIER TO GROUP MULTIPLE HOTELS WITH SAME NAME
$hotel_offer_count = 0;
if (isset($content_front[$hotel_key]['offersCount'])) {
$hotel_offer_count = $content_front[$hotel_key]['offersCount'];
}
$content_front[$hotel_key]['hotel'] = $hotel['hotel'];
$content_front[$hotel_key]['hotel']['customRating'] = $this->hotelApiService->getHotelCustomRating($hotel['hotel']['name']);
// TODO Add function getReviewRating(hotelName, reviewsSource);
$content_front[$hotel_key]['hotel']['reviewRating'] = null;//rand(4, 10); // TODO GET RATING FROM External src like Tripadvisor
if (count($hotel['sources']) > 0) {
$content_front[$hotel_key]['hotel']['facilities'] = $hotel['sources'][0]['facilities'];
}
$content_front[$hotel_key]['hotel']['promos'] = [];
$content_front[$hotel_key]['hotel']['discounts'] = [];
$content_front[$hotel_key]['hotel']['infos'] = [];
foreach ($hotel['sources'] as $hotel_source) {
$sourceKey = $hotel['hotel']['id']; // considering the full-hotel-id as the sourceKey (string containing the end-source-id)
$content_front[$hotel_key]['sources'][$sourceKey]['sourceKey'] = $sourceKey;
$hotelSourcePrice = 0;
$hotelSourceAvailable = false;
$hotelSourceFreeCancellation = false;
$tab_boards = [];
foreach ($hotel_source['rooms'] as $index => $index_hrts) { // room_index
// SORT hrts for current hotel-source by salePromoRate
usort($index_hrts, function ($a, $b) {
return $a['salePromoRate'] <=> $b['salePromoRate']; // Ascending
});
foreach ($index_hrts as $hrt) {
// freeCancellation FLAG to use in React Filter (i.e. is there a possibility to cancel without fees )
if ($hrt['NRF']) {
$hrt['freeCancellation'] = false;
} else {
if (is_null($hrt['deadline'])) {
$hrt['freeCancellation'] = true;
} else {
$deadline = new DateTime($hrt['deadline']);
$difference = date_diff($today, $deadline); // Interval before deadline.
if ($difference->invert) { // deadline is exceeded
$hrt['freeCancellation'] = false;
} else {
$hrt['freeCancellation'] = true;
}
}
}
$hotelSourceFreeCancellation = $hotelSourceFreeCancellation || $hrt['freeCancellation'];
// END freeCancellation FLAG
// SHOW SaleResellingRates rather thant saleRate applied to the customer
if ($showResellingRates) {
$hrt['salePromoRate'] = $hrt['salePromoRate'] * (100.0 + $customer->getResellerMargin()) / 100.0;
$hrt['saleRate'] = $hrt['saleRate'] * (100.0 + $customer->getResellerMargin()) / 100.0;
}
$content_front[$hotel_key]['sources'][$sourceKey]['hrts'][$hrt['boardName']][$index][] = $hrt;
$tab_boards[$hrt['boardName']] = $hrt['boardXmlName'];
// Increment the number of available-offers
if ($hrt['available']) {
$hotel_offer_count++;
$hotelSourceAvailable = true;
}
}
} // END Room Indexes
// HOTEL BADGES (Promos, Discounts, Infos)
foreach ($hotel_source['promos'] as $item) {
$content_front[$hotel_key]['hotel']['promos'][] = $item;
}
foreach ($hotel_source['discounts'] as $item) {
$content_front[$hotel_key]['hotel']['discounts'][] = $item;
}
foreach ($hotel_source['infos'] as $item) {
$content_front[$hotel_key]['hotel']['infos'][] = $item;
}
$bestOffers = $this->hotelApiService->getBestPriceBoard($content_front[$hotel_key], $sourceKey);
$content_front[$hotel_key]['sources'][$sourceKey]['bestPrice'] = $bestOffers['bestPrice_currentSourceKey'];
$content_front[$hotel_key]['bestPrice'] = $bestOffers['bestPrice'];
$content_front[$hotel_key]['bestBoard'] = $bestOffers['bestBoard'];
$content_front[$hotel_key]['bestRooms'] = !empty($sourceBest['bestRooms'])
? $this->helpers->formatArrayCounts($sourceBest['bestRooms'])
: '';
$content_front[$hotel_key]['offersCount'] = $hotel_offer_count;
foreach ($content_front[$hotel_key]['sources'][$sourceKey]['hrts'] as $board_key => $board_hrts) {
if (count($board_hrts) < $nbRooms) {
unset($content_front[$hotel_key]['sources'][$sourceKey]['hrts'][$board_key]);
}
}
foreach ($tab_boards as $board_key => $board_value) {
$content_front[$hotel_key]['sources'][$sourceKey]['boards'][] =
[
'key' => $board_value,
'value' => $board_value,
];
}
// set general stock
$content_front[$hotel_key]['sources'][$sourceKey]['generalStock'] = $hotel_source['generalStock'];
// set search code
$content_front[$hotel_key]['sources'][$sourceKey]['searchCode'] = $hotel_source['searchCode'];
// set source id
$content_front[$hotel_key]['sources'][$sourceKey]['xmlSourceId'] = $hotel_source['xmlSourceId'];
// set xml source name
$content_front[$hotel_key]['sources'][$sourceKey]['xmlSourceName'] = $hotel_source['xmlSourceName'];
// set hotel Source available
$content_front[$hotel_key]['sources'][$sourceKey]['available'] = $hotelSourceAvailable;
// set hotel Source available
$content_front[$hotel_key]['sources'][$sourceKey]['freeCancellation'] = $hotelSourceFreeCancellation;
// set hotel Source available
$content_front[$hotel_key]['sources'][$sourceKey]['associationRequired'] = $hotel_source['associationRequired'];
}
}
// array values hotels
$data['content'] = array_values($content_front);
// array values sources
foreach ($data['content'] as &$hotel) {
usort($hotel['sources'], function ($a, $b) {
return $a['bestPrice'] <=> $b['bestPrice']; // Ascending
});
$hotel['sources'] = array_values($hotel['sources']);
foreach ($hotel['sources'] as &$source) {
$source['hrts'] = array_values($source['hrts']);
}
}
}
return $this->json($data);
}
#[Route('/autocomplete', name: 'hotel_autocomplete', methods: ['GET'])]
public function autoComplete(Request $request): JsonResponse
{
$query = $request->query->get('query');
if ($query == null) {
return $this->json([]);
}
$query_trim = trim($query);
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
try {
$httpClient = HttpClient::create();
$response = $httpClient->request('GET', $request->getSchemeAndHttpHost() . '/api/hotel/autocomplete', [
'headers' => $headers,
'query' => ['query' => $query_trim]
]);
$data = json_decode($response->getContent(), true);
return $this->json($data['response']);
} catch (Exception $e) {
return $this->json(['error' => $e->getMessage()]);
}
}
/**
* The detail of selected offer from the front : hotel search results page. --- Check-Rate ---
* Supposed to be called either from "hotel/search or "orderline_add_room"
*/
#[Route('/offer-detail', name: 'hotel_offer_detail', methods: ['GET', 'POST'])]
public function hotelOfferDetail(
Request $request,
OrderLineRepository $orderLineRepository,
OrderRepository $orderRepository,
): Response
{
$guestData = [];
if ($request->isMethod('POST')) {
$guestData = [
'first_name' => $request->request->get('first_name'),
'last_name' => $request->request->get('last_name'),
'phone' => $request->request->get('phone'),
'email' => $request->request->get('email'),
];
$request->getSession()->set('guestData', $guestData);
}
$customer = $this->apiAuthenticationService->getCustomer($this->getUser());
$auth_data = $this->apiAuthenticationService->getAuthenticationArray($this->getUser());
$rooms_rate_keys = [];
for ($i = 0; $i < $request->get('nbRooms'); $i++) {
$rooms_rate_keys[] = ["rateKey" => $request->get("room-" . $i)];
}
$checkRateRequest = [
'rooms' => $rooms_rate_keys,
'searchCode' => $request->get('searchCode')
];
$saleCurrency = $this->currencyService->getSessionCurrency();
$product_fee = $this->frontService->getProductFee(ModuleEnum::hotel->getValue(), $customer);
/** call api/hotel/checkrate */
$httpClient = HttpClient::create();
$response = $httpClient->request(
'POST', $request->getUriForPath('/') . 'api/hotel/checkrate',
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'currency' => $saleCurrency,
'user' => $auth_data['user'],
'timestamp' => $auth_data['timestamp'],
'signature' => hash('sha256', $auth_data['signature'])
],
'json' => $checkRateRequest
]
);
$responseData = json_decode($response->getContent(), true);
$searchCriteriaArray = json_decode($request->get("searchCriteria"), true);
$twig_parameters = [
'searchCriteriaArray' => $searchCriteriaArray,
'searchCode' => null,
'checkRateData' => null,
"social_networks" => $this->frontService->getSocialNetworks(),
'menu_front' => $this->frontService->getFrontMenu(),
"currencies" => $this->frontService->getCurrencies(),
"agencies" => $this->frontService->getAgencies(),
"society" => $this->parameterService->getSocietyParameters(),
"referenceCurrency" => $this->parameterService->getReferenceCurrency(),
'guestData' => $guestData,
'action' => '',
'customer' => $customer
];
$template = "";
if (is_null($responseData) or $responseData['error']) {
// CASE 0 : Template for Error
return $this->render("front/hotel/offer_error.html.twig", $twig_parameters);
} else {
$checkRateData = reset($responseData['content']);
$checkRateData['available'] = $this->hotelApiService->isAvailable($checkRateData) ? "true" : "false";
$rooms = [];
foreach ($checkRateData['sources'][0]['rooms'] as $room_index) {
$rooms[] = $room_index[0]; // the selected room for the index.
}
//$checkRateData['cancellation'] = $this->hotelApiService->getDeadlineFromRooms($rooms);
$twig_parameters['checkRateData'] = $checkRateData;
$twig_parameters['searchCode'] = $responseData['searchCode'];
$twig_parameters['product_fee'] = $product_fee;
$twig_parameters['META_PIXEL_ID'] = $this->parameterService->getTrackerMetaId();
// CASE 1 : Template to add something to an existing orderline
if ($request->get('orderlineId')) {
$orderlineID = $request->get('orderlineId');
$twig_parameters['orderLine'] = $orderLineRepository->find($orderlineID);
$twig_parameters['action'] = $this->generateUrl("orderline_add_room", ['id' => $orderlineID]);
}
// CASE 2 : new Order and new OrderLine
if (empty($request->get('orderlineId')) && empty($request->get('orderId'))) { //
$twig_parameters['action'] = $this->generateUrl("app_admin_add_cart_line_hotel");
}
// CASE 3 : Add new OrderLine into an existing Order
if (empty($request->get('orderlineId')) && $request->get('orderId')) { //
$orderID = $request->get('orderId');
$twig_parameters['order'] = $orderRepository->find($orderID);
$twig_parameters['action'] = $this->generateUrl("order_add_orderline_hotel_book", ['id' => $orderID]);
}
return $this->render("front/hotel/offer_detail.html.twig", $twig_parameters);
}
}
#[Route('/chatbot/hotelBook', name: 'chatbot_hotel_book', methods: ['GET', 'POST'])]
public function chatbotHotelBook(
Request $request,
OrderLineRepository $orderLineRepository,
OrderRepository $orderRepository,
HttpClientInterface $httpClient,
): Response
{
$customer = $this->apiAuthenticationService->getCustomer($this->getUser());
$auth_data = $this->apiAuthenticationService->getAuthenticationArray($this->getUser());
$saleCurrency = $this->currencyService->getSessionCurrency();
$product_fee = $this->frontService->getProductFee(ModuleEnum::hotel->getValue(), $customer);
// Was hardcoded as "azerty" in two places (decrypt key + generateSearchCode arg).
// Pull from parameters/env instead of literal strings in code.
$secretKey = $this->getParameter('app.hotel_ratekey_secret');
$ratekeysParam = $request->query->get('ratekeys', '');
$rawRateKeys = array_filter(array_map('trim', explode('@@@@', $ratekeysParam)));
try {
$parsed = $this->parseRateKeys($rawRateKeys, $secretKey);
} catch (\Throwable $e) {
// Any malformed rate key (bad format, missing segments, bad date) lands here
// instead of throwing a raw ErrorException back to the client.
return $this->renderOfferError();
}
if (empty($parsed['rooms_rate_keys'])) {
return $this->renderOfferError();
}
$searchCode = $this->generateSearchCode($secretKey, $saleCurrency);
$checkRateRequest = [
'rooms' => $parsed['rooms_rate_keys'],
'searchCode' => $searchCode,
];
$response = $httpClient->request(
'POST',
$request->getUriForPath('/') . 'api/hotel/checkrate',
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'currency' => $saleCurrency,
'user' => $auth_data['user'],
'timestamp' => $auth_data['timestamp'],
'signature' => hash('sha256', $auth_data['signature']),
],
'json' => $checkRateRequest,
]
);
$responseData = json_decode($response->getContent(), true);
if (!is_array($responseData) || !empty($responseData['error'] ?? null) || empty($responseData['content'])) {
return $this->renderOfferError();
}
$searchCriteriaArray = [
'destination' => [
'id' => $parsed['cityId'],
'name' => $responseData['content'][0]['hotel']['name'] ?? '',
'type' => 'hotel',
],
'checkIn' => $parsed['checkIn'],
'checkOut' => $parsed['checkOut'],
'rooms' => $parsed['jsonOccupation'],
];
$twig_parameters = [
'searchCriteriaArray' => $searchCriteriaArray,
'searchCode' => null,
'checkRateData' => null,
'social_networks' => $this->frontService->getSocialNetworks(),
'menu_front' => $this->frontService->getFrontMenu(),
'currencies' => $this->frontService->getCurrencies(),
'agencies' => $this->frontService->getAgencies(),
'society' => $this->parameterService->getSocietyParameters(),
'referenceCurrency' => $this->parameterService->getReferenceCurrency(),
'guestData' => [],
'action' => '',
'customer' => $customer,
];
$checkRateData = reset($responseData['content']);
$checkRateData['available'] = $this->hotelApiService->isAvailable($checkRateData) ? 'true' : 'false';
$twig_parameters['checkRateData'] = $checkRateData;
$twig_parameters['searchCode'] = $responseData['searchCode'] ?? $searchCode;
$twig_parameters['product_fee'] = $product_fee;
$twig_parameters['META_PIXEL_ID'] = $this->parameterService->getTrackerMetaId();
$twig_parameters['action'] = $this->generateUrl('app_admin_add_cart_line_hotel');
return $this->render('front/hotel/offer_detail.html.twig', $twig_parameters);
}
/**
* The detail of a given hotelId
*/
#[Route('/{country_label}/{city_label}/{hotel_label}/{id}', name: 'hotel_detail', methods: ['GET'])]
public function hotelDetail(
Request $request,
$id,
string $city_label,
string $hotel_label,
HotelXmlRepository $hotelXmlRepository
): Response
{
$uid = $id; // source@uid
$hotel_source = strstr($uid, '@', true); // left subString before @
$hotel_uid = substr(strstr($uid, '@'), 1); // right subString after -
if ($hotel_source == 0) {
$hotelDetails = $this->hotelApiService->hotelDetails($hotel_uid, $request->getLocale());
} else {
$hotelDetails = $this->hotelApiService->hotelDetailsHUB($hotel_uid);
}
// hotelDetailsHUB renvoie [message] en cas d'erreur : on le traite comme « hôtel introuvable »
if (!isset($hotelDetails['name'])) {
$hotelDetails = [];
}
//$recommendedHotels = $hotelXmlRepository->getRecommendedHotels($city_label, $hotel_uid);
$recommendedHotels = [];
//dd(count($recommendedHotels));
// Dates du moteur « Vérifier la disponibilité » : ?checkIn=&checkOut= (Y-m-d) si valides, sinon aujourd'hui / demain
$arrivalDate = (new DateTime())->format('Y-m-d');
$departureDate = (new DateTime('tomorrow'))->format('Y-m-d');
$requestedCheckIn = DateTime::createFromFormat('!Y-m-d', (string)$request->query->get('checkIn'));
$requestedCheckOut = DateTime::createFromFormat('!Y-m-d', (string)$request->query->get('checkOut'));
if ($requestedCheckIn && $requestedCheckOut
&& $requestedCheckIn->format('Y-m-d') >= $arrivalDate
&& $requestedCheckOut > $requestedCheckIn) {
$arrivalDate = $requestedCheckIn->format('Y-m-d');
$departureDate = $requestedCheckOut->format('Y-m-d');
}
return $this->render('front/hotel/hotel_detail.html.twig', [
'hotel_data' => $hotelDetails,
'society' => $this->parameterService->getSocietyParameters(),
'social_networks' => $this->frontService->getSocialNetworks(),
'currencies' => $this->frontService->getCurrencies(),
'agencies' => $this->frontService->getAgencies(),
'menu_front' => $this->frontService->getFrontMenu(),
'recommendedHotels' => $recommendedHotels,
"arrivalDate" => $arrivalDate,
"departureDate" => $departureDate,
"city_label" => $city_label,
"hotel_label" => $hotel_label,
"META_PIXEL_ID" => $this->parameterService->getTrackerMetaId(),
'faqs' => $this->frontService->getFaqs(FaqRouteEnum::HOTEL_DETAIL, $uid),
]);
}
#[Route('/{id}/gallery', name: 'hotel_gallery_json', methods: ['GET'])]
public function hotelGallery(
Request $request,
$id
): JsonResponse
{
$auth_data = $this->apiAuthenticationService->getAuthenticationArray($this->getUser());
// call hotel detail and get images
$hotelDetailsRequest = [
'hotelId' => $id
];
$httpClient = HttpClient::create();
$response = $httpClient->request(
'POST', $request->getUriForPath('/') . 'api/hotel/details',
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'user' => $auth_data['user'],
'timestamp' => $auth_data['timestamp'],
'signature' => hash('sha256', $auth_data['signature'])
],
'json' => $hotelDetailsRequest
]
);
$responseData = json_decode($response->getContent(), true);
if (is_null($responseData) || !key_exists('content', $responseData)) {
return $this->json([]);
}
$hotel_data_array = $responseData['content']['images'];
return $this->json($hotel_data_array);
}
/**
* @throws Exception
*/
#[Route('/promo', name: 'app_front_hotel_promo', methods: ['GET'])]
public function hotelPromo(HotelXmlPriceRepository $hotelXmlPriceRepository,
FrontService $frontService
): Response
{
$hotels = [];
$hotelXmlPricesWithPromos = $hotelXmlPriceRepository->getHotelXmlPriceWithPromos();
foreach ($hotelXmlPricesWithPromos as $hotelXmlPricesWithPromo) {
$hotels[] = $frontService->getItemHotel($hotelXmlPricesWithPromo);
}
return $this->render('front/hotel/hotel_promo.html.twig', [
'hotelsWithPromos' => $hotels,
'social_networks' => $this->frontService->getSocialNetworks(),
'currencies' => $this->frontService->getCurrencies(),
'agencies' => $this->frontService->getAgencies(),
'society' => $this->parameterService->getSocietyParameters(),
'currencySwitcher' => true,
'META_PIXEL_ID' => $this->parameterService->getTrackerMetaId()
]);
}
#[Route('/cart/addProductHotel', name: 'app_admin_add_cart_line_hotel', methods: ['POST'])]
public function addProductHotel(
Request $request,
SessionInterface $session,
CartService $cartService,
): RedirectResponse
{
$cart_array = $cartService->initCart($this->getUser(), $request);
/* 2- add a line into cart_array['products'] */
$nbRooms = $request->get("nbRooms"); // quantity : nbRooms
$cart_line_label = $request->get('product_label');
$cart_line_date = $request->get('product_date');
$beneficiaryName = $request->get('beneficiary_name');
if (empty($beneficiaryName)) {
$beneficiaryFirstName = trim((string)$request->get('beneficiary_first_name', ''));
$beneficiaryLastName = trim((string)$request->get('beneficiary_last_name', ''));
$beneficiaryName = trim($beneficiaryFirstName . ' ' . $beneficiaryLastName);
}
if (empty($beneficiaryName)) {
$beneficiaryName = trim(
(string)$request->get('room_0_pax_0_firstName', '') . ' ' .
(string)$request->get('room_0_pax_0_lastName', '')
);
}
$beneficiaryEmail = $request->get('beneficiary_email');
$product_price = $request->get("totalAmount");
$productFee = $request->get('product_fee') ?? 0;
$dates = explode('-', $cart_line_date);
$date1 = DateTime::createFromFormat('d/m/Y', $dates[0]);
$date2 = DateTime::createFromFormat('d/m/Y', $dates[1]);
$interval = $date1->diff($date2);
$nbNights = $interval->days;
$hotel_with_party = false;
$product_elements = null;
for ($room_idx = 0; $room_idx < $nbRooms; $room_idx++) {
/* guests */
$room_guests = [];
$room_adults_count = $request->get("room_" . $room_idx . "_adults_count");
$room_children_count = $request->get("room_" . $room_idx . "_children_count");
$room_guest_count = $room_adults_count + $room_children_count;
for ($guest_index = 0; $guest_index < $room_guest_count; $guest_index++) {
$paxCivility = $request->get("room_" . $room_idx . "_pax_" . $guest_index . "_civility");
$paxFirstName = $request->get("room_" . $room_idx . "_pax_" . $guest_index . "_firstName");
$paxLastName = $request->get("room_" . $room_idx . "_pax_" . $guest_index . "_lastName");
$paxAge = $request->get("room_" . $room_idx . "_pax_" . $guest_index . "_age");
if (!empty($paxFirstName) || !empty($paxLastName)) {
// Use the detailed per-pax data
$guest = [
'civility' => $paxCivility ?? '',
'firstName' => $paxFirstName ?? '',
'lastName' => $paxLastName ?? '',
'age' => $paxAge,
];
} else {
// Fallback: use the primary beneficiary name (backward compatibility)
$guest = [
'civility' => '',
'firstName' => $beneficiaryName,
'lastName' => '-',
];
}
$room_guests[] = $guest;
}
$room = [
'rateKey' => $request->get("room_" . $room_idx . "_key"),
'label' => $request->get("room_" . $room_idx . "_label"),
'isAvailable' => $request->get("room_" . $room_idx . "_available"),
'price' => $request->get("room_" . $room_idx . "_price"),
'adultsCount' => $room_adults_count,
'childrenCount' => $room_children_count,
'guests' => $room_guests,
'cancellations_fees' => $request->get("room_" . $room_idx . "_cancellations_fees"),
'isNFR' => $request->get("room_" . $room_idx . "_nfr"),
];
$room_nb_parties = $request->get("room_" . $room_idx . "_nbParties");
$room_parties_zones = [];
if (!is_null($room_nb_parties)) { // if there is parties associated to the current room
$hotel_with_party = true;
for ($party_index = 0; $party_index < $room_nb_parties; $party_index++) {
$room_parties_zones[] = $request->get("room_" . $room_idx . "_party_" . $party_index);
}
$room['parties'] = $room_parties_zones;
//$product_price+= $request->get("partyPrice_".$party_index);
}
$product_elements[] = $room;
}
if ($request->isMethod('POST')) {
$guestData = [
'first_name' => $request->request->get('first_name'),
'last_name' => $request->request->get('last_name'),
'phone' => $request->request->get('phone'),
'email' => $request->request->get('email'),
];
$request->getSession()->set('guestData', $guestData);
}
$cart_line_hotel = [
'module' => ModuleEnum::hotel->getValue(),
'elements' => $product_elements,
'label' => $cart_line_label,
'date' => $cart_line_date,
'checkIn' => $dates[0],
'checkOut' => $dates[1],
'nbNights' => $nbNights,
'quantity' => $nbRooms,
'options' => $request->get("options"),
'comment' => $request->get("comment"),
'price' => $product_price,
'fee' => $productFee,
'available' => $request->get('available'),
'searchCode' => $request->get('searchCode'),
'tokenForBook' => $request->get('tokenForBook'),
'beneficiary' => [
'name' => $beneficiaryName,
'email' => $beneficiaryEmail
],
'image' => $request->get('hotel_image'),
'address' => $request->get('hotel_address'),
'phone' => $request->get('hotel_phone'),
'city' => $request->get('city'),
'country' => $request->get('country'),
'rating' => $request->get('rating'),
'guestData' => $guestData,
];
$cart_array['products'][] = $cart_line_hotel; // add a cart line ( a product )
$session->set("cart", $cart_array);
//return $this->redirectToRoute('app_shared_cart_index', [], Response::HTTP_SEE_OTHER);
return $this->redirectToRoute('app_shared_cart_hotel_product_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/search_stream', name: 'hotel_search_stream', methods: ['POST'])]
public function hotelSearchStream(Request $request, HotelSearchStreamService $searchService): StreamedResponse|JsonResponse
{
$ctx = $searchService->buildContext($this->getUser(), $request);
$request->getSession()?->save(); // libère le verrou de session avant le stream
try {
return $searchService->createStreamedResponse(
$ctx,
$request->getContent(),
(int)($request->headers->get('xmlSourceId') ?? 0),
(int)($request->headers->get('isAllowedPagination') ?? 0) !== 0,
$request->query->get('idSearch'),
);
} catch (\InvalidArgumentException $e) {
return $this->json($this->helpers->getArrayError($e->getMessage()), 400);
}
}
/**
* @throws Exception
*/
#[Route('/getOfferDetails', name: 'hotel_offer_details', methods: ['POST'])]
public function getRateDetail(
Request $request,
HotelSearchStreamService $searchService,
HotelOfferDetailsService $offerService,bool $isMobile = false
): JsonResponse {
$body = json_decode($request->getContent(), true);
if (!is_array($body)) {
return $this->json(['error' => 'Invalid JSON body'], 400);
}
$idSearch = $body['idSearch'] ?? null;
$hotelsKey = $body['hotelsKey'] ?? null;
if (!is_string($idSearch) || $idSearch === '' || !is_array($hotelsKey) || $hotelsKey === []) {
return $this->json(['error' => 'Missing parameters'], 400);
}
$hotelsKey = array_values(array_unique(array_filter($hotelsKey, 'is_string')));
if ($hotelsKey === []) {
return $this->json(['error' => 'Invalid hotelsKey'], 400);
}
$ctx = $searchService->buildContext($this->getUser(), $request);
$request->getSession()?->save();
$content = $offerService->getOfferDetails(
$ctx,
$idSearch,
$hotelsKey,
(int)$request->headers->get('sourceId', 0),
(int)($body['nbRooms'] ?? 1), $isMobile
);
return $content === null
? $this->json(['error' => 'rate_expired'], 404)
: $this->json($content);
}
private function parseRateKeys(array $rawRateKeys, string $secretKey): array
{
$rooms_rate_keys = [];
$rooms = []; // raw per-room occupation strings, built once
$checkIn = null;
$checkOut = null;
$cityId = 0;
foreach ($rawRateKeys as $encryptedRateKey) {
$decryptedRateKey = $this->decryptRateKey($encryptedRateKey, $secretKey);
$tab = explode('~~@@', $decryptedRateKey);
if (!isset($tab[1])) {
throw new \RuntimeException('Malformed rate key: missing segment.');
}
$newRateKey = strtr($tab[1], ['pp' => '+', 'plus' => '+']);
if (str_contains($newRateKey, '@')) {
[$rateKey, $checkInStr, $checkOutStr, $roomStr, $rateCityId] =
$this->parseXmlRateKey($newRateKey);
$rooms_rate_keys[] = ['rateKey' => $rateKey];
$rooms[] = $roomStr;
} else {
[$rateKey, $checkInStr, $checkOutStr, $roomStr] =
$this->parseLocalRateKey($newRateKey);
$rooms_rate_keys[] = ['rateKey' => $rateKey];
$rooms[] = $roomStr;
$rateCityId = $cityId; // local rate keys don't carry a cityId today
}
// Original code silently overwrote these per rate key, keeping only the
// last one. We instead assert consistency — all rooms in one search
// should share the same stay dates / destination.
$checkIn ??= $checkInStr;
$checkOut ??= $checkOutStr;
$cityId = $cityId ?: $rateCityId;
}
return [
'rooms_rate_keys' => $rooms_rate_keys,
'jsonOccupation' => $this->buildJsonOccupation($rooms),
'checkIn' => $checkIn ?? '',
'checkOut' => $checkOut ?? '',
'cityId' => $cityId,
];
}
private function parseXmlRateKey(string $newRateKey): array
{
$tab1 = explode('@', $newRateKey);
if (!isset($tab1[1])) {
throw new \RuntimeException('Malformed XML rate key.');
}
$rateKey = '@' . $tab1[1];
$tab2 = explode('+', $tab1[1]);
$tab3 = explode('|', $tab2[0]);
if (!isset($tab3[0], $tab3[1], $tab3[9], $tab3[10])) {
throw new \RuntimeException('Malformed XML rate key segments.');
}
$checkInDate = \DateTime::createFromFormat('Ymd', $tab3[0]);
$checkOutDate = \DateTime::createFromFormat('Ymd', $tab3[1]);
if (!$checkInDate || !$checkOutDate) {
throw new \RuntimeException('Invalid date in rate key.');
}
return [
$rateKey,
$checkInDate->format('Y-m-d'),
$checkOutDate->format('Y-m-d'),
$tab3[9],
$tab3[10],
];
}
/**
* @return array{0:string,1:string,2:string,3:string} [rateKey, checkIn, checkOut, roomStr]
*/
private function parseLocalRateKey(string $newRateKey): array
{
$tabLocal = explode('|', $newRateKey);
if (!isset($tabLocal[0], $tabLocal[1], $tabLocal[7])) {
throw new \RuntimeException('Malformed local rate key.');
}
$checkInDate = \DateTime::createFromFormat('Ymd', $tabLocal[0]);
$checkOutDate = \DateTime::createFromFormat('Ymd', $tabLocal[1]);
if (!$checkInDate || !$checkOutDate) {
throw new \RuntimeException('Invalid date in rate key.');
}
return [
$newRateKey,
$checkInDate->format('Y-m-d'),
$checkOutDate->format('Y-m-d'),
$tabLocal[7],
];
}
/**
* Builds occupation data once for the full room list (this replaces the
* O(n^2) rebuild that happened inside the rate-key loop in the original).
*/
private function buildJsonOccupation(array $rooms): array
{
$jsonOccupation = [];
foreach ($rooms as $key => $room) {
$adults = [];
$children = [];
if (str_contains($room, '~')) {
// XML-style "adults~children~childAge"
$tab = explode('~', $room);
$adultCount = (int)($tab[0] ?? 0);
$childCount = (int)($tab[1] ?? 0);
$childAge = $tab[2] ?? null;
// NOTE: original hardcoded adult age as 40 — kept as-is since the
// downstream API apparently expects a placeholder age, but this
// should ideally come from real guest ages rather than a magic number.
$adults = array_fill(0, $adultCount, 40);
$children = $childCount > 0 ? array_fill(0, $childCount, $childAge) : [];
} else {
// Local-style "A:n+C:n"
$tabRooms = explode('+', $room);
if (count($tabRooms) >= 2) {
$adults = explode(':', $tabRooms[0])[1] ?? [];
$children = explode(':', $tabRooms[1])[1] ?? [];
}
}
$jsonOccupation[] = [
'id' => $key,
'adults' => $adults,
'children' => $children,
];
}
return $jsonOccupation;
}
private function renderOfferError(): Response
{
return $this->render('front/hotel/offer_error.html.twig', [
'social_networks' => $this->frontService->getSocialNetworks(),
'menu_front' => $this->frontService->getFrontMenu(),
'society' => $this->parameterService->getSocietyParameters(),
]);
}
/**
* Déchiffre une rate key chiffrée en AES-128-ECB puis urldecode le résultat.
*/
private function decryptRateKey(string $encryptedRateKey, string $secretKey): ?string
{
$decrypted = openssl_decrypt($encryptedRateKey, 'AES-128-ECB', $secretKey);
if ($decrypted === false) {
return null;
}
return urldecode($decrypted);
}
private function generateSearchCode(
string $secret,
string $currency = 'TND'
): string
{
$base64UrlEncode = function ($data) {
return rtrim(
strtr(base64_encode($data), '+/', '-_'),
'='
);
};
$now = time();
$searchResultsKey = bin2hex(random_bytes(16));
$uuid = bin2hex(random_bytes(16));
$header = [
'typ' => 'JWT',
'alg' => 'HS256'
];
$payload = [
'searchResultsKey' => $searchResultsKey,
'showSupplier' => false,
'viewCurrency' => $currency,
'showNetPrice' => false,
'showPriceComponents' => '',
'iat' => $now,
'nbf' => $now,
'exp' => $now + 36000,
'uuid' => $uuid,
'v' => hash('sha256', $searchResultsKey),
];
$encodedHeader = $base64UrlEncode(
json_encode($header, JSON_UNESCAPED_SLASHES)
);
$encodedPayload = $base64UrlEncode(
json_encode($payload, JSON_UNESCAPED_SLASHES)
);
$signature = hash_hmac(
'sha256',
$encodedHeader . '.' . $encodedPayload,
$secret,
true
);
$jwt = $encodedHeader . '.' .
$encodedPayload . '.' .
$base64UrlEncode($signature);
// Partie aléatoire contenant @
$randomPart = bin2hex(random_bytes(7))
. '.'
. random_int(10000000, 99999999)
. '@1';
return $randomPart . '#TFBEND#' . $jwt;
}
}