Files
cheerstothe_season_2.0/app/Services/APIs/RestaurantService/RedeemApiService.php
sayaliparab 6aa37e8ba0 Changestoday
2024-05-29 14:53:06 +05:30

249 lines
13 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\MyPassportVoucher;
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)->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();
$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 ($vouchers as $voucher) {
$userDetail = IamPrincipal::select('id', 'first_name', 'email_address', 'profile_photo', 'address_line1')
->where('id', $voucher->iam_principal_xid)
->first();
if ($userDetail) {
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);
}
}
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) {
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);
}
}
$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);
}
}
public function undoRedemption($restIamId, $request)
{
try {
$voucherDetail = MyPassportVoucher::with('passportData', 'voucherData')->where('id', $request->voucher_id)->first();
if ($voucherDetail) {
$voucherDetail->update([
'is_redeem' => 0,
'redeem_date' => null,
'is_redeemption_undone' => 1,
'redeemption_undone_date' => now(),
]);
$imagePath = ListingImageUrl('voucher_images', $voucherDetail->voucherData->image);
$customerTitle = "Your voucher was successfully undo redemption for " . $voucherDetail->passportData->passport_name;
$customerMessage = $voucherDetail->voucherData->coupon_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', $voucherDetail->voucherData->image);
$restTitle = "voucher Undo redemption successful for " . $voucherDetail->passportData->passport_name;
$restMessage = $voucherDetail->voucherData->coupon_name . " 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 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);
}
}
}