431 lines
18 KiB
PHP
431 lines
18 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Services\APIs\CustomerAPIs;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\CustomerFavouriteRestaurant;
|
|
use App\Models\IamPrincipal;
|
|
use App\Models\IamPrincipalRestaurantRole;
|
|
use App\Models\ManageRestaurant;
|
|
use App\Models\RedeemRestaurant;
|
|
use App\Helpers\onesignalhelper;
|
|
use App\Models\RestaurantTimeInterval;
|
|
use App\Models\TimeInterval;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
class RestaurantApiServices
|
|
{
|
|
public function getCoordinates($customerIamId)
|
|
{
|
|
try {
|
|
$restaurants = ManageRestaurant::with('operatingHours')->select(
|
|
'id',
|
|
'name',
|
|
'image',
|
|
'address',
|
|
'short_id',
|
|
'latitude',
|
|
'longtitude'
|
|
)
|
|
->where('is_active', '1')
|
|
->get()
|
|
->toArray();
|
|
|
|
foreach ($restaurants as &$restaurant) {
|
|
$restaurant['image'] = ListingImageUrl('restaurant_images', $restaurant['image']);
|
|
|
|
$isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $restaurant['id'])
|
|
->exists();
|
|
$restaurant['is_favourite'] = $isFavourite;
|
|
}
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $restaurants, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('Restaurant Get service failed : ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
public function addToFavourite($customerIamId, $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$restaurant = ManageRestaurant::where('short_id', $request->id)->where('is_active', '1')->first();
|
|
if (!$restaurant) {
|
|
return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404);
|
|
}
|
|
|
|
$existingFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $restaurant->id)
|
|
->first();
|
|
|
|
if ($existingFavourite) {
|
|
return jsonResponseWithErrorMessage(__('auth.restaurant_already_favourite'), 409);
|
|
}
|
|
|
|
$favRestaurant = new CustomerFavouriteRestaurant();
|
|
$favRestaurant->principal_xid = $customerIamId;
|
|
$favRestaurant->restaurant_xid = $restaurant->id;
|
|
$favRestaurant->save();
|
|
|
|
DB::commit();
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), 200);
|
|
} catch (\Exception $ex) {
|
|
DB::rollBack();
|
|
Log::error('Favourite Restaurant service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function listFavRestaurant($customerIamId)
|
|
{
|
|
try {
|
|
$list = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)->get()->toArray();
|
|
$customerFavouriteRestaurants = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->pluck('restaurant_xid')
|
|
->toArray();
|
|
|
|
$restaurants = ManageRestaurant::with('operatingHours')->where('is_active', '1')->whereIn('id', $customerFavouriteRestaurants)->get();
|
|
|
|
foreach ($restaurants as &$res) {
|
|
$res['image'] = ListingImageUrl('restaurant_images', $res['image']);
|
|
}
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), $restaurants, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('List of Favourite Restaurant service failed : ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
public function DetailRestaurant($customerIamId, $id)
|
|
{
|
|
try {
|
|
$rest = ManageRestaurant::with('operatingHours')->select('id', 'short_id', 'name', 'description', 'restaurant_id', 'address', 'image', 'bio', 'try_on_1', 'try_on_2', 'try_on_3', 'try_on_4', 'exclusion', 'latitude', 'longtitude')->where('short_id', $id)->where('is_active', '1')->first();
|
|
|
|
if ($rest) {
|
|
$rest->image = ListingImageUrl('restaurant_images', $rest->image);
|
|
|
|
$isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $rest->id)
|
|
->exists();
|
|
$rest->is_favourite = $isFavourite;
|
|
|
|
$redeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
|
|
->where('manage_restaurants_xid', $rest->id)
|
|
->where('is_redeem', "1")
|
|
->first();
|
|
|
|
if ($redeem) {
|
|
$rest->is_Redeemed = true;
|
|
$rest->redeem_date = \Carbon\Carbon::parse($redeem->redeem_date)->addHours(4)->toDateTimeString();
|
|
} else {
|
|
$rest->is_Redeemed = false;
|
|
$rest->redeem_date = null;
|
|
}
|
|
}
|
|
|
|
if (!$rest) {
|
|
return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404);
|
|
}
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $rest, 200);
|
|
} catch (Exception $e) {
|
|
Log::error("Error fetching restaurant data: " . $e->getMessage());
|
|
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
public function removeFromFavourite($customerIamId, $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$restaurant = ManageRestaurant::where('short_id', $request->id)->where('is_active', '1')->first();
|
|
if (!$restaurant) {
|
|
return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404);
|
|
}
|
|
|
|
$existingFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $restaurant->id)
|
|
->first();
|
|
if (!$existingFavourite) {
|
|
return jsonResponseWithErrorMessage(__('auth.data_not_found'), 404);
|
|
}
|
|
if ($existingFavourite) {
|
|
$existingFavourite->delete();
|
|
}
|
|
DB::commit();
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), 200);
|
|
} catch (\Exception $ex) {
|
|
DB::rollBack();
|
|
Log::error('Favourite Restaurant service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
public function searchFromFavourite($customerIamId, $request)
|
|
{
|
|
try {
|
|
$customerFavouriteRestaurants = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->pluck('restaurant_xid')
|
|
->toArray();
|
|
|
|
$restaurantsQuery = ManageRestaurant::with('operatingHours')
|
|
->where('is_active', '1')
|
|
->whereIn('id', $customerFavouriteRestaurants);
|
|
|
|
$searchData = $request->input('search_data');
|
|
|
|
if (!empty($searchData)) {
|
|
$restaurantsQuery->where(function ($query) use ($searchData) {
|
|
$query->where('name', 'like', "%$searchData%")
|
|
->orWhere('description', 'like', "%$searchData%")
|
|
->orWhere('address', 'like', "%$searchData%")
|
|
->orWhereHas('state', function ($stateQuery) use ($searchData) {
|
|
$stateQuery->where('name', 'like', "%$searchData%");
|
|
});
|
|
});
|
|
}
|
|
|
|
$restaurants = $restaurantsQuery->get();
|
|
foreach ($restaurants as &$res) {
|
|
$res['image'] = ListingImageUrl('restaurant_images', $res['image']);
|
|
}
|
|
|
|
|
|
return jsonResponseWithSuccessMessageApi(__('auth.restaurant_search'), $restaurants, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('Search from favourite restaurant service failed: ' . $ex->getMessage());
|
|
return response()->json([
|
|
'message' => __('auth.something_went_wrong')
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
|
|
public function redeemRestaurant($customerIamId, $request)
|
|
{
|
|
try {
|
|
$restaurantId = $request->id;
|
|
|
|
$restaurant = ManageRestaurant::with('operatingHours')->where('short_id', $restaurantId)->first();
|
|
if (!$restaurant) {
|
|
return jsonResponseWithErrorMessageApi(__('auth.restaurant_not_found'), 404);
|
|
}
|
|
|
|
// Check if the restaurant has already been redeemed
|
|
$restaurantExist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)
|
|
->where('iam_principal_xid', $customerIamId)
|
|
->where('is_redeem', 1)
|
|
->first();
|
|
|
|
if ($restaurantExist) {
|
|
return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 400);
|
|
}
|
|
|
|
$stateLimitation = TimeInterval::where('manage_state_xid', $restaurant->state_xid)->first();
|
|
$stateMaxLimitation = $stateLimitation ? $stateLimitation->quantity : 0;
|
|
$stateMaxIntervalLimitation = $stateLimitation ? $stateLimitation->time_interval : "month";
|
|
|
|
$restaurantLimitation = RestaurantTimeInterval::where('manage_restaurants_xid', $restaurant->id)->first();
|
|
$restaurantMaxLimitation = $restaurantLimitation ? $restaurantLimitation->quantity : 0;
|
|
$restaurantMaxIntervalLimitation = $restaurantLimitation ? $restaurantLimitation->time_interval : "month";
|
|
|
|
|
|
// Calculate the state interval start date
|
|
$stateIntervalStartDate = $this->calculateIntervalStartDate($stateMaxIntervalLimitation);
|
|
|
|
// Count the redeems within the state interval
|
|
$redeemCountState = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)
|
|
->where('redeem_date', '>=', $stateIntervalStartDate)
|
|
->count();
|
|
|
|
if ($redeemCountState >= $stateMaxLimitation) {
|
|
return jsonResponseWithErrorMessageApi(__('auth.state_limit_reached'), 400);
|
|
}
|
|
|
|
// Calculate the restaurant interval start date
|
|
$restaurantIntervalStartDate = $this->calculateIntervalStartDate($restaurantMaxIntervalLimitation);
|
|
|
|
// Count the redeems within the restaurant interval
|
|
$redeemCountRestaurant = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)
|
|
->where('redeem_date', '>=', $restaurantIntervalStartDate)
|
|
->count();
|
|
|
|
if ($redeemCountRestaurant >= $restaurantMaxLimitation) {
|
|
return jsonResponseWithErrorMessageApi(__('auth.restaurant_limit_reached'), 400);
|
|
}
|
|
|
|
// Get the last redeem time
|
|
$lastRedeem = RedeemRestaurant::where('iam_principal_xid', $customerIamId)
|
|
->orderBy('redeem_date', 'desc')
|
|
->first();
|
|
|
|
$restTime = RestaurantTimeInterval::select('time_hours')->where('manage_restaurants_xid', $restaurant->id)->first();
|
|
$stateTime = TimeInterval::select('time_hours')->where('manage_state_xid', $restaurant->state_xid)->first();
|
|
|
|
$restTimeHours = $restTime ? $restTime->time_hours : 0;
|
|
$stateTimeHours = $stateTime ? $stateTime->time_hours : 0;
|
|
|
|
Log::info('Rest time interval time_hours: ' . $restTimeHours);
|
|
Log::info('State time interval time_hours: ' . $stateTimeHours);
|
|
|
|
if ($lastRedeem) {
|
|
$lastRedeemTime = Carbon::parse($lastRedeem->redeem_date);
|
|
$currentTime = Carbon::now();
|
|
|
|
// Calculate the allowed redeem time based on rest and state time intervals
|
|
$allowedRedeemTime = $lastRedeemTime->copy()->addHours($stateTimeHours);
|
|
|
|
if ($currentTime < $allowedRedeemTime) {
|
|
return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet'), 400);
|
|
}
|
|
|
|
$allowedRedeemTime = $lastRedeemTime->copy()->addHours($restTimeHours);
|
|
|
|
if ($currentTime < $allowedRedeemTime) {
|
|
return jsonResponseWithErrorMessageApi(__('auth.redeem_not_allowed_yet'), 400);
|
|
}
|
|
}
|
|
|
|
// Create a new redeem entry if it doesn't exist
|
|
$redeemRestaurant = new RedeemRestaurant();
|
|
$redeemRestaurant->iam_principal_xid = $customerIamId;
|
|
$redeemRestaurant->manage_restaurants_xid = $restaurant->id;
|
|
$redeemRestaurant->is_redeem = 1; // Redeem restaurant
|
|
$redeemRestaurant->redeem_date = now();
|
|
$redeemRestaurant->is_redeemption_undone = 0;
|
|
$redeemRestaurant->redeemption_undone_date = null;
|
|
$redeemRestaurant->save();
|
|
|
|
$imagePath = ListingImageUrl('restaurant_images', $restaurant->image);
|
|
|
|
$customerTitle = "Your Redemption was successful for " . $restaurant->name;
|
|
$customerMessage = $restaurant->name . " Voucher Redeemed Successfully";
|
|
$customerContentType = 'Voucher_Redemption';
|
|
$customerImageUrl = $imagePath;
|
|
|
|
$customerData = IamPrincipal::where('id', $customerIamId)
|
|
->where('notification_status', 1)
|
|
->where('principal_type_xid', 3)
|
|
->first();
|
|
|
|
$restaurants = IamPrincipalRestaurantRole::select('id', 'principal_xid')
|
|
->where('restaurant_xid', $restaurant->id)
|
|
->get();
|
|
|
|
foreach ($restaurants as $restaurantRole) {
|
|
$restUser = IamPrincipal::where('id', $restaurantRole->principal_xid)
|
|
->where('notification_status', 1)
|
|
->where('principal_type_xid', 4)
|
|
->first();
|
|
|
|
if ($restUser) {
|
|
$restImagePath = ListingImageUrl('restaurant_images', $restaurant->image);
|
|
$restTitle = "New redemption for " . $restaurant->name;
|
|
$restMessage = $restaurant->name . " new voucher Redeemed Successfully";
|
|
$restContent_type = 'Voucher_Redemption';
|
|
$restImageUrl = $restImagePath;
|
|
|
|
// Sending notification to restaurant
|
|
onesignalhelper::restSendNotificationApi(
|
|
$restUser->one_signal_player_id,
|
|
$restTitle,
|
|
$restMessage,
|
|
$restContent_type,
|
|
$restImageUrl,
|
|
$id = null
|
|
);
|
|
onesignalhelper::StoreNotificationDetails($restUser->id, $restContent_type, $restTitle, $restImageUrl);
|
|
}
|
|
}
|
|
|
|
if ($customerData) {
|
|
// Sending notification to customer
|
|
onesignalhelper::sendNotificationApi(
|
|
$customerData->one_signal_player_id,
|
|
$customerTitle,
|
|
$customerMessage,
|
|
$customerContentType,
|
|
$customerImageUrl,
|
|
$id = null
|
|
);
|
|
onesignalhelper::StoreNotificationDetails($customerData->id, $customerContentType, $customerTitle, $customerImageUrl);
|
|
}
|
|
|
|
return jsonResponseWithSuccessMessageApi(__('success.restaurant_redeem'), $redeemRestaurant, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('Restaurant Redeem service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
private function calculateIntervalStartDate($interval)
|
|
{
|
|
$now = Carbon::now();
|
|
|
|
switch ($interval) {
|
|
case 'day':
|
|
return $now->copy()->subDay();
|
|
case 'week':
|
|
return $now->copy()->subWeek();
|
|
case 'month':
|
|
default:
|
|
return $now->copy()->subMonth();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function searchRestaurant($customerIamId, $request)
|
|
{
|
|
try {
|
|
$restaurantsQuery = ManageRestaurant::with(['operatingHours', 'state'])
|
|
->where('is_active', '1');
|
|
|
|
$searchData = $request->input('search_data');
|
|
|
|
if (!empty($searchData)) {
|
|
$restaurantsQuery->where(function ($query) use ($searchData) {
|
|
$query->where('name', 'like', "%$searchData%")
|
|
->orWhere('description', 'like', "%$searchData%")
|
|
->orWhere('address', 'like', "%$searchData%")
|
|
->orWhereHas('state', function ($stateQuery) use ($searchData) {
|
|
$stateQuery->where('name', 'like', "%$searchData%");
|
|
});
|
|
});
|
|
}
|
|
|
|
$restaurants = $restaurantsQuery->get();
|
|
|
|
foreach ($restaurants as &$res) {
|
|
$res['image'] = ListingImageUrl('restaurant_images', $res['image']);
|
|
$res['is_favourite'] = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $res->id)
|
|
->exists();
|
|
}
|
|
|
|
return jsonResponseWithSuccessMessageApi(__('auth.restaurant_search'), $restaurants, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('Search from restaurant service failed: ' . $ex->getMessage());
|
|
return response()->json([
|
|
'message' => __('auth.something_went_wrong')
|
|
], 500);
|
|
}
|
|
}
|
|
}
|