restExportChanges

This commit is contained in:
sayaliparab
2024-07-17 17:55:00 +05:30
parent 889da666bb
commit f35c960dfa
4 changed files with 165 additions and 41 deletions

View File

@@ -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',
];
}
}
}

View File

@@ -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'
];
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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.");
}
});