2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2020-02-11 23:51:18 +08:00
|
|
|
use App\Http\Controllers\Admin\Traits\Importable;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Http\Requests\CreateFlightRequest;
|
|
|
|
use App\Http\Requests\UpdateFlightRequest;
|
2018-01-30 10:51:00 +08:00
|
|
|
use App\Models\Enums\FlightType;
|
2020-02-11 23:51:18 +08:00
|
|
|
use App\Models\Enums\ImportExportType;
|
2018-01-08 00:38:16 +08:00
|
|
|
use App\Models\Flight;
|
2018-03-21 02:06:06 +08:00
|
|
|
use App\Models\FlightField;
|
|
|
|
use App\Models\FlightFieldValue;
|
2017-12-01 10:28:45 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\AirportRepository;
|
2018-01-08 00:38:16 +08:00
|
|
|
use App\Repositories\FareRepository;
|
2018-03-21 02:47:47 +08:00
|
|
|
use App\Repositories\FlightFieldRepository;
|
2017-06-18 06:25:36 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
2017-06-24 06:33:18 +08:00
|
|
|
use App\Repositories\SubfleetRepository;
|
2018-03-22 08:12:36 +08:00
|
|
|
use App\Services\ExportService;
|
2018-01-08 00:38:16 +08:00
|
|
|
use App\Services\FareService;
|
2018-03-24 03:10:18 +08:00
|
|
|
use App\Services\FleetService;
|
2018-01-01 23:32:04 +08:00
|
|
|
use App\Services\FlightService;
|
2018-03-22 08:12:36 +08:00
|
|
|
use App\Services\ImportService;
|
2018-02-20 03:33:26 +08:00
|
|
|
use App\Support\Units\Time;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-08-27 00:32:46 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Laracasts\Flash\Flash;
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class FlightController extends Controller
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
2020-02-11 23:51:18 +08:00
|
|
|
use Importable;
|
|
|
|
|
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;
|
2017-12-02 01:11:03 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* FlightController constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 02:47:47 +08:00
|
|
|
* @param AirlineRepository $airlineRepo
|
|
|
|
* @param AirportRepository $airportRepo
|
|
|
|
* @param FareRepository $fareRepo
|
|
|
|
* @param FlightRepository $flightRepo
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param FlightFieldRepository $flightFieldRepo
|
2018-03-21 02:47:47 +08:00
|
|
|
* @param FareService $fareSvc
|
|
|
|
* @param FlightService $flightSvc
|
2018-03-22 08:12:36 +08:00
|
|
|
* @param ImportService $importSvc
|
2018-03-21 02:47:47 +08:00
|
|
|
* @param SubfleetRepository $subfleetRepo
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-06-20 03:29:10 +08:00
|
|
|
public function __construct(
|
2017-12-01 10:28:45 +08:00
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
AirportRepository $airportRepo,
|
2018-01-08 00:38:16 +08:00
|
|
|
FareRepository $fareRepo,
|
2017-06-20 03:29:10 +08:00
|
|
|
FlightRepository $flightRepo,
|
2018-03-21 02:47:47 +08:00
|
|
|
FlightFieldRepository $flightFieldRepo,
|
2018-01-08 00:38:16 +08:00
|
|
|
FareService $fareSvc,
|
2018-01-01 23:32:04 +08:00
|
|
|
FlightService $flightSvc,
|
2018-03-22 08:12:36 +08:00
|
|
|
ImportService $importSvc,
|
2017-06-24 06:33:18 +08:00
|
|
|
SubfleetRepository $subfleetRepo
|
2017-12-01 10:28:45 +08:00
|
|
|
) {
|
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->airportRepo = $airportRepo;
|
2018-01-08 00:38:16 +08:00
|
|
|
$this->fareRepo = $fareRepo;
|
2017-12-01 10:28:45 +08:00
|
|
|
$this->flightRepo = $flightRepo;
|
2018-03-21 02:47:47 +08:00
|
|
|
$this->flightFieldRepo = $flightFieldRepo;
|
2018-01-08 00:38:16 +08:00
|
|
|
$this->fareSvc = $fareSvc;
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->flightSvc = $flightSvc;
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->importSvc = $importSvc;
|
2017-06-24 06:33:18 +08:00
|
|
|
$this->subfleetRepo = $subfleetRepo;
|
2017-06-20 03:29:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:06:06 +08:00
|
|
|
/**
|
|
|
|
* Save any custom fields found
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 02:06:06 +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),
|
2018-03-21 02:06:06 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('PIREP Custom Fields', $custom_fields);
|
2018-08-27 02:50:08 +08:00
|
|
|
$this->flightSvc->updateCustomFields($flight, $custom_fields);
|
2018-03-21 02:06:06 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2017-06-24 06:33:18 +08:00
|
|
|
protected function getAvailSubfleets($flight)
|
2017-06-20 03:29:10 +08:00
|
|
|
{
|
|
|
|
$retval = [];
|
|
|
|
|
|
|
|
$flight->refresh();
|
2017-06-24 06:33:18 +08:00
|
|
|
$all_aircraft = $this->subfleetRepo->all();
|
|
|
|
$avail_fleets = $all_aircraft->except($flight->subfleets->modelKeys());
|
2017-06-20 03:29:10 +08:00
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
foreach ($avail_fleets as $ac) {
|
2021-04-06 04:55:50 +08:00
|
|
|
$retval[$ac->id] = '['.$ac->airline->icao.'] '.$ac->type.' - '.$ac->name;
|
2017-06-20 03:29:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 01:11:03 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
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();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-07-02 10:06:55 +08:00
|
|
|
return view('admin.flights.index', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'flights' => $flights,
|
2017-12-05 02:05:31 +08:00
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(true),
|
|
|
|
'airports' => $this->airportRepo->selectBoxList(true),
|
2017-07-02 10:06:55 +08:00
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new Flight.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-06-18 06:25:36 +08:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2017-12-02 01:11:03 +08:00
|
|
|
return view('admin.flights.create', [
|
2018-03-21 02:47:47 +08:00
|
|
|
'flight' => null,
|
2021-05-20 01:42:40 +08:00
|
|
|
'days' => 0,
|
2018-03-21 02:47:47 +08:00
|
|
|
'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),
|
2017-12-02 01:11:03 +08:00
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CreateFlightRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 01:11:03 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function store(CreateFlightRequest $request)
|
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
try {
|
|
|
|
$flight = $this->flightSvc->createFlight($request->all());
|
|
|
|
Flash::success('Flight saved successfully.');
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
return redirect(route('admin.flights.edit', $flight->id));
|
|
|
|
} catch (\Exception $e) {
|
2019-10-24 00:01:31 +08:00
|
|
|
Log::error($e);
|
2019-08-27 00:32:46 +08:00
|
|
|
Flash::error($e->getMessage());
|
2018-01-20 06:16:15 +08:00
|
|
|
return redirect()->back()->withInput($request->all());
|
|
|
|
}
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
$avail_subfleets = $this->getAvailSubfleets($flight);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-07-02 10:06:55 +08:00
|
|
|
return view('admin.flights.show', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight' => $flight,
|
2018-03-21 02:47:47 +08:00
|
|
|
'flight_fields' => $this->flightFieldRepo->all(),
|
2017-07-02 10:06:55 +08:00
|
|
|
'avail_subfleets' => $avail_subfleets,
|
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-06-18 06:25:36 +08:00
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2018-02-20 03:33:26 +08:00
|
|
|
$time = new Time($flight->flight_time);
|
2018-02-28 03:25:32 +08:00
|
|
|
|
2018-02-20 03:33:26 +08:00
|
|
|
$flight->hours = $time->hours;
|
|
|
|
$flight->minutes = $time->minutes;
|
|
|
|
|
2017-07-02 10:06:55 +08:00
|
|
|
return view('admin.flights.edit', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight' => $flight,
|
2018-03-23 10:21:35 +08:00
|
|
|
'days' => $flight->days,
|
2018-03-21 02:47:47 +08:00
|
|
|
'flight_fields' => $this->flightFieldRepo->all(),
|
2018-03-20 09:50:40 +08:00
|
|
|
'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),
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight_types' => FlightType::select(true),
|
2017-07-02 10:06:55 +08:00
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2017-06-18 06:25:36 +08:00
|
|
|
* @param UpdateFlightRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 01:11:03 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdateFlightRequest $request)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
try {
|
|
|
|
$this->flightSvc->updateFlight($flight, $request->all());
|
|
|
|
Flash::success('Flight updated successfully.');
|
2018-01-20 06:16:15 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
} catch (\Exception $e) {
|
2019-10-24 00:01:31 +08:00
|
|
|
Log::error($e);
|
2019-08-27 00:32:46 +08:00
|
|
|
Flash::error($e->getMessage());
|
2018-01-20 06:16:15 +08:00
|
|
|
return redirect()->back()->withInput($request->all());
|
|
|
|
}
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-01 23:32:04 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
$this->flightSvc->deleteFlight($flight);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-03-22 08:12:36 +08:00
|
|
|
$exporter = app(ExportService::class);
|
2018-03-22 06:07:30 +08:00
|
|
|
$flights = $this->flightRepo->all();
|
|
|
|
|
2018-03-23 01:43:58 +08:00
|
|
|
$path = $exporter->exportFlights($flights);
|
2018-03-22 06:07:30 +08:00
|
|
|
return response()
|
|
|
|
->download($path, 'flights.csv', [
|
|
|
|
'content-type' => 'text/csv',
|
|
|
|
])
|
|
|
|
->deleteFileAfterSend(true);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* @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
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
|
|
|
public function import(Request $request)
|
|
|
|
{
|
|
|
|
$logs = [
|
|
|
|
'success' => [],
|
2018-08-27 00:40:04 +08:00
|
|
|
'errors' => [],
|
2018-03-21 08:17:11 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($request->isMethod('post')) {
|
2020-02-11 23:51:18 +08:00
|
|
|
$logs = $this->importFile($request, ImportExportType::FLIGHTS);
|
2018-03-21 08:17:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.flights.import', [
|
|
|
|
'logs' => $logs,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
|
|
|
* @param $flight
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
2017-12-02 01:11:03 +08:00
|
|
|
*/
|
2017-07-12 06:44:12 +08:00
|
|
|
protected function return_fields_view($flight)
|
|
|
|
{
|
|
|
|
$flight->refresh();
|
|
|
|
return view('admin.flights.flight_fields', [
|
2018-03-21 02:47:47 +08:00
|
|
|
'flight' => $flight,
|
|
|
|
'flight_fields' => $this->flightFieldRepo->all(),
|
2017-07-12 06:44:12 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
2018-03-24 04:02:26 +08:00
|
|
|
* @param $flight_id
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
2017-12-02 01:11:03 +08:00
|
|
|
*/
|
2018-03-24 04:02:26 +08:00
|
|
|
public function field_values($flight_id, Request $request)
|
2017-07-12 06:44:12 +08:00
|
|
|
{
|
2018-03-21 02:47:47 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($flight_id);
|
2017-07-12 06:44:12 +08:00
|
|
|
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());
|
|
|
|
|
2018-03-24 09:29:37 +08:00
|
|
|
$field = new FlightFieldValue();
|
2018-03-21 02:47:47 +08:00
|
|
|
$field->flight_id = $flight_id;
|
|
|
|
$field->name = $request->input('name');
|
|
|
|
$field->value = $request->input('value');
|
2017-07-12 06:44:12 +08:00
|
|
|
$field->save();
|
2018-03-20 09:50:40 +08:00
|
|
|
} elseif ($request->isMethod('put')) {
|
2018-03-24 04:02:26 +08:00
|
|
|
Log::info('Updating flight field, flight: '.$flight_id, $request->input());
|
2018-03-24 09:29:37 +08:00
|
|
|
$field = FlightFieldValue::where([
|
2018-08-27 00:40:04 +08:00
|
|
|
'name' => $request->input('name'),
|
2018-03-24 09:29:37 +08:00
|
|
|
'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');
|
2018-03-21 02:47:47 +08:00
|
|
|
$field = new FlightFieldValue();
|
|
|
|
$field->name = $request->input('name');
|
|
|
|
}
|
|
|
|
|
2018-03-24 09:29:37 +08:00
|
|
|
$field->flight_id = $flight_id;
|
2018-03-21 02:47:47 +08:00
|
|
|
$field->value = $request->input('value');
|
2017-07-12 06:44:12 +08:00
|
|
|
$field->save();
|
2018-08-27 00:40:04 +08:00
|
|
|
// update the field value
|
2018-03-20 09:50:40 +08:00
|
|
|
} // remove custom field from flight
|
2017-07-12 06:44:12 +08:00
|
|
|
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')) {
|
2018-03-21 02:47:47 +08:00
|
|
|
FlightFieldValue::destroy($request->input('field_id'));
|
|
|
|
}
|
2017-07-12 06:44:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->return_fields_view($flight);
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
|
|
|
* @param $flight
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 01:11:03 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2017-06-24 06:33:18 +08:00
|
|
|
protected function return_subfleet_view($flight)
|
2017-06-20 03:29:10 +08:00
|
|
|
{
|
2017-06-24 06:33:18 +08:00
|
|
|
$avail_subfleets = $this->getAvailSubfleets($flight);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-07-02 10:06:55 +08:00
|
|
|
return view('admin.flights.subfleets', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight' => $flight,
|
2017-07-02 10:06:55 +08:00
|
|
|
'avail_subfleets' => $avail_subfleets,
|
|
|
|
]);
|
2017-06-20 03:29:10 +08:00
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
2017-12-02 01:11:03 +08:00
|
|
|
*/
|
2018-01-07 22:58:57 +08:00
|
|
|
public function subfleets($id, Request $request)
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2018-03-24 03:10:18 +08:00
|
|
|
$fleetSvc = app(FleetService::class);
|
|
|
|
|
2017-06-21 01:14:10 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2017-06-18 06:25:36 +08:00
|
|
|
if ($request->isMethod('post')) {
|
2018-03-24 03:10:18 +08:00
|
|
|
$fleetSvc->addSubfleetToFlight($subfleet, $flight);
|
2018-03-20 09:50:40 +08:00
|
|
|
} // remove aircraft from flight
|
2017-06-18 06:25:36 +08:00
|
|
|
elseif ($request->isMethod('delete')) {
|
2018-03-24 03:10:18 +08:00
|
|
|
$fleetSvc->removeSubfleetFromFlight($subfleet, $flight);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
2017-06-20 03:29:10 +08:00
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
return $this->return_subfleet_view($flight);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
2018-01-08 00:38:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-01-08 00:38:16 +08:00
|
|
|
*/
|
|
|
|
protected function getAvailFares($flight)
|
|
|
|
{
|
|
|
|
$retval = [];
|
|
|
|
$all_fares = $this->fareRepo->all();
|
|
|
|
$avail_fares = $all_fares->except($flight->fares->modelKeys());
|
|
|
|
foreach ($avail_fares as $fare) {
|
2018-03-21 02:47:47 +08:00
|
|
|
$retval[$fare->id] = $fare->name.' (base price: '.$fare->price.')';
|
2018-01-08 00:38:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Flight $flight
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-08 00:38:16 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
protected function return_fares_view(Flight $flight)
|
|
|
|
{
|
|
|
|
$flight->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-08 00:38:16 +08:00
|
|
|
return view('admin.flights.fares', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight' => $flight,
|
2018-01-08 00:38:16 +08:00
|
|
|
'avail_fares' => $this->getAvailFares($flight),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-08 00:38:16 +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);
|
|
|
|
}
|
|
|
|
|
2018-02-24 05:34:14 +08:00
|
|
|
if ($request->isMethod('delete')) {
|
|
|
|
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
|
|
|
|
$this->fareSvc->delFareFromFlight($flight, $fare);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-24 05:34:14 +08:00
|
|
|
return $this->return_fares_view($flight);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'value' => 'nullable', // regex:/([\d%]*)/
|
|
|
|
]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
/*
|
2018-01-08 00:38:16 +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);
|
|
|
|
}
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|