Files
cheerstothe_season_2.0/app/Http/Controllers/Admin/ManageCustomerController.php

283 lines
9.2 KiB
PHP
Raw Normal View History

2024-05-23 15:20:21 +05:30
<?php
namespace App\Http\Controllers\Admin;
2024-05-31 12:20:15 +05:30
2024-05-28 12:45:23 +05:30
use App\Models\IamPrincipal;
2024-05-23 15:20:21 +05:30
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
2024-05-28 12:45:23 +05:30
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\OrderedPassport;
2024-05-28 18:53:50 +05:30
use App\Exports\customer_export;
use App\Exports\customer_export_selected;
2024-05-28 19:29:09 +05:30
use App\Exports\CustomerExportSelected;
2024-06-05 16:30:25 +05:30
use App\Models\ManageRestaurant;
2024-06-07 11:44:25 +05:30
use App\Models\ManageState;
2024-06-05 16:30:25 +05:30
use App\Models\RedeemRestaurant;
2024-06-06 15:53:41 +05:30
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
2024-05-28 19:29:09 +05:30
use Barryvdh\DomPDF\PDF as DomPDFPDF;
2024-06-07 11:44:25 +05:30
use Carbon\Carbon;
2024-05-28 12:45:23 +05:30
use Exception;
2024-05-28 18:53:50 +05:30
use PDF;
2024-05-23 15:20:21 +05:30
class ManageCustomerController extends Controller
{
2024-05-28 12:45:23 +05:30
2024-06-07 11:44:25 +05:30
/*
2024-06-05 20:10:10 +05:30
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get User Page.
*/
2024-05-28 18:53:50 +05:30
public function index()
2024-05-31 12:20:15 +05:30
{
try {
// Eager load the state relationship for all customers
$customers = IamPrincipal::where('principal_type_xid', 3)
->with('state')
->orderBy('created_at', 'desc')
->get();
return view('Admin.pages.manage_users.manage_customer.customer', compact('customers'));
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
2024-05-28 18:53:50 +05:30
}
2024-06-07 11:44:25 +05:30
/*
2024-06-05 20:10:10 +05:30
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get Passport Page.
*/
2024-05-31 12:20:15 +05:30
public function view_customer($id)
{
2024-05-28 19:29:09 +05:30
2024-05-31 12:20:15 +05:30
try {
$customers_data = IamPrincipal::findOrFail($id);
return view('Admin.pages.manage_users.manage_customer.view_customer_details', compact('customers_data'));
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
2024-05-28 18:53:50 +05:30
}
2024-06-07 11:44:25 +05:30
/*
2024-06-05 20:10:10 +05:30
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get Customer data.
*/
2024-05-28 18:53:50 +05:30
2024-05-31 12:20:15 +05:30
public function edit_customer($id)
{
2024-05-28 19:29:09 +05:30
2024-05-31 12:20:15 +05:30
try {
2024-06-07 11:44:25 +05:30
$customers_data = IamPrincipal::with('state')->findOrFail($id);
$state = ManageState::where('is_active', 1)->get();
return view('Admin.pages.manage_users.manage_customer.edit_customer', compact('customers_data', 'state'));
2024-05-31 12:20:15 +05:30
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
2024-05-28 18:53:50 +05:30
}
2024-06-07 11:44:25 +05:30
/*
2024-06-05 20:10:10 +05:30
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Update Customer Form.
*/
2024-05-28 18:53:50 +05:30
2024-06-07 11:44:25 +05:30
public function update(Request $request)
{
// Validation rules
$rules = [
'email_address' => [
'required',
'string',
'email',
'max:100',
Rule::unique('iam_principal')->where(function ($query) use ($request) {
return $query->where('principal_type_xid', 3)
->whereNull('deleted_at')
->where('id', '!=', $request->customer_id);
}),
],
'date_of_birth' => [
'required',
'date',
function ($attribute, $value, $fail) {
$dob = Carbon::parse($value);
$age = $dob->age;
if ($age < 21) {
$fail('You must be at least 21 years old.');
}
},
],
];
$messages = [
'email_address.required' => 'Enter email address',
'email_address.email' => 'Please enter a valid email address',
'email_address.unique' => 'This email is already registered',
'date_of_birth.required' => 'Date of Birth is required',
'date_of_birth.date' => 'Please enter a valid date',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 422);
}
2024-06-06 15:53:41 +05:30
2024-06-07 11:44:25 +05:30
try {
DB::beginTransaction();
2024-06-06 15:53:41 +05:30
2024-06-07 11:44:25 +05:30
$customer_data = IamPrincipal::where('id', $request->customer_id)->first();
$customer_data->first_name = $request->input('name');
$customer_data->last_name = $request->input('last_name');
$customer_data->phone_number = $request->input('phone');
$customer_data->email_address = $request->input('email_address');
$customer_data->date_of_birth = $request->input('date_of_birth');
$customer_data->state_xid = $request->input('state_xid');
$customer_data->save();
2024-06-06 15:53:41 +05:30
2024-06-07 11:44:25 +05:30
DB::commit();
2024-06-06 15:53:41 +05:30
2024-06-07 11:44:25 +05:30
return jsonResponseWithSuccessMessage(__('success.update_data'));
} catch (Exception $e) {
DB::rollBack();
Log::error("updateCustomerNewsArticle Services Page Load Failed " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
2024-05-31 12:20:15 +05:30
}
2024-06-07 11:44:25 +05:30
}
2024-05-28 18:53:50 +05:30
2024-05-31 12:20:15 +05:30
public function archive_customer()
{
/*
2024-06-05 20:10:10 +05:30
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get archieve customer.
*/
2024-05-31 12:20:15 +05:30
try {
$customers = IamPrincipal::where('principal_type_xid', 3)->onlyTrashed()->latest()->get();
return view('Admin.pages.manage_users.manage_customer.archive_manage_customer', compact('customers'));
} catch (Exception $e) {
Log::error("Manage Voucher Page Not Load " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
2024-05-28 18:53:50 +05:30
}
2024-06-05 20:10:10 +05:30
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get delete customer.
*/
2024-05-31 12:20:15 +05:30
public function delete_customer($id)
{
2024-05-28 19:29:09 +05:30
2024-05-31 12:20:15 +05:30
try {
DB::beginTransaction();
2024-05-28 18:53:50 +05:30
2024-05-31 12:20:15 +05:30
$passport = IamPrincipal::find($id);
$passport->one_signal_player_id = null;
$passport->deleted_by_admin = 1;
$passport->save();
2024-05-28 18:53:50 +05:30
2024-05-31 12:20:15 +05:30
$passport->delete();
// dd($passport);
DB::commit();
2024-05-28 18:53:50 +05:30
2024-05-31 12:20:15 +05:30
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')]);
}
2024-05-28 18:53:50 +05:30
}
2024-06-07 11:44:25 +05:30
/*
2024-06-05 20:10:10 +05:30
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get pdf.
*/
2024-05-31 12:20:15 +05:30
public function download_pdf($id)
{
try {
$customers_data = IamPrincipal::findOrFail($id);
$data = [
'customers_data' => $customers_data
];
$pdf = PDF::loadView('Admin.pages.manage_users.manage_customer.view_customer_details_pdf', $data);
return $pdf->download('customer_details.pdf');
} catch (Exception $e) {
Log::error("Error generating PDF: " . $e->getMessage());
return response()->json(['error' => __('auth.something_went_wrong')], 500);
}
2024-05-28 18:53:50 +05:30
}
2024-06-13 13:34:34 +05:30
public function exportSelectedCustomer(Request $request)
{
// dd($request->all());
2024-05-31 12:20:15 +05:30
2024-05-28 18:53:50 +05:30
2024-06-13 13:34:34 +05:30
try {
if ($request->has('all_id')) {
return Excel::download(new customer_export, 'Passport_data.xlsx');
}
$ids = $request->selected_id;
// dd( $ids);
$fileName = 'selected_customer_data.xlsx';
return Excel::download(new customer_export_selected($ids), $fileName);
} catch (\Exception $e) {
return response()->json(['error' => 'Export failed. Something went wrong.'], 500);
}
}
2024-05-28 18:53:50 +05:30
2024-06-13 15:05:59 +05:30
2024-05-28 12:45:23 +05:30
2024-06-13 13:34:34 +05:30
2024-06-07 11:44:25 +05:30
/*
2024-06-05 20:10:10 +05:30
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Deleted Data Restore.
*/
2024-05-31 12:20:15 +05:30
public function unarchive_customer($id)
{
2024-05-28 19:29:09 +05:30
2024-05-31 12:20:15 +05:30
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')]);
}
}
2024-06-05 16:30:25 +05:30
/*
Created By : Sayli Raut
Created at : 05 June 2024
Use : To get customer redeem restaurant details.
*/
public function manageCustomerRestaurants(Request $request, $id)
{
try {
2024-06-13 12:51:49 +05:30
$redeemDetails = RedeemRestaurant::with('restaurant', 'customer')->where('iam_principal_xid', $id)->get();
2024-06-05 16:35:00 +05:30
return view('Admin.pages.manage_users.manage_customer.customer_restaurants', compact('redeemDetails'));
2024-06-05 16:30:25 +05:30
} catch (Exception $e) {
Log::error("Error getting restaurant details: " . $e->getMessage());
return response()->json(['error' => __('auth.something_went_wrong')], 500);
}
}
2024-05-23 15:20:21 +05:30
}