station page export csv works
This commit is contained in:
parent
9809ef792a
commit
b350d6ad4a
|
@ -16,8 +16,8 @@ class ReportsController extends Controller
|
|||
$this->apiBaseUrl = env('EXTERNAL_API_URL', 'http://localhost:8081/api/');
|
||||
}
|
||||
|
||||
private function makeApiRequest($endpoint, $params)
|
||||
{
|
||||
private function makeApiRequest($endpoint, $params, $isDownload = false)
|
||||
{
|
||||
try {
|
||||
$user = Session::get('user');
|
||||
$accessToken = $user['access_token'] ?? null;
|
||||
|
@ -27,25 +27,25 @@ class ReportsController extends Controller
|
|||
return redirect()->route('login')->with('error', 'Please log in to view reports.');
|
||||
}
|
||||
|
||||
// Ensure proper URL formation
|
||||
$baseUrl = rtrim($this->apiBaseUrl, '/');
|
||||
$endpoint = ltrim($endpoint, '/');
|
||||
$fullUrl = "{$baseUrl}/{$endpoint}";
|
||||
|
||||
Log::info("Making API request to: {$fullUrl}", ['params' => $params]);
|
||||
$response = Http::withHeaders([
|
||||
'Accept' => 'application/json',
|
||||
$client = Http::withHeaders([
|
||||
'Accept' => $isDownload ? 'text/csv' : 'application/json',
|
||||
'Authorization' => 'Bearer ' . $accessToken,
|
||||
])->timeout(30)->get($fullUrl, $params);
|
||||
])->timeout(30);
|
||||
|
||||
$response = $client->get($fullUrl, $params);
|
||||
|
||||
Log::info("API response status: {$response->status()}", [
|
||||
'endpoint' => $endpoint,
|
||||
'response_body' => $response->body(),
|
||||
'headers' => $response->headers()
|
||||
]);
|
||||
|
||||
if ($response->status() === 401 || $response->status() === 403) {
|
||||
Log::warning("Unauthorized or Forbidden API response for {$endpoint}", ['response' => $response->json()]);
|
||||
Log::warning("Unauthorized or Forbidden API response for {$endpoint}", ['response' => $response->body()]);
|
||||
return redirect()->route('login')->with('error', 'Your session has expired. Please log in again.');
|
||||
}
|
||||
|
||||
|
@ -58,6 +58,10 @@ class ReportsController extends Controller
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($isDownload) {
|
||||
return $response; // Return raw response for file download
|
||||
}
|
||||
|
||||
return $response->json();
|
||||
} catch (\Illuminate\Http\Client\ConnectionException $e) {
|
||||
Log::error("Connection error during API request to {$endpoint}", [
|
||||
|
@ -72,7 +76,7 @@ class ReportsController extends Controller
|
|||
]);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public function mobileUsage(Request $request)
|
||||
{
|
||||
$params = [
|
||||
|
@ -258,4 +262,31 @@ class ReportsController extends Controller
|
|||
|
||||
return view('pages.reports.top-up-usage-report', compact('topUpData', 'currentPage', 'lastPage', 'total', 'errorMessage'));
|
||||
}
|
||||
|
||||
public function exportStationRating(Request $request)
|
||||
{
|
||||
$params = [
|
||||
'date_start' => $request->input('date_start'),
|
||||
'date_end' => $request->input('date_end'),
|
||||
'_search' => $request->input('_search'),
|
||||
'sorting' => $request->input('sort', 'date|desc'),
|
||||
];
|
||||
|
||||
$response = $this->makeApiRequest('cms/reportStationRatingsExport', $params, true);
|
||||
|
||||
if (is_a($response, '\Illuminate\Http\RedirectResponse')) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
if (!$response) {
|
||||
Log::warning("Failed to export station rating data", ['params' => $params]);
|
||||
return redirect()->back()->with('error', 'Failed to export CSV.');
|
||||
}
|
||||
|
||||
$filename = "reports_station_rating" . date("mdY") . ".csv";
|
||||
|
||||
return response($response->body(), 200)
|
||||
->header('Content-Type', 'text/csv')
|
||||
->header('Content-Disposition', "attachment; filename=\"$filename\"");
|
||||
}
|
||||
}
|
|
@ -83,7 +83,8 @@
|
|||
if (sortColumn && sortDirection) {
|
||||
url.searchParams.set('sort', `${sortColumn}|${sortDirection}`);
|
||||
}
|
||||
console.log('Exporting CSV to:', url.toString());
|
||||
window.location.href = url.toString();
|
||||
});
|
||||
</script>
|
||||
</script>
|
||||
@endsection
|
|
@ -202,7 +202,7 @@ Route::get('/mobile-usage-report', [ReportsController::class, 'mobileUsage'])->n
|
|||
Route::get('/registration-report', [ReportsController::class, 'registration'])->name('registration-report');
|
||||
Route::get('/station-rating-report', [ReportsController::class, 'stationRating'])->name('station-rating-report');
|
||||
Route::get('/top-up-usage-report', [ReportsController::class, 'topUp'])->name('top-up-usage-report');
|
||||
|
||||
Route::get('/station-rating-reportExport', [ReportsController::class, 'exportStationRating'])->name('station-rating-reportExport');
|
||||
//Station
|
||||
Route::get('/station-management', [StationController::class, 'index'])->name('stations');
|
||||
Route::get('/stations/create', [StationController::class, 'create'])->name('stations.create');
|
||||
|
|
Loading…
Reference in New Issue