From 10aa81cc0856db14f2542067c21bc714a90dae29 Mon Sep 17 00:00:00 2001 From: sayaliparab Date: Mon, 8 Jul 2024 20:11:31 +0530 Subject: [PATCH] dashoabrdExcel --- app/Exports/DashboardExportUser.php | 44 ++ app/Exports/DashboardSelectedExportUser.php | 50 ++ app/Exports/customer_export.php | 79 +- app/Exports/customer_export_selected.php | 34 +- .../Controllers/Admin/DashboardController.php | 28 + .../Admin/ManageCustomerController.php | 2 +- app/Models/Subscriptions.php | 12 +- resources/views/Admin/dashboard.blade.php | 731 +++++++++--------- routes/web.php | 1 + 9 files changed, 565 insertions(+), 416 deletions(-) create mode 100644 app/Exports/DashboardExportUser.php create mode 100644 app/Exports/DashboardSelectedExportUser.php diff --git a/app/Exports/DashboardExportUser.php b/app/Exports/DashboardExportUser.php new file mode 100644 index 0000000..0d693e4 --- /dev/null +++ b/app/Exports/DashboardExportUser.php @@ -0,0 +1,44 @@ +recentTransactions = $recentTransactions; + } + + public function collection() + { + $serial = 1; + $mappedTransactions = collect($this->recentTransactions)->map(function ($transaction) use (&$serial) { + return [ + 'Sr No.' => $serial++, // Increment serial number + 'Name' => $transaction['subscription']['first_name'] . ' ' . $transaction['subscription']['last_name'], + 'Customer Id' => $transaction['subscription']['id'], + 'Amount' => $transaction['amount'], + 'Payment Details' => $transaction['stripe_customer_id'], // Adjust as needed + ]; + }); + + return new Collection($mappedTransactions); + } + + public function headings(): array + { + return [ + 'Sr No.', + 'Name', + 'Customer Id', + 'Amount', + 'Payment Details', + ]; + } +} diff --git a/app/Exports/DashboardSelectedExportUser.php b/app/Exports/DashboardSelectedExportUser.php new file mode 100644 index 0000000..8f98485 --- /dev/null +++ b/app/Exports/DashboardSelectedExportUser.php @@ -0,0 +1,50 @@ +ids = explode(',', $ids); + } + + public function collection() + { + $selectedTransactions = Subscriptions::whereIn('id', $this->ids) + ->with('subscription') + ->get() + ->toArray(); + + $serial = 1; + $mappedTransactions = collect($selectedTransactions)->map(function ($transaction) use (&$serial) { + return [ + 'Sr No.' => $serial++, // Increment serial number + 'Name' => $transaction['subscription']['first_name'] . ' ' . $transaction['subscription']['last_name'], + 'Customer Id' => $transaction['subscription']['id'], + 'Amount' => $transaction['amount'], + 'Payment Details' => $transaction['stripe_customer_id'], // Adjust as needed + ]; + }); + + return new Collection($mappedTransactions); + } + + public function headings(): array + { + return [ + 'Sr No.', + 'Name', + 'Customer Id', + 'Amount', + 'Payment Details', + ]; + } +} diff --git a/app/Exports/customer_export.php b/app/Exports/customer_export.php index 9c7a37f..56be1e1 100644 --- a/app/Exports/customer_export.php +++ b/app/Exports/customer_export.php @@ -5,59 +5,54 @@ namespace App\Exports; use Maatwebsite\Excel\Concerns\FromCollection; use App\Models\IamPrincipal; use Maatwebsite\Excel\Concerns\WithHeadings; +use Illuminate\Support\Collection; - - -class customer_export implements FromCollection , WithHeadings +class customer_export implements FromCollection, WithHeadings { /** - * @return \Illuminate\Support\Collection - */ + * @return \Illuminate\Support\Collection + */ + public function collection() + { + $customers = IamPrincipal::where('principal_type_xid', 3) + ->select( + 'id', + 'first_name', + 'last_name', + 'email_address', + 'date_of_birth', + 'state_xid', + 'phone_number' + ) + ->get(); - - public function collection(){ - return IamPrincipal::where('principal_type_xid',3) - ->select('id', - 'first_name', - 'last_name', - 'email_address', - 'date_of_birth', - 'state_xid', - 'phone_number' - ) - ->get() - ->map(function ($customer) { + $serial = 1; + return $customers->map(function ($customer) use (&$serial) { return [ - 'id' => $customer->id, - 'first_name' => $customer->first_name, - 'last_name' => $customer->last_name, + 'Sr No.' => $serial++, // Increment serial number + '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 ?? '', // Access the state name and handle null - 'phone_number' => $customer->phone_number, + 'date_of_birth' => \Carbon\Carbon::parse($customer->date_of_birth)->format('m/d/Y'), + 'state_xid' => $customer->state->name ?? '', // Access the state name and handle null + 'phone_number' => $customer->phone_number, ]; }); - - } - //function header in excel - public function headings(): array - { - return [ + // 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' + 'First Name', + 'Last Name', + 'Email Address', + 'Date of Birth', + 'State Name', + 'Phone Number', ]; } - - - - - - } diff --git a/app/Exports/customer_export_selected.php b/app/Exports/customer_export_selected.php index e44c203..e8d9b69 100644 --- a/app/Exports/customer_export_selected.php +++ b/app/Exports/customer_export_selected.php @@ -25,7 +25,7 @@ class customer_export_selected implements FromCollection, WithHeadings // Log the ids for debugging purposes Log::info('Fetching data for IDs: ' . implode(',', $this->ids)); - $selected_customer = IamPrincipal::whereIn('id', $this->ids) + $selected_customers = IamPrincipal::whereIn('id', $this->ids) ->with('state:id,name') // Eager load the state relationship ->orderBy('id', 'desc') ->select( @@ -37,28 +37,32 @@ class customer_export_selected implements FromCollection, WithHeadings 'state_xid', 'phone_number' ) - ->get() - ->map(function ($customer) { - return [ - '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'), // Format the date - 'state_xid' => $customer->state->name ?? '', // Access the state name and handle null - 'phone_number' => $customer->phone_number, - ]; - }); + ->get(); + + $serial = 1; + $mappedCustomers = $selected_customers->map(function ($customer) use (&$serial) { + return [ + 'Sr No.' => $serial++, // Increment serial number + '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'), // Format the date + 'state_xid' => $customer->state->name ?? '', // Access the state name and handle null + 'phone_number' => $customer->phone_number, + ]; + }); // Log the fetched data for debugging purposes - Log::info('Fetched customer data: ' . $selected_customer->toJson()); + Log::info('Fetched customer data: ' . $mappedCustomers->toJson()); - return new Collection($selected_customer); + return new Collection($mappedCustomers); } public function headings(): array { return [ + 'Sr No.', 'User Id', 'First Name', 'Last Name', diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index 0480db5..99808a3 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -8,6 +8,10 @@ use App\Models\IamPrincipal; use Illuminate\Support\Facades\DB; use App\Models\ManageRestaurant; use App\Models\Subscriptions; +use App\Exports\DashboardExportUser; +use App\Exports\DashboardSelectedExportUser; +use Maatwebsite\Excel\Facades\Excel; + class DashboardController extends Controller { @@ -93,4 +97,28 @@ class DashboardController extends Controller 'recent_transaction' )); } + + + /* + Created By : Sayali Parab + Created at : 08 July 2024 + Use : To Get Excel. + */ + public function exportRecentTransactions(Request $request) + { + try { + if ($request->has('all_id')) { + $recentTransaction = Subscriptions::with('subscription')->get()->toArray(); + // return $recentTransaction; + return Excel::download(new DashboardExportUser($recentTransaction), 'recent_transactions.xlsx'); + } + + $ids = $request->input('selected_id'); + $fileName = 'selected_customer_transaction_data.xlsx'; + return Excel::download(new DashboardSelectedExportUser($ids), $fileName); + } catch (\Exception $e) { + return response()->json(['error' => 'Export failed. Something went wrong.'], 500); + } + } } + diff --git a/app/Http/Controllers/Admin/ManageCustomerController.php b/app/Http/Controllers/Admin/ManageCustomerController.php index e56993e..ff54382 100644 --- a/app/Http/Controllers/Admin/ManageCustomerController.php +++ b/app/Http/Controllers/Admin/ManageCustomerController.php @@ -244,7 +244,7 @@ class ManageCustomerController extends Controller { try { if ($request->has('all_id')) { - return Excel::download(new customer_export, 'Passport_data.xlsx'); + return Excel::download(new customer_export, 'customer_data.xlsx'); } $ids = $request->selected_id; diff --git a/app/Models/Subscriptions.php b/app/Models/Subscriptions.php index bdd96eb..7dfe187 100644 --- a/app/Models/Subscriptions.php +++ b/app/Models/Subscriptions.php @@ -12,6 +12,7 @@ class Subscriptions extends Model protected $table = 'subscriptions'; protected $guarded = []; + public function subscription() { return $this->belongsTo(IamPrincipal::class, 'iam_principal_xid', 'id'); @@ -19,11 +20,8 @@ class Subscriptions extends Model -public function iamPrincipal() -{ - return $this->belongsTo(IamPrincipal::class, 'iam_principal_xid', 'id'); + public function iamPrincipal() + { + return $this->belongsTo(IamPrincipal::class, 'iam_principal_xid', 'id'); + } } - -} - - diff --git a/resources/views/Admin/dashboard.blade.php b/resources/views/Admin/dashboard.blade.php index 8e23a70..56a9fdf 100644 --- a/resources/views/Admin/dashboard.blade.php +++ b/resources/views/Admin/dashboard.blade.php @@ -1,30 +1,30 @@ @extends('Admin.layouts.master') @section('content') - @php - $currentPage = 'dashboard'; - @endphp -
-
-
-
-
-
-
Dashboard
+@php +$currentPage = 'dashboard'; +@endphp +
+
+
+
+
+
+
Dashboard
+
+
+
+
+ -
- - {{--
+ {{--
--}} - +
+
+
- -
No of Restaurants
-

{{ $restaurantCount }}

-
+
+
User Graph
+ +
+
-
-
-
-
-
-
-
User Graph
- -
-
-
-
-
-
- {{--
+ {{--
@@ -99,117 +99,100 @@
--}} -
-
-
-
-
-
Recent Transactions
-
- @csrf - - -
- - - - - - - - - - - - - @foreach ($recent_transaction as $recent_transactions) - - - - - - - - - @endforeach - -
-
- -
-
Sr noNameCustomer IdAmountpayment Details
-
- -
-
{{ $loop->iteration }} - {{ $recent_transactions['subscription']['first_name'] }} - {{ $recent_transactions['subscription']['last_name'] }} - - {{ $recent_transactions['subscription']['id'] }}$ {{ $recent_transactions['amount'] }} - View -
-
-
-
+
+
+
+
+
+
Recent Transactions
+
+ @csrf + + +
+ + + + + + + + + + + + + @foreach ($recent_transaction as $recent_transactions) + + + + + + + + + @endforeach + +
+
+ +
+
Sr noNameCustomer IdAmountpayment Details
+
+ +
+
{{ $loop->iteration }} + {{ $recent_transactions['subscription']['first_name'] }} + {{ $recent_transactions['subscription']['last_name'] }} + + + {{ $recent_transactions['subscription']['id'] }} + $ {{ $recent_transactions['amount'] }} + View +
+
+
+
- -