201 lines
6.7 KiB
PHP
201 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Api\ApiService;
|
|
use Illuminate\Http\Request;
|
|
|
|
class FuelPriceController extends Controller
|
|
{
|
|
protected ApiService $apiService;
|
|
|
|
public function __construct(ApiService $apiService)
|
|
{
|
|
$this->apiService = $apiService;
|
|
}
|
|
|
|
public function onDemand()
|
|
{
|
|
try {
|
|
$response = $this->apiService->get('/fuel-prices/current');
|
|
$stationsResponse = $this->apiService->get('/stations');
|
|
$fuelTypesResponse = $this->apiService->get('/fuel-types');
|
|
|
|
if ($response->successful() && $stationsResponse->successful() && $fuelTypesResponse->successful()) {
|
|
return view('pages.fuel-price-on-demand', [
|
|
'prices' => $response->json()['data'],
|
|
'stations' => $stationsResponse->json()['data'],
|
|
'fuelTypes' => $fuelTypesResponse->json()['data']
|
|
]);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to fetch fuel prices.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function updateOnDemand(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'prices' => 'required|array',
|
|
'prices.*.*' => 'nullable|numeric|min:0'
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->post('/fuel-prices/update', $validated);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('fuel-price.on-demand')
|
|
->with('success', 'Fuel prices updated successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to update fuel prices.');
|
|
}
|
|
}
|
|
|
|
public function importPrices(Request $request)
|
|
{
|
|
$request->validate([
|
|
'csv_file' => 'required|file|mimes:csv,txt'
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->post('/fuel-prices/import', [
|
|
'file' => $request->file('csv_file')
|
|
], true);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('fuel-price.on-demand')
|
|
->with('success', 'Fuel prices imported successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to import fuel prices.');
|
|
}
|
|
}
|
|
|
|
public function exportPrices()
|
|
{
|
|
try {
|
|
$response = $this->apiService->get('/fuel-prices/export');
|
|
|
|
if ($response->successful()) {
|
|
return response()->streamDownload(
|
|
function () use ($response) {
|
|
echo $response->body();
|
|
},
|
|
'fuel-prices.csv'
|
|
);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to export fuel prices.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function schedule()
|
|
{
|
|
try {
|
|
$response = $this->apiService->get('/fuel-prices/schedule');
|
|
$stationsResponse = $this->apiService->get('/stations');
|
|
$fuelTypesResponse = $this->apiService->get('/fuel-types');
|
|
|
|
if ($response->successful() && $stationsResponse->successful() && $fuelTypesResponse->successful()) {
|
|
return view('pages.fuel-price-schedule', [
|
|
'schedules' => $response->json()['data'],
|
|
'stations' => $stationsResponse->json()['data'],
|
|
'fuelTypes' => $fuelTypesResponse->json()['data']
|
|
]);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to fetch scheduled updates.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function storeSchedule(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'station_id' => 'required|integer',
|
|
'fuel_type_id' => 'required|integer',
|
|
'new_price' => 'required|numeric|min:0',
|
|
'scheduled_for' => 'required|date|after:now'
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->post('/fuel-prices/schedule', $validated);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('fuel-price.schedule')
|
|
->with('success', 'Price update scheduled successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to schedule price update.');
|
|
}
|
|
}
|
|
|
|
public function updateSchedule(Request $request, $id)
|
|
{
|
|
$validated = $request->validate([
|
|
'station_id' => 'required|integer',
|
|
'fuel_type_id' => 'required|integer',
|
|
'new_price' => 'required|numeric|min:0',
|
|
'scheduled_for' => 'required|date|after:now'
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->put("/fuel-prices/schedule/{$id}", $validated);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('fuel-price.schedule')
|
|
->with('success', 'Scheduled update modified successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to modify scheduled update.');
|
|
}
|
|
}
|
|
|
|
public function deleteSchedule($id)
|
|
{
|
|
try {
|
|
$response = $this->apiService->delete("/fuel-prices/schedule/{$id}");
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('fuel-price.schedule')
|
|
->with('success', 'Scheduled update deleted successfully.');
|
|
}
|
|
|
|
return back()->with('error', 'Unable to delete scheduled update.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function logs()
|
|
{
|
|
try {
|
|
$response = $this->apiService->get('/fuel-prices/logs');
|
|
|
|
if ($response->successful()) {
|
|
return view('pages.fuel-price-update-logs', [
|
|
'logs' => $response->json()['data']
|
|
]);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to fetch update logs.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
}
|