phpvms/app/Http/Controllers/Admin/FlightController.php

567 lines
17 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Admin;
2018-02-21 12:33:09 +08:00
use App\Http\Requests\CreateFlightRequest;
use App\Http\Requests\UpdateFlightRequest;
use App\Contracts\Controller;
use App\Models\Enums\Days;
2018-01-30 10:51:00 +08:00
use App\Models\Enums\FlightType;
use App\Models\Flight;
use App\Models\FlightField;
use App\Models\FlightFieldValue;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Repositories\FareRepository;
use App\Repositories\FlightFieldRepository;
use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository;
use App\Services\ExportService;
use App\Services\FareService;
use App\Services\FleetService;
use App\Services\FlightService;
use App\Services\ImportService;
use App\Support\Units\Time;
use Flash;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Log;
use Response;
use Storage;
/**
* Class FlightController
*/
class FlightController extends Controller
{
2018-08-27 00:40:04 +08:00
private $airlineRepo;
private $airportRepo;
private $fareRepo;
private $flightRepo;
private $flightFieldRepo;
private $fareSvc;
private $flightSvc;
private $importSvc;
private $subfleetRepo;
2018-02-21 12:33:09 +08:00
/**
* FlightController constructor.
2018-08-27 00:40:04 +08:00
*
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param FareRepository $fareRepo
* @param FlightRepository $flightRepo
* @param FlightFieldRepository $flightFieldRepo
* @param FareService $fareSvc
* @param FlightService $flightSvc
* @param ImportService $importSvc
* @param SubfleetRepository $subfleetRepo
2018-02-21 12:33:09 +08:00
*/
2017-06-20 03:29:10 +08:00
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FareRepository $fareRepo,
2017-06-20 03:29:10 +08:00
FlightRepository $flightRepo,
FlightFieldRepository $flightFieldRepo,
FareService $fareSvc,
FlightService $flightSvc,
ImportService $importSvc,
SubfleetRepository $subfleetRepo
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->fareRepo = $fareRepo;
$this->flightRepo = $flightRepo;
$this->flightFieldRepo = $flightFieldRepo;
$this->fareSvc = $fareSvc;
$this->flightSvc = $flightSvc;
$this->importSvc = $importSvc;
$this->subfleetRepo = $subfleetRepo;
2017-06-20 03:29:10 +08:00
}
/**
* Save any custom fields found
2018-08-27 00:40:04 +08:00
*
* @param Flight $flight
* @param Request $request
*/
protected function saveCustomFields(Flight $flight, Request $request): void
{
$custom_fields = [];
$flight_fields = FlightField::all();
foreach ($flight_fields as $field) {
if (!$request->filled($field->slug)) {
continue;
}
$custom_fields[] = [
2018-08-27 00:40:04 +08:00
'name' => $field->name,
'value' => $request->input($field->slug),
];
}
Log::info('PIREP Custom Fields', $custom_fields);
2018-08-27 02:50:08 +08:00
$this->flightSvc->updateCustomFields($flight, $custom_fields);
}
2018-02-21 12:33:09 +08:00
/**
* @param $flight
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return array
*/
protected function getAvailSubfleets($flight)
2017-06-20 03:29:10 +08:00
{
$retval = [];
$flight->refresh();
$all_aircraft = $this->subfleetRepo->all();
$avail_fleets = $all_aircraft->except($flight->subfleets->modelKeys());
2017-06-20 03:29:10 +08:00
foreach ($avail_fleets as $ac) {
$retval[$ac->id] = $ac->type.' - '.$ac->name;
2017-06-20 03:29:10 +08:00
}
return $retval;
}
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
2018-03-23 11:50:41 +08:00
$flights = $this->flightRepo
2018-09-04 20:02:16 +08:00
->with(['dpt_airport', 'arr_airport', 'alt_airport', 'airline'])
2018-03-23 11:50:41 +08:00
->searchCriteria($request, false)
->orderBy('flight_number', 'asc')
->paginate();
2017-07-02 10:06:55 +08:00
return view('admin.flights.index', [
'flights' => $flights,
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
2017-07-02 10:06:55 +08:00
]);
}
/**
* Show the form for creating a new Flight.
2018-08-27 00:40:04 +08:00
*
* @return Response
*/
public function create()
{
return view('admin.flights.create', [
'flight' => null,
'days' => [],
'flight_fields' => $this->flightFieldRepo->all(),
'airlines' => $this->airlineRepo->selectBoxList(),
'airports' => $this->airportRepo->selectBoxList(true, false),
'alt_airports' => $this->airportRepo->selectBoxList(true),
'flight_types' => FlightType::select(true),
]);
}
/**
* @param CreateFlightRequest $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(CreateFlightRequest $request)
{
$input = $request->all();
// Create a temporary flight so we can validate
$flight = new Flight($input);
if ($this->flightSvc->isFlightDuplicate($flight)) {
Flash::error('Duplicate flight with same number/code/leg found, please change to proceed');
return redirect()->back()->withInput($request->all());
}
if (array_key_exists('days', $input) && filled($input['days'])) {
$input['days'] = Days::getDaysMask($input['days']);
}
$input['active'] = get_truth_state($input['active']);
$time = new Time($input['minutes'], $input['hours']);
$input['flight_time'] = $time->getMinutes();
$flight = $this->flightRepo->create($input);
Flash::success('Flight saved successfully.');
return redirect(route('admin.flights.edit', $flight->id));
}
/**
* @param $id
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return mixed
*/
public function show($id)
{
$flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));
}
$avail_subfleets = $this->getAvailSubfleets($flight);
2017-07-02 10:06:55 +08:00
return view('admin.flights.show', [
'flight' => $flight,
'flight_fields' => $this->flightFieldRepo->all(),
2017-07-02 10:06:55 +08:00
'avail_subfleets' => $avail_subfleets,
]);
}
/**
* @param $id
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return mixed
*/
public function edit($id)
{
$flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));
}
$time = new Time($flight->flight_time);
2018-02-28 03:25:32 +08:00
$flight->hours = $time->hours;
$flight->minutes = $time->minutes;
2017-07-02 10:06:55 +08:00
return view('admin.flights.edit', [
'flight' => $flight,
'days' => $flight->days,
'flight_fields' => $this->flightFieldRepo->all(),
'airlines' => $this->airlineRepo->selectBoxList(),
'airports' => $this->airportRepo->selectBoxList(),
'alt_airports' => $this->airportRepo->selectBoxList(true),
'avail_fares' => $this->getAvailFares($flight),
2018-02-28 03:25:32 +08:00
'avail_subfleets' => $this->getAvailSubfleets($flight),
'flight_types' => FlightType::select(true),
2017-07-02 10:06:55 +08:00
]);
}
/**
* @param $id
* @param UpdateFlightRequest $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update($id, UpdateFlightRequest $request)
{
$flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));
}
$input = $request->all();
2018-08-27 00:40:04 +08:00
// apply the updates here temporarily, don't save
// the repo->update() call will actually do it
$flight->fill($input);
2018-08-27 00:40:04 +08:00
if ($this->flightSvc->isFlightDuplicate($flight)) {
Flash::error('Duplicate flight with same number/code/leg found, please change to proceed');
return redirect()->back()->withInput($request->all());
}
if (array_key_exists('days', $input) && filled($input['days'])) {
$input['days'] = Days::getDaysMask($input['days']);
}
$input['flight_time'] = Time::init(
$input['minutes'],
2019-05-13 02:26:44 +08:00
$input['hours']
)->getMinutes();
$input['active'] = get_truth_state($input['active']);
$this->flightRepo->update($input, $id);
Flash::success('Flight updated successfully.');
return redirect(route('admin.flights.index'));
}
/**
* @param $id
2018-08-27 00:40:04 +08:00
*
* @throws \Exception
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function destroy($id)
{
$flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));
}
$this->flightSvc->deleteFlight($flight);
Flash::success('Flight deleted successfully.');
return redirect(route('admin.flights.index'));
}
2018-03-22 06:07:30 +08:00
/**
* Run the flight exporter
2018-08-27 00:40:04 +08:00
*
2018-03-22 06:07:30 +08:00
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-03-22 06:07:30 +08:00
* @throws \League\Csv\Exception
2018-08-27 00:40:04 +08:00
*
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
2018-03-22 06:07:30 +08:00
*/
public function export(Request $request)
{
$exporter = app(ExportService::class);
2018-03-22 06:07:30 +08:00
$flights = $this->flightRepo->all();
$path = $exporter->exportFlights($flights);
2018-03-22 06:07:30 +08:00
return response()
->download($path, 'flights.csv', [
'content-type' => 'text/csv',
])
->deleteFileAfterSend(true);
}
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:48:57 +08:00
* @throws \Illuminate\Validation\ValidationException
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function import(Request $request)
{
$logs = [
'success' => [],
2018-08-27 00:40:04 +08:00
'errors' => [],
];
if ($request->isMethod('post')) {
$path = Storage::putFileAs(
2019-05-13 02:26:44 +08:00
'import',
$request->file('csv_file'),
'import_flights.csv'
);
$path = storage_path('app/'.$path);
Log::info('Uploaded flights import file to '.$path);
$logs = $this->importSvc->importFlights($path);
}
return view('admin.flights.import', [
'logs' => $logs,
]);
}
/**
* @param $flight
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return mixed
*/
protected function return_fields_view($flight)
{
$flight->refresh();
return view('admin.flights.flight_fields', [
'flight' => $flight,
'flight_fields' => $this->flightFieldRepo->all(),
]);
}
/**
2018-03-24 04:02:26 +08:00
* @param $flight_id
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return mixed
*/
2018-03-24 04:02:26 +08:00
public function field_values($flight_id, Request $request)
{
$flight = $this->flightRepo->findWithoutFail($flight_id);
if (empty($flight)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));
}
// add custom field to flight
if ($request->isMethod('post')) {
2018-03-24 04:02:26 +08:00
Log::info('Adding new flight field, flight: '.$flight_id, $request->input());
$field = new FlightFieldValue();
$field->flight_id = $flight_id;
$field->name = $request->input('name');
$field->value = $request->input('value');
$field->save();
} elseif ($request->isMethod('put')) {
2018-03-24 04:02:26 +08:00
Log::info('Updating flight field, flight: '.$flight_id, $request->input());
$field = FlightFieldValue::where([
2018-08-27 00:40:04 +08:00
'name' => $request->input('name'),
'flight_id' => $flight_id,
])->first();
2018-08-27 00:40:04 +08:00
if (!$field) {
2018-03-24 04:02:26 +08:00
Log::info('Field not found, creating new');
$field = new FlightFieldValue();
$field->name = $request->input('name');
}
$field->flight_id = $flight_id;
$field->value = $request->input('value');
$field->save();
2018-08-27 00:40:04 +08:00
// update the field value
} // remove custom field from flight
elseif ($request->isMethod('delete')) {
2018-03-24 04:02:26 +08:00
Log::info('Deleting flight field, flight: '.$flight_id, $request->input());
2018-08-27 00:40:04 +08:00
if ($flight_id && $request->input('field_id')) {
FlightFieldValue::destroy($request->input('field_id'));
}
}
return $this->return_fields_view($flight);
}
/**
* @param $flight
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function return_subfleet_view($flight)
2017-06-20 03:29:10 +08:00
{
$avail_subfleets = $this->getAvailSubfleets($flight);
2017-07-02 10:06:55 +08:00
return view('admin.flights.subfleets', [
'flight' => $flight,
2017-07-02 10:06:55 +08:00
'avail_subfleets' => $avail_subfleets,
]);
2017-06-20 03:29:10 +08:00
}
/**
* @param $id
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return mixed
*/
public function subfleets($id, Request $request)
{
$flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));
}
$fleetSvc = app(FleetService::class);
// add aircraft to flight
2018-03-24 04:02:26 +08:00
$subfleet = $this->subfleetRepo->findWithoutFail($request->subfleet_id);
2018-08-27 00:40:04 +08:00
if (!$subfleet) {
2018-03-24 04:02:26 +08:00
return $this->return_subfleet_view($flight);
}
if ($request->isMethod('post')) {
$fleetSvc->addSubfleetToFlight($subfleet, $flight);
} // remove aircraft from flight
elseif ($request->isMethod('delete')) {
$fleetSvc->removeSubfleetFromFlight($subfleet, $flight);
}
2017-06-20 03:29:10 +08:00
return $this->return_subfleet_view($flight);
}
/**
* Get all the fares that haven't been assigned to a given subfleet
2018-08-27 00:40:04 +08:00
*
* @param mixed $flight
2018-08-27 02:50:08 +08:00
*
* @return mixed
* @return array
*/
protected function getAvailFares($flight)
{
$retval = [];
$all_fares = $this->fareRepo->all();
$avail_fares = $all_fares->except($flight->fares->modelKeys());
foreach ($avail_fares as $fare) {
$retval[$fare->id] = $fare->name.' (base price: '.$fare->price.')';
}
return $retval;
}
/**
* @param Flight $flight
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function return_fares_view(Flight $flight)
{
$flight->refresh();
return view('admin.flights.fares', [
'flight' => $flight,
'avail_fares' => $this->getAvailFares($flight),
]);
}
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function fares(Request $request)
{
$id = $request->id;
$flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) {
return $this->return_fares_view($flight);
}
if ($request->isMethod('get')) {
return $this->return_fares_view($flight);
}
if ($request->isMethod('delete')) {
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
$this->fareSvc->delFareFromFlight($flight, $fare);
return $this->return_fares_view($flight);
}
$this->validate($request, [
'value' => 'nullable', // regex:/([\d%]*)/
]);
2018-08-27 00:40:04 +08:00
/*
* update specific fare data
*/
if ($request->isMethod('post')) {
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
$this->fareSvc->setForFlight($flight, $fare);
} // update the pivot table with overrides for the fares
elseif ($request->isMethod('put')) {
$override = [];
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
$override[$request->name] = $request->value;
$this->fareSvc->setForFlight($flight, $fare, $override);
} // dissassociate fare from teh aircraft
return $this->return_fares_view($flight);
}
}