#32 moved fares to subfleets

This commit is contained in:
Nabeel Shahzad 2017-06-24 11:09:27 -05:00
parent ca74afacd8
commit 3736e530bf
15 changed files with 216 additions and 144 deletions

View File

@ -17,23 +17,10 @@ class AircraftController extends BaseController
/** @var SubfleetRepository */ /** @var SubfleetRepository */
private $aircraftRepository, $fareRepository; private $aircraftRepository, $fareRepository;
protected function getAvailFares($aircraft) public function __construct(
{ SubfleetRepository $aircraftRepo,
$retval = []; FareRepository $fareRepo
$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)
{
$this->fareRepository = $fareRepo; $this->fareRepository = $fareRepo;
$this->aircraftRepository = $aircraftRepo; $this->aircraftRepository = $aircraftRepo;
} }
@ -70,6 +57,7 @@ class AircraftController extends BaseController
$aircraft = $this->aircraftRepository->create($input); $aircraft = $this->aircraftRepository->create($input);
Flash::success('Aircraft saved successfully.'); Flash::success('Aircraft saved successfully.');
return redirect(route('admin.aircraft.index')); return redirect(route('admin.aircraft.index'));
} }
@ -85,11 +73,8 @@ class AircraftController extends BaseController
return redirect(route('admin.aircraft.index')); return redirect(route('admin.aircraft.index'));
} }
$avail_fares = $this->getAvailFares($aircraft);
return view('admin.aircraft.show', [ return view('admin.aircraft.show', [
'aircraft' => $aircraft, 'aircraft' => $aircraft,
'avail_fares' => $avail_fares,
]); ]);
} }
@ -102,12 +87,13 @@ class AircraftController extends BaseController
if (empty($aircraft)) { if (empty($aircraft)) {
Flash::error('Aircraft not found'); Flash::error('Aircraft not found');
return redirect(route('admin.aircraft.index')); return redirect(route('admin.aircraft.index'));
} }
return view('admin.aircraft.edit', [ return view('admin.aircraft.edit', [
'subfleets' => Subfleet::all()->pluck('name', 'id'), 'subfleets' => Subfleet::all()->pluck('name', 'id'),
'aircraft' => $aircraft, 'aircraft' => $aircraft,
]); ]);
} }
@ -120,12 +106,14 @@ class AircraftController extends BaseController
if (empty($aircraft)) { if (empty($aircraft)) {
Flash::error('Aircraft not found'); Flash::error('Aircraft not found');
return redirect(route('admin.aircraft.index')); return redirect(route('admin.aircraft.index'));
} }
$aircraft = $this->aircraftRepository->update($request->all(), $id); $aircraft = $this->aircraftRepository->update($request->all(), $id);
Flash::success('Aircraft updated successfully.'); Flash::success('Aircraft updated successfully.');
return redirect(route('admin.aircraft.index')); return redirect(route('admin.aircraft.index'));
} }
@ -138,6 +126,7 @@ class AircraftController extends BaseController
if (empty($aircraft)) { if (empty($aircraft)) {
Flash::error('Aircraft not found'); Flash::error('Aircraft not found');
return redirect(route('admin.aircraft.index')); return redirect(route('admin.aircraft.index'));
} }
@ -148,54 +137,4 @@ class AircraftController extends BaseController
return redirect(route('admin.aircraft.index')); 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);
}
} }

View File

@ -4,8 +4,11 @@ namespace App\Http\Controllers\Admin;
use App\Http\Requests; use App\Http\Requests;
use App\Models\Airline; use App\Models\Airline;
use App\Models\Subfleet;
use App\Http\Requests\CreateSubfleetRequest; use App\Http\Requests\CreateSubfleetRequest;
use App\Http\Requests\UpdateSubfleetRequest; use App\Http\Requests\UpdateSubfleetRequest;
use App\Models\Fare;
use App\Repositories\FareRepository;
use App\Repositories\SubfleetRepository; use App\Repositories\SubfleetRepository;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Flash; use Flash;
@ -15,11 +18,30 @@ use Response;
class SubfleetController extends BaseController class SubfleetController extends BaseController
{ {
/** @var SubfleetRepository */ /** @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->subfleetRepo = $subfleetRepo;
$this->fareRepo = $fareRepo;
} }
/** /**
@ -82,7 +104,11 @@ class SubfleetController extends BaseController
return redirect(route('admin.subfleets.index')); 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.'); Flash::success('Subfleet deleted successfully.');
return redirect(route('admin.subfleets.index')); 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);
}
} }

View File

@ -55,14 +55,6 @@ class Aircraft extends Model
* foreign keys * foreign keys
*/ */
public function fares()
{
return $this->belongsToMany(
'App\Models\Fare',
'aircraft_fare'
)->withPivot('price', 'cost', 'capacity');
}
public function subfleet() public function subfleet()
{ {
return $this->belongsTo('App\Models\Subfleet', 'subfleet_id'); return $this->belongsTo('App\Models\Subfleet', 'subfleet_id');

View File

@ -55,10 +55,10 @@ class Fare extends Model
* any foreign keys * any foreign keys
*/ */
public function aircraft() { public function subfleets() {
return $this->belongsToMany( return $this->belongsToMany(
'App\Models\Aircraft', 'App\Models\Subfleet',
'aircraft_fare' 'subfleet_fare'
)->withPivot('price', 'cost', 'capacity'); )->withPivot('price', 'cost', 'capacity');
} }

View File

@ -50,6 +50,14 @@ class Subfleet extends Model
return $this->belongsTo('App\Models\Airline', 'airline_id'); 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() public function flights()
{ {
return $this->belongsToMany('App\Models\Flight', 'subfleet_flight'); return $this->belongsToMany('App\Models\Flight', 'subfleet_flight');

View File

@ -0,0 +1,9 @@
<?php
namespace App\Services;
class DatabaseService extends BaseService {
}

View File

@ -2,7 +2,7 @@
namespace App\Services; namespace App\Services;
use App\Models\Aircraft; use App\Models\Subfleet;
use App\Models\Fare; use App\Models\Fare;
class FareService extends BaseService { class FareService extends BaseService {
@ -10,39 +10,39 @@ class FareService extends BaseService {
/** /**
* Attach a fare to an aircraft * Attach a fare to an aircraft
* *
* @param Aircraft $aircraft * @param Aircraft $subfleet
* @param Fare $fare * @param Fare $fare
* @param array set the price/cost/capacity * @param array set the price/cost/capacity
* *
* @return Aircraft * @return Aircraft
*/ */
public function setForAircraft( public function setForAircraft(
Aircraft &$aircraft, Subfleet &$subfleet,
Fare &$fare, Fare &$fare,
array $override=[] array $override=[]
) { ) {
$aircraft->fares()->syncWithoutDetaching([$fare->id]); $subfleet->fares()->syncWithoutDetaching([$fare->id]);
# modify any pivot values? # modify any pivot values?
if(count($override) > 0) { if(count($override) > 0) {
$aircraft->fares()->updateExistingPivot($fare->id, $override); $subfleet->fares()->updateExistingPivot($fare->id, $override);
} }
$aircraft->save(); $subfleet->save();
$aircraft = $aircraft->fresh(); $subfleet = $subfleet->fresh();
return $aircraft; return $subfleet;
} }
/** /**
* return all the fares for an aircraft. check the pivot * return all the fares for an aircraft. check the pivot
* table to see if the price/cost/capacity has been overridden * table to see if the price/cost/capacity has been overridden
* and return the correct amounts. * and return the correct amounts.
* @param Aircraft $aircraft * @param Aircraft $subfleet
* @return Fare[] * @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)) { if(!is_null($fare->pivot->price)) {
$fare->price = $fare->pivot->price; $fare->price = $fare->pivot->price;
} }
@ -61,10 +61,10 @@ class FareService extends BaseService {
return $fares; return $fares;
} }
public function delFromAircraft(Aircraft &$aircraft, Fare &$fare) public function delFromAircraft(Subfleet &$subfleet, Fare &$fare)
{ {
$aircraft->fares()->detach($fare->id); $subfleet->fares()->detach($fare->id);
$aircraft = $aircraft->fresh(); $subfleet = $subfleet->fresh();
return $aircraft; return $subfleet;
} }
} }

View File

@ -24,18 +24,6 @@ class CreateFaresTable extends Migration
$table->boolean('active')->default(true); $table->boolean('active')->default(true);
$table->timestamps(); $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() public function down()
{ {
Schema::drop('fares'); Schema::drop('fares');
Schema::drop('aircraft_fare');
} }
} }

View File

@ -22,6 +22,18 @@ class CreateSubfleetsTable extends Migration
$table->softDeletes(); $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) { Schema::create('subfleet_flight', function(Blueprint $table) {
$table->integer('subfleet_id')->unsigned(); $table->integer('subfleet_id')->unsigned();
$table->integer('flight_id')->unsigned(); $table->integer('flight_id')->unsigned();

View File

@ -110,29 +110,6 @@ fares:
price: 800 price: 800
capacity: 5 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: flights:
- id: 1 - id: 1
airline_id: 1 airline_id: 1
@ -151,6 +128,29 @@ subfleets:
name: 777-200 LR name: 777-200 LR
type: 772-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_flight:
- subfleet_id: 1 - subfleet_id: 1
flight_id: 1 flight_id: 1

View File

@ -23,7 +23,7 @@
Fares assigned to the current aircraft. These can be overridden, Fares assigned to the current aircraft. These can be overridden,
otherwise, the value used is the default, which comes from the fare. otherwise, the value used is the default, which comes from the fare.
</div> </div>
@include('admin.aircraft.fares') @include('admin.subfleets.fares')
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,4 +1,4 @@
@extends('layouts.app') @extends('admin.app')
@section('content') @section('content')
<section class="content-header"> <section class="content-header">

View File

@ -34,7 +34,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach($aircraft->fares as $atf) @foreach($subfleet->fares as $atf)
<tr role="row" class="@if ($loop->iteration%2) even @else odd @endif"> <tr role="row" class="@if ($loop->iteration%2) even @else odd @endif">
<td class="sorting_1">{!! $atf->name !!}</td> <td class="sorting_1">{!! $atf->name !!}</td>
<td>{!! $atf->code !!}</td> <td>{!! $atf->code !!}</td>
@ -47,7 +47,7 @@
<span class="small background-color-grey-light">({!! $atf->cost!!})</span></td> <span class="small background-color-grey-light">({!! $atf->cost!!})</span></td>
<td style="text-align: right; width:3%;"> <td style="text-align: right; width:3%;">
<div class='btn-group'> <div class='btn-group'>
{!! 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::hidden('fare_id', $atf->id) !!}
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', {!! Form::button('<i class="glyphicon glyphicon-trash"></i>',
['type' => 'submit', ['type' => 'submit',
@ -63,7 +63,7 @@
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12">
<div class="input-group input-group-lg pull-right"> <div class="input-group input-group-lg pull-right">
{!! Form::open(['url' => '/admin/aircraft/'.$aircraft->id.'/fares', {!! Form::open(['url' => '/admin/subfleets/'.$subfleet->id.'/fares',
'method' => 'post', 'method' => 'post',
'class' => 'rm_fare form-inline' 'class' => 'rm_fare form-inline'
]) ])

View File

@ -9,9 +9,52 @@
<div class="box-body"> <div class="box-body">
<div class="row" style="padding-left: 20px"> <div class="row" style="padding-left: 20px">
@include('admin.subfleets.show_fields') @include('admin.subfleets.show_fields')
<a href="{!! route('admin.subfleets.index') !!}" class="btn btn-default">Back</a> </div>
</div>
</div>
<div class="box box-primary">
<div class="box-body">
<div class="row">
<div class="col-xs-12">
<h3>fares</h3>
<div class="box-body">
<div class="callout callout-info">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
Fares assigned to the current subfleet. These can be overridden,
otherwise, the value used is the default, which comes from the fare.
</div>
@include('admin.subfleets.fares')
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
@endsection @endsection
@section('scripts')
<script>
$(document).ready(function() {
$(".ac-fare-dropdown").select2();
$('#aircraft_fares a').editable({
type: 'text',
mode: 'inline',
emptytext: 'default',
url: '/admin/subfleets/{!! $subfleet->id !!}/fares',
title: 'Enter override value',
ajaxOptions: { 'type': 'put'},
params: function(params) {
return {
fare_id: params.pk,
name: params.name,
value: params.value
}
}
});
$(document).on('submit', 'form.rm_fare', function(event) {
event.preventDefault();
$.pjax.submit(event, '#aircraft_fares_wrapper', {push: false});
});
});
</script>
@endsection

View File

@ -14,10 +14,10 @@ Route::group([
# subfleet # subfleet
Route::resource('subfleets', 'SubfleetController'); Route::resource('subfleets', 'SubfleetController');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares');
# aircraft and fare associations # aircraft and fare associations
Route::resource('aircraft', 'AircraftController'); Route::resource('aircraft', 'AircraftController');
Route::match(['get', 'post', 'put', 'delete'], 'aircraft/{id}/fares', 'AircraftController@fares');
# flights and aircraft associations # flights and aircraft associations
Route::resource('flights', 'FlightController'); Route::resource('flights', 'FlightController');