From f35c960dfa8a72f1d129ab8731835c616b05dfd9 Mon Sep 17 00:00:00 2001 From: sayaliparab Date: Wed, 17 Jul 2024 17:55:00 +0530 Subject: [PATCH] restExportChanges --- app/Exports/restaurant_export.php | 98 ++++++++++++++----- app/Exports/restaurant_export_selected.php | 98 ++++++++++++++++--- .../Admin/RestaurantAppController.php | 8 +- .../restaurants_users.blade.php | 2 +- 4 files changed, 165 insertions(+), 41 deletions(-) diff --git a/app/Exports/restaurant_export.php b/app/Exports/restaurant_export.php index f2ffca6..1f7318d 100644 --- a/app/Exports/restaurant_export.php +++ b/app/Exports/restaurant_export.php @@ -7,41 +7,87 @@ use App\Models\IamPrincipal; use Maatwebsite\Excel\Concerns\WithHeadings; use Illuminate\Support\Collection; +// class restaurant_export implements FromCollection, WithHeadings +// { +// /** +// * @return \Illuminate\Support\Collection +// */ +// public function collection() +// { +// $restaurant_users = IamPrincipal::where('principal_type_xid', 4) +// ->with('restaurant') +// ->select( +// 'id', +// 'first_name', +// 'last_name', +// 'email_address', +// 'date_of_birth', +// 'state_xid', +// 'phone_number' +// ) +// ->get(); + +// $serial = 1; +// return $restaurant_users->map(function ($restaurant_user) use (&$serial) { +// return [ +// 'Sr No.' => $serial++, // Increment serial number +// 'id' =>$restaurant_user->id, +// 'first_name' => $restaurant_user->first_name, +// 'last_name' => $restaurant_user->last_name, +// 'email_address' => $restaurant_user->email_address, +// 'date_of_birth' => \Carbon\Carbon::parse($restaurant_user->date_of_birth)->format('m/d/Y'), +// 'state_xid' =>$restaurant_user->state->name ?? '', // Access the state name and handle null +// 'phone_number' => $restaurant_user->phone_number, + +// ]; +// }); +// } + +// // Function to provide the headings in Excel +// public function headings(): array +// { +// return [ +// 'Sr No.', +// 'User Id', +// 'First Name', +// 'Last Name', +// 'Email Address', +// 'Date of Birth', +// 'State Name', +// 'Phone Number', +// ]; +// } +// } + +// class restaurant_export implements FromCollection, WithHeadings { - /** - * @return \Illuminate\Support\Collection - */ public function collection() { + // Fetch restaurant users with their related restaurant roles and state $restaurant_users = IamPrincipal::where('principal_type_xid', 4) - ->select( - 'id', - 'first_name', - 'last_name', - 'email_address', - 'date_of_birth', - 'state_xid', - 'phone_number' - ) + ->with(['getresturant.restaurant', 'state']) ->get(); $serial = 1; - return $restaurant_users->map(function ($restaurant_user) use (&$serial) { - return [ - 'Sr No.' => $serial++, // Increment serial number - 'id' =>$restaurant_user->id, - 'first_name' => $restaurant_user->first_name, - 'last_name' => $restaurant_user->last_name, - 'email_address' => $restaurant_user->email_address, - 'date_of_birth' => \Carbon\Carbon::parse($restaurant_user->date_of_birth)->format('m/d/Y'), - 'state_xid' =>$restaurant_user->state->name ?? '', // Access the state name and handle null - 'phone_number' => $restaurant_user->phone_number, - ]; + return $restaurant_users->flatMap(function ($restaurant_user) use (&$serial) { + return $restaurant_user->getresturant->map(function ($role) use (&$serial, $restaurant_user) { + return [ + 'Sr No.' => $serial++, + 'User Id' => $restaurant_user->id, + 'First Name' => $restaurant_user->first_name, + 'Last Name' => $restaurant_user->last_name, + 'Email Address' => $restaurant_user->email_address, + 'Date of Birth' => optional($restaurant_user->date_of_birth)->format('m/d/Y'), + 'State Name' => optional($restaurant_user->state)->name ?? '', + 'Phone Number' => $restaurant_user->phone_number, + 'Restaurant Name' => optional($role->restaurant)->name ?? '', + 'Restaurant Address' => optional($role->restaurant)->address ?? '', + ]; + }); }); } - // Function to provide the headings in Excel public function headings(): array { return [ @@ -53,6 +99,8 @@ class restaurant_export implements FromCollection, WithHeadings 'Date of Birth', 'State Name', 'Phone Number', + 'Restaurant Name', + 'Restaurant Address', ]; } -} +} \ No newline at end of file diff --git a/app/Exports/restaurant_export_selected.php b/app/Exports/restaurant_export_selected.php index 05492a6..1db67f3 100644 --- a/app/Exports/restaurant_export_selected.php +++ b/app/Exports/restaurant_export_selected.php @@ -7,6 +7,70 @@ use App\Models\IamPrincipal; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; +// class restaurant_export_selected implements FromCollection, WithHeadings +// { +// protected $ids; + +// public function __construct($ids) +// { +// $this->ids = is_array($ids) ? $ids : explode(',', $ids); +// } + +// /** +// * @return \Illuminate\Support\Collection +// */ +// public function collection() +// { +// Log::info('Fetching data for IDs: ' . implode(',', $this->ids)); + +// $selected_customers = IamPrincipal::whereIn('id', $this->ids) +// ->with('state:id,name') +// ->orderBy('id', 'desc') +// ->select( +// 'id', +// 'first_name', +// 'last_name', +// 'email_address', +// 'date_of_birth', +// 'state_xid', +// 'phone_number' +// ) +// ->get(); + +// $serial = 1; +// $mappedCustomers = $selected_customers->map(function ($customer) use (&$serial) { +// return [ +// 'Sr No.' => $serial++, +// 'id' => $customer->id, +// 'first_name' => $customer->first_name, +// 'last_name' => $customer->last_name, +// 'email_address' => $customer->email_address, +// 'date_of_birth' => \Carbon\Carbon::parse($customer->date_of_birth)->format('m/d/Y'), +// 'state_xid' => $customer->state->name ?? '', +// 'phone_number' => $customer->phone_number, +// ]; +// }); + +// Log::info('Fetched customer data: ' . $mappedCustomers->toJson()); + +// return new Collection($mappedCustomers); +// } + +// public function headings(): array +// { +// return [ +// 'Sr No.', +// 'User Id', +// 'First Name', +// 'Last Name', +// 'Email Address', +// 'Date of Birth', +// 'State Name', +// 'Phone Number' +// ]; +// } +// } + class restaurant_export_selected implements FromCollection, WithHeadings { protected $ids; @@ -24,7 +88,7 @@ class restaurant_export_selected implements FromCollection, WithHeadings Log::info('Fetching data for IDs: ' . implode(',', $this->ids)); $selected_customers = IamPrincipal::whereIn('id', $this->ids) - ->with('state:id,name') + ->with(['state', 'getresturant.restaurant']) ->orderBy('id', 'desc') ->select( 'id', @@ -38,17 +102,21 @@ class restaurant_export_selected implements FromCollection, WithHeadings ->get(); $serial = 1; - $mappedCustomers = $selected_customers->map(function ($customer) use (&$serial) { - return [ - 'Sr No.' => $serial++, - 'id' => $customer->id, - 'first_name' => $customer->first_name, - 'last_name' => $customer->last_name, - 'email_address' => $customer->email_address, - 'date_of_birth' => \Carbon\Carbon::parse($customer->date_of_birth)->format('m/d/Y'), - 'state_xid' => $customer->state->name ?? '', - 'phone_number' => $customer->phone_number, - ]; + $mappedCustomers = $selected_customers->flatMap(function ($customer) use (&$serial) { + return $customer->getresturant->map(function ($role) use (&$serial, $customer) { + return [ + 'Sr No.' => $serial++, + 'User Id' => $customer->id, + 'First Name' => $customer->first_name, + 'Last Name' => $customer->last_name, + 'Email Address' => $customer->email_address, + 'Date of Birth' => \Carbon\Carbon::parse($customer->date_of_birth)->format('m/d/Y'), + 'State Name' => optional($customer->state)->name ?? 'NA', + 'Phone Number' => $customer->phone_number, + 'Restaurant Name' => optional($role->restaurant)->name ?? 'NA', + 'Restaurant Address' => optional($role->restaurant)->address ?? 'NA', + ]; + }); }); Log::info('Fetched customer data: ' . $mappedCustomers->toJson()); @@ -66,7 +134,9 @@ class restaurant_export_selected implements FromCollection, WithHeadings 'Email Address', 'Date of Birth', 'State Name', - 'Phone Number' + 'Phone Number', + 'Restaurant Name', + 'Restaurant Address' ]; } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/Admin/RestaurantAppController.php b/app/Http/Controllers/Admin/RestaurantAppController.php index 122da8d..6c2a738 100644 --- a/app/Http/Controllers/Admin/RestaurantAppController.php +++ b/app/Http/Controllers/Admin/RestaurantAppController.php @@ -54,7 +54,7 @@ class RestaurantAppController extends Controller ->orderBy('created_at', 'desc') ->get(); } - +// return $restaurant_users; foreach ($restaurant_users as $user) { $restaurantIds = $user->getresturant->pluck('restaurant_xid')->toArray(); @@ -305,4 +305,10 @@ class RestaurantAppController extends Controller return response()->json(['error' => 'Export failed. Something went wrong.'], 500); } } + + + + + + } diff --git a/resources/views/Admin/pages/manage_users/restaurants_app/restaurants_users.blade.php b/resources/views/Admin/pages/manage_users/restaurants_app/restaurants_users.blade.php index 7d97a7e..617d5ee 100644 --- a/resources/views/Admin/pages/manage_users/restaurants_app/restaurants_users.blade.php +++ b/resources/views/Admin/pages/manage_users/restaurants_app/restaurants_users.blade.php @@ -587,7 +587,7 @@ $(function (e) { location.reload(); }, 5000); // Adjust the timeout as needed } else { - toastr.error("Please select at least one customer to download."); + toastr.error("Please select at least one restaurant to download."); } });