Files
cheerstothe_season_2.0/app/Http/Controllers/Admin/ManageCustomerController.php
2024-07-08 20:11:31 +05:30

336 lines
11 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Models\IamPrincipal;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\OrderedPassport;
use App\Exports\customer_export;
use App\Exports\customer_export_selected;
use App\Exports\CustomerExportSelected;
use App\Models\ManageRestaurant;
use App\Models\ManageState;
use App\Models\RedeemRestaurant;
use App\Models\Subscriptions;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Barryvdh\DomPDF\PDF as DomPDFPDF;
use Carbon\Carbon;
use Exception;
use PDF;
class ManageCustomerController extends Controller
{
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get User Page.
*/
public function index()
{
try {
$customers = IamPrincipal::with(['isSubscribed', 'state'])
->where('principal_type_xid', 3)
->orderBy('created_at', 'desc')
->get();
$dateTime = now();
$formattedDateTime = $dateTime->format('Y-m-d H:i:s');
foreach ($customers as $customer) {
$customer->is_subscribed = $customer->isSubscribed->contains(function ($subscription) use ($formattedDateTime) {
return $subscription->subscription_status == 'active' && $subscription->next_payment_date >= $formattedDateTime;
});
}
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);
}
}
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get Passport Page.
*/
public function view_customer($id)
{
try {
$customers_data = IamPrincipal::with('state', 'contactMessages','isSubscribed')->findOrFail($id);
if ($customers_data->contactMessages->isEmpty()) {
Log::info('No contact messages found for customer with ID: ' . $id);
}
if ($customers_data->isSubscribed->isEmpty()) {
Log::info('No payment history found for customer with ID: ' . $id);
}
// return $customers_data;
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);
}
}
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get Customer data.
*/
public function edit_customer($id)
{
try {
$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'));
} 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 : 28 May 2024
Use : To Update Customer Form.
*/
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);
}
try {
DB::beginTransaction();
$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();
DB::commit();
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);
}
}
public function archive_customer()
{
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get archieve customer.
*/
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);
}
}
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get delete customer.
*/
public function delete_customer($id)
{
try {
DB::beginTransaction();
$passport = IamPrincipal::find($id);
$passport->one_signal_player_id = null;
$passport->deleted_by_admin = 1;
$passport->save();
$passport->delete();
// dd($passport);
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 : 28 May 2024
Use : To Get pdf.
*/
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);
}
}
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Get Excel.
*/
public function exportSelectedCustomer(Request $request)
{
try {
if ($request->has('all_id')) {
return Excel::download(new customer_export, 'customer_data.xlsx');
}
$ids = $request->selected_id;
if (empty($ids)) {
return response()->json(['error' => 'No IDs provided for export.'], 400);
}
Log::info("Selected IDs for export: " . $ids);
$fileName = 'selected_customer_data.xlsx';
Log::info("Attempting to export selected customers to file: " . $fileName);
return Excel::download(new customer_export_selected($ids), $fileName);
} catch (\Exception $e) {
Log::error('Export failed: ' . $e->getMessage());
return response()->json(['error' => 'Export failed. Something went wrong.'], 500);
}
}
/*
Created By : Sayali Parab
Created at : 28 May 2024
Use : To Deleted Data Restore.
*/
public function unarchive_customer($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 : Sayli Raut
Created at : 05 June 2024
Use : To get customer redeem restaurant details.
*/
public function manageCustomerRestaurants(Request $request, $id)
{
try {
$redeemDetails = RedeemRestaurant::with('restaurant', 'customer')->where('iam_principal_xid', $id)->get();
// return $redeemDetails;
return view('Admin.pages.manage_users.manage_customer.customer_restaurants', compact('redeemDetails'));
} catch (Exception $e) {
Log::error("Error getting restaurant details: " . $e->getMessage());
return response()->json(['error' => __('auth.something_went_wrong')], 500);
}
}
/*
Created By : Sayali parab
Created at : 27 June 2024
Use : To delete customer details.
*/
public function deleteCustomerUser($id)
{
try {
DB::beginTransaction();
$deleteCustomer = IamPrincipal::find($id);
// dd($id );
$deleteCustomer->delete();
DB::commit();
return response()->json(['sucess' => 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')]);
}
}
}