diff --git a/app/Http/Controllers/Admin/AircraftController.php b/app/Http/Controllers/Admin/AircraftController.php index 9d55492e..31113fb4 100644 --- a/app/Http/Controllers/Admin/AircraftController.php +++ b/app/Http/Controllers/Admin/AircraftController.php @@ -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); - } } diff --git a/app/Http/Controllers/Admin/SubfleetController.php b/app/Http/Controllers/Admin/SubfleetController.php index a329d448..8412c231 100644 --- a/app/Http/Controllers/Admin/SubfleetController.php +++ b/app/Http/Controllers/Admin/SubfleetController.php @@ -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); + } } diff --git a/app/Models/Aircraft.php b/app/Models/Aircraft.php index e38473ef..1df4ca0e 100644 --- a/app/Models/Aircraft.php +++ b/app/Models/Aircraft.php @@ -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'); diff --git a/app/Models/Fare.php b/app/Models/Fare.php index fbf458c2..0eff7a20 100644 --- a/app/Models/Fare.php +++ b/app/Models/Fare.php @@ -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'); } diff --git a/app/Models/Subfleet.php b/app/Models/Subfleet.php index fa59fcf1..1586487e 100644 --- a/app/Models/Subfleet.php +++ b/app/Models/Subfleet.php @@ -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'); diff --git a/app/Services/DatabaseService.php b/app/Services/DatabaseService.php new file mode 100644 index 00000000..4fbbee9e --- /dev/null +++ b/app/Services/DatabaseService.php @@ -0,0 +1,9 @@ +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; } } diff --git a/database/migrations/2017_06_10_040335_create_fares_table.php b/database/migrations/2017_06_10_040335_create_fares_table.php index 68369248..68fc3a05 100644 --- a/database/migrations/2017_06_10_040335_create_fares_table.php +++ b/database/migrations/2017_06_10_040335_create_fares_table.php @@ -24,18 +24,6 @@ class CreateFaresTable extends Migration $table->boolean('active')->default(true); $table->timestamps(); }); - - Schema::create('aircraft_fare', function (Blueprint $table) { - $table->integer('aircraft_id')->unsigned(); - $table->integer('fare_id')->unsigned(); - $table->decimal('price', 19, 2)->nullable(); - $table->decimal('cost', 19, 2)->nullable(); - $table->integer('capacity')->nullable()->unsigned(); - $table->timestamps(); - - $table->primary(['aircraft_id', 'fare_id']); - $table->index(['fare_id', 'aircraft_id']); - }); } /** @@ -46,6 +34,5 @@ class CreateFaresTable extends Migration public function down() { Schema::drop('fares'); - Schema::drop('aircraft_fare'); } } diff --git a/database/migrations/2017_06_23_011011_create_subfleets_table.php b/database/migrations/2017_06_23_011011_create_subfleets_table.php index 6fcac74d..f539374b 100644 --- a/database/migrations/2017_06_23_011011_create_subfleets_table.php +++ b/database/migrations/2017_06_23_011011_create_subfleets_table.php @@ -22,6 +22,18 @@ class CreateSubfleetsTable extends Migration $table->softDeletes(); }); + Schema::create('subfleet_fare', function (Blueprint $table) { + $table->integer('subfleet_id')->unsigned(); + $table->integer('fare_id')->unsigned(); + $table->decimal('price', 19, 2)->nullable(); + $table->decimal('cost', 19, 2)->nullable(); + $table->integer('capacity')->nullable()->unsigned(); + $table->timestamps(); + + $table->primary(['subfleet_id', 'fare_id']); + $table->index(['fare_id', 'subfleet_id']); + }); + Schema::create('subfleet_flight', function(Blueprint $table) { $table->integer('subfleet_id')->unsigned(); $table->integer('flight_id')->unsigned(); diff --git a/database/seeds/dev.yml b/database/seeds/dev.yml index 41714388..a3550698 100644 --- a/database/seeds/dev.yml +++ b/database/seeds/dev.yml @@ -110,29 +110,6 @@ fares: price: 800 capacity: 5 -# add a few mods to aircraft and fares -aircraft_fare: - - # Fare classes on the 747 - - aircraft_id: 1 - fare_id: 1 - price: 200 - capacity: 400 - - aircraft_id: 1 - fare_id: 2 - capacity: 20 - - aircraft_id: 1 - fare_id: 3 - price: 1000 - capacity: 10 - - # Fare classes on the 777 - - aircraft_id: 2 - fare_id: 1 - - aircraft_id: 2 - fare_id: 3 - capacity: 10 - flights: - id: 1 airline_id: 1 @@ -151,6 +128,29 @@ subfleets: name: 777-200 LR type: 772-LR +# add a few mods to aircraft and fares +subfleet_fare: + + # Fare classes on the 747 + - subfleet_id: 1 + fare_id: 1 + price: 200 + capacity: 400 + - subfleet_id: 1 + fare_id: 2 + capacity: 20 + - subfleet_id: 1 + fare_id: 3 + price: 1000 + capacity: 10 + + # Fare classes on the 777 + - subfleet_id: 2 + fare_id: 1 + - subfleet_id: 2 + fare_id: 3 + capacity: 10 + subfleet_flight: - subfleet_id: 1 flight_id: 1 diff --git a/resources/views/admin/aircraft/show.blade.php b/resources/views/admin/aircraft/show.blade.php index 79eff73d..44f14327 100644 --- a/resources/views/admin/aircraft/show.blade.php +++ b/resources/views/admin/aircraft/show.blade.php @@ -23,7 +23,7 @@ Fares assigned to the current aircraft. These can be overridden, otherwise, the value used is the default, which comes from the fare. - @include('admin.aircraft.fares') + @include('admin.subfleets.fares') diff --git a/resources/views/admin/subfleets/edit.blade.php b/resources/views/admin/subfleets/edit.blade.php index 467daf3f..ca2954ec 100644 --- a/resources/views/admin/subfleets/edit.blade.php +++ b/resources/views/admin/subfleets/edit.blade.php @@ -1,4 +1,4 @@ -@extends('layouts.app') +@extends('admin.app') @section('content')
diff --git a/resources/views/admin/aircraft/fares.blade.php b/resources/views/admin/subfleets/fares.blade.php similarity index 93% rename from resources/views/admin/aircraft/fares.blade.php rename to resources/views/admin/subfleets/fares.blade.php index b0937a06..74ba2c9e 100644 --- a/resources/views/admin/aircraft/fares.blade.php +++ b/resources/views/admin/subfleets/fares.blade.php @@ -34,7 +34,7 @@ - @foreach($aircraft->fares as $atf) + @foreach($subfleet->fares as $atf) {!! $atf->name !!} {!! $atf->code !!} @@ -47,7 +47,7 @@ ({!! $atf->cost!!})
- {!! Form::open(['url' => '/admin/aircraft/'.$aircraft->id.'/fares', 'method' => 'delete', 'class' => 'rm_fare']) !!} + {!! Form::open(['url' => '/admin/subfleets/'.$subfleet->id.'/fares', 'method' => 'delete', 'class' => 'rm_fare']) !!} {!! Form::hidden('fare_id', $atf->id) !!} {!! Form::button('', ['type' => 'submit', @@ -63,7 +63,7 @@
- {!! Form::open(['url' => '/admin/aircraft/'.$aircraft->id.'/fares', + {!! Form::open(['url' => '/admin/subfleets/'.$subfleet->id.'/fares', 'method' => 'post', 'class' => 'rm_fare form-inline' ]) diff --git a/resources/views/admin/subfleets/show.blade.php b/resources/views/admin/subfleets/show.blade.php index 77073699..a9891af7 100644 --- a/resources/views/admin/subfleets/show.blade.php +++ b/resources/views/admin/subfleets/show.blade.php @@ -9,9 +9,52 @@
@include('admin.subfleets.show_fields') - Back +
+
+
+
+
+
+
+

fares

+
+
+    + Fares assigned to the current subfleet. These can be overridden, + otherwise, the value used is the default, which comes from the fare. +
+ @include('admin.subfleets.fares') +
+
@endsection +@section('scripts') + +@endsection diff --git a/routes/admin.php b/routes/admin.php index 9bf132bf..e1f93f27 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -14,10 +14,10 @@ Route::group([ # subfleet Route::resource('subfleets', 'SubfleetController'); + Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares'); # aircraft and fare associations Route::resource('aircraft', 'AircraftController'); - Route::match(['get', 'post', 'put', 'delete'], 'aircraft/{id}/fares', 'AircraftController@fares'); # flights and aircraft associations Route::resource('flights', 'FlightController');