Add fares and set prices on flights #125

This commit is contained in:
Nabeel Shahzad
2018-01-07 10:38:16 -06:00
parent 7e45291b27
commit 0495ff27cb
15 changed files with 285 additions and 102 deletions

View File

@@ -2,14 +2,19 @@
namespace App\Http\Controllers\Admin;
use App\Models\Flight;
use App\Models\FlightFields;
use App\Http\Requests\CreateFlightRequest;
use App\Http\Requests\UpdateFlightRequest;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Repositories\FareRepository;
use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository;
use App\Services\FareService;
use App\Services\FlightService;
use Illuminate\Http\Request;
use Flash;
use Response;
@@ -18,20 +23,26 @@ class FlightController extends BaseController
{
private $airlineRepo,
$airportRepo,
$fareRepo,
$flightRepo,
$fareSvc,
$flightSvc,
$subfleetRepo;
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FareRepository $fareRepo,
FlightRepository $flightRepo,
FareService $fareSvc,
FlightService $flightSvc,
SubfleetRepository $subfleetRepo
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->fareRepo = $fareRepo;
$this->flightRepo = $flightRepo;
$this->fareSvc = $fareSvc;
$this->flightSvc = $flightSvc;
$this->subfleetRepo = $subfleetRepo;
}
@@ -135,6 +146,7 @@ class FlightController extends BaseController
'flight' => $flight,
'airlines' => $this->airlineRepo->selectBoxList(),
'airports' => $this->airportRepo->selectBoxList(),
'avail_fares' => $this->getAvailFares($flight),
'avail_subfleets' => $avail_subfleets,
]);
}
@@ -270,4 +282,71 @@ class FlightController extends BaseController
return $this->return_subfleet_view($flight);
}
/**
* Get all the fares that haven't been assigned to a given subfleet
*/
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
* @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
* @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);
}
/**
* 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
elseif ($request->isMethod('delete')) {
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
$this->fareSvc->delFareFromFlight($flight, $fare);
}
return $this->return_fares_view($flight);
}
}

View File

@@ -11,15 +11,19 @@ use App\Models\Enums\FuelType;
use App\Models\Airline;
use App\Models\Subfleet;
use App\Http\Requests\CreateSubfleetRequest;
use App\Http\Requests\UpdateSubfleetRequest;
use App\Repositories\FareRepository;
use App\Repositories\SubfleetRepository;
use App\Services\FareService;
class SubfleetController extends BaseController
{
/** @var SubfleetRepository */
private $subfleetRepo, $fareRepo;
private $subfleetRepo, $fareRepo, $fareSvc;
/**
* SubfleetController constructor.
@@ -29,10 +33,12 @@ class SubfleetController extends BaseController
*/
public function __construct(
SubfleetRepository $subfleetRepo,
FareRepository $fareRepo
FareRepository $fareRepo,
FareService $fareSvc
) {
$this->subfleetRepo = $subfleetRepo;
$this->fareRepo = $fareRepo;
$this->fareSvc = $fareSvc;
}
/**
@@ -84,10 +90,9 @@ class SubfleetController extends BaseController
/**
* Store a newly created Subfleet in storage.
*
* @param CreateSubfleetRequest $request
*
* @return Response
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function store(CreateSubfleetRequest $request)
{
@@ -100,9 +105,7 @@ class SubfleetController extends BaseController
/**
* Display the specified Subfleet.
*
* @param int $id
*
* @return Response
*/
public function show($id)
@@ -123,9 +126,7 @@ class SubfleetController extends BaseController
/**
* Show the form for editing the specified Subfleet.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
@@ -148,11 +149,10 @@ class SubfleetController extends BaseController
/**
* Update the specified Subfleet in storage.
*
* @param int $id
* @param int $id
* @param UpdateSubfleetRequest $request
*
* @return Response
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function update($id, UpdateSubfleetRequest $request)
{
@@ -163,7 +163,7 @@ class SubfleetController extends BaseController
return redirect(route('admin.subfleets.index'));
}
$subfleet = $this->subfleetRepo->update($request->all(), $id);
$this->subfleetRepo->update($request->all(), $id);
Flash::success('Subfleet updated successfully.');
return redirect(route('admin.subfleets.index'));
@@ -171,9 +171,7 @@ class SubfleetController extends BaseController
/**
* Remove the specified Subfleet from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
@@ -191,6 +189,10 @@ class SubfleetController extends BaseController
return redirect(route('admin.subfleets.index'));
}
/**
* @param Subfleet $subfleet
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function return_fares_view(Subfleet $subfleet)
{
$subfleet->refresh();
@@ -204,7 +206,6 @@ class SubfleetController extends BaseController
/**
* @param Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function fares(Request $request)
@@ -217,8 +218,6 @@ class SubfleetController extends BaseController
//return view('admin.aircraft.fares', ['fares' => []]);
}
$fare_svc = app('App\Services\FareService');
if ($request->isMethod('get')) {
return $this->return_fares_view($subfleet);
}
@@ -228,7 +227,7 @@ class SubfleetController extends BaseController
*/
if ($request->isMethod('post')) {
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
$fare_svc->setForSubfleet($subfleet, $fare);
$this->fareSvc->setForSubfleet($subfleet, $fare);
}
// update the pivot table with overrides for the fares
@@ -236,13 +235,13 @@ class SubfleetController extends BaseController
$override = [];
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
$override[$request->name] = $request->value;
$fare_svc->setForSubfleet($subfleet, $fare, $override);
$this->fareSvc->setForSubfleet($subfleet, $fare, $override);
}
// dissassociate fare from teh aircraft
elseif ($request->isMethod('delete')) {
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
$fare_svc->delFareFromSubfleet($subfleet, $fare);
$this->fareSvc->delFareFromSubfleet($subfleet, $fare);
}
return $this->return_fares_view($subfleet);