Files
cheerstothe_season_2.0/app/Http/Controllers/Admin/RestaurantAppController.php
2024-07-17 17:55:00 +05:30

315 lines
9.8 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use App\Models\IamPrincipal;
use App\Models\ManageRestaurant;
use App\Mail\RestUserApproval;
use App\Models\IamPrincipalRestaurantRole;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\restaurant_export;
use App\Exports\restaurant_export_selected;
use Illuminate\Support\Facades\Mail;
use Exception;
class RestaurantAppController extends Controller
{
/**
* Created By : sayali parab
* Created at : 03 June 2024
* Use : To get restaturant user.
*/
public function index_restraunt_users(Request $request)
{
try {
$activeQuery = $request->query('active');
if ($activeQuery == 1) {
$restaurant_users = IamPrincipal::with('getresturant')
->where('principal_type_xid', 4)
->where('is_active', 1)
->orderBy('created_at', 'desc')
->get();
} else if ($activeQuery == 0 && $activeQuery != null) {
$restaurant_users = IamPrincipal::with('getresturant')
->where('principal_type_xid', 4)
->where('is_active', '0')
->orderBy('created_at', 'desc')
->get();
} else {
$restaurant_users = IamPrincipal::with('getresturant')
->where('principal_type_xid', 4)
->orderBy('created_at', 'desc')
->get();
}
// return $restaurant_users;
foreach ($restaurant_users as $user) {
$restaurantIds = $user->getresturant->pluck('restaurant_xid')->toArray();
$user->restaurants = ManageRestaurant::whereIn('id', $restaurantIds)
->select('id', 'restaurant_id', 'name', 'image', 'address', 'latitude', 'longtitude')
->get();
// Optionally, if you want to format the images as in the previous example
foreach ($user->restaurants as &$restaurant) {
$restaurant->image = ListingImageUrl('restaurant_images', $restaurant->image);
}
}
// Return the response as JSON
// return response()->json($restaurant_users);
return view('Admin.pages.manage_users.restaurants_app.restaurants_users', compact('restaurant_users'));
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
/**
* Created By : sayali parab
* Created at : 03 June 2024
* Use : To change the status restaturant user.
*/
public function change_rest_user_status(Request $request)
{
try {
DB::beginTransaction();
$status = IamPrincipal::find($request->rest_user_id);
$status->is_active = $request->status;
// Generate a random password if status is 1 (approved)
if ($request->status == 1) {
$randomPassword = \Str::random(8); // Adjust the length as per your requirements
$status->password = bcrypt($randomPassword); // Make sure to hash the password
}
$status->save();
if ($request->status == 1) {
// Fetch user data based on user ID
$user = IamPrincipal::find($request->rest_user_id);
if ($user) {
// Send email only if user exists
$data = [
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email_address,
'password' => $randomPassword, // Pass the random password to the email template
];
Mail::to($user->email_address)->send(new RestUserApproval($data));
}
}
DB::commit();
return jsonResponseWithSuccessMessage(__('success.update_data'));
} catch (Exception $e) {
Log::error("Update Status function Load Failed " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
/**
* Created By : sayali parab
* Created at : 03 June 2024
* Use : To view restaturant user.
*/
public function view_rest($id)
{
try {
$restdata = IamPrincipalRestaurantRole::with('restaurant', 'customer')->where('principal_xid', $id)->first();
// return $restdata;
return view('Admin.pages.manage_users.restaurants_app.view_restaurant_users', compact('restdata'));
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
/**
* Created By : sayali parab
* Created at : 03 June 2024
* Use : To edit restaturant user.
*/
public function edit_Restaurant($id)
{
try {
$editRest = IamPrincipal::findOrFail($id);
return view('Admin.pages.manage_users.restaurants_app.edit_restaurant_users', compact('editRest'));
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
/**
* Created By : sayali parab
* Created at : 04 June 2024
* Use : To update restaturant user.
*/
public function updateRest(Request $request)
{
try {
DB::beginTransaction();
$rest_data = IamPrincipal::find($request->input('rest_id'));
// dd( $rest_data ) ;
if (!$rest_data) {
throw new Exception('Restaurant not found.');
}
$rest_data->first_name = $request->input('first_name');
$rest_data->last_name = $request->input('last_name');
$rest_data->phone_number = $request->input('restaurant_phone');
$rest_data->date_of_birth = $request->input('user_birth');
$rest_data->save();
DB::commit();
return response()->json(['status_code' => 200, 'message' => __('success.update_data')], 200);
} catch (Exception $e) {
DB::rollBack();
Log::error("Update Restaurant Failed: " . $e->getMessage());
return response()->json(['status_code' => 500, 'message' => __('auth.something_went_wrong')], 500);
}
}
/**
* Created By : sayali parab
* Created at : 04 June 2024
* Use : To delete restaturant user.
*/
public function deleteRestaurantsUsers($id)
{
try {
DB::beginTransaction();
$restaurant = IamPrincipal::find($id);
$restaurant->delete();
DB::commit();
return response()->json(['success' => true, 'status' => 200]);
} catch (Exception $e) {
DB::rollBack();
Log::error("delete_passport function Load Failed " . $e->getMessage());
return response()->json(['success' => false, 'status' => 500, 'message' => __('auth.something_went_wrong')]);
}
}
/**
* Created By : sayali parab
* Created at : 04 June 2024
* Use : To archieve restaturant user.
*/
public function archive_restaturant()
{
try {
$rest_user = IamPrincipal::where('principal_type_xid', 4)->onlyTrashed()->latest()->get();
return view('Admin.pages.manage_users.restaurants_app.archive_manage_restaurant', compact('rest_user'));
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
/**
* Created By : sayali parab
* Created at : 04 June 2024
* Use : To unarchieve restaturant user.
*/
public function unarchive_rest($id)
{
try {
DB::beginTransaction();
IamPrincipal::withTrashed()->where('id', $id)->restore();
DB::commit();
return response()->json(['success' => true, 'status' => 200]);
} catch (Exception $e) {
DB::rollBack();
Log::error("delete_passport function Load Failed " . $e->getMessage());
return response()->json(['success' => false, 'status' => 500, 'message' => __('auth.something_went_wrong')]);
}
}
/*
Created By : Sayali Parab
Created at : 17 july 2024
Use : To Get Excel.
*/
public function exportSelectedrestaurantApp(Request $request)
{
try {
if ($request->has('all_id')){
return Excel::download(new restaurant_export, 'restaurant_user_data.xlsx');
}
// $ids = $request->selected_id;
// dd($ids);
$ids = $request->input('selected_id');
if (empty($ids)) {
return response()->json(['error' => 'No IDs provided for export.'], 400);
}
Log::info("Selected IDs for export: " . $ids);
$fileName = 'selected_restaurantusers_data.xlsx';
Log::info("Attempting to export selected customers to file: " . $fileName);
return Excel::download(new restaurant_export_selected($ids), $fileName);
} catch (\Exception $e) {
Log::error('Export failed: ' . $e->getMessage());
return response()->json(['error' => 'Export failed. Something went wrong.'], 500);
}
}
}