375 lines
18 KiB
PHP
375 lines
18 KiB
PHP
<?php
|
|
|
|
namespace App\Services\APIs\RestaurantService;
|
|
|
|
use App\Helpers\onesignalhelper;
|
|
use App\Models\admin\ManageVoucherModel;
|
|
use App\Models\IamPrincipal;
|
|
use App\Models\IamPrincipalRestaurantRole;
|
|
use App\Models\ManageRestaurant;
|
|
use App\Models\MyPassportVoucher;
|
|
use App\Models\RedeemRestaurant;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class RedeemApiService
|
|
{
|
|
public function getRedemedData($restIamId)
|
|
{
|
|
try {
|
|
$rest = IamPrincipal::findOrFail($restIamId);
|
|
$data['user_detail'] = IamPrincipal::select('id', 'first_name', 'last_name', 'email_address', 'phone_number', 'date_of_birth')->find($rest->id);
|
|
|
|
$restaurantRoles = IamPrincipalRestaurantRole::select('id', 'principal_xid', 'restaurant_xid', 'role')
|
|
->where('principal_xid', $rest->id)
|
|
->first();
|
|
|
|
|
|
$restDetail = RedeemRestaurant::with([
|
|
'customer:id,first_name,last_name,profile_photo',
|
|
'restaurant:id,image,name'
|
|
])
|
|
->select('id', 'iam_principal_xid', 'manage_restaurants_xid', 'redeem_date', 'is_active', 'count')
|
|
->where('manage_restaurants_xid', $restaurantRoles->restaurant_xid)
|
|
->where('is_redeem', 1)
|
|
->get();
|
|
|
|
foreach ($restDetail as $detail) {
|
|
if ($detail->customer) {
|
|
$detail->customer->profile_photo = $detail->customer->profile_photo
|
|
? ListingImageUrl('profile_image', $detail->customer->profile_photo)
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
|
|
if ($detail->restaurant) {
|
|
// Check if the image is already a URL
|
|
if (!filter_var($detail->restaurant->image, FILTER_VALIDATE_URL)) {
|
|
$detail->restaurant->image = ListingImageUrl('restaurant_images', $detail->restaurant->image);
|
|
} else {
|
|
// Fallback if the image is already a URL
|
|
$detail->restaurant->image = $detail->restaurant->image
|
|
? $detail->restaurant->image
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
}
|
|
}
|
|
|
|
$restDetailArray = $restDetail->toArray();
|
|
|
|
|
|
|
|
$restundoresumptionDetail = RedeemRestaurant::with([
|
|
'customer:id,first_name,last_name,profile_photo',
|
|
'restaurant:id,image,name'
|
|
])
|
|
->select('id', 'iam_principal_xid', 'manage_restaurants_xid', 'redeem_date', 'is_active', 'count', 'redeemption_undone_date')
|
|
->where('manage_restaurants_xid', $restaurantRoles->restaurant_xid)
|
|
->where('is_redeemption_undone', 1)
|
|
->get();
|
|
|
|
foreach ($restundoresumptionDetail as $detail) {
|
|
if ($detail->customer) {
|
|
$detail->customer->profile_photo = $detail->customer->profile_photo
|
|
? ListingImageUrl('profile_image', $detail->customer->profile_photo)
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
|
|
if ($detail->restaurant) {
|
|
// Check if the image is already a URL
|
|
if (!filter_var($detail->restaurant->image, FILTER_VALIDATE_URL)) {
|
|
$detail->restaurant->image = ListingImageUrl('restaurant_images', $detail->restaurant->image);
|
|
} else {
|
|
// Fallback if the image is already a URL
|
|
$detail->restaurant->image = $detail->restaurant->image
|
|
? $detail->restaurant->image
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
}
|
|
}
|
|
$restundoRedumption = $restundoresumptionDetail->toArray();
|
|
|
|
|
|
$restaurantDetail = [
|
|
'redeemed_vouchers' => $restDetailArray,
|
|
'redemption_undone_vouchers' => $restundoRedumption,
|
|
];
|
|
|
|
return jsonResponseWithSuccessMessageApi(__('auth.User_details_fetch'), $restaurantDetail, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('Restaurant Get data service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function undoRedemption($restIamId, $request)
|
|
{
|
|
try {
|
|
$voucherDetail = RedeemRestaurant::where('id', $request->voucher_id)->first();
|
|
$rest = ManageRestaurant::where('id', $voucherDetail->manage_restaurants_xid)->first();
|
|
if ($voucherDetail) {
|
|
$voucherDetail->update([
|
|
'is_redeem' => 0,
|
|
'redeem_date' => null,
|
|
'is_redeemption_undone' => 1,
|
|
'redeemption_undone_date' => now(),
|
|
|
|
]);
|
|
$imagePath = ListingImageUrl('restaurant_images', $rest->image);
|
|
$customerTitle = "Your voucher was successfully undo redemption for " . $rest->name;
|
|
$customerMessage = $rest->name . " Voucher Undo Redemption Successfully";
|
|
$customerContentType = 'Voucher_UndoRedemption';
|
|
$customerImageUrl = $imagePath;
|
|
$customerData = IamPrincipal::where('id', $voucherDetail->iam_principal_xid)->where('notification_status', 1)->where('principal_type_xid', 3)->first();
|
|
if ($customerData) {
|
|
$pushNotificationToCustomer = onesignalhelper::sendNotificationApi(
|
|
$customerData->one_signal_player_id,
|
|
$customerTitle,
|
|
$customerMessage,
|
|
$customerContentType,
|
|
$customerImageUrl,
|
|
$id = null
|
|
);
|
|
|
|
onesignalhelper::StoreNotificationDetails($customerData->id, $customerContentType, $customerTitle, $customerImageUrl);
|
|
}
|
|
$restUser = IamPrincipal::where('id', $restIamId)->where('notification_status', 1)->where('principal_type_xid', 4)->first();
|
|
if ($restUser) {
|
|
$restImagePath = ListingImageUrl('voucher_images', $rest->image);
|
|
$restTitle = "voucher Undo redemption successful for " . $rest->name;
|
|
$restMessage = $rest->image . " Voucher Undo Redemption Successfully";
|
|
$restContentType = 'Voucher_UndoRedemption';
|
|
$restImageUrl = $restImagePath;
|
|
|
|
$pushNotificationToCustomer = onesignalhelper::restSendNotificationApi(
|
|
$restUser->one_signal_player_id,
|
|
$restTitle,
|
|
$restMessage,
|
|
$restContentType,
|
|
$restImageUrl,
|
|
$id = null
|
|
);
|
|
|
|
onesignalhelper::StoreNotificationDetails($restUser->id, $restContentType, $restTitle, $restImageUrl);
|
|
}
|
|
return jsonResponseWithSuccessMessageApi(__('auth.data_updated_successfully'), 200);
|
|
} else {
|
|
return jsonResponseWithErrorMessageApi(__('auth.voucher_not_found'), 404);
|
|
}
|
|
} catch (Exception $ex) {
|
|
Log::error('Restaurant update profile service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
public function getRedemedData(Request $request)
|
|
{
|
|
try {
|
|
$restIamId = $request->input('restIamId');
|
|
$rest = IamPrincipal::findOrFail($restIamId);
|
|
$data['user_detail'] = IamPrincipal::select('id', 'first_name', 'last_name', 'email_address', 'phone_number', 'date_of_birth')->find($rest->id);
|
|
|
|
$restaurantRoles = IamPrincipalRestaurantRole::select('id', 'principal_xid', 'restaurant_xid', 'role')
|
|
->where('principal_xid', $rest->id)
|
|
->first();
|
|
|
|
$query = RedeemRestaurant::with([
|
|
'customer:id,first_name,last_name,profile_photo',
|
|
'restaurant:id,image,name'
|
|
])
|
|
->select('id', 'iam_principal_xid', 'manage_restaurants_xid', 'redeem_date', 'is_active', 'count')
|
|
->where('manage_restaurants_xid', $restaurantRoles->restaurant_xid)
|
|
->where('is_redeem', 1);
|
|
|
|
// Apply filters based on query parameters
|
|
if ($request->has('customer_first_name')) {
|
|
$query->whereHas('customer', function ($q) use ($request) {
|
|
$q->where('first_name', 'like', '%' . $request->input('customer_first_name') . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->has('customer_last_name')) {
|
|
$query->whereHas('customer', function ($q) use ($request) {
|
|
$q->where('last_name', 'like', '%' . $request->input('customer_last_name') . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->has('redeem_date')) {
|
|
$query->whereDate('redeem_date', $request->input('redeem_date'));
|
|
}
|
|
|
|
$restDetail = $query->get();
|
|
|
|
foreach ($restDetail as $detail) {
|
|
if ($detail->customer) {
|
|
$detail->customer->profile_photo = $detail->customer->profile_photo
|
|
? ListingImageUrl('profile_image', $detail->customer->profile_photo)
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
|
|
if ($detail->restaurant) {
|
|
if (!filter_var($detail->restaurant->image, FILTER_VALIDATE_URL)) {
|
|
$detail->restaurant->image = ListingImageUrl('restaurant_images', $detail->restaurant->image);
|
|
} else {
|
|
$detail->restaurant->image = $detail->restaurant->image
|
|
? $detail->restaurant->image
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
}
|
|
}
|
|
|
|
$restDetailArray = $restDetail->toArray();
|
|
|
|
// For redemption undone vouchers
|
|
$queryUndo = RedeemRestaurant::with([
|
|
'customer:id,first_name,last_name,profile_photo',
|
|
'restaurant:id,image,name'
|
|
])
|
|
->select('id', 'iam_principal_xid', 'manage_restaurants_xid', 'redeem_date', 'is_active', 'count', 'redeemption_undone_date')
|
|
->where('manage_restaurants_xid', $restaurantRoles->restaurant_xid)
|
|
->where('is_redeemption_undone', 1);
|
|
|
|
// Apply filters based on query parameters
|
|
if ($request->has('customer_first_name')) {
|
|
$queryUndo->whereHas('customer', function ($q) use ($request) {
|
|
$q->where('first_name', 'like', '%' . $request->input('customer_first_name') . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->has('customer_last_name')) {
|
|
$queryUndo->whereHas('customer', function ($q) use ($request) {
|
|
$q->where('last_name', 'like', '%' . $request->input('customer_last_name') . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->has('redeemption_undone_date')) {
|
|
$queryUndo->whereDate('redeemption_undone_date', $request->input('redeemption_undone_date'));
|
|
}
|
|
|
|
$restundoresumptionDetail = $queryUndo->get();
|
|
|
|
foreach ($restundoresumptionDetail as $detail) {
|
|
if ($detail->customer) {
|
|
$detail->customer->profile_photo = $detail->customer->profile_photo
|
|
? ListingImageUrl('profile_image', $detail->customer->profile_photo)
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
|
|
if ($detail->restaurant) {
|
|
if (!filter_var($detail->restaurant->image, FILTER_VALIDATE_URL)) {
|
|
$detail->restaurant->image = ListingImageUrl('restaurant_images', $detail->restaurant->image);
|
|
} else {
|
|
$detail->restaurant->image = $detail->restaurant->image
|
|
? $detail->restaurant->image
|
|
: asset('public/assets/img/blankProfile.png');
|
|
}
|
|
}
|
|
}
|
|
$restundoRedumption = $restundoresumptionDetail->toArray();
|
|
|
|
$restaurantDetail = [
|
|
'redeemed_vouchers' => $restDetailArray,
|
|
'redemption_undone_vouchers' => $restundoRedumption,
|
|
];
|
|
|
|
return jsonResponseWithSuccessMessageApi(__('auth.User_details_fetch'), $restaurantDetail, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('Restaurant Get data service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
// public function searchRedemption($restIamId, $request)
|
|
// {
|
|
// try {
|
|
// $searchQuery = $request->input('search_data');
|
|
|
|
// $rest = IamPrincipal::findOrFail($restIamId);
|
|
// $data['user_detail'] = IamPrincipal::select('id', 'first_name', 'last_name', 'email_address', 'phone_number', 'date_of_birth')->find($rest->id);
|
|
|
|
// $restaurantRoles = IamPrincipalRestaurantRole::select('id', 'principal_xid', 'restaurant_xid', 'role')->where('principal_xid', $rest->id)->get();
|
|
|
|
// $restaurantDetail = [];
|
|
// foreach ($restaurantRoles as $role) {
|
|
// $restaurantImage = ManageVoucherModel::select('id', 'coupon_name', 'description', 'thumbnail_image', 'image', 'location_name')->find($role->restaurant_xid);
|
|
// if ($restaurantImage) {
|
|
// $restaurantImage->thumbnail_image = ListingImageUrl('voucher_thumbnail_images', $restaurantImage->thumbnail_image);
|
|
// $restaurantImage->image = ListingImageUrl('voucher_images', $restaurantImage->image);
|
|
// }
|
|
// $restaurantDetail[] = $restaurantImage;
|
|
|
|
// $redeemedVouchers = [];
|
|
// $redemptionUndoneVouchers = [];
|
|
|
|
// $vouchers = MyPassportVoucher::select('id', 'order_xid', 'iam_principal_xid', 'manage_passports_xid', 'manage_vouchers_xid', 'is_redeem', 'count', 'redeem_date', 'is_redeemption_undone')
|
|
// ->where('manage_vouchers_xid', $role->restaurant_xid)
|
|
// ->where('is_redeem', 1)
|
|
// ->get();
|
|
|
|
// foreach ($vouchers as $voucher) {
|
|
// $userDetail = IamPrincipal::select('id', 'first_name', 'email_address', 'profile_photo', 'address_line1')
|
|
// ->where('id', $voucher->iam_principal_xid)
|
|
// ->first();
|
|
|
|
// if ($userDetail && (stripos($userDetail->first_name, $searchQuery) !== false || stripos($voucher->id, $searchQuery) !== false || stripos($voucher->redeem_date, $searchQuery) !== false)) {
|
|
// if ($userDetail->profile_photo) {
|
|
// $userDetail->profile_photo = ListingImageUrl('profile_image', $userDetail->profile_photo);
|
|
// } else {
|
|
// $userDetail->profile_photo = asset('public/assets/img/blankProfile.png');
|
|
// }
|
|
|
|
// $voucher->user_detail = $userDetail;
|
|
// $redeemedVouchers[] = $voucher;
|
|
// } else {
|
|
|
|
// Log::error('User detail not found for IAM principal ID: ' . $voucher->iam_principal_xid);
|
|
// }
|
|
// }
|
|
|
|
// $redeemptionUndone = MyPassportVoucher::select('id', 'order_xid', 'iam_principal_xid', 'manage_passports_xid', 'manage_vouchers_xid', 'is_redeem', 'count', 'redeem_date', 'is_redeemption_undone', 'redeemption_undone_date')
|
|
// ->where('manage_vouchers_xid', $role->restaurant_xid)
|
|
// ->where([['is_redeem', 0], ['is_redeemption_undone', 1]])
|
|
// ->get();
|
|
|
|
// foreach ($redeemptionUndone as $undone) {
|
|
// $userDetail = IamPrincipal::select('id', 'first_name', 'email_address', 'profile_photo', 'address_line1')
|
|
// ->where('id', $undone->iam_principal_xid)
|
|
// ->first();
|
|
|
|
// if ($userDetail && (stripos($userDetail->first_name, $searchQuery) !== false || stripos($undone->id, $searchQuery) !== false || stripos($undone->redeemption_undone_date, $searchQuery) !== false)) {
|
|
// if ($userDetail->profile_photo) {
|
|
// $userDetail->profile_photo = ListingImageUrl('profile_image', $userDetail->profile_photo);
|
|
// } else {
|
|
// $userDetail->profile_photo = asset('public/assets/img/blankProfile.png');
|
|
// }
|
|
|
|
// $undone->user_detail = $userDetail;
|
|
// $redemptionUndoneVouchers[] = $undone;
|
|
// } else {
|
|
|
|
// Log::error('User detail not found for IAM principal ID: ' . $undone->iam_principal_xid);
|
|
// }
|
|
// }
|
|
|
|
// if (empty($searchQuery)) {
|
|
// $restaurantDetail['redeemed_vouchers'] = $redeemedVouchers;
|
|
// $restaurantDetail['redemption_undone_vouchers'] = $redemptionUndoneVouchers;
|
|
// }
|
|
|
|
// $restaurantDetail['redeemed_vouchers'] = $redeemedVouchers;
|
|
// $restaurantDetail['redemption_undone_vouchers'] = $redemptionUndoneVouchers;
|
|
// }
|
|
|
|
// return jsonResponseWithSuccessMessageApi(__('auth.User_details_fetch'), $restaurantDetail, 200);
|
|
// } catch (Exception $ex) {
|
|
// Log::error('Restaurant Get data service failed : ' . $ex->getMessage());
|
|
// return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
// }
|
|
// }
|
|
}
|