diff --git a/app/Exports/customer_export.php b/app/Exports/customer_export.php index 00fd5b4..b5ef1e4 100644 --- a/app/Exports/customer_export.php +++ b/app/Exports/customer_export.php @@ -18,8 +18,10 @@ class customer_export implements FromCollection , WithHeadings public function collection(){ return IamPrincipal::where('principal_type_xid',3) ->select('first_name', + 'last_name', 'email_address', 'date_of_birth', + 'state_xid', 'phone_number')->get(); @@ -30,8 +32,10 @@ class customer_export implements FromCollection , WithHeadings { return [ 'first_name', + 'last_name', 'email_address', 'date_of_birth', + 'state_xid', 'phone_number' ]; } diff --git a/app/Exports/customer_export_selected.php b/app/Exports/customer_export_selected.php index bea8598..fdad05f 100644 --- a/app/Exports/customer_export_selected.php +++ b/app/Exports/customer_export_selected.php @@ -5,48 +5,64 @@ namespace App\Exports; use Maatwebsite\Excel\Concerns\FromCollection; use App\Models\IamPrincipal; use Maatwebsite\Excel\Concerns\WithHeadings; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; - - -class customer_export_selected implements FromCollection ,WithHeadings +class customer_export_selected implements FromCollection, WithHeadings { protected $ids; public function __construct($ids) { - $this->ids = $ids; + $this->ids = explode(',', $ids); // Ensure ids are split into an array } /** - * @return \Illuminate\Support\Collection - */ + * @return \Illuminate\Support\Collection + */ public function collection() { + // Log the ids for debugging purposes + Log::info('Fetching data for IDs: ' . implode(',', $this->ids)); - $selectedCareIds = explode(',', $this->ids); + $selected_customer = IamPrincipal::whereIn('id', $this->ids) + ->with('state:id,name') // Eager load the state relationship + ->orderBy('id', 'desc') + ->select( + 'first_name', + 'last_name', + 'email_address', + 'date_of_birth', + 'state_xid', + 'phone_number' + ) + ->get() + ->map(function ($customer) { + return [ + 'first_name' => $customer->first_name, + 'last_name' => $customer->last_name, + 'email_address' => $customer->email_address, + 'date_of_birth' => $customer->date_of_birth, + 'state_xid' => $customer->state->name ?? '', // Access the state name and handle null + 'phone_number' => $customer->phone_number, + ]; + }); - $selected_customer = IamPrincipal::whereIn('id', $selectedCareIds) - ->orderBy('id', 'Desc') - ->select( - 'first_name', - 'email_address', - 'date_of_birth', - 'phone_number' - ) - ->get(); - return $selected_customer; + // Log the fetched data for debugging purposes + Log::info('Fetched customer data: ' . $selected_customer->toJson()); + + return new Collection($selected_customer); } - - //function header in excel + public function headings(): array { return [ - 'first_name', - 'email_address', - 'date_of_birth', - 'phone_number' - ]; + 'First Name', + 'Last Name', + 'Email Address', + 'Date of Birth', + 'state_name', + 'Phone Number' + ]; } - - } diff --git a/app/Http/Controllers/Admin/ManageCustomerController.php b/app/Http/Controllers/Admin/ManageCustomerController.php index abdfd31..87c8179 100644 --- a/app/Http/Controllers/Admin/ManageCustomerController.php +++ b/app/Http/Controllers/Admin/ManageCustomerController.php @@ -219,24 +219,57 @@ class ManageCustomerController extends Controller } + // public function exportSelectedCustomer(Request $request) + // { + // // dd($request->all()); + + + // 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); + // // dd($ids); + // } catch (\Exception $e) { + // return response()->json(['error' => 'Export failed. Something went wrong.'], 500); + // } + // } + public function exportSelectedCustomer(Request $request) { - // dd($request->all()); - - try { if ($request->has('all_id')) { return Excel::download(new customer_export, 'Passport_data.xlsx'); } $ids = $request->selected_id; - // dd( $ids); + + // Check if ids is not null or empty + if (empty($ids)) { + return response()->json(['error' => 'No IDs provided for export.'], 400); + } + + // Log the ids for debugging purposes + Log::info("Selected IDs for export: " . $ids); + $fileName = 'selected_customer_data.xlsx'; + + // Additional log before attempting download + Log::info("Attempting to export selected customers to file: " . $fileName); + + // Attempt to create and download the Excel file 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); } } + + diff --git a/app/Http/Controllers/Admin/ManageNotificationsController.php b/app/Http/Controllers/Admin/ManageNotificationsController.php index 30d2463..10cb55d 100644 --- a/app/Http/Controllers/Admin/ManageNotificationsController.php +++ b/app/Http/Controllers/Admin/ManageNotificationsController.php @@ -171,4 +171,5 @@ class ManageNotificationsController extends Controller $notification = NotificationDetails::with('notification')->findOrFail($id); return view('Admin.pages.manage_notification.manage_notifications_view', compact('notification')); } + } diff --git a/app/Http/Controllers/Admin/PrivacyPolicyController.php b/app/Http/Controllers/Admin/PrivacyPolicyController.php index 0f06c2d..39860d8 100644 --- a/app/Http/Controllers/Admin/PrivacyPolicyController.php +++ b/app/Http/Controllers/Admin/PrivacyPolicyController.php @@ -34,12 +34,12 @@ class PrivacyPolicyController extends Controller * Created at : 29 May 2024 * Use : To edit. */ - public function edit($id){ + public function edit($id) + { $edit_privacy_policy = PrivacyPolicy::find($id)->toArray(); return view('Admin.pages.manage_cms.manage_privacy.manage_privacy_policy_edit', compact('edit_privacy_policy')); } - /** * Created By : sayali parab * Created at : 29 May 2024