first commit

This commit is contained in:
ExtraNetwork
2026-05-12 17:04:54 +03:00
commit e5c4b6aa13
1425 changed files with 284735 additions and 0 deletions

View File

@@ -0,0 +1,488 @@
<?php
namespace App\Http\Controllers\ChannelManager\Athena\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
class AthenaController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
//Note: channel_manager_property_mapping tablosunda channel_manager_property_id alanı NULL ise, bu ENW nin CHANNEL tarafından yönetiliyor olması demek.
//Eğer channel_manager_property_id alanında bir otel id var ise, bu CHANNEL ın ENW tarafından güncelleniyor olması demek.
$this->username = 'athena';
$this->password = 'AU3EUmA9LChTzAzv';
$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();
$payloadJson = json_decode($payload, 1);
$this->param = $payloadJson;
$this->channelId = 1;
$this->channelManagerId = 5; //Athena
$getRequestUri = $request->getRequestUri();
$getRequestUriExplode = explode('/', $getRequestUri);
$serviceRequestName = last($getRequestUriExplode);
//channelManagerLogService
$logArray = ['update-room-availability', 'update-room-rate'];
$this->channelManagerLogId = null;
if (in_array($serviceRequestName, $logArray)) {
$insertDataLog = [
'property_id' => $this->param['hotel_id'],
'channel_manager_id' => $this->channelManagerId,
'type' => 'C2E',
'service' => $serviceRequestName,
'request' => json_encode($payloadJson),
'response' => null,
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
//channelManagerLogService
}
public function checkAuthentication($username, $password)
{
$response = ['status' => false, 'message' => ''];
if ($this->username != $username || $this->password != $password) {
$response['message'] = 'Your username or password is incorrect.';
} else {
$response['status'] = true;
}
return $response;
}
public function responseError($errorMessage)
{
$response = [
'status' => false,
'message' => $errorMessage,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($responseData = null)
{
$response = [
'status' => true,
'message' => null,
'data' => $responseData,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
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');
}
$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' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['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'];
$response = [];
$roomRates = [];
foreach ($channelPropertyRoomRate as $roomRate) {
if($roomRate['property_room_rate_mapping']['property_room_rate']['name'] == 'Best Available Rate') {
continue;
}
if($roomRate['status'] == 0) {
continue;
}
if(empty($roomRate['property_room_rate_mapping'])) {
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'],
];
}
$roomKey = 0;
$response['rooms'] = [];
foreach ($roomRates as $roomId => $roomRate) {
$response['rooms'][$roomKey] = [
'id' => $roomId,
'room_name' => $roomRate['room']['name'],
];
$roomRateKey = 0;
$response['rooms'][$roomKey]['rates'] = [];
foreach ($roomRate['rate'] as $roomRateMappingId => $rateData) {
$response['rooms'][$roomKey]['rates'][$roomRateKey] = [
'id' => $roomRateMappingId,
'rate_name' => $rateData['name'] . ' - ' . $rateData['rate'],
'board_id' => $rateData['accommodationId'],
'board_name' => $rateData['name'],
];
$roomRateKey++;
}
$roomKey++;
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
public function channel(Request $request)
{
$response = ['status' => false, 'message' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
try {
$propertyId = $this->param['hotel_id'];
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($propertyId);
if (!$channelManagerPropertyCheck['status']) {
throw new ApiErrorException($channelManagerPropertyCheck['message']);
}
$propertyChannelCriteria = [
'criteria' => [
['field' => 'status', 'condition' => '=', 'value' => 1],
],
'with' => ['parentChannel','propertyChannelCategory'],
'orderBy' => [
['field' => 'id', 'value' => 'ASC']
],
];
$propertyChannel = $this->propertyChannelService->select($propertyChannelCriteria);
if (!$propertyChannel['status']) {
throw new ApiErrorException($propertyChannel['message']);
}
$propertyChannel = $propertyChannel['data'];
$response = [];
$response['channels'] = [];
foreach ($propertyChannel as $channel) {
$response['channels'][] = [
'id' => $channel['id'],
'name' => $channel['name'],
'category_id' => $channel['property_channel_category']['id'],
'category_name' => $channel['property_channel_category']['name'],
];
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
}

View File

@@ -0,0 +1,228 @@
<?php
namespace App\Http\Controllers\ChannelManager\Channex\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Core\Mail\ChannelManagerNotificationMail;
use App\Core\Service\ChannelManager\Channex;
use App;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
class ChannexController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
Channex $channexService,
Mailer $mailer
)
{
$this->username = 'channex';
$this->password = 'AU3EUmA9LChTzAzv';
$this->request = $request;
$this->propertyChannelService = $propertyChannelService;
$this->propertyChannelMappingService = $propertyChannelMappingService;
$this->channelManagerPropertyMappingService = $channelManagerPropertyMappingService;
$this->mailer = $mailer;
$this->channexService = $channexService;
$payloadJson = $this->request->all();
$this->param = $payloadJson;
Log::debug($payloadJson);
$this->channelManagerId = 2; //Channex
}
public function checkAuthentication($username, $password)
{
$response = ['status' => false, 'message' => ''];
if ($this->username != $username || $this->password != $password) {
$response['message'] = 'Your username or password is incorrect.';
} else {
$response['status'] = true;
}
return $response;
}
public function responseError($errorMessage)
{
$response = [
'status' => false,
'message' => $errorMessage,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($responseData = null)
{
$response = [
'status' => true,
'message' => null,
'data' => $responseData,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function channelManagerProperty($propertyId)
{
$response = ['status' => false, 'message' => ''];
try {
$requestParam =
[
'criteria' =>
[
['field' => 'status', 'condition' => '=', 'value' => 1],
['field' => 'channel_manager_property_id', 'condition' => '=', 'value' => $propertyId],
['field' => 'channel_manager_id', 'condition' => '=', 'value' => $this->channelManagerId],
],
'with' => ['property'],
'firstRow' => true
];
$channelManagerPropertyMapping = $this->channelManagerPropertyMappingService->select($requestParam);
if ($channelManagerPropertyMapping['status'] != 'success' || empty($channelManagerPropertyMapping['data'])) {
throw new ApiErrorException('Property mapping not found');
}
$response = [
'status' => true,
'data' => $channelManagerPropertyMapping['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 notification(Request $request)
{
$response = ['status' => false, 'message' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
try {
$propertyId = $this->param['property_id'];
$propertyChannelMapping = $this->channelManagerProperty($propertyId);
if (!$propertyChannelMapping['status']) {
throw new ApiErrorException($propertyChannelMapping['message']);
}
$propertyChannelMapping = $propertyChannelMapping['data'];
$channelDetails = $this->channexService->getChannelDetails($this->param['payload']['channel_id']);
if (!$channelDetails['status']) {
throw new ApiErrorException($channelDetails['message']);
}
$channelDetails = $channelDetails['data'];
$mailParams = [
'propertyId' => $propertyChannelMapping['property']['id'],
'propertyName' => $propertyChannelMapping['property']['name'],
'channelName' => $channelDetails['attributes']['channel']
];
$this->mailer->onQueue('channelManagerNotificationMail', new ChannelManagerNotificationMail($mailParams));
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
}

View File

@@ -0,0 +1,893 @@
<?php
namespace App\Http\Controllers\ChannelManager\ElektraWeb\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
class ElektraWebController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
private $channelManagerRequestTime;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
//Note: channel_manager_property_mapping tablosunda channel_manager_property_id alanı NULL ise, bu ENW nin CHANNEL tarafından yönetiliyor olması demek.
//Eğer channel_manager_property_id alanında bir otel id var ise, bu CHANNEL ın ENW tarafından güncelleniyor olması demek.
$this->username = 'elektraweb';
$this->password = 'XGgK2BSYCERDaVAx';
$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();
$payloadJson = json_decode($payload, 1);
$this->param = $payloadJson;
$this->channelId = 1;
$this->channelManagerId = 4; //ElektraWeb
$getRequestUri = $request->getRequestUri();
$getRequestUriExplode = explode('/', $getRequestUri);
$serviceRequestName = last($getRequestUriExplode);
//channelManagerLogService
$logArray = ['update-room-availability', 'update-room-rate'];
$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' => json_encode($payloadJson),
'ip_address' => $this->request->ip(),
'response' => null,
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
//channelManagerLogService
}
public function checkAuthentication($username, $password)
{
$response = ['status' => false, 'message' => ''];
if ($this->username != $username || $this->password != $password) {
$response['message'] = 'Your username or password is incorrect.';
} else {
$response['status'] = true;
}
return $response;
}
public function responseError($errorMessage)
{
$response = [
'status' => false,
'message' => $errorMessage,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($responseData = null)
{
$response = [
'status' => true,
'message' => null,
'data' => $responseData,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
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);
//TODO: burada otelinde kendisine bakmak lazım status
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' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['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'];
$response = [];
$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',
'max_adult' => $roomRate['property_room_rate_mapping']['property_room']['max_adult'],
'max_child' => $roomRate['property_room_rate_mapping']['property_room']['max_child'],
'max_occupancy' => $roomRate['property_room_rate_mapping']['property_room']['max_occupancy'],
];
$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'],
];
}
$roomKey = 0;
$response['rooms'] = [];
foreach ($roomRates as $roomId => $roomRate) {
$response['rooms'][$roomKey] = [
'id' => $roomId,
'room_name' => $roomRate['room']['name'],
'max_adult' => $roomRate['room']['max_adult'],
'max_child' => $roomRate['room']['max_child'],
'max_occupancy' => $roomRate['room']['max_occupancy'],
];
$roomRateKey = 0;
$response['rooms'][$roomKey]['rates'] = [];
foreach ($roomRate['rate'] as $roomRateMappingId => $rateData) {
$response['rooms'][$roomKey]['rates'][$roomRateKey] = [
'id' => $roomRateMappingId,
'rate_name' => $rateData['name'] . ' - ' . $rateData['rate'],
'board_id' => $rateData['accommodationId'],
'board_name' => $rateData['name'],
'included_occupancy' => floor($rateData['included_occupancy']),
];
$roomRateKey++;
}
$roomKey++;
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
public function updateRoomAvailability(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
$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'];
$roomAvailabilities = $this->param['rooms'];
// Merge consecutive dates with same availability and stop_sell
usort($roomAvailabilities, function ($a, $b) {
if ($a['room_id'] === $b['room_id']) {
return strcmp($a['start_date'], $b['start_date']);
}
return $a['room_id'] <=> $b['room_id'];
});
$mergedAvailabilities = [];
$currentAvailability = null;
foreach ($roomAvailabilities as $availability) {
if ($currentAvailability === null) {
$currentAvailability = $availability;
continue;
}
$currentEnd = Carbon::parse($currentAvailability['end_date']);
$nextStart = Carbon::parse($availability['start_date']);
$isConsecutive = $currentEnd->addDay()->toDateString() === $nextStart->toDateString();
$sameRoomId = ($currentAvailability['room_id'] === $availability['room_id']);
$sameAvailability = (isset($currentAvailability['availability']) && isset($availability['availability']) && $currentAvailability['availability'] === $availability['availability']) || (!isset($currentAvailability['availability']) && !isset($availability['availability']));
$sameStopSell = (isset($currentAvailability['stop_sell']) && isset($availability['stop_sell']) && $currentAvailability['stop_sell'] === $availability['stop_sell']) || (!isset($currentAvailability['stop_sell']) && !isset($availability['stop_sell']));
if ($isConsecutive && $sameRoomId && $sameAvailability && $sameStopSell) {
$currentAvailability['end_date'] = $availability['end_date'];
} else {
$mergedAvailabilities[] = $currentAvailability;
$currentAvailability = $availability;
}
}
if ($currentAvailability !== null) {
$mergedAvailabilities[] = $currentAvailability;
}
$roomAvailabilities = $mergedAvailabilities;
$roomAvailabilitiesCollect = collect($roomAvailabilities);
DB::beginTransaction();
$startDate = $roomAvailabilitiesCollect->sortBy('start_date')->first();
$startDate = $startDate['start_date'];
$endDate = $roomAvailabilitiesCollect->sortByDesc('end_date')->first();
$endDate = $endDate['end_date'];
if (Carbon::parse($startDate)->isBefore(Carbon::now()->toDateString()) || Carbon::parse($endDate)->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($roomAvailabilities as $availability) {
$roomId = $availability['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($availability['availability'])) {
$totalInventoryAvailable = $availability['availability'];
}
$startDate = Carbon::parse($availability['start_date'])->toDateString();
$endDate = Carbon::parse($availability['end_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' => $startDate,
'end_date' => $endDate,
'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']);
}
}
if (isset($availability['stop_sell']) && !is_null($availability['stop_sell'])) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'room_stop_sell';
$requestParams['value'] = $availability['stop_sell'];
$requestParams['room_rates'] = [
['room_id' => $roomId]
];
$propertyRoomRateMapping = $this->propertyRoomAvailabilityService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
DB::commit();
return $this->responseSuccess(['confirmCode' => $this->channelManagerLogId]);
} 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']) {
return $this->responseError($response['message']);
}
}
public function updateRoomRate(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
$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'];
$roomRates = $this->param['rooms'];
$roomRatesCollect = collect($roomRates);
$paramsListChannel = [];
$paramsListChannel[] = $this->channelId;
//CONNECTED CHANNEL RATE UPDATE
/*$propertyChannelMappingConnectedCriteria = [
'criteria' => [
['field' => 'property_id', 'condition' => '=', 'value' => $propertyId],
['field' => 'connected_channel_id', 'condition' => '=', 'value' => $this->channelId],
['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
DB::beginTransaction();
foreach ($roomRates as $roomRate) {
$roomId = $roomRate['room_id'];
$roomCheck = $channelPropertyRoomRateCollect->where('property_room_rate_mapping.room_id', $roomId)->isEmpty();
if ($roomCheck) {
//throw new ApiErrorException('Undefined or inactive room accommodation');
continue;
}
$rates = $roomRate['rates'];
if (empty($rates)) {
continue;
}
// Sort by rate_id and then start_date to ensure we process them in order for each rate_id
usort($rates, function ($a, $b) {
if ($a['rate_id'] === $b['rate_id']) {
return strcmp($a['start_date'], $b['start_date']);
}
return strcmp($a['rate_id'], $b['rate_id']);
});
$mergedRates = [];
$currentRate = null;
foreach ($rates as $rate) {
if ($currentRate === null) {
$currentRate = $rate;
continue;
}
$currentEnd = Carbon::parse($currentRate['end_date']);
$nextStart = Carbon::parse($rate['start_date']);
$isConsecutive = $currentEnd->addDay()->toDateString() === $nextStart->toDateString();
$sameRateId = ($currentRate['rate_id'] === $rate['rate_id']);
$sameAmount = (isset($currentRate['amount']) && isset($rate['amount']) && (float)$currentRate['amount'] === (float)$rate['amount']) || (!isset($currentRate['amount']) && !isset($rate['amount']));
$sameStopSell = (isset($currentRate['stop_sell']) && isset($rate['stop_sell']) && $currentRate['stop_sell'] === $rate['stop_sell']) || (!isset($currentRate['stop_sell']) && !isset($rate['stop_sell']));
$sameMinStay = (isset($currentRate['min_stay']) && isset($rate['min_stay']) && $currentRate['min_stay'] === $rate['min_stay']) || (!isset($currentRate['min_stay']) && !isset($rate['min_stay']));
if ($isConsecutive && $sameRateId && $sameAmount && $sameStopSell && $sameMinStay) {
$currentRate['end_date'] = $rate['end_date'];
} else {
$mergedRates[] = $currentRate;
$currentRate = $rate;
}
}
if ($currentRate !== null) {
$mergedRates[] = $currentRate;
}
$roomRatesCollect = collect($mergedRates);
$startDate = $roomRatesCollect->sortBy('start_date')->first();
$startDate = $startDate['start_date'];
$endDate = $roomRatesCollect->sortByDesc('end_date')->first();
$endDate = $endDate['end_date'];
if (Carbon::parse($startDate)->isBefore(Carbon::now()->toDateString()) || Carbon::parse($endDate)->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($mergedRates as $rate) {
$startDate = Carbon::parse($rate['start_date'])->toDateString();
$endDate = Carbon::parse($rate['end_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' => $startDate,
'end_date' => $endDate,
'include_days' => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
];
$roomRateMappingId = $rate['rate_id'];
$isCloseRoomRateMappingSale = null;
if (isset($rate['stop_sell'])) {
$isCloseRoomRateMappingSale = $rate['stop_sell'] == 1 ? true : false;
}
$channelRoomRateMappingCheck = $channelPropertyRoomRateCollect->where('room_rate_mapping_id', $roomRateMappingId)->isEmpty();
if ($channelRoomRateMappingCheck) {
//throw new ApiErrorException('Undefined or inactive room accommodation');
continue;
}
$roomRates = [];
$roomRates[] = [
'room_id' => $roomId,
'room_rate_mapping_id' => [
$roomRateMappingId
]
];
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($rate['amount'])) {
$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();
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'rate';
$requestParams['value'] = $rate['amount'];
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$requestParams['ip_address'] = $this->request->ip();
$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;
$requestParams['ip_address'] = $this->request->ip();
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
//Kısıtlamalar var ise min los
if (isset($rate['min_stay'])) {
//Minimum Konaklama Gün Sayısı
if (isset($rate['min_stay'])) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'min_stay';
$requestParams['value'] = $rate['min_stay'];
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$requestParams['ip_address'] = $this->request->ip();
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
}
}
}
DB::commit();
return $this->responseSuccess(['confirmCode' => $this->channelManagerLogId]);
} 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']) {
return $this->responseError($response['message']);
}
}
}

View File

@@ -0,0 +1,809 @@
<?php
namespace App\Http\Controllers\ChannelManager\ElektraWeb\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
class ElektraWebController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
private $channelManagerRequestTime;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
//Note: channel_manager_property_mapping tablosunda channel_manager_property_id alanı NULL ise, bu ENW nin CHANNEL tarafından yönetiliyor olması demek.
//Eğer channel_manager_property_id alanında bir otel id var ise, bu CHANNEL ın ENW tarafından güncelleniyor olması demek.
$this->username = 'elektraweb';
$this->password = 'XGgK2BSYCERDaVAx';
$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();
$payloadJson = json_decode($payload, 1);
$this->param = $payloadJson;
$this->channelId = 1;
$this->channelManagerId = 4; //ElektraWeb
$getRequestUri = $request->getRequestUri();
$getRequestUriExplode = explode('/', $getRequestUri);
$serviceRequestName = last($getRequestUriExplode);
//channelManagerLogService
$logArray = ['update-room-availability', 'update-room-rate'];
$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' => json_encode($payloadJson),
'ip_address' => $this->request->ip(),
'response' => null,
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
//channelManagerLogService
}
public function checkAuthentication($username, $password)
{
$response = ['status' => false, 'message' => ''];
if ($this->username != $username || $this->password != $password) {
$response['message'] = 'Your username or password is incorrect.';
} else {
$response['status'] = true;
}
return $response;
}
public function responseError($errorMessage)
{
$response = [
'status' => false,
'message' => $errorMessage,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($responseData = null)
{
$response = [
'status' => true,
'message' => null,
'data' => $responseData,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
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);
//TODO: burada otelinde kendisine bakmak lazım status
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' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['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'];
$response = [];
$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',
'max_adult' => $roomRate['property_room_rate_mapping']['property_room']['max_adult'],
'max_child' => $roomRate['property_room_rate_mapping']['property_room']['max_child'],
'max_occupancy' => $roomRate['property_room_rate_mapping']['property_room']['max_occupancy'],
];
$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'],
];
}
$roomKey = 0;
$response['rooms'] = [];
foreach ($roomRates as $roomId => $roomRate) {
$response['rooms'][$roomKey] = [
'id' => $roomId,
'room_name' => $roomRate['room']['name'],
'max_adult' => $roomRate['room']['max_adult'],
'max_child' => $roomRate['room']['max_child'],
'max_occupancy' => $roomRate['room']['max_occupancy'],
];
$roomRateKey = 0;
$response['rooms'][$roomKey]['rates'] = [];
foreach ($roomRate['rate'] as $roomRateMappingId => $rateData) {
$response['rooms'][$roomKey]['rates'][$roomRateKey] = [
'id' => $roomRateMappingId,
'rate_name' => $rateData['name'] . ' - ' . $rateData['rate'],
'board_id' => $rateData['accommodationId'],
'board_name' => $rateData['name'],
'included_occupancy' => floor($rateData['included_occupancy']),
];
$roomRateKey++;
}
$roomKey++;
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
public function updateRoomAvailability(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
$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'];
$roomAvailabilities = $this->param['rooms'];
$roomAvailabilitiesCollect = collect($roomAvailabilities);
DB::beginTransaction();
$startDate = $roomAvailabilitiesCollect->sortBy('start_date')->first();
$startDate = $startDate['start_date'];
$endDate = $roomAvailabilitiesCollect->sortByDesc('end_date')->first();
$endDate = $endDate['end_date'];
if (Carbon::parse($startDate)->isBefore(Carbon::now()->toDateString()) || Carbon::parse($endDate)->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($roomAvailabilities as $availability) {
$roomId = $availability['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($availability['availability'])) {
$totalInventoryAvailable = $availability['availability'];
}
$startDate = Carbon::parse($availability['start_date'])->toDateString();
$endDate = Carbon::parse($availability['end_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' => $startDate,
'end_date' => $endDate,
'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']);
}
}
if (isset($availability['stop_sell']) && !is_null($availability['stop_sell'])) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'room_stop_sell';
$requestParams['value'] = $availability['stop_sell'];
$requestParams['room_rates'] = [
['room_id' => $roomId]
];
$propertyRoomRateMapping = $this->propertyRoomAvailabilityService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
DB::commit();
return $this->responseSuccess(['confirmCode' => $this->channelManagerLogId]);
} 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']) {
return $this->responseError($response['message']);
}
}
public function updateRoomRate(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
$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'];
$roomRates = $this->param['rooms'];
$roomRatesCollect = collect($roomRates);
$paramsListChannel = [];
$paramsListChannel[] = $this->channelId;
//CONNECTED CHANNEL RATE UPDATE
/*$propertyChannelMappingConnectedCriteria = [
'criteria' => [
['field' => 'property_id', 'condition' => '=', 'value' => $propertyId],
['field' => 'connected_channel_id', 'condition' => '=', 'value' => $this->channelId],
['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
DB::beginTransaction();
foreach ($roomRates as $roomRate) {
$roomId = $roomRate['room_id'];
$roomCheck = $channelPropertyRoomRateCollect->where('property_room_rate_mapping.room_id', $roomId)->isEmpty();
if ($roomCheck) {
//throw new ApiErrorException('Undefined or inactive room accommodation');
continue;
}
$roomRatesCollect = collect($roomRate['rates']);
$startDate = $roomRatesCollect->sortBy('start_date')->first();
$startDate = $startDate['start_date'];
$endDate = $roomRatesCollect->sortByDesc('end_date')->first();
$endDate = $endDate['end_date'];
if (Carbon::parse($startDate)->isBefore(Carbon::now()->toDateString()) || Carbon::parse($endDate)->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($roomRate['rates'] as $rate) {
$startDate = Carbon::parse($rate['start_date'])->toDateString();
$endDate = Carbon::parse($rate['end_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' => $startDate,
'end_date' => $endDate,
'include_days' => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
];
$roomRateMappingId = $rate['rate_id'];
$isCloseRoomRateMappingSale = null;
if (isset($rate['stop_sell'])) {
$isCloseRoomRateMappingSale = $rate['stop_sell'] == 1 ? true : false;
}
$channelRoomRateMappingCheck = $channelPropertyRoomRateCollect->where('room_rate_mapping_id', $roomRateMappingId)->isEmpty();
if ($channelRoomRateMappingCheck) {
//throw new ApiErrorException('Undefined or inactive room accommodation');
continue;
}
$roomRates = [];
$roomRates[] = [
'room_id' => $roomId,
'room_rate_mapping_id' => [
$roomRateMappingId
]
];
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($rate['amount'])) {
$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();
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'rate';
$requestParams['value'] = $rate['amount'];
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$requestParams['ip_address'] = $this->request->ip();
$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;
$requestParams['ip_address'] = $this->request->ip();
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
//Kısıtlamalar var ise min los
if (isset($rate['min_stay'])) {
//Minimum Konaklama Gün Sayısı
if (isset($rate['min_stay'])) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'min_stay';
$requestParams['value'] = $rate['min_stay'];
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$requestParams['ip_address'] = $this->request->ip();
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
}
}
}
DB::commit();
return $this->responseSuccess(['confirmCode' => $this->channelManagerLogId]);
} 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']) {
return $this->responseError($response['message']);
}
}
}

View File

@@ -0,0 +1,488 @@
<?php
namespace App\Http\Controllers\ChannelManager\Fina\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
class FinaController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
//Note: channel_manager_property_mapping tablosunda channel_manager_property_id alanı NULL ise, bu ENW nin CHANNEL tarafından yönetiliyor olması demek.
//Eğer channel_manager_property_id alanında bir otel id var ise, bu CHANNEL ın ENW tarafından güncelleniyor olması demek.
$this->username = 'fina';
$this->password = '6T3VpfsNvLwWFY2gtXjz8y';
$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();
$payloadJson = json_decode($payload, 1);
$this->param = $payloadJson;
$this->channelId = 1;
$this->channelManagerId = 8; //Fina
$getRequestUri = $request->getRequestUri();
$getRequestUriExplode = explode('/', $getRequestUri);
$serviceRequestName = last($getRequestUriExplode);
//channelManagerLogService
$logArray = ['update-room-availability', 'update-room-rate'];
$this->channelManagerLogId = null;
if (in_array($serviceRequestName, $logArray)) {
$insertDataLog = [
'property_id' => $this->param['hotel_id'],
'channel_manager_id' => $this->channelManagerId,
'type' => 'C2E',
'service' => $serviceRequestName,
'request' => json_encode($payloadJson),
'response' => null,
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
//channelManagerLogService
}
public function checkAuthentication($username, $password)
{
$response = ['status' => false, 'message' => ''];
if ($this->username != $username || $this->password != $password) {
$response['message'] = 'Your username or password is incorrect.';
} else {
$response['status'] = true;
}
return $response;
}
public function responseError($errorMessage)
{
$response = [
'status' => false,
'message' => $errorMessage,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($responseData = null)
{
$response = [
'status' => true,
'message' => null,
'data' => $responseData,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
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');
}
$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' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['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'];
$response = [];
$roomRates = [];
foreach ($channelPropertyRoomRate as $roomRate) {
if($roomRate['property_room_rate_mapping']['property_room_rate']['name'] == 'Best Available Rate') {
continue;
}
if($roomRate['status'] == 0) {
continue;
}
if(empty($roomRate['property_room_rate_mapping'])) {
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'],
];
}
$roomKey = 0;
$response['rooms'] = [];
foreach ($roomRates as $roomId => $roomRate) {
$response['rooms'][$roomKey] = [
'id' => $roomId,
'room_name' => $roomRate['room']['name'],
];
$roomRateKey = 0;
$response['rooms'][$roomKey]['rates'] = [];
foreach ($roomRate['rate'] as $roomRateMappingId => $rateData) {
$response['rooms'][$roomKey]['rates'][$roomRateKey] = [
'id' => $roomRateMappingId,
'rate_name' => $rateData['name'] . ' - ' . $rateData['rate'],
'board_id' => $rateData['accommodationId'],
'board_name' => $rateData['name'],
];
$roomRateKey++;
}
$roomKey++;
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
public function channel(Request $request)
{
$response = ['status' => false, 'message' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
try {
$propertyId = $this->param['hotel_id'];
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($propertyId);
if (!$channelManagerPropertyCheck['status']) {
throw new ApiErrorException($channelManagerPropertyCheck['message']);
}
$propertyChannelCriteria = [
'criteria' => [
['field' => 'status', 'condition' => '=', 'value' => 1],
],
'with' => ['parentChannel','propertyChannelCategory'],
'orderBy' => [
['field' => 'id', 'value' => 'ASC']
],
];
$propertyChannel = $this->propertyChannelService->select($propertyChannelCriteria);
if (!$propertyChannel['status']) {
throw new ApiErrorException($propertyChannel['message']);
}
$propertyChannel = $propertyChannel['data'];
$response = [];
$response['channels'] = [];
foreach ($propertyChannel as $channel) {
$response['channels'][] = [
'id' => $channel['id'],
'name' => $channel['name'],
'category_id' => $channel['property_channel_category']['id'],
'category_name' => $channel['property_channel_category']['name'],
];
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
}

View File

@@ -0,0 +1,825 @@
<?php
namespace App\Http\Controllers\ChannelManager\HotelRunner\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
class HotelRunnerController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
//Note: channel_manager_property_mapping tablosunda channel_manager_property_id alanı NULL ise, bu ENW nin CHANNEL tarafından yönetiliyor olması demek.
//Eğer channel_manager_property_id alanında bir otel id var ise, bu CHANNEL ın ENW tarafından güncelleniyor olması demek.
$this->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('<?xml version="1.0" encoding="UTF-8"?><response></response>');
$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('<?xml version="1.0" encoding="UTF-8"?><rooms></rooms>');
$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('<?xml version="1.0" encoding="UTF-8"?><inventories></inventories>');
$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('<?xml version="1.0" encoding="UTF-8"?><response></response>');
$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);
}
}
}

View File

@@ -0,0 +1,635 @@
<?php
namespace App\Http\Controllers\ChannelManager\HyperGuest\v1;
use App\Core\Mail\LogMail;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomService;
use App\Exceptions\ApiErrorException;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Exception;
class HyperGuestController
{
private $channelManagerId;
private $channelManagerName;
private $mailer;
private $userApiKey;
public function __construct(
Request $request,
Client $restClient,
Mailer $mailer,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
$this->request = $request;
$this->restClient = $restClient;
$this->mailer = $mailer;
$this->param = $this->request->all();
$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;
if (App::environment() == 'production') {
$this->url = 'https://pdm.hyperguest.io/api/';
$this->urlstatic = 'https://hg-static.hyperguest.com/';
$this->bearerToken = '63d51938dc3749788ac3e88ea6d58950';
} else {
$this->url = 'https://pdm.hyperguest.com/api/';
$this->urlstatic = 'https://hg-static.hyperguest.com/';
$this->bearerToken = '63d51938dc3749788ac3e88ea6d58950';
}
$this->channelId = 1;
$this->channelManagerId = 10;
$this->channelManagerName = 'HyperGuest';
$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)) {
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($this->param['propertyId']);
if ($channelManagerPropertyCheck['status']) {
$insertDataLog = [
'property_id' => $channelManagerPropertyCheck['data']['property_id'],
'channel_manager_id' => $this->channelManagerId,
'type' => 'C2E',
'service' => $serviceRequestName,
'request' => json_encode($this->param),
'response' => null,
'ip_address' => $this->request->ip(),
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
}
//channelManagerLogService
}
public function request($type, $method, $jsonPayload)
{
$response = ['status' => false, 'message' => ''];
try {
$this->restClient = new Client(['http_errors' => false]);
if ($type == 'POST') {
$parameter = [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->bearerToken
]
];
if (!empty($jsonPayload)) {
$parameter['body'] = $jsonPayload;
}
$res = $this->restClient->request('POST', $this->url . $method, $parameter);
$getResponseBody = $res->getBody();
$getResponse = $getResponseBody->getContents();
$getResponse = json_decode($getResponse, 1);
} else if ($type == 'GET') {
$res = $this->restClient->request('GET', $this->url . $method,
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->bearerToken
],
'query' => $jsonPayload
]
);
$getResponseBody = $res->getBody();
$getResponse = $getResponseBody->getContents();
$getResponse = json_decode($getResponse, 1);
}
if ($res->getStatusCode() != 200) {
$errors = singleElementArray($getResponse['errors']);
$firstError = reset($errors);
throw new Exception($firstError['code'] . ': ' . $firstError['title']);
}
$response = ["status" => true, 'message' => '', "data" => fillOnUndefined($getResponse, 'data', [])];
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return $response;
}
public function responseError($errors)
{
$response = ['success' => false, 'message' => null];
$response['message'] = implode(',', $errors);
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($data = [])
{
$response = ['success' => true, 'message' => null];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
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');
}
$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' => 'channel_manager_property_id', 'condition' => '=', 'value' => $propertyId],
['field' => 'channel_manager_id', 'condition' => '=', 'value' => $this->channelManagerId],
],
'with' => ['channelManagerRoomRate.propertyRoomRateMapping'],
'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'];
$response = [
'status' => true,
'data' => $channelManagerPropertyMapping
];
} 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 availabilityRateUpdate(Request $request)
{
$errors = [];
$response = ['status' => false, 'message' => ''];
try {
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($this->param['propertyId']);
if (!$channelManagerPropertyCheck['status']) {
throw new ApiErrorException($channelManagerPropertyCheck['message']);
}
$channelManagerPropertyCheck = $channelManagerPropertyCheck['data'];
$channelManagerPropertyRoomRateCollect = collect($channelManagerPropertyCheck['channel_manager_room_rate']);
$propertyId = $channelManagerPropertyCheck['property_id'];
$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 = $this->param['ARIUpdate'];
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) {
$roomIdCheck = $channelManagerPropertyRoomRateCollect->where('channel_manager_room_id', $availRateUpdate['roomTypeCode'])->first();
if (empty($roomIdCheck)) {
continue;
}
$roomId = $roomIdCheck['property_room_rate_mapping']['room_id'];
$roomCheck = $channelPropertyRoomRateCollect->where('property_room_rate_mapping.room_id', $roomId)->isEmpty();
if ($roomCheck) {
continue;
}
$totalInventoryAvailable = null;
if (isset($availRateUpdate['numberOfAvailableRooms'])) {
$totalInventoryAvailable = $availRateUpdate['numberOfAvailableRooms'];
}
$currentDate = Carbon::parse($availRateUpdate['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']);
}
}
foreach ($availRateUpdate['ratePlans'] as $ratePlan) {
$roomRateMappingIdCheck = $channelManagerPropertyRoomRateCollect->where('channel_manager_room_id', $availRateUpdate['roomTypeCode'])->where('channel_manager_room_rate_id', $ratePlan['ratePlanCode'])->first();
if (empty($roomRateMappingIdCheck)) {
continue;
}
$roomRateMappingId = $roomRateMappingIdCheck['property_room_rate_mapping_id'];
$channelRoomRateMappingCheck = $channelPropertyRoomRateCollect->where('room_rate_mapping_id', $roomRateMappingId)->isEmpty();
if ($channelRoomRateMappingCheck) {
continue;
}
$includedOccupancy = (int)$roomRateMappingIdCheck['included_occupancy'];
$ratePlanPrice = collect($ratePlan['prices'])->where('numberOfGuests.adults', $includedOccupancy)
->where('numberOfGuests.adults', $includedOccupancy)
->where('numberOfGuests.children', 0)
->where('numberOfGuests.infants', 0)
->first();
if (empty($ratePlanPrice)) {
continue;
}
$ratePlanPrice = $ratePlanPrice['price'];
$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($ratePlanPrice)) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'rate';
$requestParams['value'] = $ratePlanPrice;
$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
$isCloseRoomRateMappingSale = null;
if (isset($ratePlan['isOpen'])) {
$isCloseRoomRateMappingSale = $ratePlan['isOpen'] == 1 ? false : true;
}
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']);
}
}
//Minimum Konaklama Gün Sayısı
if (isset($ratePlan['minLOS'])) {
$minStay = $ratePlan['minLOS'];
$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();
//$this->channelManagerLogId
return $this->responseSuccess();
} 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[] = $response['message'];
return $this->responseError($errors);
}
}
}

View File

@@ -0,0 +1,640 @@
<?php
namespace App\Http\Controllers\ChannelManager\OneC\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
use Illuminate\Support\Facades\Validator;
class OneCController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
//Note: channel_manager_property_mapping tablosunda channel_manager_property_id alanı NULL ise, bu ENW nin CHANNEL tarafından yönetiliyor olması demek.
//Eğer channel_manager_property_id alanında bir otel id var ise, bu CHANNEL ın ENW tarafından güncelleniyor olması demek.
$this->username = '1CHotel';
$this->password = 'w2Dffb9EGXPZfiUDxWKZqB';
$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();
$payloadJson = json_decode($payload, 1);
$this->param = $payloadJson;
$this->channelId = 1;
$this->channelManagerId = 9; //1C Hotels
$getRequestUri = $request->getRequestUri();
$getRequestUriExplode = explode('/', $getRequestUri);
$serviceRequestName = last($getRequestUriExplode);
//channelManagerLogService
$logArray = ['update-room-availability', 'update-room-rate'];
$this->channelManagerLogId = null;
if (in_array($serviceRequestName, $logArray)) {
$insertDataLog = [
'property_id' => $this->param['hotel_id'],
'channel_manager_id' => $this->channelManagerId,
'type' => 'C2E',
'service' => $serviceRequestName,
'request' => json_encode($payloadJson),
'response' => null,
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
//channelManagerLogService
}
public function checkAuthentication($username, $password)
{
$response = ['status' => false, 'message' => ''];
if ($this->username != $username || $this->password != $password) {
$response['message'] = 'Your username or password is incorrect.';
} else {
$response['status'] = true;
}
return $response;
}
public function responseError($errorMessage)
{
$response = [
'status' => false,
'message' => $errorMessage,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($responseData = null)
{
$response = [
'status' => true,
'message' => null,
'data' => $responseData,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
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');
}
$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' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['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'];
$response = [];
$roomRates = [];
foreach ($channelPropertyRoomRate as $roomRate) {
if($roomRate['property_room_rate_mapping']['property_room_rate']['name'] == 'Best Available Rate') {
continue;
}
if($roomRate['status'] == 0) {
continue;
}
if(empty($roomRate['property_room_rate_mapping'])) {
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'],
];
}
$roomKey = 0;
$response['rooms'] = [];
foreach ($roomRates as $roomId => $roomRate) {
$response['rooms'][$roomKey] = [
'id' => $roomId,
'room_name' => $roomRate['room']['name'],
];
$roomRateKey = 0;
$response['rooms'][$roomKey]['rates'] = [];
foreach ($roomRate['rate'] as $roomRateMappingId => $rateData) {
$response['rooms'][$roomKey]['rates'][$roomRateKey] = [
'id' => $roomRateMappingId,
'rate_name' => $rateData['name'] . ' - ' . $rateData['rate'],
'board_id' => $rateData['accommodationId'],
'board_name' => $rateData['name'],
];
$roomRateKey++;
}
$roomKey++;
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
public function channel(Request $request)
{
$response = ['status' => false, 'message' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
try {
$propertyId = $this->param['hotel_id'];
$channelManagerPropertyCheck = $this->channelManagerPropertyCheck($propertyId);
if (!$channelManagerPropertyCheck['status']) {
throw new ApiErrorException($channelManagerPropertyCheck['message']);
}
$propertyChannelCriteria = [
'criteria' => [
['field' => 'status', 'condition' => '=', 'value' => 1],
],
'with' => ['parentChannel','propertyChannelCategory'],
'orderBy' => [
['field' => 'id', 'value' => 'ASC']
],
];
$propertyChannel = $this->propertyChannelService->select($propertyChannelCriteria);
if (!$propertyChannel['status']) {
throw new ApiErrorException($propertyChannel['message']);
}
$propertyChannel = $propertyChannel['data'];
$response = [];
$response['channels'] = [];
foreach ($propertyChannel as $channel) {
$response['channels'][] = [
'id' => $channel['id'],
'name' => $channel['name'],
'category_id' => $channel['property_channel_category']['id'],
'category_name' => $channel['property_channel_category']['name'],
];
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
public function updateRoomAvailability(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
$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'];
$roomAvailabilities = $this->param['rooms'];
$roomAvailabilitiesCollect = collect($roomAvailabilities);
DB::beginTransaction();
$startDate = $roomAvailabilitiesCollect->sortBy('start_date')->first();
$startDate = $startDate['start_date'];
$endDate = $roomAvailabilitiesCollect->sortByDesc('end_date')->first();
$endDate = $endDate['end_date'];
if (Carbon::parse($startDate)->isBefore(Carbon::now()->toDateString()) || Carbon::parse($endDate)->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($roomAvailabilities as $availability) {
$roomId = $availability['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($availability['availability'])) {
$totalInventoryAvailable = $availability['availability'];
}
$startDate = Carbon::parse($availability['start_date'])->toDateString();
$endDate = Carbon::parse($availability['end_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' => $startDate,
'end_date' => $endDate,
'include_days' => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
];
//Days Filter
if (isset($availability['days']) && !empty($availability['days']) && is_array($availability['days']) && count($availability['days']) <= 7) {
$requestParams = [
'days' => $availability['days']
];
$validator = Validator::make($requestParams, ['days' => 'required|array|in:Mon,Tue,Wed,Thu,Fri,Sat,Sun']);//Mon,Tue,Wed,Thu,Fri,Sat,Sun
if (empty($validator->errors()->messages())) {
$requestParamBase['include_days'] = $availability['days'];
}
}
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']);
}
}
if (isset($availability['stop_sell']) && !is_null($availability['stop_sell'])) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'room_stop_sell';
$requestParams['value'] = $availability['stop_sell'];
$requestParams['room_rates'] = [
['room_id' => $roomId]
];
$propertyRoomRateMapping = $this->propertyRoomAvailabilityService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
DB::commit();
return $this->responseSuccess(['confirmCode' => $this->channelManagerLogId]);
} 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']) {
return $this->responseError($response['message']);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,827 @@
<?php
namespace App\Http\Controllers\ChannelManager\SistemOtel\v1;
use App\Core\Service\PropertyChannelService;
use App\Core\Service\PropertyChannelMappingService;
use App\Core\Service\PropertyRoomRateChannelMappingService;
use App\Core\Service\PropertyRoomRatePriceService;
use App\Core\Service\PropertyRoomAvailabilityService;
use App\Core\Service\PropertyRoomService;
use App\Core\Service\BookingService;
use App\Core\Service\ChannelManagerLogService;
use App\Core\Service\ChannelManagerPropertyMappingService;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
use Illuminate\Support\Facades\Validator;
class SistemOtelController extends Controller
{
private $username;
private $password;
private $request;
private $propertyChannelService;
private $propertyChannelMappingService;
private $propertyRoomRatePriceService;
private $param;
private $channelId;
private $channelManagerLogId;
private $channelManagerRequestTime;
public function __construct(
Request $request,
PropertyChannelService $propertyChannelService,
PropertyChannelMappingService $propertyChannelMappingService,
PropertyRoomRateChannelMappingService $propertyRoomRateChannelMappingService,
PropertyRoomRatePriceService $propertyRoomRatePriceService,
PropertyRoomAvailabilityService $propertyRoomAvailabilityService,
PropertyRoomService $propertyRoomService,
BookingService $bookingService,
ChannelManagerPropertyMappingService $channelManagerPropertyMappingService,
ChannelManagerLogService $channelManagerLogService
)
{
//Note: channel_manager_property_mapping tablosunda channel_manager_property_id alanı NULL ise, bu ENW nin CHANNEL tarafından yönetiliyor olması demek.
//Eğer channel_manager_property_id alanında bir otel id var ise, bu CHANNEL ın ENW tarafından güncelleniyor olması demek.
$this->username = 'sistemotel';
$this->password = 'ys6paYBmEWa5q7Mt';
$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();
$payloadJson = json_decode($payload, 1);
$this->param = $payloadJson;
$this->channelId = 1;
$this->channelManagerId = 6; //SistemOtel
$getRequestUri = $request->getRequestUri();
$getRequestUriExplode = explode('/', $getRequestUri);
$serviceRequestName = last($getRequestUriExplode);
//channelManagerLogService
$logArray = ['update-room-availability', 'update-room-rate'];
$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' => json_encode($payloadJson),
'response' => null,
'ip_address' => $this->request->ip(),
'status' => null
];
$channelManagerLog = $this->channelManagerLogService->create($insertDataLog);
if ($channelManagerLog['status'] == 'success') {
$this->channelManagerLogId = $channelManagerLog['data']['id'];
}
}
//channelManagerLogService
}
public function checkAuthentication($username, $password)
{
$response = ['status' => false, 'message' => ''];
if ($this->username != $username || $this->password != $password) {
$response['message'] = 'Your username or password is incorrect.';
} else {
$response['status'] = true;
}
return $response;
}
public function responseError($errorMessage)
{
$response = [
'status' => false,
'message' => $errorMessage,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 0
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
public function responseSuccess($responseData = null)
{
$response = [
'status' => true,
'message' => null,
'data' => $responseData,
];
//channelManagerLogService
if (!is_null($this->channelManagerLogId)) {
$updateDataLog = [
'response' => json_encode($response),
'status' => 1
];
if (!is_null($this->channelManagerRequestTime)) {
$updateDataLog['response_time'] = microtime(true) - $this->channelManagerRequestTime;
}
$channelManagerLog = $this->channelManagerLogService->update($this->channelManagerLogId, $updateDataLog);
}
//channelManagerLogService
return response()->json($response);
}
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' => ''];
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['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'];
$response = [];
$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'],
];
}
$roomKey = 0;
$response['rooms'] = [];
foreach ($roomRates as $roomId => $roomRate) {
$response['rooms'][$roomKey] = [
'id' => $roomId,
'room_name' => $roomRate['room']['name'],
];
$roomRateKey = 0;
$response['rooms'][$roomKey]['rates'] = [];
foreach ($roomRate['rate'] as $roomRateMappingId => $rateData) {
$response['rooms'][$roomKey]['rates'][$roomRateKey] = [
'id' => $roomRateMappingId,
'rate_name' => $rateData['name'] . ' - ' . $rateData['rate'],
'board_id' => $rateData['accommodationId'],
'board_name' => $rateData['name'],
];
$roomRateKey++;
}
$roomKey++;
}
return $this->responseSuccess($response);
} 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']) {
return $this->responseError($response['message']);
}
}
public function updateRoomAvailability(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
$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'];
$roomAvailabilities = $this->param['rooms'];
$roomAvailabilitiesCollect = collect($roomAvailabilities);
DB::beginTransaction();
$startDate = $roomAvailabilitiesCollect->sortBy('start_date')->first();
$startDate = $startDate['start_date'];
$endDate = $roomAvailabilitiesCollect->sortByDesc('end_date')->first();
$endDate = $endDate['end_date'];
if (Carbon::parse($startDate)->isBefore(Carbon::now()->toDateString()) || Carbon::parse($endDate)->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($roomAvailabilities as $availability) {
$roomId = $availability['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($availability['availability'])) {
$totalInventoryAvailable = $availability['availability'];
}
$startDate = Carbon::parse($availability['start_date'])->toDateString();
$endDate = Carbon::parse($availability['end_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' => $startDate,
'end_date' => $endDate,
'include_days' => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
];
//Days Filter
if (isset($availability['days']) && !empty($availability['days']) && is_array($availability['days']) && count($availability['days']) <= 7) {
$requestParams = [
'days' => $availability['days']
];
$validator = Validator::make($requestParams, ['days' => 'required|array|in:Mon,Tue,Wed,Thu,Fri,Sat,Sun']);//Mon,Tue,Wed,Thu,Fri,Sat,Sun
if (empty($validator->errors()->messages())) {
$requestParamBase['include_days'] = $availability['days'];
}
}
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']);
}
}
if (isset($availability['stop_sell']) && !is_null($availability['stop_sell'])) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'room_stop_sell';
$requestParams['value'] = $availability['stop_sell'];
$requestParams['room_rates'] = [
['room_id' => $roomId]
];
$propertyRoomRateMapping = $this->propertyRoomAvailabilityService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
DB::commit();
return $this->responseSuccess(['confirmCode' => $this->channelManagerLogId]);
} 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']) {
return $this->responseError($response['message']);
}
}
public function updateRoomRate(Request $request)
{
$response = ['status' => false, 'message' => ''];
try {
//checkAuthentication
$checkAuthentication = $this->checkAuthentication($this->param['username'], $this->param['password']);
if (!$checkAuthentication['status']) {
return $this->responseError($checkAuthentication['message']);
}
$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'];
$roomRates = $this->param['rooms'];
$roomRatesCollect = collect($roomRates);
DB::beginTransaction();
foreach ($roomRates as $roomRate) {
$roomId = $roomRate['room_id'];
$roomCheck = $channelPropertyRoomRateCollect->where('property_room_rate_mapping.room_id', $roomId)->isEmpty();
if ($roomCheck) {
throw new ApiErrorException('Undefined or inactive room accommodation');
}
$roomRatesCollect = collect($roomRate['rates']);
$startDate = $roomRatesCollect->sortBy('start_date')->first();
$startDate = $startDate['start_date'];
$endDate = $roomRatesCollect->sortByDesc('end_date')->first();
$endDate = $endDate['end_date'];
if (Carbon::parse($startDate)->isBefore(Carbon::now()->toDateString()) || Carbon::parse($endDate)->isBefore(Carbon::now()->toDateString())) {
throw new ApiErrorException('Dates to be updated cannot be earlier than today');
}
foreach ($roomRate['rates'] as $rate) {
$startDate = Carbon::parse($rate['start_date'])->toDateString();
$endDate = Carbon::parse($rate['end_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' => $startDate,
'end_date' => $endDate,
'include_days' => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
];
//Days Filter
if (isset($rate['days']) && !empty($rate['days']) && is_array($rate['days']) && count($rate['days']) <= 7) {
$requestParams = [
'days' => $rate['days']
];
$validator = Validator::make($requestParams, ['days' => 'required|array|in:Mon,Tue,Wed,Thu,Fri,Sat,Sun']);
if (empty($validator->errors()->messages())) {
$requestParamBase['include_days'] = $rate['days'];
}
}
$roomRateMappingId = $rate['rate_id'];
$isCloseRoomRateMappingSale = null;
if (isset($rate['stop_sell'])) {
$isCloseRoomRateMappingSale = $rate['stop_sell'] == 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($rate['amount'])) {
$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();
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'rate';
$requestParams['value'] = $rate['amount'];
$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($rate['min_stay'])) {
//Minimum Konaklama Gün Sayısı
if (isset($rate['min_stay'])) {
$requestParams = [];
$requestParams = $requestParamBase;
$requestParams['update_type'] = 'min_stay';
$requestParams['value'] = $rate['min_stay'];
$requestParams['room_rates'] = $roomRates;
$requestParams['channel_id'] = $paramChannelId;
$propertyRoomRateMapping = $this->propertyRoomRatePriceService->bulkUpdate($requestParams);
if ($propertyRoomRateMapping['status'] != 'success') {
throw new ApiErrorException($propertyRoomRateMapping['message']);
}
}
}
}
}
}
DB::commit();
return $this->responseSuccess(['confirmCode' => $this->channelManagerLogId]);
} 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']) {
return $this->responseError($response['message']);
}
}
}