2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Http\Requests\CreateFlightRequest;
|
|
|
|
use App\Http\Requests\UpdateFlightRequest;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Controller;
|
2018-03-23 10:21:35 +08:00
|
|
|
use App\Models\Enums\Days;
|
2018-01-30 10:51:00 +08:00
|
|
|
use App\Models\Enums\FlightType;
|
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;
|
2017-06-18 06:25:36 +08:00
|
|
|
use Flash;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-03-21 02:06:06 +08:00
|
|
|
use Log;
|
2017-06-18 06:25:36 +08:00
|
|
|
use Response;
|
2018-03-21 08:17:11 +08:00
|
|
|
use Storage;
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class FlightController
|
|
|
|
*/
|
|
|
|
class FlightController extends Controller
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
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);
|
|
|
|
$this->flightSvc->updateCustomFields($flight->id, $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
|
|
|
|
*/
|
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) {
|
|
|
|
$retval[$ac->id] = $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
|
|
|
|
->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,
|
2018-03-23 10:21:35 +08:00
|
|
|
'days' => [],
|
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)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
|
|
|
|
2018-04-26 00:53:32 +08:00
|
|
|
// Create a temporary flight so we can validate
|
|
|
|
$flight = new Flight($input);
|
|
|
|
if ($this->flightSvc->isFlightDuplicate($flight)) {
|
2018-01-20 06:16:15 +08:00
|
|
|
Flash::error('Duplicate flight with same number/code/leg found, please change to proceed');
|
|
|
|
return redirect()->back()->withInput($request->all());
|
|
|
|
}
|
|
|
|
|
2018-03-31 07:25:27 +08:00
|
|
|
if (array_key_exists('days', $input) && filled($input['days'])) {
|
|
|
|
$input['days'] = Days::getDaysMask($input['days']);
|
|
|
|
}
|
|
|
|
|
2018-02-20 03:33:26 +08:00
|
|
|
$input['active'] = get_truth_state($input['active']);
|
|
|
|
|
|
|
|
$time = new Time($input['minutes'], $input['hours']);
|
|
|
|
$input['flight_time'] = $time->getMinutes();
|
|
|
|
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->create($input);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
Flash::success('Flight saved successfully.');
|
2017-12-02 01:11:03 +08:00
|
|
|
return redirect(route('admin.flights.edit', $flight->id));
|
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'));
|
|
|
|
}
|
|
|
|
|
2018-01-24 03:23:32 +08:00
|
|
|
$input = $request->all();
|
2018-01-20 06:16:15 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// apply the updates here temporarily, don't save
|
|
|
|
// the repo->update() call will actually do it
|
2018-04-26 00:53:32 +08:00
|
|
|
$flight->fill($input);
|
2018-01-20 06:16:15 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($this->flightSvc->isFlightDuplicate($flight)) {
|
2018-01-20 06:16:15 +08:00
|
|
|
Flash::error('Duplicate flight with same number/code/leg found, please change to proceed');
|
|
|
|
return redirect()->back()->withInput($request->all());
|
|
|
|
}
|
|
|
|
|
2018-03-31 07:25:27 +08:00
|
|
|
if (array_key_exists('days', $input) && filled($input['days'])) {
|
|
|
|
$input['days'] = Days::getDaysMask($input['days']);
|
|
|
|
}
|
2018-03-23 10:21:35 +08:00
|
|
|
|
2018-02-20 03:33:26 +08:00
|
|
|
$input['flight_time'] = Time::init(
|
|
|
|
$input['minutes'],
|
|
|
|
$input['hours'])->getMinutes();
|
|
|
|
|
2018-01-24 03:23:32 +08:00
|
|
|
$input['active'] = get_truth_state($input['active']);
|
2018-02-20 03:33:26 +08:00
|
|
|
|
2018-01-24 03:23:32 +08:00
|
|
|
$this->flightRepo->update($input, $id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
Flash::success('Flight updated successfully.');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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')) {
|
|
|
|
$path = Storage::putFileAs(
|
2018-03-23 06:48:57 +08:00
|
|
|
'import', $request->file('csv_file'), 'import_flights.csv'
|
2018-03-21 08:17:11 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
$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,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|