#32 moved fares to subfleets
This commit is contained in:
@@ -17,23 +17,10 @@ class AircraftController extends BaseController
|
||||
/** @var SubfleetRepository */
|
||||
private $aircraftRepository, $fareRepository;
|
||||
|
||||
protected function getAvailFares($aircraft)
|
||||
{
|
||||
$retval = [];
|
||||
$all_fares = $this->fareRepository->all();
|
||||
$avail_fares = $all_fares->except($aircraft->fares->modelKeys());
|
||||
foreach ($avail_fares as $fare) {
|
||||
$retval[$fare->id] = $fare->name.
|
||||
' (price: '.$fare->price.
|
||||
', cost: '.$fare->cost.
|
||||
', capacity: '.$fare->capacity.')';
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
public function __construct(SubfleetRepository $aircraftRepo, FareRepository $fareRepo)
|
||||
{
|
||||
public function __construct(
|
||||
SubfleetRepository $aircraftRepo,
|
||||
FareRepository $fareRepo
|
||||
) {
|
||||
$this->fareRepository = $fareRepo;
|
||||
$this->aircraftRepository = $aircraftRepo;
|
||||
}
|
||||
@@ -70,6 +57,7 @@ class AircraftController extends BaseController
|
||||
$aircraft = $this->aircraftRepository->create($input);
|
||||
|
||||
Flash::success('Aircraft saved successfully.');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
@@ -85,11 +73,8 @@ class AircraftController extends BaseController
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
$avail_fares = $this->getAvailFares($aircraft);
|
||||
|
||||
return view('admin.aircraft.show', [
|
||||
'aircraft' => $aircraft,
|
||||
'avail_fares' => $avail_fares,
|
||||
'aircraft' => $aircraft,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -102,12 +87,13 @@ class AircraftController extends BaseController
|
||||
|
||||
if (empty($aircraft)) {
|
||||
Flash::error('Aircraft not found');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
return view('admin.aircraft.edit', [
|
||||
'subfleets' => Subfleet::all()->pluck('name', 'id'),
|
||||
'aircraft' => $aircraft,
|
||||
'aircraft' => $aircraft,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -120,12 +106,14 @@ class AircraftController extends BaseController
|
||||
|
||||
if (empty($aircraft)) {
|
||||
Flash::error('Aircraft not found');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
$aircraft = $this->aircraftRepository->update($request->all(), $id);
|
||||
|
||||
Flash::success('Aircraft updated successfully.');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
@@ -138,6 +126,7 @@ class AircraftController extends BaseController
|
||||
|
||||
if (empty($aircraft)) {
|
||||
Flash::error('Aircraft not found');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
@@ -148,54 +137,4 @@ class AircraftController extends BaseController
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
protected function return_fares_view($aircraft)
|
||||
{
|
||||
$aircraft->refresh();
|
||||
$avail_fares = $this->getAvailFares($aircraft);
|
||||
|
||||
return view('admin.aircraft.fares', [
|
||||
'aircraft' => $aircraft,
|
||||
'avail_fares' => $avail_fares,
|
||||
]);
|
||||
}
|
||||
|
||||
public function fares(Request $request)
|
||||
{
|
||||
$id = $request->id;
|
||||
|
||||
$aircraft = $this->aircraftRepository->findWithoutFail($id);
|
||||
if (empty($aircraft)) {
|
||||
return view('admin.aircraft.fares', ['fares' => []]);
|
||||
}
|
||||
|
||||
$fare_svc = app('App\Services\FareService');
|
||||
|
||||
if ($request->isMethod('get')) {
|
||||
return $this->return_fares_view($aircraft);
|
||||
}
|
||||
|
||||
/**
|
||||
* update specific fare data
|
||||
*/
|
||||
if ($request->isMethod('post')) {
|
||||
$fare = $this->fareRepository->findWithoutFail($request->fare_id);
|
||||
$fare_svc->setForAircraft($aircraft, $fare);
|
||||
}
|
||||
|
||||
// update the pivot table with overrides for the fares
|
||||
elseif ($request->isMethod('put')) {
|
||||
$override = [];
|
||||
$fare = $this->fareRepository->findWithoutFail($request->fare_id);
|
||||
$override[$request->name] = $request->value;
|
||||
$fare_svc->setForAircraft($aircraft, $fare, $override);
|
||||
}
|
||||
|
||||
// dissassociate fare from teh aircraft
|
||||
elseif ($request->isMethod('delete')) {
|
||||
$fare = $this->fareRepository->findWithoutFail($request->fare_id);
|
||||
$fare_svc->delFromAircraft($aircraft, $fare);
|
||||
}
|
||||
|
||||
return $this->return_fares_view($aircraft);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,11 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Requests;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Subfleet;
|
||||
use App\Http\Requests\CreateSubfleetRequest;
|
||||
use App\Http\Requests\UpdateSubfleetRequest;
|
||||
use App\Models\Fare;
|
||||
use App\Repositories\FareRepository;
|
||||
use App\Repositories\SubfleetRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Flash;
|
||||
@@ -15,11 +18,30 @@ use Response;
|
||||
class SubfleetController extends BaseController
|
||||
{
|
||||
/** @var SubfleetRepository */
|
||||
private $subfleetRepo;
|
||||
private $subfleetRepo, $fareRepo;
|
||||
|
||||
public function __construct(SubfleetRepository $subfleetRepo)
|
||||
protected function getAvailFares($subfleet)
|
||||
{
|
||||
$retval = [];
|
||||
$all_fares = $this->fareRepo->all();
|
||||
$avail_fares = $all_fares->except($subfleet->fares->modelKeys());
|
||||
foreach ($avail_fares as $fare) {
|
||||
$retval[$fare->id] = $fare->name.
|
||||
' (price: '.$fare->price.
|
||||
', cost: '.$fare->cost.
|
||||
', capacity: '.$fare->capacity.')';
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
public function __construct(
|
||||
SubfleetRepository $subfleetRepo,
|
||||
FareRepository $fareRepo
|
||||
)
|
||||
{
|
||||
$this->subfleetRepo = $subfleetRepo;
|
||||
$this->fareRepo = $fareRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +104,11 @@ class SubfleetController extends BaseController
|
||||
return redirect(route('admin.subfleets.index'));
|
||||
}
|
||||
|
||||
return view('admin.subfleets.show', ['subfleet' => $subfleet]);
|
||||
$avail_fares = $this->getAvailFares($subfleet);
|
||||
return view('admin.subfleets.show', [
|
||||
'subfleet' => $subfleet,
|
||||
'avail_fares' => $avail_fares,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,4 +177,60 @@ class SubfleetController extends BaseController
|
||||
Flash::success('Subfleet deleted successfully.');
|
||||
return redirect(route('admin.subfleets.index'));
|
||||
}
|
||||
|
||||
protected function return_fares_view(Subfleet $subfleet)
|
||||
{
|
||||
$subfleet->refresh();
|
||||
$avail_fares = $this->getAvailFares($subfleet);
|
||||
|
||||
return view('admin.subfleets.fares', [
|
||||
'subfleet' => $subfleet,
|
||||
'avail_fares' => $avail_fares,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function fares(Request $request)
|
||||
{
|
||||
$id = $request->id;
|
||||
|
||||
$subfleet = $this->subfleetRepo->findWithoutFail($id);
|
||||
if (empty($subfleet)) {
|
||||
return view('admin.aircraft.fares', ['fares' => []]);
|
||||
}
|
||||
|
||||
$fare_svc = app('App\Services\FareService');
|
||||
|
||||
if ($request->isMethod('get')) {
|
||||
return $this->return_fares_view($subfleet);
|
||||
}
|
||||
|
||||
/**
|
||||
* update specific fare data
|
||||
*/
|
||||
if ($request->isMethod('post')) {
|
||||
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
|
||||
$fare_svc->setForAircraft($subfleet, $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;
|
||||
$fare_svc->setForAircraft($subfleet, $fare, $override);
|
||||
}
|
||||
|
||||
// dissassociate fare from teh aircraft
|
||||
elseif ($request->isMethod('delete')) {
|
||||
$fare = $this->fareRepo->findWithoutFail($request->fare_id);
|
||||
$fare_svc->delFromAircraft($subfleet, $fare);
|
||||
}
|
||||
|
||||
return $this->return_fares_view($subfleet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,14 +55,6 @@ class Aircraft extends Model
|
||||
* foreign keys
|
||||
*/
|
||||
|
||||
public function fares()
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
'App\Models\Fare',
|
||||
'aircraft_fare'
|
||||
)->withPivot('price', 'cost', 'capacity');
|
||||
}
|
||||
|
||||
public function subfleet()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Subfleet', 'subfleet_id');
|
||||
|
||||
@@ -55,10 +55,10 @@ class Fare extends Model
|
||||
* any foreign keys
|
||||
*/
|
||||
|
||||
public function aircraft() {
|
||||
public function subfleets() {
|
||||
return $this->belongsToMany(
|
||||
'App\Models\Aircraft',
|
||||
'aircraft_fare'
|
||||
'App\Models\Subfleet',
|
||||
'subfleet_fare'
|
||||
)->withPivot('price', 'cost', 'capacity');
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,14 @@ class Subfleet extends Model
|
||||
return $this->belongsTo('App\Models\Airline', 'airline_id');
|
||||
}
|
||||
|
||||
public function fares()
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
'App\Models\Fare',
|
||||
'subfleet_fare'
|
||||
)->withPivot('price', 'cost', 'capacity');
|
||||
}
|
||||
|
||||
public function flights()
|
||||
{
|
||||
return $this->belongsToMany('App\Models\Flight', 'subfleet_flight');
|
||||
|
||||
9
app/Services/DatabaseService.php
Normal file
9
app/Services/DatabaseService.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
|
||||
class DatabaseService extends BaseService {
|
||||
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Subfleet;
|
||||
use App\Models\Fare;
|
||||
|
||||
class FareService extends BaseService {
|
||||
@@ -10,39 +10,39 @@ class FareService extends BaseService {
|
||||
/**
|
||||
* Attach a fare to an aircraft
|
||||
*
|
||||
* @param Aircraft $aircraft
|
||||
* @param Aircraft $subfleet
|
||||
* @param Fare $fare
|
||||
* @param array set the price/cost/capacity
|
||||
*
|
||||
* @return Aircraft
|
||||
*/
|
||||
public function setForAircraft(
|
||||
Aircraft &$aircraft,
|
||||
Subfleet &$subfleet,
|
||||
Fare &$fare,
|
||||
array $override=[]
|
||||
) {
|
||||
$aircraft->fares()->syncWithoutDetaching([$fare->id]);
|
||||
$subfleet->fares()->syncWithoutDetaching([$fare->id]);
|
||||
|
||||
# modify any pivot values?
|
||||
if(count($override) > 0) {
|
||||
$aircraft->fares()->updateExistingPivot($fare->id, $override);
|
||||
$subfleet->fares()->updateExistingPivot($fare->id, $override);
|
||||
}
|
||||
|
||||
$aircraft->save();
|
||||
$aircraft = $aircraft->fresh();
|
||||
return $aircraft;
|
||||
$subfleet->save();
|
||||
$subfleet = $subfleet->fresh();
|
||||
return $subfleet;
|
||||
}
|
||||
|
||||
/**
|
||||
* return all the fares for an aircraft. check the pivot
|
||||
* table to see if the price/cost/capacity has been overridden
|
||||
* and return the correct amounts.
|
||||
* @param Aircraft $aircraft
|
||||
* @param Aircraft $subfleet
|
||||
* @return Fare[]
|
||||
*/
|
||||
public function getForAircraft(Aircraft &$aircraft)
|
||||
public function getForAircraft(Subfleet &$subfleet)
|
||||
{
|
||||
$fares = $aircraft->fares->map(function($fare) {
|
||||
$fares = $subfleet->fares->map(function($fare) {
|
||||
if(!is_null($fare->pivot->price)) {
|
||||
$fare->price = $fare->pivot->price;
|
||||
}
|
||||
@@ -61,10 +61,10 @@ class FareService extends BaseService {
|
||||
return $fares;
|
||||
}
|
||||
|
||||
public function delFromAircraft(Aircraft &$aircraft, Fare &$fare)
|
||||
public function delFromAircraft(Subfleet &$subfleet, Fare &$fare)
|
||||
{
|
||||
$aircraft->fares()->detach($fare->id);
|
||||
$aircraft = $aircraft->fresh();
|
||||
return $aircraft;
|
||||
$subfleet->fares()->detach($fare->id);
|
||||
$subfleet = $subfleet->fresh();
|
||||
return $subfleet;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user