Add flight time to flight create/edit form

This commit is contained in:
Nabeel Shahzad 2018-02-19 13:33:26 -06:00
parent 246c828d7a
commit ad4259625c
4 changed files with 50 additions and 16 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Facades\Utils;
use App\Models\Enums\FlightType; use App\Models\Enums\FlightType;
use App\Models\Flight; use App\Models\Flight;
use App\Models\FlightFields; use App\Models\FlightFields;
@ -16,6 +17,7 @@ use App\Repositories\SubfleetRepository;
use App\Services\FareService; use App\Services\FareService;
use App\Services\FlightService; use App\Services\FlightService;
use App\Support\Units\Time;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Flash; use Flash;
use Response; use Response;
@ -121,7 +123,11 @@ class FlightController extends BaseController
return redirect()->back()->withInput($request->all()); return redirect()->back()->withInput($request->all());
} }
$input['active'] = get_truth_state($input['active']);; $input['active'] = get_truth_state($input['active']);
$time = new Time($input['minutes'], $input['hours']);
$input['flight_time'] = $time->getMinutes();
$flight = $this->flightRepo->create($input); $flight = $this->flightRepo->create($input);
Flash::success('Flight saved successfully.'); Flash::success('Flight saved successfully.');
@ -155,12 +161,15 @@ class FlightController extends BaseController
public function edit($id) public function edit($id)
{ {
$flight = $this->flightRepo->findWithoutFail($id); $flight = $this->flightRepo->findWithoutFail($id);
if (empty($flight)) { if (empty($flight)) {
Flash::error('Flight not found'); Flash::error('Flight not found');
return redirect(route('admin.flights.index')); return redirect(route('admin.flights.index'));
} }
$time = new Time($flight->flight_time);
$flight->hours = $time->hours;
$flight->minutes = $time->minutes;
$avail_subfleets = $this->getAvailSubfleets($flight); $avail_subfleets = $this->getAvailSubfleets($flight);
return view('admin.flights.edit', [ return view('admin.flights.edit', [
'flight' => $flight, 'flight' => $flight,
@ -209,7 +218,12 @@ class FlightController extends BaseController
return redirect()->back()->withInput($request->all()); return redirect()->back()->withInput($request->all());
} }
$input['flight_time'] = Time::init(
$input['minutes'],
$input['hours'])->getMinutes();
$input['active'] = get_truth_state($input['active']); $input['active'] = get_truth_state($input['active']);
$this->flightRepo->update($input, $id); $this->flightRepo->update($input, $id);
Flash::success('Flight updated successfully.'); Flash::success('Flight updated successfully.');

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Services\UserService; use App\Services\UserService;
use App\Support\Units\Time;
use Log; use Log;
use Flash; use Flash;
use Response; use Response;
@ -184,9 +185,9 @@ class PirepController extends BaseController
return redirect(route('admin.pireps.index')); return redirect(route('admin.pireps.index'));
} }
$hms = Utils::minutesToTimeParts($pirep->flight_time); $time = new Time($pirep->flight_time);
$pirep->hours = $hms['h']; $pirep->hours = $time->hours;
$pirep->minutes = $hms['m']; $pirep->minutes = $time->minutes;
# Can we modify? # Can we modify?
$read_only = false; $read_only = false;
@ -208,6 +209,7 @@ class PirepController extends BaseController
* @param UpdatePirepRequest $request * @param UpdatePirepRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Prettus\Validator\Exceptions\ValidatorException * @throws \Prettus\Validator\Exceptions\ValidatorException
* @throws \Exception
*/ */
public function update($id, UpdatePirepRequest $request) public function update($id, UpdatePirepRequest $request)
{ {
@ -224,9 +226,9 @@ class PirepController extends BaseController
$attrs = $request->all(); $attrs = $request->all();
# Fix the time # Fix the time
$hours = (int) $attrs['hours']; $attrs['flight_time'] = Time::init(
$minutes = (int) $attrs['minutes']; $attrs['minutes'],
$attrs['flight_time'] = Utils::hoursToMinutes($hours) + $minutes; $attrs['hours'])->getMinutes();
$pirep = $this->pirepRepo->update($attrs, $id); $pirep = $this->pirepRepo->update($attrs, $id);

View File

@ -12,6 +12,16 @@ class Time implements Arrayable
public $hours, public $hours,
$minutes; $minutes;
/**
* @param $minutes
* @param $hours
* @return static
*/
public static function init($minutes, $hours)
{
return new Time($minutes, $hours);
}
/** /**
* Pass just minutes to figure out how many hours * Pass just minutes to figure out how many hours
* Or both hours and minutes * Or both hours and minutes

View File

@ -28,10 +28,6 @@
</div> </div>
</div> </div>
<!--
SAME ROW
-->
<div class="row"> <div class="row">
<div class="form-group col-sm-3"> <div class="form-group col-sm-3">
@ -64,25 +60,37 @@ SAME ROW
<div class="row"> <div class="row">
<div class="form-group col-sm-3"> <div class="form-group col-sm-2">
{!! Form::label('dpt_time', 'Departure Time:') !!} {!! Form::label('dpt_time', 'Departure Time:') !!}
{!! Form::text('dpt_time', null, ['class' => 'form-control']) !!} {!! Form::text('dpt_time', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('dpt_time') }}</p> <p class="text-danger">{{ $errors->first('dpt_time') }}</p>
</div> </div>
<div class="form-group col-sm-3"> <div class="form-group col-sm-2">
{!! Form::label('arr_time', 'Arrival Time:') !!} {!! Form::label('arr_time', 'Arrival Time:') !!}
{!! Form::text('arr_time', null, ['class' => 'form-control']) !!} {!! Form::text('arr_time', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('arr_time') }}</p> <p class="text-danger">{{ $errors->first('arr_time') }}</p>
</div> </div>
<div class="form-group col-sm-3"> <div class="form-group col-sm-4">
{!! Form::label('flight_time', 'Flight Time (hours & minutes):') !!}
<div style="float: left">
{!! Form::number('hours', null, ['class' => 'form-control', 'placeholder' => 'hours']) !!}
</div>
<div style="float: left">
{!! Form::number('minutes', null, ['class' => 'form-control', 'placeholder' => 'minutes']) !!}
<p class="text-danger">{{ $errors->first('hours') }}</p>
<p class="text-danger">{{ $errors->first('minutes') }}</p>
</div>
</div>
<div class="form-group col-sm-2">
{!! Form::label('level', 'Flight Level:') !!} {!! Form::label('level', 'Flight Level:') !!}
{!! Form::text('level', null, ['class' => 'form-control']) !!} {!! Form::text('level', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('level') }}</p> <p class="text-danger">{{ $errors->first('level') }}</p>
</div> </div>
<div class="form-group col-sm-3"> <div class="form-group col-sm-2">
{!! Form::label('distance', 'Distance:') !!} <span class="small">in miles</span> {!! Form::label('distance', 'Distance:') !!} <span class="small">in miles</span>
{!! Form::text('distance', null, ['class' => 'form-control']) !!} {!! Form::text('distance', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('distance') }}</p> <p class="text-danger">{{ $errors->first('distance') }}</p>