#11 assign/remove fares from aircraft

This commit is contained in:
Nabeel Shahzad 2017-06-13 14:01:06 -05:00
parent 8c87c697ac
commit 4b137460e3
4 changed files with 53 additions and 11 deletions

View File

@ -16,6 +16,20 @@ class AircraftController extends BaseController
/** @var AircraftRepository */
private $aircraftRepository, $fareRepository;
protected function getAvailFares($all_fares, $attached_fares)
{
$retval = [];
$avail_fares = $all_fares->except($attached_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(AircraftRepository $aircraftRepo, FareRepository $fareRepo)
{
$this->fareRepository = $fareRepo;
@ -69,7 +83,7 @@ class AircraftController extends BaseController
$attached_fares = $aircraft->fares;
$all_fares = $this->fareRepository->all();
$avail_fares = $all_fares->except($attached_fares->modelKeys());
$avail_fares = $this->getAvailFares($all_fares, $attached_fares);
return view('admin.aircraft.show')
->with('aircraft', $aircraft)
@ -132,9 +146,10 @@ class AircraftController extends BaseController
protected function return_fares_view($aircraft)
{
$aircraft->refresh();
$attached_fares = $aircraft->fares;
$all_fares = $this->fareRepository->all();
$avail_fares = $all_fares->except($attached_fares->modelKeys());
$avail_fares = $this->getAvailFares($all_fares, $attached_fares);
return view('admin.aircraft.fares')
->with('aircraft', $aircraft)
@ -151,11 +166,15 @@ class AircraftController extends BaseController
return view('admin.aircraft.fares')->with('fares', []);
}
$fare_svc = app('App\Services\FareService');
// associate or dissociate the fare with this aircraft
if ($request->isMethod('post') || $request->isMethod('put')) {
// add
$fare = $this->fareRepository->findWithoutFail($request->fare_id);
$fare_svc->setForAircraft($aircraft, $fare);
} elseif ($request->isMethod('delete')) {
print_r($request->request);
$fare = $this->fareRepository->findWithoutFail($request->fare_id);
$fare_svc->delFromAircraft($aircraft, $fare);
}
return $this->return_fares_view($aircraft);

View File

@ -56,5 +56,25 @@
</tbody>
</table>
<hr />
{!! $avail_fares !!}
<div class="row">
<div class="col-xs-12" style="text-align: right;">
<div class="input-group input-group-lg">
{!! Form::open(['url' => '/admin/aircraft/'.$aircraft->id.'/fares',
'method' => 'post',
'class' => 'rm_fare form-inline'
])
!!}
{!! Form::select('fare_id', $avail_fares, null, [
'placeholder' => 'Select Fare',
'class' => 'ac-fare-dropdown form-control input-lg',
])
!!}
{!! Form::button('<i class="glyphicon glyphicon-plus"></i> add',
['type' => 'submit',
'class' => 'btn btn-success btn-s']) !!}
{!! Form::close() !!}
</div>
</div>
</div>
</div>

View File

@ -24,8 +24,8 @@
@section('scripts')
<script>
$(document).ready(function() {
$(".ac-fare-dropdown").select2();
$(document).on('submit', 'form.rm_fare', function(event) {
console.log('saving!');
event.preventDefault();
$.pjax.submit(event, '#aircraft_fares_wrapper', {push: false});
});

View File

@ -3,9 +3,11 @@
<div class="box box-solid">
<div class="box-header with-border">
{{--<i class="fa fa-text-width"></i>--}}
<h3 class="box-title">{!! Form::label('icao', 'ICAO:') !!}</h3>
<h3 class="box-title">{!! Form::label('icao', 'ICAO') !!}</h3>
</div>
<div class="box-body">
<p class="lead">{!! $aircraft->icao !!}/<strong>{!! $aircraft->class->code !!}</strong> ({!! $aircraft->class->name !!})</p>
</div>
<div class="box-body"><p class="lead">{!! $aircraft->icao !!}</p></div>
</div>
</div>
@ -14,7 +16,7 @@
<div class="box box-solid">
<div class="box-header with-border">
{{--<i class="fa fa-text-width"></i>--}}
<h3 class="box-title">{!! Form::label('name', 'Name:') !!}</h3>
<h3 class="box-title">{!! Form::label('name', 'Name') !!}</h3>
</div>
<div class="box-body"><p class="lead">{!! $aircraft->name !!}</p></div>
</div>
@ -25,9 +27,10 @@
<div class="box box-solid">
<div class="box-header with-border">
{{--<i class="fa fa-text-width"></i>--}}
<h3 class="box-title">{!! Form::label('registration', 'Registration:') !!}</h3>
<h3 class="box-title">{!! Form::label('registration', 'Registration') !!}/
{!! Form::label('tail_number', 'Tail Number') !!}</h3>
</div>
<div class="box-body"><p class="lead">{!! $aircraft->registration !!}</p></div>
<div class="box-body"><p class="lead">{!! $aircraft->registration !!}/{!! $aircraft->tail_number !!}</p></div>
</div>
</div>