ReportExcel

This commit is contained in:
sayaliparab
2024-07-15 19:11:04 +05:30
parent 63b0a6a990
commit 57c4820219
10 changed files with 1034 additions and 486 deletions

View File

@@ -17,10 +17,9 @@ class DashboardExportUser implements FromArray, WithHeadings
public function array(): array public function array(): array
{ {
return array_map(function ($transaction) use (&$serial) { return array_map(function ($transaction) {
return [ return [
// 'ID' => $transaction['id'], 'ID' => $transaction['id'],
'Sr No.'=> $serial++,
'Customer Name' => $transaction['iam_principal']['first_name'] . ' ' . $transaction['iam_principal']['last_name'], 'Customer Name' => $transaction['iam_principal']['first_name'] . ' ' . $transaction['iam_principal']['last_name'],
'Customer ID' => $transaction['iam_principal']['id'], 'Customer ID' => $transaction['iam_principal']['id'],
'Amount' => '$' . number_format($transaction['amount'], 2), 'Amount' => '$' . number_format($transaction['amount'], 2),
@@ -30,7 +29,9 @@ class DashboardExportUser implements FromArray, WithHeadings
'Subscription Period Start' => Carbon::parse($transaction['current_period_start'])->format('m/d/Y h:i A'), 'Subscription Period Start' => Carbon::parse($transaction['current_period_start'])->format('m/d/Y h:i A'),
'Subscription Period End' => Carbon::parse($transaction['current_period_end'])->format('m/d/Y h:i A'), 'Subscription Period End' => Carbon::parse($transaction['current_period_end'])->format('m/d/Y h:i A'),
'Next Payment Date' => Carbon::parse($transaction['next_payment_date'])->format('m/d/Y h:i A'), 'Next Payment Date' => Carbon::parse($transaction['next_payment_date'])->format('m/d/Y h:i A'),
// 'Subscription Period Start' => $transaction['current_period_start'],
// 'Subscription Period End' => $transaction['current_period_end'],
// 'Next Payment Date' => $transaction['next_payment_date'],
]; ];
}, $this->data); }, $this->data);
} }
@@ -38,7 +39,7 @@ class DashboardExportUser implements FromArray, WithHeadings
public function headings(): array public function headings(): array
{ {
return [ return [
'Sr No.', 'ID',
'Customer Name', 'Customer Name',
'Customer ID', 'Customer ID',
'Amount', 'Amount',

View File

@@ -0,0 +1,184 @@
<?php
namespace App\Exports;
use App\Models\IamPrincipal;
use App\Models\RedeemRestaurant;
use App\Models\Subscriptions;
use App\Models\ReferralUsers;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class ExportReports implements FromView
{
protected $reportType;
protected $states;
protected $startDate;
protected $endDate;
protected $restaurants;
public function __construct($reportType, $states, $startDate, $endDate, $restaurants = [])
{
$this->reportType = $reportType;
$this->states = $states;
$this->startDate = $startDate;
$this->endDate = $endDate;
$this->restaurants = $restaurants;
}
public function view(): View
{
$data = collect();
if ($this->reportType === 'Total Subscribed') {
$query = Subscriptions::query();
if (!empty($this->states)) {
$query->whereHas('iamPrincipal', function ($q) {
$q->whereIn('state_xid', $this->states);
});
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
// dd($data);
} elseif ($this->reportType === 'Total Users') {
$query = IamPrincipal::query()->where('principal_type_xid',3);
if (!empty($this->states)) {
$query->whereIn('state_xid', $this->states);
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
} elseif ($this->reportType === 'Redemptions') {
$query = RedeemRestaurant::with(['restaurant', 'customer'])->where('is_redeem', 0);
if (!empty($this->states)) {
$query->whereHas('customer', function ($q) {
$q->whereIn('state_xid', $this->states);
});
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
} elseif ($this->reportType === 'Redemptions for Specific Restaurants') {
$query = RedeemRestaurant::with('restaurant', 'customer');
if (!empty($this->restaurants)) {
$query->whereIn('manage_restaurants_xid', $this->restaurants);
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
} elseif ($this->reportType === 'Subscriptions Cancelled') {
$query = Subscriptions::query()->where('is_cancelled_subscription', 1);
if (!empty($this->states)) {
$query->whereHas('iamPrincipal', function ($q) {
$q->whereIn('state_xid', $this->states);
});
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
}
elseif ($this->reportType === 'Subscriptions Cancelled') {
$query = Subscriptions::with('iamPrincipal')->where('is_cancelled_subscription', 1);
if (!empty($this->states)) {
$query->whereHas('iamPrincipal', function ($q) {
$q->whereIn('state_xid', $this->states);
});
}
if ($this->startDate) {
$query->whereDate('created_at', '>=', $this->startDate);
}
if ($this->endDate) {
$query->whereDate('created_at', '<=', $this->endDate);
}
$data = $query->get();
}
elseif ($this->reportType === 'Referrals Made') {
$query = ReferralUsers::query()
->with(['referredUser', 'referredUser.state'])
->whereHas('referredUser', function ($query) {
$query->whereIn('state_xid', $this->states);
});
if (!empty($this->startDate)) {
$query->whereDate('referred_date_time', '>=', $this->startDate);
}
if (!empty($this->endDate)) {
$query->whereDate('referred_date_time', '<=', $this->endDate);
}
$data = $query->get();
}
elseif ($this->reportType === 'Referees Joined') {
$query = ReferralUsers::query()
->with(['refeersUser', 'refeersUser.state'])
->whereHas('refeersUser', function ($query) {
$query->whereIn('state_xid', $this->states);
});
if (!empty($this->startDate)) {
$query->whereDate('referred_date_time', '>=', $this->startDate);
}
if (!empty($this->endDate)) {
$query->whereDate('referred_date_time', '<=', $this->endDate);
}
$data = $query->get();
}
return view('exports.reports', [
'data' => $data,
'reportType' => $this->reportType
]);
return $data;
}
}

View File

@@ -9,7 +9,7 @@ use App\Models\ReferralUsers;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView; use Maatwebsite\Excel\Concerns\FromView;
class ReportExport implements FromView class ReportExports implements FromView
{ {
protected $reportType; protected $reportType;
protected $states; protected $states;
@@ -48,6 +48,7 @@ class ReportExport implements FromView
} }
$data = $query->get(); $data = $query->get();
// dd($data);
} elseif ($this->reportType === 'Total Users') { } elseif ($this->reportType === 'Total Users') {
$query = IamPrincipal::query()->where('principal_type_xid',3); $query = IamPrincipal::query()->where('principal_type_xid',3);

View File

@@ -7,10 +7,10 @@ use Illuminate\Http\Request;
use App\Models\ManageState; use App\Models\ManageState;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ReportExport; use App\Exports\ReportExports;
use App\Models\ManageRestaurant; use App\Models\ManageRestaurant;
use App\Models\ReferralUsers; use App\Models\ReferralUsers;
use App\Exports\ExportReports;
class ManageReportsController extends Controller class ManageReportsController extends Controller
{ {
@@ -41,8 +41,39 @@ class ManageReportsController extends Controller
* Use : To download the excel. * Use : To download the excel.
*/ */
public function exportReports(Request $request) // public function exportReports(Request $request)
// {
// $reportType = $request->input('reportType');
// $states = $request->input('states');
// $restaurants = $request->input('restaurants', []);
// $startDate = $request->input('startDate');
// $endDate = $request->input('endDate');
// return Excel::download(new ReportExport($reportType, $states, $startDate, $endDate, $restaurants), 'report.xlsx');
// }
// public function exportReports(Request $request)
// {
// // dd($request->all());
// $reportType = $request->input('reportType');
// $states = $request->input('states');
// $restaurants = $request->input('restaurants', []);
// $startDate = $request->input('startDate');
// $endDate = $request->input('endDate');
// try {
// return Excel::download(new ReportExports($reportType, $states, $startDate, $endDate, $restaurants), 'report.xlsx');
// } catch (\Exception $e) {
// // Log the error for debugging
// \Log::error('Export failed: ' . $e->getMessage());
// return response()->json(['error' => 'Export failed. Something went wrong.'], 500);
// }
// }
public function exportReport(Request $request)
{ {
// dd($request->all());
$reportType = $request->input('reportType'); $reportType = $request->input('reportType');
$states = $request->input('states'); $states = $request->input('states');
$restaurants = $request->input('restaurants', []); $restaurants = $request->input('restaurants', []);
@@ -50,9 +81,14 @@ public function exportReports(Request $request)
$startDate = $request->input('startDate'); $startDate = $request->input('startDate');
$endDate = $request->input('endDate'); $endDate = $request->input('endDate');
return Excel::download(new ReportExport($reportType, $states, $startDate, $endDate, $restaurants), 'report.xlsx'); try {
return Excel::download(new ExportReports($reportType, $states, $startDate, $endDate, $restaurants), 'reports.Xlsx');
} catch (\Exception $e) {
\Log::error('Export failed: ' . $e->getMessage());
\Log::error('Stack trace: ' . $e->getTraceAsString());
return response()->json(['error' => 'Export failed. Something went wrong.'], 500);
}
} }
} }

View File

@@ -16,7 +16,8 @@
"laravel/sanctum": "^4.0", "laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9", "laravel/tinker": "^2.9",
"livewire/livewire": "^3.0", "livewire/livewire": "^3.0",
"maatwebsite/excel": "^3.1", "maatwebsite/excel": "3.1.55",
"phpoffice/phpspreadsheet": "1.29.0",
"stripe/stripe-php": "^15.0", "stripe/stripe-php": "^15.0",
"tymon/jwt-auth": "^2.1" "tymon/jwt-auth": "^2.1"
}, },

1063
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -17,7 +17,7 @@ $currentPage = 'manage-reports';
</div> </div>
</div> </div>
</div> </div>
<form id="reportForm" action="{{ route('export.reports') }}" method="POST"> <form id="reportForm" action="{{route('reports.export')}}" method="POST">
@csrf @csrf
<div class="row widget-content widget-content-area br-8 position-btn m-auto py-3" style="overflow: auto;"> <div class="row widget-content widget-content-area br-8 position-btn m-auto py-3" style="overflow: auto;">
<div class="col-6"> <div class="col-6">

View File

@@ -8,9 +8,9 @@
<body> <body>
<h1>{{ $reportType }} Report</h1> <h1>{{ $reportType }} Report</h1>
@if ($data->isEmpty()) <!-- @if ($data->isEmpty())
<p>No data available</p> <p>No data available</p>
@else @else -->
@if ($reportType === 'Total Subscribed') @if ($reportType === 'Total Subscribed')
<table> <table>

View File

@@ -0,0 +1,201 @@
<!DOCTYPE html>
<html>
<head>
<title>Report</title>
</head>
<body>
<h1>{{ $reportType }} Report</h1>
<!-- @if ($data->isEmpty())
<p>No data available</p>
@else -->
@if ($reportType === 'Total Subscribed')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Subscription ID</th>
<th>User Name</th>
<th>State</th>
<th>Subscription Date Time</th>
</tr>
</thead>
<tbody>
@foreach($data as $subscription)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $subscription->id }}</td>
<td>{{ $subscription->iamPrincipal->first_name ?? 'N/A' }} {{ $subscription->iamPrincipal->last_name ?? 'N/A' }} </td>
<td>{{ $subscription->iamPrincipal->state->name ?? 'N/A' }}</td>
<td>{{\Carbon\Carbon::parse($subscription->created_at)->format('m-d-Y H:i')}}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Total Users')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>State</th>
<th>Customer Join Date Time</th>
</tr>
</thead>
<tbody>
@foreach($data as $user)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $user->id }}</td>
<td>{{ $user->first_name }} {{ $user->last_name }}</td>
<td>{{ $user->state->name }}</td>
<td>{{ \Carbon\Carbon::parse($user->created_at)->format('m-d-Y H:i') }}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Redemptions')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Redemption ID</th>
<th>Restaurant Name</th>
<th>Customer Name</th>
<th>State</th>
<th>Redemption Date Time</th>
</tr>
</thead>
<tbody>
@foreach($data as $redemption)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $redemption->id }}</td>
<td>{{ $redemption->restaurant->name }}</td>
<td>{{ $redemption->customer->first_name }} {{ $redemption->customer->last_name }}</td>
<td>{{ $redemption->customer->state->name }}</td>
<td>{{\Carbon\Carbon::parse($redemption->created_at)->format('m-d-Y H:i')}} </td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Redemptions for Specific Restaurants')
<table>
<thead>
<tr>
<th>Sr No.</th>
<th>Redemption ID</th>
<th>Restaurant Name</th>
<th>Customer Name</th>
<th>State</th>
<th>Redemption Date Time</th>
</tr>
</thead>
<tbody>
@foreach($data as $redemption)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $redemption->id }}</td>
<td>{{ $redemption->restaurant->name }}</td>
<td>{{ $redemption->customer->first_name }} {{ $redemption->customer->last_name }}</td>
<td>{{ $redemption->customer->state->name }}</td>
<td>{{\Carbon\Carbon::parse($redemption->created_at)->format('m-d-Y H:i')}} </td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Subscriptions Cancelled')
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>State</th>
<th>Subscription Status</th>
<th>Subscription Cancel Date Time</th>
</tr>
</thead>
<tbody>
@foreach($data as $item)
<tr>
<td>{{ $item->iamPrincipal->first_name }} {{ $item->iamPrincipal->last_name }}</td>
<td>{{ $item->iamPrincipal->email_address }}</td>
<td>{{ $item->iamPrincipal->state->name }}</td>
<td>{{ $item->subscription_status }}</td>
<td>{{ \Carbon\Carbon::parse($item->cancelled_at)->format('m-d-Y H:i') }}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Referrals Made')
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>State</th>
<th>Referred Date Time</th>
</tr>
</thead>
<tbody>
@foreach($data as $referral)
<tr>
<td>{{ $referral->id }}</td>
<td>{{ $referral->referredUser->first_name }} {{ $referral->referredUser->last_name }}</td>
<td>{{ $referral->referredUser->email_address }}</td>
<td>{{ $referral->referredUser->phone_number}}</td>
<td>{{ $referral->referredUser->state->name }}</td>
<td> {{ \Carbon\Carbon::parse($referral->referred_date_time)->format('m-d-Y H:i') }}</td>
</tr>
@endforeach
</tbody>
</table>
@elseif ($reportType === 'Referees Joined')
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Phone number</th>
<th>State</th>
<th>Referred Date Time</th>
</tr>
</thead>
<tbody>
@foreach($data as $referees)
<tr>
<td>{{$referees->id }}</td>
<td>{{ $referees->refeersUser->first_name }} {{ $referees->refeersUser->last_name }}</td>
<td>{{ $referees->refeersUser->email_address }}</td>
<td>{{ $referees->refeersUser->phone_number}}</td>
<td>{{ $referees->refeersUser->state->name }}</td>
<td> {{ \Carbon\Carbon::parse($referees->referred_date_time)->format('m-d-Y H:i') }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
<!-- @endif -->
</body>
</html>

View File

@@ -176,7 +176,8 @@ Route::group(['middleware' => ['checkStatus']], function () {
//*******************************************************manage reports******************************************************** //*******************************************************manage reports********************************************************
Route::get('/manage-reports', [ManageReportsController::class, 'index'])->name('manage.reports'); Route::get('/manage-reports', [ManageReportsController::class, 'index'])->name('manage.reports');
Route::post('/export-reports', [ManageReportsController::class, 'exportReports'])->name('export.reports'); // Route::post('/export-reports', [ManageReportsController::class, 'exportReports'])->name('export.reports');
Route::post('/export-reports', [ManageReportsController::class, 'exportReport'])->name('reports.export');
//*******************************************************manage feedback******************************************************** //*******************************************************manage feedback********************************************************
Route::get('/manage-feedback', [ManageFeedbackController::class, 'index'])->name('manage.feedback'); Route::get('/manage-feedback', [ManageFeedbackController::class, 'index'])->name('manage.feedback');