<?php
/**
* Created by PhpStorm.
* User: lskalytska
* Date: 11/11/19
* Time: 6:25 PM
*/
namespace App\Services\Tours;
use App\Entity\Otpusk\Country;
use App\Entity\Otpusk\City;
use App\Entity\Otpusk\GeoCategoryValues;
use App\Entity\Otpusk\HotelServices;
use App\Entity\Otpusk\HotelsRCache;
use App\Entity\Otpusk\Chain;
use App\Entity\Otpusk\PriceIndex;
use App\Entity\Otpusk\Rubric;
use App\Entity\Otpusk\Selection;
use App\Services\OtpuskApiService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class TourService
{
private EntityManagerInterface $repository;
private RequestStack $requestStack;
private HttpClientInterface $httpClient;
private OtpuskApiService $apiService;
private $cache;
private $accessToken = '4b26a-e5237-9b0fa-bcc36-67732';
private $seasonsPeriod = array(
'win' => array('begin' => '12', 'end' => '02'),
'spr' => array('begin' => '03', 'end' => '05'),
'sum' => array('begin' => '06', 'end' => '08'),
'aut' => array('begin' => '09', 'end' => '11'),
'winter' => array('begin' => '12', 'end' => '02'),
'spring' => array('begin' => '03', 'end' => '05'),
'summer' => array('begin' => '06', 'end' => '08'),
'autumn' => array('begin' => '09', 'end' => '11'),
'january' => array('begin' => '01', 'end' => '01'),
'february' => array('begin' => '02', 'end' => '02'),
'march' => array('begin' => '03', 'end' => '03'),
'april' => array('begin' => '04', 'end' => '04'),
'may' => array('begin' => '05', 'end' => '05'),
'june' => array('begin' => '06', 'end' => '06'),
'july' => array('begin' => '07', 'end' => '07'),
'august' => array('begin' => '08', 'end' => '08'),
'september' => array('begin' => '09', 'end' => '09'),
'october' => array('begin' => '10', 'end' => '10'),
'november' => array('begin' => '11', 'end' => '11'),
'december' => array('begin' => '12', 'end' => '12'),
'1' => array('begin' => '01', 'end' => '01'),
'2' => array('begin' => '02', 'end' => '02'),
'3' => array('begin' => '03', 'end' => '03'),
'4' => array('begin' => '04', 'end' => '04'),
'5' => array('begin' => '05', 'end' => '05'),
'6' => array('begin' => '06', 'end' => '06'),
'7' => array('begin' => '07', 'end' => '07'),
'8' => array('begin' => '08', 'end' => '08'),
'9' => array('begin' => '09', 'end' => '09'),
'10' => array('begin' => '10', 'end' => '10'),
'11' => array('begin' => '11', 'end' => '11'),
'12' => array('begin' => '12', 'end' => '12'),
);
private $months = [
1 => 'january',
2 => 'february',
3 => 'march',
4 => 'april',
5 => 'may',
6 => 'june',
7 => 'july',
8 => 'august',
9 => 'september',
10 => 'october',
11 => 'november',
12 => 'december'
];
/**
* Dates for start and ending tours searching
*/
const _ADD_START_DAY = 7; // +7 days from now
const _ADD_END_DAY = 21; // +21 day from start
const _ADD_START_DAY_HOT = 2; // +2 days from now for hot tours
const _ADD_END_DAY_HOT = 12; // +12 day from start for hot tours
const _ADD_START_DAY_MIN = 2; // +2 days from now for min tours
const _ADD_END_DAY_MIN = 118; // +118 day from start for min tours
public function __construct(EntityManagerInterface $repository, RequestStack $requestStack, HttpClientInterface $httpClient, OtpuskApiService $apiService)
{
$this->repository = $repository;
$this->requestStack = $requestStack;
$this->httpClient = $httpClient;
$this->apiService = $apiService;
$option = array(
\Memcached::OPT_PREFIX_KEY => (getenv('MEMCACHED_PREFIX')?getenv('MEMCACHED_PREFIX'):($_ENV['MEMCACHED_PREFIX']??'')),
);
$this->cache = MemcachedAdapter::createConnection(getenv('MEMCACHED_SERV')?getenv('MEMCACHED_SERV'):$_ENV['MEMCACHED_SERV'], $option);
}
/**
* @param int|null $countryId
* @param int|null $cityId
* @param string|null $season
* @param string|null $month
* @param int|null $budget
* @param string|null $currency
* @param int $cityFrom
* @return ArrayCollection
* @throws \ErrorException
*/
public function getGeneralToursPage($market, $dbNormalizer, int $countryId = null, int $cityId = null, string $season = null, int $month = null, int $budget = null, string $currency = null, int $cityFrom, $locale = 'ru', $isDeparture = false, $isRubric = false)
{
$key = 'mainpage_' . $market['id'] . '_' . $countryId . '_' . $cityId . '_' . $season . '_' . $month . '_' . $budget . '_' . $currency . '_' . $currency. '_' . $locale. '_' . (int)$isDeparture. '_' . $cityFrom. '_' . (int)$isRubric;
if (($tours = $this->cache->get($key)) !== false) return $tours;
$tours = new ArrayCollection();
$tours = $this->getMainInfo($market, $dbNormalizer, $tours, $countryId, $cityId, $locale, $isDeparture, $cityFrom, $season, $month, $isRubric);
$tours = $this->getTours($market, $dbNormalizer, $tours, $countryId, $cityId, $season, $month, $budget, $currency, false, null, $cityFrom, $locale, $isDeparture);
if(isset($season) && property_exists($tours, 'info'))
$tours->info['season'] = $season;
$this->cache->set($key, $tours, 30 * 60);
return $tours;
}
public function getSelectionPage(string $marketCode, int $selectionId, $locale = 'ru')
{
$tours = $this->cache->get('selection_'.$marketCode.'_'.$selectionId.'_'.$locale);
return $tours;
}
public function getSelectionsForGeoById(string $marketCode, int $geoId)
{
$key = 'selections_page_'.$marketCode.'_by_geo_id_'.$geoId;
if (($results = $this->cache->get($key)) !== false) return $results;
$results = [];
$selectionIds = $this->cache->get('selections_'.$marketCode.'_by_geo_id_'.$geoId);
if ($selectionIds) {
$results = $this->repository->getRepository(Selection::class)->findByIds($selectionIds);
}
$this->cache->set($key, $results, 5 * 60);
return $results;
}
public function getPageCountries($market, $dbNormalizer, $locale = 'ru')
{
$key = 'countries_' . $market['id'] . '_'.$locale;
if (($results = $this->cache->get($key)) !== false) return $results;
$results = new ArrayCollection();
$results->popularNow = $this->repository->getRepository(Country::class, 'otpusk')->getByCategory($market, 'countries-popular-now', $locale);
$results->popularNow = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCountries($market, $results->popularNow);
$results->natureAndMountains = $this->repository->getRepository(Country::class, 'otpusk')->getByCategory($market, 'countries-nature-and-mountains', $locale);
$results->natureAndMountains = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCountries($market, $results->natureAndMountains);
$results->earlyBooking = $this->repository->getRepository(Country::class, 'otpusk')->getByCategory($market, 'countries-early-booking', $locale);
$results->earlyBooking = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCountries($market, $results->earlyBooking);
$results->citiesAndLandmarks = $this->repository->getRepository(Country::class, 'otpusk')->getByCategory($market, 'countries-cities-and-landmarks', $locale);
$results->citiesAndLandmarks = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCountries($market, $results->citiesAndLandmarks);
$results->exoticCountries = $this->repository->getRepository(Country::class, 'otpusk')->getByCategory($market, 'countries-exotic-countries', $locale);
$results->exoticCountries = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCountries($market, $results->exoticCountries);
$results->cities = $this->repository->getRepository(City::class, 'otpusk')->getGuidCitiesByIds($market, $locale, null, $dbNormalizer);
$results->cities = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCities($market, $results->cities);
$results->excursions = $this->repository->getRepository(Country::class, 'otpusk')->getExcursions();
$this->cache->set($key, $results, 30 * 60);
return $results;
}
public function getRubrics($dbNormalizer, $countryObj, $rubricObj = null, $locale = 'ru', $citiesStatus = false)
{
$key = (is_null($rubricObj))
? 'country_' . $countryObj->getId(). '_rubrics_' . $locale
: 'country_' . $countryObj->getId(). '_rubric_' . $rubricObj->getId(). '_' . $locale
;
if (($rubrics = $this->cache->get($key)) !== false) return $rubrics;
$rubrics = $this->repository->getRepository(Rubric::class)->findByCountry($countryObj, $rubricObj, $citiesStatus);
foreach ($rubrics as $k => $rubric) {
$rubrics[$k]->setCitiesOutput($this->repository->getRepository(City::class, 'otpusk')->getCitiesByIds($dbNormalizer, $locale, $rubric->getCities()));
}
$this->cache->set($key, $rubrics, 30 * 60);
return $rubrics;
}
/**
* @param ArrayCollection $tours
* @param int|null $countryId
* @param int|null $cityId
* @param bool $guide
* @return ArrayCollection
* @throws \ErrorException
*/
public function getMainInfo($market, $dbNormalizer, ArrayCollection $tours, int $countryId = null, int $cityId = null, $locale = 'ru', $isDeparture = false, $cityFrom, string $season = null, int $month = null, $isRubric = false)
{
$date = new \DateTime();
$date->add(new \DateInterval('P' . self::_ADD_START_DAY . 'D'));
if ($countryId && is_null($cityId)) {
$tours->info = $this->getCountryInfo($countryId, $locale, $dbNormalizer);
$tours->faq = $this->getCountryFaq($countryId, 'Часто задаваемые вопросы', $locale, $dbNormalizer);
$tours->cities = $this->getPopCities($countryId, $cityId, $locale, $isDeparture, $cityFrom, $dbNormalizer);
$tours->similarCountries = $this->repository->getRepository(Country::class, 'otpusk')->getSimilarById($countryId, $locale, $dbNormalizer);
if (!empty($tours->similarCountries)) {
// $tours->similarCountries = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceForCountryPage($market, $tours->similarCountries);
foreach ($tours->similarCountries as $k => $similarCountry) {
$key = 'similar_country_price_' . $market['id'] . '_'.$similarCountry['id'];
if (($similarCountryPrice = $this->cache->get($key)) !== false) {
$tours->similarCountries[$k]['price'] = $similarCountryPrice;
} else {
$similarCountryPrice = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceForCountryPageByCountryId($market, $similarCountry['id']);
$tours->similarCountries[$k]['price'] = $similarCountryPrice;
$this->cache->set($key, $similarCountryPrice, 30 * 60);
}
}
$tours->similarCountries = $this->repository->getRepository(Country::class, 'otpusk')->getCountriesDescription($tours->similarCountries, 'Описание для посадочных страниц', $locale, $dbNormalizer);
}
$tours->monthly = $this->getCountryMonthly($market, $countryId);
$tours->description = null;
if (!$isDeparture && is_null($season) && is_null($month)) {
$tours->description = $this->getCountryFaq($countryId, 'Туры в страну', $locale, $dbNormalizer);
}
$tours->descriptionNew = $this->getCountryFaqNew($countryId, $locale, $season, (!is_null($month)?$this->months[$month]:null), $isDeparture, $cityFrom, $isRubric, $dbNormalizer);
} elseif ($cityId) {
$tours->info = $this->getCityInfo($cityId, $locale, $dbNormalizer);
$tours->faq = $this->getCityFaq($cityId, 'Часто задаваемые вопросы', $locale, $dbNormalizer);
$tours->weather = $this->getWeather($cityId, $dbNormalizer['temperatures']['language'][$locale]);
$tours->cities = $this->getPopCities(null, $cityId, $locale, $isDeparture, $cityFrom, $dbNormalizer);
$tours->similarCities = $this->repository->getRepository(City::class, 'otpusk')->getGuidCitiesByIds($market, $locale, $cityId, $dbNormalizer);
// $tours->similarCities = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCities($market, $tours->similarCities);
foreach ($tours->similarCities as $k => $similarCity) {
$key = 'similar_city_price_' . $market['id'] . '_'.$similarCity['id'];
if (($similarCityPrice = $this->cache->get($key)) !== false) {
$tours->similarCities[$k]['price'] = $similarCityPrice;
} else {
$similarCityPrice = $this->repository->getRepository(PriceIndex::class, 'otpusk')->getPriceCitiesByCityId($market, $similarCity['id']);
$tours->similarCities[$k]['price'] = $similarCityPrice;
$this->cache->set($key, $similarCityPrice, 30 * 60);
}
}
$tours->monthly = $this->getCityMonthly($market, $cityId);
$tours->description = null;
if (!$isDeparture && is_null($season) && is_null($month)) {
$tours->description = $this->getCityFaq($cityId, 'Туры в страну', $locale, $dbNormalizer);
}
$tours->descriptionNew = $this->getCityFaqNew($cityId, $locale, $season, (!is_null($month)?$this->months[$month]:null), $isDeparture, $cityFrom, $dbNormalizer);
}
return $tours;
}
private function _getCalendarDate($id)
{
if (empty($id) || $id<0 || $id>5000) return '';
$token = getenv('OTPUSK_API_TOKEN') ? getenv('OTPUSK_API_TOKEN') : $_ENV['OTPUSK_API_TOKEN'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.otpusk.com/api/2.4/tours/dates?to='.$id.'&access_token='.$token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, 'dev:dev');
curl_setopt($ch, CURLOPT_USERAGENT, 'web.otpusk.com getCalendarDate');
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
return '';
} else {
$response = @json_decode($response,1);
if (empty($response)) return '';
if (empty($response['dates'])) return '';
$dates = array_keys($response['dates']);
if (empty($dates)) return '';
return $dates[0];
}
}
/**
* @param ArrayCollection $tours
* @param int|null $countryId
* @param int|null $cityId
* @param string|null $season
* @param int|null $month
* @param int|null $budget
* @param string|null $currency
* @param bool $hot
* @param string|null $order
* @param int $cityFrom
* @return ArrayCollection
* @throws \Exception
*/
public function getTours($market, $dbNormalizer, ArrayCollection $tours, int $countryId = null, int $cityId = null, string $season = null, int $month = null, int $budget = null, string $currency = null, bool $hot = false, string $order = null, int $cityFrom, $locale = 'ru', $isDeparture = false)
{
$date = new \DateTime($this->_getCalendarDate(max($countryId,$cityId))??'now');
$period = $this->checkPeriod($season, $month, $date, $hot);
if(isset($order) && $order == 'quality')
$order = true;
else
$order = false;
$tours->qualityPrice = $this->getBestQualityTours($market, $dbNormalizer, $locale, null, null, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $isDeparture);
if(is_null($month) && is_null($season) && !$hot){
$hotPeriod = $this->checkPeriod(null, null, new \DateTime(), true);
$tours->hot = $this->getCheapestTours($market, $dbNormalizer, $locale, 'hot', $countryId, $cityId, $cityFrom, $hotPeriod, $budget, $currency, false, $isDeparture);
$tours->hotLux = $this->getCheapestTours($market, $dbNormalizer, $locale, null, $countryId, $cityId, $cityFrom, $hotPeriod, $budget, $currency, true, $isDeparture);
}
$dateCheapest = new \DateTime($this->_getCalendarDate(max($countryId,$cityId))??'now');
$periodCheapest = array(
'dateBeg' => $dateCheapest
->add(new \DateInterval('P' . self::_ADD_START_DAY_MIN . 'D'))
->format('Y-m-d'),
'dateEnd' => $dateCheapest
->add(new \DateInterval('P' . self::_ADD_END_DAY_MIN . 'D'))
->format('Y-m-d')
);
$tours->cheapest = $this->getCheapestTours($market, $dbNormalizer, $locale, null, $countryId, $cityId, $cityFrom, $periodCheapest, $budget, $currency, false, $isDeparture);
if(is_null($month) && is_null($season) && !$hot){
$tours->hotBudget = $tours->cheapest;
}
$tours->friendsBudget = $this->getTypeTours($market, $dbNormalizer, $locale, 'friends', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, false, $isDeparture);
$tours->friends = $this->getTypeTours($market, $dbNormalizer, $locale, 'friends', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, false, false, $isDeparture);
$tours->friendsLux = $this->getTypeTours($market, $dbNormalizer, $locale, 'friends', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, true, $isDeparture);
$tours->romanticBudget = $this->getTypeTours($market, $dbNormalizer, $locale, 'romantic', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, false, $isDeparture);
$tours->romantic = $this->getTypeTours($market, $dbNormalizer, $locale, 'romantic', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, false, false, $isDeparture);
$tours->romanticLux = $this->getTypeTours($market, $dbNormalizer, $locale, 'romantic', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, true, $isDeparture);
$tours->familyBudget = $this->getTypeTours($market, $dbNormalizer, $locale, 'family', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, false, $isDeparture);
$tours->family = $this->getTypeTours($market, $dbNormalizer, $locale, 'family', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, false, false, $isDeparture);
$tours->familyLux = $this->getTypeTours($market, $dbNormalizer, $locale, 'family', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, true, $isDeparture);
$tours->peacefulBudget = $this->getTypeTours($market, $dbNormalizer, $locale, 'peaceful', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, false, $isDeparture);
$tours->peaceful = $this->getTypeTours($market, $dbNormalizer, $locale, 'peaceful', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, false, false, $isDeparture);
$tours->peacefulLux = $this->getTypeTours($market, $dbNormalizer, $locale, 'peaceful', $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, true, true, $isDeparture);
$tours->hotelsChains = $this->getHotelsChains($countryId, $cityId);
$tours->bestHotels = array(
5 => $this->getBestHotelsTours($market, $dbNormalizer, $locale, null, 5, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $isDeparture),
4 => $this->getBestHotelsTours($market, $dbNormalizer, $locale, null, 4, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $isDeparture),
3 => $this->getBestHotelsTours($market, $dbNormalizer, $locale, null, 3, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $isDeparture),
2 => $this->getBestHotelsTours($market, $dbNormalizer, $locale, null, 2, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $isDeparture),
);
$tours->sport = $this->getServicesCount($date->format('Y-m-d'), $countryId, $cityId, $period);
$tours->hotelServices = $this->getHotelServcies($dbNormalizer, $locale);
$tours->catalogue = new ArrayCollection();
$tours->catalogue->friends = count($tours->friends) >= 1 ? 1 : 0;
$tours->catalogue->romantic = count($tours->romantic) >= 1 ? 1 : 0;
$tours->catalogue->family = count($tours->family) >= 1 ? 1 : 0;
$tours->catalogue->peaceful = count($tours->peaceful) >= 1 ? 1 : 0;
$tours->catalogue->aquapark = (int)$tours->sport[0]['aquapark'];
$tours->catalogue->spa = (int)$tours->sport[0]['spa'];
$tours->departureCities = $this->getDepartureCities($market, $dbNormalizer, $locale, $countryId);
$tours->departureCity = $this->getDepartureCity($market, $tours->departureCities);
return $tours;
}
public function getDepartureCities($market, $dbNormalizer, $locale, $countryId)
{
$key = 'departureCities_' . $market['id'] . '_' . $countryId . '_' . $locale;
if (($cities = $this->cache->get($key)) !== false) return $cities;
$fromCities = $this->apiService->getInfo($market['apiToken'], 'cities', $countryId);
$uniqueIds = (count($fromCities) > 0) ? '\''.implode('\',\'', array_values($fromCities)).'\'' : null;
$cities = (!is_null($uniqueIds)) ? $this->repository->getRepository(City::class)->getDepartureCitiesForCountrySearchPage($uniqueIds, $locale, $dbNormalizer) : [];
$this->cache->set($key, $cities, 8 * 60 * 60);
return $cities;
}
public function getDepartureCity($market, $departureCities)
{
$marketCities = [];
foreach ($market['mainCities'] as $marketCity) {
if (!is_null($marketCity['id'])) {
$marketCities[] = $marketCity['id'];
}
}
$marketCities = array_unique($marketCities);
$departureCityIds = [];
foreach ($departureCities as $departureCityItem) {
$departureCityIds[] = $departureCityItem['id'];
}
$departureCitiesIntersect = array_intersect($marketCities, $departureCityIds);
if (count($departureCitiesIntersect) > 0) {
$departureCity = array_shift($departureCitiesIntersect);
} elseif (count($departureCityIds) > 0) {
$departureCity = array_shift($departureCityIds);
} else {
$departureCity = null;
}
return $departureCity;
}
public function getPopCities($countryId, $cityId, $locale = 'ru', $isDeparture = false, $cityFrom, $dbNormalizer)
{
$key = 'popCities_' . $countryId . '_' . $cityId. '_' . $locale. '_' . (int)$isDeparture. '_' . $cityFrom;
if (($cities = $this->cache->get($key)) !== false) return $cities;
$cities = $this->repository
->getRepository(City::class, 'otpusk')
->getPopCities($countryId, $cityId, $locale, $isDeparture, $cityFrom, $dbNormalizer);
$this->cache->set($key, $cities, 8 * 60 * 60);
return $cities;
}
public function getBestQualityTours($market, $dbNormalizer, $locale, $type = null, $stars = null, $countryId = null, $cityId = null, $cityFrom, $period = null, $budget = null, $currency = null, $isDeparture = false)
{
$tours = $this->repository
->getRepository(HotelsRCache::class, 'otpusk')
->getBestQualityTours($market, $dbNormalizer, $locale, $type, $stars, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $isDeparture);
return $tours;
}
public function getCheapestTours($market, $dbNormalizer, $locale, $type = null, $countryId = null, $cityId = null, $cityFrom, $period = null, $budget = null, $currency = null, $vipStatus = false, $isDeparture = false)
{
$tours = $this->repository
->getRepository(HotelsRCache::class, 'otpusk')
->getCheapestTours($market, $dbNormalizer, $locale, $type, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $vipStatus, $isDeparture);
return $tours;
}
public function getBestHotelsTours($market, $dbNormalizer, $locale, $type = null, $stars = null, $countryId = null, $cityId = null, $cityFrom, $period = null, $budget = null, $currency = null, $isDeparture = false)
{
$tours = $this->repository
->getRepository(HotelsRCache::class, 'otpusk')
->getBestHotelsTours($market, $dbNormalizer, $locale, $type, $stars, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $isDeparture);
return $tours;
}
public function getTypeTours($market, $dbNormalizer, $locale, $type = null, $countryId = null, $cityId = null, $cityFrom, $period = null, $budget = null, $currency = null, $order = false, $withoutRating = false, $vipStatus = false, $isDeparture = false)
{
$tours = $this->repository
->getRepository(HotelsRCache::class, 'otpusk')
->getTypeTours($market, $dbNormalizer, $locale, $type, $countryId, $cityId, $cityFrom, $period, $budget, $currency, $order, $withoutRating, $vipStatus, $isDeparture);
return $tours;
}
public function getHotelsChains($countryId = null, $cityId = null)
{
$chains = $this->repository
->getRepository( Chain::class, 'otpusk')
->getChainsByCount($countryId, $cityId);
return $chains;
}
public function getServicesCount($date, $countryId = null, $cityId = null, $period = null)
{
$services = $this->repository
->getRepository( HotelsRCache::class, 'otpusk')
->getServicesCount($date, $countryId, $cityId, $period);
return $services;
}
public function getHotelServcies($dbNormalizer, $locale)
{
$services = $this->repository
->getRepository( HotelServices::class, 'otpusk')
->getHotelServcies($dbNormalizer, $locale);
return $services;
}
private function getMonthsArray(){
return array(
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Jan', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Feb', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Mar', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Apr', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'May', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Jun', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Jul', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Aug', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Sep', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Oct', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Nov', 'weather' => null, 'water' => null),
array('price' => null, 'currency' => null, 'uah' => null, 'date' => null, 'monthName' => 'Dec', 'weather' => null, 'water' => null),
);
}
public function getCountryMonthly($market, $countryId = null)
{
$months = $this->getMonthsArray();
if(isset($countryId)){
$res = $this->repository
->getRepository( HotelsRCache::class, 'otpusk')
->getMonthlyInfo($market, $countryId, 'countryId');
$temps = $this->repository
->getRepository(Country::class, 'otpusk')
->getTemperaturesByCountry($countryId);
foreach ($months as &$month) {
foreach ($res as $item) {
$date = new \DateTime($item['date']);
if($month['monthName'] == $date->format('M')){
foreach ($item as $index => $value) {
$month[$index] = $value;
}
if(is_array($temps) && count($temps) > 0){
foreach ($temps AS $temp){
$month[$temp['type']] = array_key_exists($month['monthName'], $temp) ? round($temp[$month['monthName']]) : null;
}
}
continue;
}
}
if(is_array($temps) && count($temps) > 0){
foreach ($temps AS $temp){
$month[$temp['type']] = array_key_exists($month['monthName'], $temp) ? round($temp[$month['monthName']]) : null;
}
}
}
}
return $months;
}
public function getCityMonthly($market, $cityId = null)
{
$months = $this->getMonthsArray();
if(isset($cityId)){
$res = $this->repository
->getRepository( HotelsRCache::class, 'otpusk')
->getMonthlyInfo($market, $cityId, 'cityId');
$temps = $this->repository
->getRepository(City::class, 'otpusk')
->getTemperaturesByCity($cityId);
foreach ($months as &$month) {
foreach ($res as $item) {
$date = new \DateTime($item['date']);
if($month['monthName'] == $date->format('M')){
foreach ($item as $index => $value) {
$month[$index] = $value;
}
if(is_array($temps) && count($temps) > 0){
foreach ($temps AS $temp){
$month[$temp['type']] = array_key_exists($month['monthName'], $temp) ? (int)$temp[$month['monthName']] : null;
}
}
continue;
}
}
if(is_array($temps) && count($temps) > 0){
foreach ($temps AS $temp){
$month[$temp['type']] = array_key_exists($month['monthName'], $temp) ? (int)$temp[$month['monthName']] : null;
}
}
}
}
return $months;
}
/**
* @param $season
* @param $month
* @param $date
* @param bool $hot
* @return array|null
* @throws \Exception
*/
public function checkPeriod($season, $month, $date, $hot = false)
{
// general period
if (is_null($season) && is_null($month) && !$hot)
return array(
'dateBeg' => $date
->add(new \DateInterval('P' . self::_ADD_START_DAY . 'D'))
->format('Y-m-d'),
'dateEnd' => $date
->add(new \DateInterval('P' . self::_ADD_END_DAY . 'D'))
->format('Y-m-d')
);
// period for hot tours
elseif (is_null($season) && is_null($month) && $hot)
return array(
'dateBeg' => $date
->add(new \DateInterval('P' . self::_ADD_START_DAY_HOT . 'D'))
->format('Y-m-d'),
'dateEnd' => $date
->add(new \DateInterval('P' . self::_ADD_END_DAY_HOT . 'D'))
->format('Y-m-d')
);
// period for months period
if (isset($month) && ($month >= 1 || $month <= 12)) {
if ((int)$date->format('m') > $month) {
$year = $date->format('Y') + 1;
return array(
'dateBeg' => $year . '-' . $this->seasonsPeriod[$month]['begin'] . '-01',
'dateEnd' => $year . '-' . $this->seasonsPeriod[$month]['begin'] . '-31'
);
} elseif ((int)$date->format('m') == $month) {
return array(
'dateBeg' => $date->format('Y-n-d'),
'dateEnd' => $date->format('Y-n') . '-31'
);
} else {
return array(
'dateBeg' => $date->format('Y') . '-' . $this->seasonsPeriod[$month]['begin'] . '-01',
'dateEnd' => $date->format('Y') . '-' . $this->seasonsPeriod[$month]['begin'] . '-31'
);
}
} // period for season
elseif (isset($season) && array_key_exists($season, $this->seasonsPeriod)) {
if ($season == 'win' || $season == 'winter') {
$year = $date->format('Y') + 1;
return array(
'dateBeg' => $date->format('Y') . '-' . $this->seasonsPeriod[$season]['begin'] . '-01',
'dateEnd' => $year . '-' . $this->seasonsPeriod[$season]['end'] . '-31'
);
} elseif ((int)$date->format('m') > (int)$this->seasonsPeriod[$season]['begin']) {
$year = $date->format('Y') + 1;
return array(
'dateBeg' => $year . '-' . $this->seasonsPeriod[$season]['begin'] . '-01',
'dateEnd' => $year . '-' . $this->seasonsPeriod[$season]['end'] . '-31'
);
} elseif ((int)$date->format('m') >= (int)$this->seasonsPeriod[$season]['begin'] && (int)$date->format('m') <= (int)$this->seasonsPeriod[$season]['end']) {
return array(
'dateBeg' => $date->format('Y-n-d'),
'dateEnd' => $date->format('Y') . '-' . $this->seasonsPeriod[$season]['end'] . '-31'
);
} else {
return array(
'dateBeg' => $date->format('Y') . '-' . $this->seasonsPeriod[$season]['begin'] . '-01',
'dateEnd' => $date->format('Y') . '-' . $this->seasonsPeriod[$season]['end'] . '-31'
);
}
}
return null;
}
/**
* @param $countryId
* @return mixed
*/
public function getCountryInfo($countryId, $locale = 'ru', $dbNormalizer)
{
return $this->repository
->getRepository(Country::class, 'otpusk')
->getCountryInfo($countryId, $locale, $dbNormalizer);
}
/**
* @param $countryId
* @return mixed
*/
public function getCityInfo($cityId, $locale = 'ru', $dbNormalizer)
{
return $this->repository
->getRepository(City::class, 'otpusk')
->getCityInfo($cityId, $locale, $dbNormalizer);
}
/**
* @param $countryAlias
* @param $cityAlias
* @return mixed
*/
public function getIdByAlias($countryAlias, $cityAlias)
{
$teritory = $this->repository
->getRepository(Country::class, 'otpusk')
->getIdByAlias($countryAlias, $cityAlias);
if(is_null($teritory) || !array_key_exists('countryId', $teritory))
throw new NotFoundHttpException("Can't find country or city");
elseif(isset($cityAlias) && !array_key_exists('cityId', $teritory))
throw new NotFoundHttpException("Can't find city");
elseif(is_null($cityAlias))
$teritory['cityId'] = null;
return $teritory;
}
public function getCountryFaq($countryId, $type, $locale = 'ru', $dbNormalizer)
{
return $this->repository
->getRepository(Country::class, 'otpusk')
->getCountryFaq($countryId, $type, $locale, $dbNormalizer);
}
public function getCountryFaqNew($countryId, $locale = 'ru', $season = null, $month = null, $isDeparture = false, $cityFrom, $isRubric = false, $dbNormalizer)
{
return $this->repository
->getRepository(Country::class, 'otpusk')
->getCountryFaqNew($countryId, $locale, $season, $month, $isDeparture, $cityFrom, $isRubric, $dbNormalizer);
}
public function getCityFaq($cityId, $type, $locale = 'ru', $dbNormalizer)
{
return $this->repository
->getRepository(City::class, 'otpusk')
->getCityFaq($cityId, $type, $locale, $dbNormalizer);
}
public function getCityFaqNew($cityId, $locale = 'ru', $season = null, $month = null, $isDeparture = false, $cityFrom, $dbNormalizer)
{
return $this->repository
->getRepository(City::class, 'otpusk')
->getCityFaqNew($cityId, $locale, $season, $month, $isDeparture, $cityFrom, $dbNormalizer);
}
public function getWeather($cityId, $lang)
{
return $this->repository
->getRepository(City::class, 'otpusk')
->getWeather($cityId, $lang);
}
public function fetchOfferDetails(string $offerId, string $lang = 'uk', string $currency = 'uah'): ?array
{
$request = $this->requestStack->getCurrentRequest();
$host = str_replace(['web','www'],'api', $request->getSchemeAndHttpHost());
$url = sprintf(
'https://api.otpusk.com/api/2.4/tours/offer?offerId=%s&access_token=%s&lang=%s¤cyLocal=%s',
$offerId,
$this->accessToken,
$lang,
$currency
);
$response = $this->httpClient->request('GET', $url);
if ($response->getStatusCode() === 200) {
return $response->toArray();
}
return null;
}
public function fetchHotelsDetails(string $hotelId, string $lang = 'uk')
{
$request = $this->requestStack->getCurrentRequest();
$host = str_replace(['web','www'],'api', $request->getSchemeAndHttpHost());
$url = sprintf(
'https://api.otpusk.com/api/2.4/tours/hotels?hotelId=%s&access_token=%s&lang=%s',
$hotelId,
$this->accessToken,
$lang,
);
$response = $this->httpClient->request('GET', $url);
if ($response->getStatusCode() === 200) {
return $response->toArray();
}
return null;
}
public function fetchHotelDetails(string $hotelId, string $lang = 'uk')
{
$request = $this->requestStack->getCurrentRequest();
$host = str_replace(['web','www'],'api', $request->getSchemeAndHttpHost());
$url = sprintf(
'https://api.otpusk.com/api/2.4/tours/hotel?hotelId=%s&access_token=%s&lang=%s',
$hotelId,
$this->accessToken,
$lang,
);
$response = $this->httpClient->request('GET', $url);
if ($response->getStatusCode() === 200) {
return $response->toArray();
}
return null;
}
}