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']); } } }