username = 'hotelrunner';
$this->password = '2otNDLCgJz9Tgdga';
$this->request = $request;
$this->propertyChannelService = $propertyChannelService;
$this->propertyChannelMappingService = $propertyChannelMappingService;
$this->propertyRoomRateChannelMappingService = $propertyRoomRateChannelMappingService;
$this->propertyRoomRatePriceService = $propertyRoomRatePriceService;
$this->propertyRoomAvailabilityService = $propertyRoomAvailabilityService;
$this->propertyRoomService = $propertyRoomService;
$this->bookingService = $bookingService;
$this->channelManagerPropertyMappingService = $channelManagerPropertyMappingService;
$this->channelManagerLogService = $channelManagerLogService;
$payload = $this->request->getContent();
$payloadXML = simplexml_load_string($payload);
$payloadParam = json_decode(json_encode($payloadXML), 1);
$this->param = $payloadParam;
$this->channelId = 1;
$this->channelManagerId = 3; //HotelRunner
$getRequestUri = $request->getRequestUri();
$getRequestUriExplode = explode('/', $getRequestUri);
$serviceRequestName = last($getRequestUriExplode);
//channelManagerLogService
$logArray = ['room-inventory-update'];
$this->channelManagerLogId = null;
$this->channelManagerRequestTime = microtime(true);
if (in_array($serviceRequestName, $logArray)) {
$insertDataLog = [
'property_id' => $this->param['hotel_id'],
'channel_manager_id' => $this->channelManagerId,
'type' => 'C2E',
'service' => $serviceRequestName,
'request' => $payload,
'response' => null,
'ip_address' => $this->request->ip(),
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
//channelManagerLogService
//Authentication
$errors = [];
if ($this->username != $payloadParam['username'] || $this->password != $payloadParam['password']) {
$errors[] = [
//'code' => 100,
'message' => 'Your username or password is incorrect.'
];
}
if (!empty($errors)) {
$this->responseError($errors);
}
}
public function responseError($errors)
{
$xmlResponse = new \SimpleXMLElement('');
$xmlResponse->addChild('status', -1);
foreach ($errors as $error) {
$xmlResponseError = $xmlResponse->addChild('message', $error['message']);
if (isset($error['code'])) {
$xmlResponseError->addAttribute('code', $error['code']);
}
}
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => $xmlResponse->asXML(),
'status' => 0
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
header('Content-type: text/xml');
echo $xmlResponse->asXML();
die();
}
public function responseSuccess($xmlPayload)
{
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => $xmlPayload->asXML(),
'status' => 1
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
header('Content-type: text/xml');
//echo $xmlPayload->asXML();
echo html_entity_decode($xmlPayload->asXML());
die();
}
public function propertyChannelMapping($propertyId)
{
$response = ['status' => false, 'message' => ''];
try {
$propertyChannelMappingCriteria = [
'criteria' => [
['field' => 'channel_id', 'condition' => '=', 'value' => $this->channelId],
['field' => 'property_id', 'condition' => '=', 'value' => $propertyId],
['field' => 'status', 'condition' => '=', 'value' => 1],
],
'firstRow' => true
];
$propertyChannelMapping = $this->propertyChannelMappingService->select($propertyChannelMappingCriteria);
if ($propertyChannelMapping['status'] != 'success') {
throw new ApiErrorException($propertyChannelMapping['message']);
}
$response = [
'status' => true,
'data' => $propertyChannelMapping['data']
];
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return $response;
}
public function channelPropertyRoomRate($propertyId)
{
$response = ['status' => false, 'message' => ''];
try {
$requestParam = [
'criteria' => [
['field' => 'channel_id', 'condition' => '=', 'value' => $this->channelId],
['field' => 'property_id', 'condition' => '=', 'value' => $propertyId],
['field' => 'status', 'condition' => '=', 'value' => 1],
],
'with' => [
'propertyRoomRateMapping.propertyRoomRate.propertyRoomRateAccommodation',
'propertyRoomRateMapping.propertyRoom.propertyRoomType',
]
];
$getChannelPropertyRoomRate = $this->propertyRoomRateChannelMappingService->select($requestParam);
if ($getChannelPropertyRoomRate['status'] != 'success' || empty($getChannelPropertyRoomRate['data'])) {
throw new ApiErrorException('Property Room Rate not found');
}
$getChannelPropertyRoomRate['data'] = collect($getChannelPropertyRoomRate['data'])->where('property_room_rate_mapping.property_room.status',1)->toArray();
$response = [
'status' => true,
'data' => $getChannelPropertyRoomRate['data']
];
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return $response;
}
public function channelManagerPropertyCheck($propertyId)
{
$response = ['status' => false, 'message' => ''];
try {
$requestParam =
[
'criteria' =>
[
['field' => 'status', 'condition' => '=', 'value' => 1],
['field' => 'property_id', 'condition' => '=', 'value' => $propertyId],
['field' => 'channel_manager_id', 'condition' => '=', 'value' => $this->channelManagerId],
],
'firstRow' => true
];
$channelManagerPropertyMapping = $this->channelManagerPropertyMappingService->select($requestParam);
if ($channelManagerPropertyMapping['status'] != 'success' || empty($channelManagerPropertyMapping['data'])) {
throw new ApiErrorException('Property Room Rate not found');
}
$channelManagerPropertyMapping = $channelManagerPropertyMapping['data'];
if (!is_null($channelManagerPropertyMapping['channel_manager_property_id'])) {
throw new ApiErrorException('This hotel can only be updated by ENW');
}
$response = [
'status' => true
];
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return $response;
}
public function roomRate(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
$propertyId = $this->param['hotel_id'];
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($propertyId);
if (!$channelManagerPropertyCheck['status']) {
throw new ApiErrorException($channelManagerPropertyCheck['message']);
}
$channelPropertyRoomRate = $this->channelPropertyRoomRate($propertyId);
if (!$channelPropertyRoomRate['status']) {
throw new ApiErrorException($channelPropertyRoomRate['message']);
}
$channelPropertyRoomRate = $channelPropertyRoomRate['data'];
$propertyChannelMapping = $this->propertyChannelMapping($propertyId);
if (!$propertyChannelMapping['status']) {
throw new ApiErrorException($propertyChannelMapping['message']);
}
$propertyChannelMapping = $propertyChannelMapping['data'];
$xmlResponse = new \SimpleXMLElement('');
$roomRates = [];
foreach ($channelPropertyRoomRate as $roomRate) {
if($roomRate['property_room_rate_mapping']['property_room_rate']['name'] == 'Best Available Rate') {
continue;
}
$roomRates[$roomRate['property_room_rate_mapping']['room_id']]['room'] = [
'id' => $roomRate['property_room_rate_mapping']['property_room']['id'],
'name' => $roomRate['property_room_rate_mapping']['property_room']['name'],
'status' => 'Active',
'capacity' => $roomRate['property_room_rate_mapping']['property_room']['max_adult'],
'capacity_child' => $roomRate['property_room_rate_mapping']['property_room']['max_child'],
];
$roomRates[$roomRate['property_room_rate_mapping']['room_id']]
['rate'][$roomRate['property_room_rate_mapping']['id']] = [
'id' => $roomRate['property_room_rate_mapping']['id'],
'name' => $roomRate['property_room_rate_mapping']['property_room_rate']['property_room_rate_accommodation']['name'],
'rate' => $roomRate['property_room_rate_mapping']['property_room_rate']['name'],
'accommodationId' => $roomRate['property_room_rate_mapping']['property_room_rate']['property_room_rate_accommodation']['id'],
'included_occupancy' => $roomRate['property_room_rate_mapping']['included_occupancy'],
];
}
foreach ($roomRates as $roomId => $roomRate) {
$room = $xmlResponse->addChild('room');
$room->addAttribute('id', $roomId);
$room->addAttribute('room_name', $roomRate['room']['name']);
$rates = $room->addChild('rates');
foreach ($roomRate['rate'] as $roomRateMappingId => $rateData) {
$rate = $rates->addChild('rate');
$rate->addAttribute('id', $roomRateMappingId);
$rate->addAttribute('board_id', $rateData['accommodationId']);
$rate->addAttribute('rate_name', $rateData['name'] . ' - ' . $rateData['rate'].' - '. $rateData['included_occupancy'].' Person');
$rate->addAttribute('allocation_group', $roomId);
$rate->addAttribute('stop_sale_group', $roomId . ':' . $roomRateMappingId);
$rate->addAttribute('min_stay_group', $roomId . ':' . $roomRateMappingId);
$rate->addAttribute('included_occupancy', $rateData['included_occupancy']);
}
}
$this->responseSuccess($xmlResponse);
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
if (!$response['status']) {
$errors[] = [
'message' => $response['message']
];
$this->responseError($errors);
}
}
public function availabilityRate(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
$propertyId = $this->param['hotel_id'];
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($propertyId);
if (!$channelManagerPropertyCheck['status']) {
throw new ApiErrorException($channelManagerPropertyCheck['message']);
}
$startDate = $this->param['start_date'];
$finishDate = $this->param['end_date'];
$channelPropertyRoomRate = $this->channelPropertyRoomRate($propertyId);
if (!$channelPropertyRoomRate['status']) {
throw new ApiErrorException($channelPropertyRoomRate['message']);
}
$channelPropertyRoomRate = $channelPropertyRoomRate['data'];
$propertyChannelMapping = $this->propertyChannelMapping($propertyId);
if (!$propertyChannelMapping['status']) {
throw new ApiErrorException($propertyChannelMapping['message']);
}
$propertyChannelMapping = $propertyChannelMapping['data'];
$requestParams = [
'property_id' => $propertyId,
'room_id' => null,
'room_rate_mapping_id' => null,
'channel_id' => $this->channelId,
'start_date' => $startDate,
'end_date' => $finishDate,
];
$propertyRoomType = $this->propertyRoomService->getPropertyRoomInventory($requestParams);
if ($propertyRoomType['status'] != 'success') {
throw new ApiErrorException($propertyRoomType['message']);
}
$propertyRoomType = $propertyRoomType['data'];
$propertyRoomRateAvailability = collect($propertyRoomType);
$roomRates = [];
foreach ($propertyRoomRateAvailability as $room) {
foreach ($room['property_room_rate_mapping'] as $roomRateMapping) {
foreach ($room['room_availability'] as $date => $roomAvailability) {
$roomRates[$room['id']]['availability'][$date] = $roomAvailability['value'];
}
$roomRates[$room['id']]['rate'][$roomRateMapping['id']] = [
'roomName' => $room['name'],
'roomRateName' => $roomRateMapping['name'],
//'minStay' => $roomRateMapping['min_stay'],
//'maxStay' => $roomRateMapping['max_stay'],
'currencyCode' => $roomRateMapping['currency_code'],
];
$roomRatePrices = reset($roomRateMapping['prices']);
foreach ($roomRatePrices['price'] as $date => $roomRatePrice) {
$roomRates[$room['id']]['rate'][$roomRateMapping['id']]['price'][$date] = $roomRatePrice['value'];
$roomRates[$room['id']]['rate'][$roomRateMapping['id']]['stopSell'][$date] = $roomRatePrice['stop_sell'];
}
foreach ($roomRatePrices['stop_sell'] as $date => $stopSell) {
$roomRates[$room['id']]['rate'][$roomRateMapping['id']]['stopSell'][$date] = $stopSell['value'];
}
foreach ($roomRatePrices['min_stay'] as $date => $minStay) {
$roomRates[$room['id']]['rate'][$roomRateMapping['id']]['minstay'][$date] = $minStay['value'];
}
}
}
$xmlResponse = new \SimpleXMLElement('');
$diffInDays = Carbon::parse($startDate)->diffInDays(Carbon::parse($finishDate));
if ($diffInDays > 180) {
throw new ApiErrorException('A maximum of 180 days of data can be retrieved.');
}
$currentDate = $startDate;
for ($i = 0; $i <= $diffInDays; $i++) {
foreach ($roomRates as $roomId => $roomRateMapping) {
foreach ($roomRateMapping['rate'] as $roomRateMappingId => $roomRate) {
$inventory = $xmlResponse->addChild('inventory');
$inventory->addAttribute('room_id', $roomId);
$inventory->addAttribute('rate_id', $roomRateMappingId);
$inventory->addAttribute('date', $currentDate);
$inventory->addAttribute('price', $roomRate['price'][$currentDate]);
$inventory->addAttribute('allocation', $roomRateMapping['availability'][$currentDate]);
$inventory->addAttribute('stop_sale', $roomRate['stopSell'][$currentDate] == 1 ? 1 : 0);
$inventory->addAttribute('min_stay', $roomRate['minstay'][$currentDate]);
}
}
$currentDate = Carbon::parse($currentDate)->addDay()->toDateString();
}
$this->responseSuccess($xmlResponse);
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
if (!$response['status']) {
$errors[] = [
'message' => $response['message']
];
$this->responseError($errors);
}
}
public function availabilityRateUpdate(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
$propertyId = $this->param['hotel_id'];
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($propertyId);
if (!$channelManagerPropertyCheck['status']) {
throw new ApiErrorException($channelManagerPropertyCheck['message']);
}
$channelPropertyRoomRate = $this->channelPropertyRoomRate($propertyId);
if (!$channelPropertyRoomRate['status']) {
throw new ApiErrorException($channelPropertyRoomRate['message']);
}
$channelPropertyRoomRate = $channelPropertyRoomRate['data'];
$channelPropertyRoomRateCollect = collect($channelPropertyRoomRate);
$propertyChannelMapping = $this->propertyChannelMapping($propertyId);
if (!$propertyChannelMapping['status']) {
throw new ApiErrorException($propertyChannelMapping['message']);
}
$propertyChannelMapping = $propertyChannelMapping['data'];
$availRateUpdates = singleElementXMLArray($this->param['inventories']['inventory']);
DB::beginTransaction();
$dateRange = [];
$dateRange['startDate'] = $this->param['start_date'];
$dateRange['finishDate'] = $this->param['end_date'];
if (Carbon::parse($dateRange['startDate'])->isBefore(Carbon::now()->toDateString()) || Carbon::parse($dateRange['finishDate'])->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($availRateUpdates as $availRateUpdate) {
$roomId = $availRateUpdate['@attributes']['room_id'];
$roomCheck = $channelPropertyRoomRateCollect->where('property_room_rate_mapping.room_id', $roomId)->isEmpty();
if ($roomCheck) {
throw new ApiErrorException('Undefined or inactive room accommodation');
}
$totalInventoryAvailable = null;
if (isset($availRateUpdate['@attributes']['allocation'])) {
$totalInventoryAvailable = $availRateUpdate['@attributes']['allocation'];
}
$currentDate = Carbon::parse($availRateUpdate['@attributes']['date'])->toDateString();
$requestParamBase = [
'property_id' => fillOnUndefined($propertyChannelMapping, 'property_id'),
'channel_id' => fillOnUndefined($propertyChannelMapping, 'channel_id'),
'availability_type_id' => fillOnUndefined($propertyChannelMapping, 'property_availability_type_id', 1),
'start_date' => $currentDate,
'end_date' => $currentDate,
'include_days' => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
];
if (!is_null($totalInventoryAvailable)) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'availability';
$requestParams['value'] = $totalInventoryAvailable;
$requestParams['room_rates'] = [
['room_id' => $roomId]
];
$propertyRoomRateMapping = $this->propertyRoomAvailabilityService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
$roomRateMappingId = $availRateUpdate['@attributes']['rate_id'];
$isCloseRoomRateMappingSale = null;
if (isset($availRateUpdate['@attributes']['stop_sale'])) {
$isCloseRoomRateMappingSale = $availRateUpdate['@attributes']['stop_sale'] == 1 ? true : false;
}
$channelRoomRateMappingCheck = $channelPropertyRoomRateCollect->where('room_rate_mapping_id', $roomRateMappingId)->isEmpty();
if ($channelRoomRateMappingCheck) {
throw new ApiErrorException('Undefined or inactive room accommodation');
}
$roomRates = [];
$roomRates[] = [
'room_id' => $roomId,
'room_rate_mapping_id' => [
$roomRateMappingId
]
];
$paramsListChannel = [];
$paramsListChannel[] = $this->channelId;
//CONNECTED CHANNEL RATE UPDATE
$propertyChannelMappingConnectedCriteria = [
'criteria' => [
['field' => 'property_id', 'condition' => '=', 'value' => $requestParamBase['property_id']],
['field' => 'connected_channel_id', 'condition' => '=', 'value' => $requestParamBase['channel_id']],
['field' => 'channel_id', 'condition' => '=', 'value' => 5],//JUST CM
['field' => 'status', 'condition' => '=', 'value' => 1],
],
'with' => ['channel']
];
$propertyChannelMappingConnected = $this->propertyChannelMappingService->select($propertyChannelMappingConnectedCriteria);
if ($propertyChannelMappingConnected['status'] == 'success') {
foreach ($propertyChannelMappingConnected['data'] as $channel) {
if (!is_null($channel['channel']['parent_id'])) {
continue;
}
$paramsListChannel[] = $channel['channel_id'];
}
}
//CONNECTED CHANNEL RATE UPDATE
foreach ($paramsListChannel as $paramChannelId) {
//Eğer Rate var ise currency check yapılmalı ve PerDay var mı check edilmeli, burada sonra günceleme yaptırılabilri
if (isset($availRateUpdate['@attributes']['price'])) {
$currency = $this->param['currency'];
$currencyCheck = ($currency == $propertyChannelMapping['currency_code']) ? true : false;
if (!$currencyCheck) {
throw new ApiErrorException('Exchange rate that does not match the channel exchange rate, channel exchange rate: ' . $propertyChannelMapping['currency_code']);
}
$channelRoomRateMapping = $channelPropertyRoomRateCollect->where('room_rate_mapping_id', $roomRateMappingId)->first();
$amountPerDay = $availRateUpdate['@attributes']['price'];
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'rate';
$requestParams['value'] = $amountPerDay;
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
//Room Rate Stop Sale
if (!is_null($isCloseRoomRateMappingSale)) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'rate_stop_sell';
$requestParams['value'] = $isCloseRoomRateMappingSale ? 1 : 0;
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
//Kısıtlamalar var ise min los
if (isset($availRateUpdate['@attributes']['min_stay'])) {
//Minimum Konaklama Gün Sayısı
if (isset($availRateUpdate['@attributes']['min_stay'])) {
$minStay = $availRateUpdate['@attributes']['min_stay'];
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'min_stay';
$requestParams['value'] = $minStay;
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
}
}
DB::commit();
$xmlResponse = new \SimpleXMLElement('');
$xmlResponse->addChild('status', 0);
$xmlResponse->addChild('message', 'success');
$xmlResponse->addChild('ConfirmCode', $this->channelManagerLogId);
$this->responseSuccess($xmlResponse);
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
DB::rollBack();
if (!$response['status']) {
$errors[] = [
'message' => $response['message']
];
$this->responseError($errors);
}
}
}