cms-frontend/app/Http/Controllers/Api/ReportsController.php

136 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\Api\ApiService;
use Illuminate\Http\Request;
class ReportsController extends Controller
{
protected ApiService $apiService;
public function __construct(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function registrationReport(Request $request)
{
$filters = $request->validate([
'start_date' => 'nullable|date',
'end_date' => 'nullable|date|after_or_equal:start_date',
'member_type' => 'nullable|string'
]);
try {
$response = $this->apiService->get('/reports/registrations', $filters);
if ($response->successful()) {
return view('pages.reports.registration-report', [
'report' => $response->json()['data']
]);
}
return back()->with('error', 'Unable to fetch registration report.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function topUpUsageReport(Request $request)
{
$filters = $request->validate([
'start_date' => 'nullable|date',
'end_date' => 'nullable|date|after_or_equal:start_date',
'payment_method' => 'nullable|string'
]);
try {
$response = $this->apiService->get('/reports/top-up-usage', $filters);
if ($response->successful()) {
return view('pages.reports.top-up-usage-report', [
'report' => $response->json()['data']
]);
}
return back()->with('error', 'Unable to fetch top-up usage report.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function mobileUsageReport(Request $request)
{
$filters = $request->validate([
'start_date' => 'nullable|date',
'end_date' => 'nullable|date|after_or_equal:start_date',
'platform' => 'nullable|string'
]);
try {
$response = $this->apiService->get('/reports/mobile-usage', $filters);
if ($response->successful()) {
return view('pages.reports.mobile-usage-report', [
'report' => $response->json()['data']
]);
}
return back()->with('error', 'Unable to fetch mobile usage report.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function stationRatingReport(Request $request)
{
$filters = $request->validate([
'start_date' => 'nullable|date',
'end_date' => 'nullable|date|after_or_equal:start_date',
'station_id' => 'nullable|integer',
'rating' => 'nullable|integer|min:1|max:5'
]);
try {
$response = $this->apiService->get('/reports/station-ratings', $filters);
if ($response->successful()) {
return view('pages.reports.station-rating-report', [
'report' => $response->json()['data']
]);
}
return back()->with('error', 'Unable to fetch station rating report.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function exportReport(Request $request, $reportType)
{
$filters = $request->validate([
'start_date' => 'nullable|date',
'end_date' => 'nullable|date|after_or_equal:start_date',
'format' => 'required|string|in:csv,excel,pdf'
]);
try {
$response = $this->apiService->get("/reports/{$reportType}/export", $filters);
if ($response->successful()) {
return response()->streamDownload(
function () use ($response) {
echo $response->body();
},
"report.{$filters['format']}"
);
}
return back()->with('error', 'Unable to export report.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
}