From 58e0f50c48cc507fa1aca0f6d72d324485afa459 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 24 Feb 2018 15:38:25 -0600 Subject: [PATCH] specify fares, js to dynamically change fare form; get applicable fares for the flight/pirep #125 --- .../2017_06_28_195426_create_pirep_tables.php | 19 +- app/Database/seeds/sample.yml | 9 +- app/Exceptions/Handler.php | 2 +- .../Controllers/Admin/PirepController.php | 105 +++++++- .../Controllers/Frontend/PirepController.php | 216 ++++++++++++--- app/Models/Pirep.php | 5 + app/Models/PirepFare.php | 41 +++ app/Repositories/FlightRepository.php | 29 ++ app/Routes/admin.php | 3 +- app/Routes/web.php | 1 + app/Services/FareService.php | 45 +++- app/Services/FinanceService.php | 25 ++ app/Services/FlightService.php | 35 ++- app/Services/PIREPService.php | 24 ++ resources/views/admin/pireps/edit.blade.php | 7 - resources/views/admin/pireps/fares.blade.php | 29 ++ .../views/admin/pireps/field_values.blade.php | 35 ++- resources/views/admin/pireps/fields.blade.php | 161 ++++++++--- .../views/admin/pireps/scripts.blade.php | 23 ++ .../layouts/default/components/info.blade.php | 10 + .../layouts/default/pireps/create.blade.php | 12 +- .../layouts/default/pireps/edit.blade.php | 16 ++ .../layouts/default/pireps/fares.blade.php | 27 ++ .../layouts/default/pireps/fields.blade.php | 251 ++++++++++++------ .../layouts/default/pireps/script.blade.php | 36 --- .../layouts/default/pireps/scripts.blade.php | 28 ++ .../layouts/default/pireps/show.blade.php | 35 ++- tests/FinanceTest.php | 99 +++++++ 28 files changed, 1063 insertions(+), 265 deletions(-) create mode 100644 app/Models/PirepFare.php create mode 100644 app/Services/FinanceService.php create mode 100644 resources/views/admin/pireps/fares.blade.php create mode 100644 resources/views/layouts/default/components/info.blade.php create mode 100644 resources/views/layouts/default/pireps/edit.blade.php create mode 100644 resources/views/layouts/default/pireps/fares.blade.php delete mode 100644 resources/views/layouts/default/pireps/script.blade.php create mode 100644 resources/views/layouts/default/pireps/scripts.blade.php diff --git a/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php b/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php index f139be5a..fa0b6b61 100644 --- a/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php +++ b/app/Database/migrations/2017_06_28_195426_create_pirep_tables.php @@ -60,30 +60,15 @@ class CreatePirepTables extends Migration $table->timestamps(); }); - /* - * Financial tables/fields - */ - Schema::create('pirep_expenses', function (Blueprint $table) { - $table->bigIncrements('id'); - $table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH); - $table->string('name'); - $table->double('value')->nullable(); - - $table->index('pirep_id'); - }); - Schema::create('pirep_fares', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH); - $table->unsignedBigInteger('fare_id'); + $table->unsignedInteger('fare_id'); $table->unsignedInteger('count')->nullable(); $table->index('pirep_id'); }); - /* - * Additional PIREP data - */ Schema::create('pirep_fields', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name', 50); @@ -95,6 +80,7 @@ class CreatePirepTables extends Migration $table->bigIncrements('id'); $table->string('pirep_id', \App\Models\Pirep::ID_MAX_LENGTH); $table->string('name', 50); + $table->string('slug', 50)->nullable(); $table->string('value')->nullable(); $table->string('source')->nullable(); $table->timestamps(); @@ -112,7 +98,6 @@ class CreatePirepTables extends Migration { Schema::dropIfExists('pireps'); Schema::dropIfExists('pirep_comments'); - Schema::dropIfExists('pirep_expenses'); Schema::dropIfExists('pirep_fares'); Schema::dropIfExists('pirep_fields'); Schema::dropIfExists('pirep_field_values'); diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index 52e20fcc..a6d401d0 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -353,7 +353,14 @@ pirep_field_values: - id: 1 pirep_id: pirepid_1 name: arrival gate - value: B14 + slug: arrival_gate + value: 10 + source: manual + - id: 2 + pirep_id: pirepid_1 + name: departure gate + slug: departure_gate + value: B32 source: manual pirep_comments: diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 8d1c7053..c1a9f72c 100755 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -66,7 +66,7 @@ class Handler extends ExceptionHandler */ public function render($request, Exception $exception) { - if ($request->expectsJson() || $request->is('api/*')) { + if ($request->is('api/*')) { $headers = []; diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index 8524a3d1..01c75014 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -5,11 +5,14 @@ namespace App\Http\Controllers\Admin; use App\Facades\Utils; use App\Http\Requests\CreatePirepRequest; use App\Http\Requests\UpdatePirepRequest; +use App\Models\Enums\PirepSource; use App\Models\Enums\PirepState; +use App\Models\Pirep; use App\Models\PirepComment; use App\Repositories\AircraftRepository; use App\Repositories\AirlineRepository; use App\Repositories\AirportRepository; +use App\Repositories\PirepFieldRepository; use App\Repositories\PirepRepository; use App\Repositories\SubfleetRepository; use App\Services\PIREPService; @@ -30,6 +33,7 @@ class PirepController extends BaseController $aircraftRepo, $pirepSvc, $pirepRepo, + $pirepFieldRepo, $subfleetRepo, $userSvc; @@ -48,6 +52,7 @@ class PirepController extends BaseController AirlineRepository $airlineRepo, AircraftRepository $aircraftRepo, PirepRepository $pirepRepo, + PirepFieldRepository $pirepFieldRepo, PIREPService $pirepSvc, SubfleetRepository $subfleetRepo, UserService $userSvc @@ -56,6 +61,7 @@ class PirepController extends BaseController $this->airlineRepo = $airlineRepo; $this->aircraftRepo = $aircraftRepo; $this->pirepRepo = $pirepRepo; + $this->pirepFieldRepo = $pirepFieldRepo; $this->pirepSvc = $pirepSvc; $this->subfleetRepo = $subfleetRepo; $this->userSvc = $userSvc; @@ -88,6 +94,77 @@ class PirepController extends BaseController return $aircraft; } + /** + * Save any custom fields found + * @param Pirep $pirep + * @param Request $request + */ + protected function saveCustomFields(Pirep $pirep, Request $request) + { + $custom_fields = []; + $pirep_fields = $this->pirepFieldRepo->all(); + foreach ($pirep_fields as $field) { + if (!$request->filled($field->slug)) { + continue; + } + + $custom_fields[] = [ + 'name' => $field->name, + 'value' => $request->input($field->slug), + 'source' => PirepSource::MANUAL + ]; + } + + Log::info('PIREP Custom Fields', $custom_fields); + $this->pirepSvc->updateCustomFields($pirep->id, $custom_fields); + } + + /** + * Save the fares that have been specified/saved + * @param Pirep $pirep + * @param Request $request + * @throws \Exception + */ + protected function saveFares(Pirep $pirep, Request $request) + { + $fares = []; + foreach ($pirep->aircraft->subfleet->fares as $fare) { + + $field_name = 'fare_' . $fare->id; + if (!$request->filled($field_name)) { + $count = 0; + } else { + $count = $request->input($field_name); + } + + $fares[] = [ + 'fare_id' => $fare->id, + 'count' => $count, + ]; + } + + $this->pirepSvc->saveFares($pirep->id, $fares); + } + + /** + * Return the fares form for a given aircraft + * @param Request $request + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ + public function fares(Request $request) + { + $aircraft_id = $request->input('aircraft_id'); + Log::info($aircraft_id); + + $aircraft = $this->aircraftRepo->find($aircraft_id); + Log::info('aircraft', $aircraft->toArray()); + + return view('admin.pireps.fares', [ + 'aircraft' => $aircraft, + 'read_only' => false, + ]); + } + /** * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View @@ -144,6 +221,7 @@ class PirepController extends BaseController * @param CreatePirepRequest $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector * @throws \Prettus\Validator\Exceptions\ValidatorException + * @throws \Exception */ public function store(CreatePirepRequest $request) { @@ -154,6 +232,9 @@ class PirepController extends BaseController $minutes = (int) $attrs['minutes']; $pirep->flight_time = Utils::hoursToMinutes($hours) + $minutes; + $this->saveCustomFields($pirep, $request); + $this->saveFares($pirep, $request); + Flash::success('Pirep saved successfully.'); return redirect(route('admin.pireps.index')); } @@ -195,17 +276,26 @@ class PirepController extends BaseController $pirep->minutes = $time->minutes; # Can we modify? - $read_only = false; - if($pirep->state !== PirepState::PENDING) { - $read_only = false; + $read_only = $pirep->state !== PirepState::PENDING; + + # set the custom fields + foreach ($pirep->fields as $field) { + $pirep->{$field->slug} = $field->value; + } + + # set the fares + foreach ($pirep->fares as $fare) { + $field_name = 'fare_' . $fare->fare_id; + $pirep->{$field_name} = $fare->count; } return view('admin.pireps.edit', [ 'pirep' => $pirep, 'read_only' => $read_only, - 'aircraft' => $this->aircraftList(), - 'airports' => $this->airportRepo->selectBoxList(), - 'airlines' => $this->airlineRepo->selectBoxList(), + 'aircraft' => $pirep->aircraft, + 'aircraft_list' => $this->aircraftList(), + 'airports_list' => $this->airportRepo->selectBoxList(), + 'airlines_list' => $this->airlineRepo->selectBoxList(), ]); } @@ -242,6 +332,9 @@ class PirepController extends BaseController $this->pirepSvc->saveRoute($pirep); } + $this->saveCustomFields($pirep, $request); + $this->saveFares($pirep, $request); + Flash::success('Pirep updated successfully.'); return redirect(route('admin.pireps.index')); } diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index 79a0c282..c0d4ab18 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -5,20 +5,21 @@ namespace App\Http\Controllers\Frontend; use App\Facades\Utils; use App\Http\Controllers\Controller; use App\Http\Requests\CreatePirepRequest; -use App\Models\Enums\AcarsType; +use App\Http\Requests\UpdatePirepRequest; use App\Models\Enums\PirepSource; use App\Models\Enums\PirepState; use App\Models\Pirep; -use App\Repositories\AcarsRepository; +use App\Repositories\AircraftRepository; use App\Repositories\AirlineRepository; use App\Repositories\AirportRepository; use App\Repositories\Criteria\WhereCriteria; use App\Repositories\PirepFieldRepository; use App\Repositories\PirepRepository; -use App\Repositories\SubfleetRepository; use App\Services\GeoService; use App\Services\PIREPService; use App\Services\UserService; +use App\Support\Units\Time; +use Flash; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Log; @@ -26,40 +27,40 @@ use Log; class PirepController extends Controller { - private $airlineRepo, + private $aircraftRepo, + $airlineRepo, $pirepRepo, $airportRepo, $pirepFieldRepo, $geoSvc, $pirepSvc, - $subfleetRepo, $userSvc; /** * PirepController constructor. + * @param AircraftRepository $aircraftRepo * @param AirlineRepository $airlineRepo - * @param PirepRepository $pirepRepo * @param AirportRepository $airportRepo + * @param PirepRepository $pirepRepo * @param PirepFieldRepository $pirepFieldRepo * @param GeoService $geoSvc - * @param SubfleetRepository $subfleetRepo * @param PIREPService $pirepSvc * @param UserService $userSvc */ public function __construct( + AircraftRepository $aircraftRepo, AirlineRepository $airlineRepo, - PirepRepository $pirepRepo, AirportRepository $airportRepo, + PirepRepository $pirepRepo, PirepFieldRepository $pirepFieldRepo, GeoService $geoSvc, - SubfleetRepository $subfleetRepo, PIREPService $pirepSvc, UserService $userSvc ) { + $this->aircraftRepo = $aircraftRepo; $this->airlineRepo = $airlineRepo; $this->pirepRepo = $pirepRepo; $this->airportRepo = $airportRepo; - $this->subfleetRepo = $subfleetRepo; $this->pirepFieldRepo = $pirepFieldRepo; $this->geoSvc = $geoSvc; @@ -93,6 +94,58 @@ class PirepController extends Controller return $aircraft; } + /** + * Save any custom fields found + * @param Pirep $pirep + * @param Request $request + */ + protected function saveCustomFields(Pirep $pirep, Request $request) + { + $custom_fields = []; + $pirep_fields = $this->pirepFieldRepo->all(); + foreach ($pirep_fields as $field) { + if (!$request->filled($field->slug)) { + continue; + } + + $custom_fields[] = [ + 'name' => $field->name, + 'value' => $request->input($field->slug), + 'source' => PirepSource::MANUAL + ]; + } + + Log::info('PIREP Custom Fields', $custom_fields); + $this->pirepSvc->updateCustomFields($pirep->id, $custom_fields); + } + + /** + * Save the fares that have been specified/saved + * @param Pirep $pirep + * @param Request $request + * @throws \Exception + */ + protected function saveFares(Pirep $pirep, Request $request) + { + $fares = []; + foreach($pirep->aircraft->subfleet->fares as $fare) { + + $field_name = 'fare_'.$fare->id; + if(!$request->filled($field_name)) { + $count = 0; + } else { + $count = $request->input($field_name); + } + + $fares[] = [ + 'fare_id' => $fare->id, + 'count' => $count, + ]; + } + + $this->pirepSvc->saveFares($pirep->id, $fares); + } + /** * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View @@ -114,14 +167,56 @@ class PirepController extends Controller ]); } + /** + * @param $id + * @return mixed + */ + public function show($id) + { + $pirep = $this->pirepRepo->find($id); + if (empty($pirep)) { + Flash::error('Pirep not found'); + return redirect(route('frontend.pirep.index')); + } + + $map_features = $this->geoSvc->pirepGeoJson($pirep); + + return $this->view('pireps.show', [ + 'pirep' => $pirep, + 'map_features' => $map_features, + ]); + } + + /** + * Return the fares form for a given aircraft + * @param Request $request + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ + public function fares(Request $request) + { + $aircraft_id = $request->input('aircraft_id'); + $aircraft = $this->aircraftRepo->find($aircraft_id); + + return $this->view('pireps.fares', [ + 'aircraft' => $aircraft, + 'read_only' => false, + ]); + } + + /** + * Create a new flight report + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ public function create() { $user = Auth::user(); return $this->view('pireps.create', [ - 'airlines' => $this->airlineRepo->selectBoxList(true), - 'aircraft' => $this->aircraftList($user, true), - 'airports' => $this->airportRepo->selectBoxList(true), + 'aircraft' => null, + 'read_only' => false, + 'airline_list' => $this->airlineRepo->selectBoxList(true), + 'aircraft_list' => $this->aircraftList($user, true), + 'airport_list' => $this->airportRepo->selectBoxList(true), 'pirep_fields' => $this->pirepFieldRepo->all(), 'field_values' => [], ]); @@ -150,45 +245,92 @@ class PirepController extends Controller $minutes = (int) $request->input('minutes', 0); $pirep->flight_time = Utils::hoursToMinutes($hours) + $minutes; - // The custom fields from the form - $custom_fields = []; - $pirep_fields = $this->pirepFieldRepo->all(); - foreach ($pirep_fields as $field) { - if(!$request->filled($field->slug)) { - continue; - } - - $custom_fields[] = [ - 'name' => $field->name, - 'value' => $request->input($field->slug), - 'source' => PirepSource::MANUAL - ]; - } - - Log::info('PIREP Custom Fields', $custom_fields); - $pirep = $this->pirepSvc->create($pirep, $custom_fields); + $pirep = $this->pirepSvc->create($pirep); + $this->saveCustomFields($pirep, $request); + $this->saveFares($pirep, $request); $this->pirepSvc->saveRoute($pirep); return redirect(route('frontend.pireps.show', ['id' => $pirep->id])); } /** - * @param $id + * Show the form for editing the specified Pirep. + * @param int $id * @return mixed */ - public function show($id) + public function edit($id) { - $pirep = $this->pirepRepo->find($id); + $pirep = $this->pirepRepo->findWithoutFail($id); if (empty($pirep)) { Flash::error('Pirep not found'); - return redirect(route('frontend.pirep.index')); + return redirect(route('frontend.pireps.index')); } - $map_features = $this->geoSvc->pirepGeoJson($pirep); + $time = new Time($pirep->flight_time); + $pirep->hours = $time->hours; + $pirep->minutes = $time->minutes; - return $this->view('pireps.show', [ + # Can we modify? + $read_only = $pirep->state !== PirepState::PENDING; + + # set the custom fields + foreach($pirep->fields as $field) { + $pirep->{$field->slug} = $field->value; + } + + # set the fares + foreach($pirep->fares as $fare) { + $field_name = 'fare_'.$fare->fare_id; + $pirep->{$field_name} = $fare->count; + } + + return $this->view('pireps.edit', [ 'pirep' => $pirep, - 'map_features' => $map_features, + 'read_only' => $read_only, + 'aircraft' => $pirep->aircraft, + 'aircraft_list' => $this->aircraftList(), + 'airline_list' => $this->airlineRepo->selectBoxList(), + 'airport_list' => $this->airportRepo->selectBoxList(), + 'pirep_fields' => $this->pirepFieldRepo->all(), ]); } + + /** + * @param $id + * @param UpdatePirepRequest $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \Prettus\Validator\Exceptions\ValidatorException + * @throws \Exception + */ + public function update($id, UpdatePirepRequest $request) + { + $pirep = $this->pirepRepo->findWithoutFail($id); + + if (empty($pirep)) { + Flash::error('Pirep not found'); + return redirect(route('admin.pireps.index')); + } + + $orig_route = $pirep->route; + + $attrs = $request->all(); + + # Fix the time + $attrs['flight_time'] = Time::init( + $attrs['minutes'], + $attrs['hours'])->getMinutes(); + + $pirep = $this->pirepRepo->update($attrs, $id); + + // A route change in the PIREP, so update the saved points in the ACARS table + if ($pirep->route !== $orig_route) { + $this->pirepSvc->saveRoute($pirep); + } + + $this->saveCustomFields($pirep, $request); + $this->saveFares($pirep, $request); + + Flash::success('Pirep updated successfully.'); + return redirect(route('frontend.pireps.show', ['id' => $pirep->id])); + } } diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 3245ac64..d9cf0928 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -283,6 +283,11 @@ class Pirep extends BaseModel ->orderBy('created_at', 'desc'); } + public function fares() + { + return $this->hasMany(PirepFare::class, 'pirep_id'); + } + public function fields() { return $this->hasMany(PirepFieldValues::class, 'pirep_id'); diff --git a/app/Models/PirepFare.php b/app/Models/PirepFare.php new file mode 100644 index 00000000..c1e5ca5c --- /dev/null +++ b/app/Models/PirepFare.php @@ -0,0 +1,41 @@ + 'integer', + ]; + + public static $rules = [ + 'count' => 'required', + ]; + + /** + * Relationships + */ + + public function fare() + { + return $this->belongsTo(Fare::class, 'fare_id'); + } + + public function pirep() + { + return $this->belongsTo(Pirep::class, 'pirep_id'); + } +} diff --git a/app/Repositories/FlightRepository.php b/app/Repositories/FlightRepository.php index 93860404..68349425 100644 --- a/app/Repositories/FlightRepository.php +++ b/app/Repositories/FlightRepository.php @@ -16,6 +16,8 @@ class FlightRepository extends BaseRepository implements CacheableInterface 'arr_airport_id', 'dpt_airport_id', 'flight_number' => 'like', + 'flight_code' => 'like', + 'flight_leg' => 'like', 'route' => 'like', 'notes' => 'like', ]; @@ -25,6 +27,33 @@ class FlightRepository extends BaseRepository implements CacheableInterface return Flight::class; } + /** + * Find a flight based on the given criterea + * @param $airline_id + * @param $flight_num + * @param null $flight_code + * @param null $flight_leg + * @return mixed + */ + public function findFlight($airline_id, $flight_num, $flight_code=null, $flight_leg=null) + { + $where = [ + 'airline_id' => $airline_id, + 'flight_num' => $flight_num, + 'active' => true, + ]; + + if(filled($flight_code)) { + $where['flight_code'] = $flight_code; + } + + if(filled('flight_leg')) { + $where['flight_leg'] = $flight_leg; + } + + return $this->findWhere($where); + } + /** * Create the search criteria and return this with the stuff pushed * @param Request $request diff --git a/app/Routes/admin.php b/app/Routes/admin.php index e03f5e34..2a031cce 100644 --- a/app/Routes/admin.php +++ b/app/Routes/admin.php @@ -37,8 +37,9 @@ Route::group([ Route::match(['post', 'put'], 'settings', 'SettingsController@update')->name('settings.update'); # pirep related routes + Route::get('pireps/fares', 'PirepController@fares'); + Route::get('pireps/pending', 'PirepController@pending'); Route::resource('pireps', 'PirepController'); - Route::match(['get'], 'pireps/pending', 'PirepController@pending'); Route::match(['get', 'post', 'delete'], 'pireps/{id}/comments', 'PirepController@comments'); Route::match(['post', 'put'], 'pireps/{id}/status', 'PirepController@status')->name('pirep.status'); diff --git a/app/Routes/web.php b/app/Routes/web.php index 738c77ac..c3c77016 100755 --- a/app/Routes/web.php +++ b/app/Routes/web.php @@ -29,6 +29,7 @@ Route::group([ Route::get('flights/search', 'FlightController@search')->name('flights.search'); Route::resource('flights', 'FlightController'); + Route::get('pireps/fares', 'PirepController@fares'); Route::resource('pireps', 'PirepController'); Route::get('profile/regen_apikey', 'ProfileController@regen_apikey') diff --git a/app/Services/FareService.php b/app/Services/FareService.php index ad022740..26340bab 100644 --- a/app/Services/FareService.php +++ b/app/Services/FareService.php @@ -6,9 +6,50 @@ use App\Models\Fare; use App\Models\Flight; use App\Models\Subfleet; use App\Support\Math; +use Illuminate\Support\Collection; +/** + * Class FareService + * @package App\Services + */ class FareService extends BaseService { + /** + * Get the fares for a particular flight, with an optional subfleet + * This will go through if there are any fares assigned to the flight, + * and then check the fares assigned on the subfleet, and give the + * final "authoritative" list of the fares for a flight. + * + * If a subfleet is passed in, + * @param Flight|null $flight + * @param Subfleet|null $subfleet + * @return Collection + */ + public function getAllFares($flight, $subfleet) + { + if(!$flight) { + $flight_fares = collect(); + } else { + $flight_fares = $this->getForFlight($flight); + } + + $subfleet_fares = $this->getForSubfleet($subfleet); + + # Go through all of the fares assigned by the subfleet + # See if any of the same fares are assigned to the flight + $fares = $subfleet_fares->map(function($fare, $idx) use ($flight_fares) + { + $flight_fare = $flight_fares->whereStrict('id', $fare->id)->first(); + if(!$flight_fare) { + return $fare; + } + + return $flight_fare; + }); + + return $fares; + } + /** * Get fares * @param $fare @@ -70,7 +111,7 @@ class FareService extends BaseService * table to see if the price/cost/capacity has been overridden * and return the correct amounts. * @param Flight $flight - * @return Fare[] + * @return Collection */ public function getForFlight(Flight $flight) { @@ -120,7 +161,7 @@ class FareService extends BaseService * table to see if the price/cost/capacity has been overridden * and return the correct amounts. * @param Subfleet $subfleet - * @return Fare[] + * @return Collection */ public function getForSubfleet(Subfleet $subfleet) { diff --git a/app/Services/FinanceService.php b/app/Services/FinanceService.php new file mode 100644 index 00000000..1b11d364 --- /dev/null +++ b/app/Services/FinanceService.php @@ -0,0 +1,25 @@ +fareSvc = $fareSvc; + $this->flightSvc = $flightSvc; + } +} diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index a7f9223e..0763465c 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -4,6 +4,7 @@ namespace App\Services; use App\Exceptions\BidExists; use App\Models\Flight; +use App\Models\Subfleet; use App\Models\User; use App\Models\UserBid; use App\Repositories\FlightRepository; @@ -16,13 +17,19 @@ use Log; */ class FlightService extends BaseService { - protected $flightRepo, $navDataRepo, $userSvc; + private $fareSvc, + $flightRepo, + $navDataRepo, + $userSvc; public function __construct( + FareService $fareSvc, FlightRepository $flightRepo, NavdataRepository $navdataRepo, UserService $userSvc - ) { + ) + { + $this->fareSvc = $fareSvc; $this->flightRepo = $flightRepo; $this->navDataRepo = $navdataRepo; $this->userSvc = $userSvc; @@ -41,7 +48,7 @@ class FlightService extends BaseService } return $this->flightRepo - ->whereOrder($where, 'flight_number', 'asc'); + ->whereOrder($where, 'flight_number', 'asc'); } /** @@ -70,8 +77,8 @@ class FlightService extends BaseService /** * Only allow aircraft that are at the current departure airport */ - if(setting('pireps.only_aircraft_at_dep_airport', false)) { - foreach($subfleets as $subfleet) { + if (setting('pireps.only_aircraft_at_dep_airport', false)) { + foreach ($subfleets as $subfleet) { $subfleet->aircraft = $subfleet->aircraft->filter( function ($aircraft, $i) use ($flight) { if ($aircraft->airport_id === $flight->dpt_airport_id) { @@ -106,11 +113,11 @@ class FlightService extends BaseService */ public function getRoute(Flight $flight) { - if(!$flight->route) { + if (!$flight->route) { return collect(); } - $route_points = array_map(function($point) { + $route_points = array_map(function ($point) { return strtoupper($point); }, explode(' ', $flight->route)); @@ -118,7 +125,7 @@ class FlightService extends BaseService // Put it back into the original order the route is in $return_points = []; - foreach($route_points as $rp) { + foreach ($route_points as $rp) { $return_points[] = $route->where('id', $rp)->first(); } @@ -135,15 +142,15 @@ class FlightService extends BaseService public function addBid(Flight $flight, User $user) { # If it's already been bid on, then it can't be bid on again - if($flight->has_bid && setting('bids.disable_flight_on_bid')) { + if ($flight->has_bid && setting('bids.disable_flight_on_bid')) { Log::info($flight->id . ' already has a bid, skipping'); throw new BidExists(); } # See if we're allowed to have multiple bids or not - if(!setting('bids.allow_multiple_bids')) { + if (!setting('bids.allow_multiple_bids')) { $user_bids = UserBid::where(['user_id' => $user->id])->first(); - if($user_bids) { + if ($user_bids) { Log::info('User "' . $user->id . '" already has bids, skipping'); throw new BidExists(); } @@ -156,7 +163,7 @@ class FlightService extends BaseService ]; $user_bid = UserBid::where($bid_data)->first(); - if($user_bid) { + if ($user_bid) { return $user_bid; } @@ -179,12 +186,12 @@ class FlightService extends BaseService 'flight_id' => $flight->id, 'user_id' => $user->id ])->first(); - if($user_bid) { + if ($user_bid) { $user_bid->forceDelete(); } # Only flip the flag if there are no bids left for this flight - if(!UserBid::where('flight_id', $flight->id)->exists()) { + if (!UserBid::where('flight_id', $flight->id)->exists()) { $flight->has_bid = false; $flight->save(); } diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index af692c9e..29178e0a 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -12,6 +12,7 @@ use App\Models\Enums\PirepSource; use App\Models\Enums\PirepState; use App\Models\Navdata; use App\Models\Pirep; +use App\Models\PirepFare; use App\Models\PirepFieldValues; use App\Models\User; use App\Repositories\AcarsRepository; @@ -210,6 +211,29 @@ class PIREPService extends BaseService } } + /** + * Save the list of fares + * @param $pirep_id + * @param array $fares ['field_id', 'count'] + * @throws \Exception + */ + public function saveFares($pirep_id, array $fares) + { + if(!$fares) { return; } + + # Remove all the previous fares + PirepFare::where('pirep_id', $pirep_id)->delete(); + + # Add them in + foreach($fares as $fare) { + $fare['pirep_id'] = $pirep_id; + # other fields: ['fare_id', 'count'] + + $field = new PirepFare($fare); + $field->save(); + } + } + /** * @param Pirep $pirep * @param int $new_state diff --git a/resources/views/admin/pireps/edit.blade.php b/resources/views/admin/pireps/edit.blade.php index 8b08d090..3e3dffc9 100644 --- a/resources/views/admin/pireps/edit.blade.php +++ b/resources/views/admin/pireps/edit.blade.php @@ -28,13 +28,6 @@ -
-
-

field values

- @include('admin.pireps.field_values') -
-
-

comments

diff --git a/resources/views/admin/pireps/fares.blade.php b/resources/views/admin/pireps/fares.blade.php new file mode 100644 index 00000000..b31871eb --- /dev/null +++ b/resources/views/admin/pireps/fares.blade.php @@ -0,0 +1,29 @@ +@if($aircraft) + + + + + + + + @foreach($aircraft->subfleet->fares as $fare) + + + + + @endforeach + +
Count
{!! $fare->name !!} ({!! $fare->code !!}) +
+ @if($read_only) +

{!! $pirep->{'fare_'.$fare->id} !!}

+ {!! Form::hidden('fare_'.$fare->id) !!} + @else + {!! Form::number('fare_'.$fare->id, null, [ + 'class' => 'form-control', + 'min' => 0, + 'readonly' => $read_only]) !!} + @endif +
+
+@endif diff --git a/resources/views/admin/pireps/field_values.blade.php b/resources/views/admin/pireps/field_values.blade.php index 92226c6b..aef679ba 100644 --- a/resources/views/admin/pireps/field_values.blade.php +++ b/resources/views/admin/pireps/field_values.blade.php @@ -1,35 +1,30 @@ -
- +
- + - @foreach($pirep->fields as $field) - - - + @endforeach
Name Value SourceActions
{!! $field->name !!} - {!! $field->value !!} + {!! $field->name !!} + @if($field->required === true) + * + @endif {!! PirepSource::label($field->source) !!} - {!! Form::open(['url' => '/admin/pireps/'.$pirep->id.'/fields', - 'method' => 'delete', - 'class' => 'pjax_form pirep_fields' - ]) !!} - {!! Form::hidden('field_id', $field->id) !!} -
- {{--{!! Form::button('', - ['type' => 'submit', - 'class' => 'btn btn-danger btn-xs']) - !!}--}} +
+
+ {!! Form::text($field->slug, null, [ + 'class' => 'form-control' + ]) !!}
- {!! Form::close() !!} +

{{ $errors->first($field->slug) }}

+
+ {!! PirepSource::label($field->source) !!}
-
diff --git a/resources/views/admin/pireps/fields.blade.php b/resources/views/admin/pireps/fields.blade.php index 25faea76..c9d4b1bf 100644 --- a/resources/views/admin/pireps/fields.blade.php +++ b/resources/views/admin/pireps/fields.blade.php @@ -1,23 +1,46 @@ +@if($read_only) +
+
+ @component('admin.components.info') + Once a PIREP has been accepted/rejected, certain fields go into read-only mode. + @endcomponent +
+
+@endif
{!! Form::label('flight_number', 'Flight Number/Route Code/Leg') !!} -
-
- {!! Form::text('flight_number', null, ['placeholder' => 'Flight Number', 'class' => 'form-control']) !!} -

{{ $errors->first('flight_number') }}

+ @if($read_only) +

{!! $pirep->ident !!} + {!! Form::hidden('flight_number') !!} + {!! Form::hidden('flight_code') !!} + {!! Form::hidden('flight_leg') !!} +

+ @else +
+
+ {!! Form::text('flight_number', null, [ + 'placeholder' => 'Flight Number', + 'class' => 'form-control']) !!} +

{{ $errors->first('flight_number') }}

+
+
+ {!! Form::text('route_code', null, [ + 'placeholder' => 'Code (optional)', + 'class' => 'form-control']) !!} +

{{ $errors->first('route_code') }}

+
+
+ {!! Form::text('route_leg', null, [ + 'placeholder' => 'Leg (optional)', + 'class' => 'form-control']) !!} +

{{ $errors->first('route_leg') }}

+
-
- {!! Form::text('route_code', null, ['placeholder' => 'Code (optional)', 'class' => 'form-control']) !!} -

{{ $errors->first('route_code') }}

-
-
- {!! Form::text('route_leg', null, ['placeholder' => 'Leg (optional)', 'class' => 'form-control']) !!} -

{{ $errors->first('route_leg') }}

-
-
+ @endif
-

Filed Using: +

Filed Via:

{!! PirepSource::label($pirep->source) !!} @if(filled($pirep->source_name)) ({!! $pirep->source_name !!}) @@ -27,44 +50,82 @@
{!! Form::label('airline_id', 'Airline') !!} -
-
- {!! Form::select('airline_id', $airlines, null, ['class' => 'form-control select2']) !!} -

{{ $errors->first('airline_id') }}

-
-
+ @if($read_only) +

{!! $pirep->airline->name !!}

+ {!! Form::hidden('airline_id') !!} + @else + {!! Form::select('airline_id', $airlines_list, null, [ + 'class' => 'form-control select2', + 'readonly' => $read_only]) !!} +

{{ $errors->first('airline_id') }}

+ @endif
{!! Form::label('aircraft_id', 'Aircraft:') !!} - {!! Form::select('aircraft_id', $aircraft, null, ['class' => 'form-control select2']) !!} -

{{ $errors->first('aircraft_id') }}

+ @if($read_only) +

{!! $pirep->aircraft->name !!}

+ {!! Form::hidden('aircraft_id') !!} + @else + {!! Form::select('aircraft_id', $aircraft_list, null, [ + 'id' => 'aircraft_select', + 'class' => 'form-control select2', + 'readonly' => $read_only + ]) !!} +

{{ $errors->first('aircraft_id') }}

+ @endif
{!! Form::label('dpt_airport_id', 'Departure Airport:') !!} - {!! Form::select('dpt_airport_id', $airports, null, ['class' => 'form-control select2']) !!} -

{{ $errors->first('dpt_airport_id') }}

+ @if($read_only) +

{!! $pirep->dpt_airport->id !!} - {!! $pirep->dpt_airport->name !!}

+ {!! Form::hidden('dpt_airport_id') !!} + @else + {!! Form::select('dpt_airport_id', $airports_list, null, [ + 'class' => 'form-control select2', + 'readonly' => $read_only]) !!} +

{{ $errors->first('dpt_airport_id') }}

+ @endif
{!! Form::label('arr_airport_id', 'Arrival Airport:') !!} - {!! Form::select('arr_airport_id', $airports, null, ['class' => 'form-control select2']) !!} -

{{ $errors->first('arr_airport_id') }}

+ @if($read_only) +

{!! $pirep->arr_airport->id !!} - {!! $pirep->arr_airport->name !!}

+ {!! Form::hidden('arr_airport_id') !!} + @else + {!! Form::select('arr_airport_id', $airports_list, null, ['class' => 'form-control select2']) !!} +

{{ $errors->first('arr_airport_id') }}

+ @endif
{!! Form::label('flight_time', 'Flight Time (hours & minutes):') !!} -
-
- {!! Form::number('hours', null, ['class' => 'form-control', 'placeholder' => 'hours', 'readonly' => $read_only]) !!} + @if($read_only) +

+ {!! $pirep->hours !!} hours, {!! $pirep->minutes !!} minutes + {!! Form::hidden('hours') !!} + {!! Form::hidden('minutes') !!} +

+ @else +
+
+ {!! Form::number('hours', null, [ + 'class' => 'form-control', + 'placeholder' => 'hours', + 'readonly' => $read_only]) !!} +
+
+ {!! Form::number('minutes', null, [ + 'class' => 'form-control', + 'placeholder' => 'minutes', + 'readonly' => $read_only]) !!} +
+

{{ $errors->first('hours') }}

+

{{ $errors->first('minutes') }}

-
- {!! Form::number('minutes', null, ['class' => 'form-control', 'placeholder' => 'minutes', 'readonly' => $read_only]) !!} -
-

{{ $errors->first('hours') }}

-

{{ $errors->first('minutes') }}

-
+ @endif
@@ -72,7 +133,7 @@ {!! Form::label('level', 'Flight Level:') !!}
- {!! Form::text('level', null, ['class' => 'form-control']) !!} + {!! Form::number('level', null, ['class' => 'form-control', 'min' => 0]) !!}

{{ $errors->first('level') }}

@@ -93,6 +154,34 @@

{{ $errors->first('notes') }}

+ +{{-- + FARES +--}} +
+
+
+

fares

+ {{-- You don't want to change this ID unless you don't want the fares form to work :) --}} +
+ @include('admin.pireps.fares') +
+
+
+ +{{-- + CUSTOM FIELDS +--}} + +
+
+
+

field values

+ {{-- You don't want to change this ID unless you don't want the fares form to work :) --}} + @include('admin.pireps.field_values') +
+
+
diff --git a/resources/views/admin/pireps/scripts.blade.php b/resources/views/admin/pireps/scripts.blade.php index 7dcc8c45..3f3a96da 100644 --- a/resources/views/admin/pireps/scripts.blade.php +++ b/resources/views/admin/pireps/scripts.blade.php @@ -17,6 +17,29 @@ const changeStatus = (values, fn) => { $(document).ready(function() { + const select_id = "select#aircraft_select"; + const destContainer = $('#fares_container'); + + $(select_id).change((e) => { + const aircraft_id = $(select_id + " option:selected").val(); + console.log('aircraft select change: ', aircraft_id); + + $.ajax({ + url: "{!! url('/admin/pireps/fares') !!}?aircraft_id=" + aircraft_id, + type: 'GET', + headers: { + 'x-api-key': '{!! Auth::user()->api_key !!}' + }, + success: (data) => { + console.log('returned new fares', data); + destContainer.html(data); + }, + error: () => { + destContainer.html(''); + } + }); + }); + $(document).on('submit', 'form.pjax_form', function (event) { event.preventDefault(); $.pjax.submit(event, '#pirep_comments_wrapper', {push: false}); diff --git a/resources/views/layouts/default/components/info.blade.php b/resources/views/layouts/default/components/info.blade.php new file mode 100644 index 00000000..a2d219ee --- /dev/null +++ b/resources/views/layouts/default/components/info.blade.php @@ -0,0 +1,10 @@ +
+

+ + + +

+

+ {{ $slot }} +

+
diff --git a/resources/views/layouts/default/pireps/create.blade.php b/resources/views/layouts/default/pireps/create.blade.php index 455eebfc..1ae43ac7 100644 --- a/resources/views/layouts/default/pireps/create.blade.php +++ b/resources/views/layouts/default/pireps/create.blade.php @@ -1,10 +1,10 @@ @extends("layouts.${SKIN_NAME}.app") -@section('title', 'file pirep') +@section('title', 'File Flight Report') @section('content')
-

new pilot report

+

New Flight Report

@include('flash::message') {!! Form::open(['route' => 'frontend.pireps.store']) !!} @@ -15,10 +15,4 @@
@endsection -@section('scripts') - -@endsection +@include("layouts.${SKIN_NAME}.pireps.scripts") diff --git a/resources/views/layouts/default/pireps/edit.blade.php b/resources/views/layouts/default/pireps/edit.blade.php new file mode 100644 index 00000000..7abc285a --- /dev/null +++ b/resources/views/layouts/default/pireps/edit.blade.php @@ -0,0 +1,16 @@ +@extends("layouts.${SKIN_NAME}.app") +@section('title', 'Edit Flight Report') +@section('content') +
+
+

Edit Flight Report

+ @include('flash::message') + {!! Form::model($pirep, ['route' => ['frontend.pireps.update', $pirep->id], 'method' => 'patch']) !!} + + @include("layouts.${SKIN_NAME}.pireps.fields") + + {!! Form::close() !!} +
+
+@endsection +@include("layouts.${SKIN_NAME}.pireps.scripts") diff --git a/resources/views/layouts/default/pireps/fares.blade.php b/resources/views/layouts/default/pireps/fares.blade.php new file mode 100644 index 00000000..9d38943c --- /dev/null +++ b/resources/views/layouts/default/pireps/fares.blade.php @@ -0,0 +1,27 @@ +@if($aircraft) +

Fares

+ + + + + + + + @foreach($aircraft->subfleet->fares as $fare) + + + + + @endforeach + +
Count
{!! $fare->name !!} ({!! $fare->code !!}) + @if($read_only) +

{!! $pirep->{'fare_'.$fare->id} !!}

+ {!! Form::hidden('fare_'.$fare->id) !!} + @else +
+ {!! Form::number('fare_'.$fare->id, null, ['class' => 'form-control', 'min' => 0]) !!} +
+ @endif +
+@endif diff --git a/resources/views/layouts/default/pireps/fields.blade.php b/resources/views/layouts/default/pireps/fields.blade.php index 055f6619..6b9f5c2e 100644 --- a/resources/views/layouts/default/pireps/fields.blade.php +++ b/resources/views/layouts/default/pireps/fields.blade.php @@ -1,3 +1,22 @@ +{{-- + +NOTE ABOUT THIS VIEW + +The fields that are marked "read-only", make sure the read-only status doesn't change! +If you make those fields editable, after they're in a read-only state, it can have +an impact on your stats and financials, and will require a recalculation of all the +flight reports that have been filed. You've been warned! + +--}} +@if($read_only) +
+
+ @component("layouts.${SKIN_NAME}.components.info") + Once a PIREP has been accepted/rejected, certain fields go into read-only mode. + @endcomponent +
+
+@endif
@@ -5,122 +24,202 @@ - - - + + - + @endif + + - - - + + - + @endif + + - - - + + - + @endif + + - - - + + - + @endif + + - - - + + - + @endif + + - - - + + - - - {{-- - Write out the custom fields, and label if they're required - --}} - @foreach($pirep_fields as $field) - - - - - @endforeach + @endif + + + {{-- + Write out the custom fields, and label if they're required + --}} + @foreach($pirep_fields as $field) - + + @endforeach - - - - + + + + + + + + +
Airline +
Airline + @if($read_only) +

{!! $pirep->airline->name !!}

+ {!! Form::hidden('airline_id') !!} + @else
- {!! Form::select('airline_id', $airlines, null, ['class' => 'custom-select select2']) !!} + {!! Form::select('airline_id', $airline_list, null, [ + 'class' => 'custom-select select2', + 'readonly' => $read_only]) !!}

{{ $errors->first('airline_id') }}

-
Flight Number/Code/Leg +
Flight Number/Code/Leg + @if($read_only) +

{!! $pirep->ident !!} + {!! Form::hidden('flight_number') !!} + {!! Form::hidden('flight_code') !!} + {!! Form::hidden('flight_leg') !!} +

+ @else
- {!! Form::text('flight_number', null, ['placeholder' => 'Flight Number', 'class' => 'form-control']) !!} - {!! Form::text('route_code', null, ['placeholder' => 'Code (optional)', 'class' => 'form-control']) !!} - {!! Form::text('route_leg', null, ['placeholder' => 'Leg (optional)', 'class' => 'form-control']) !!} + {!! Form::text('flight_number', null, [ + 'placeholder' => 'Flight Number', + 'class' => 'form-control', + 'readonly' => $read_only]) !!} + + {!! Form::text('route_code', null, [ + 'placeholder' => 'Code (optional)', + 'class' => 'form-control', + 'readonly' => $read_only]) !!} + + {!! Form::text('route_leg', null, [ + 'placeholder' => 'Leg (optional)', + 'class' => 'form-control', + 'readonly' => $read_only]) !!}

{{ $errors->first('flight_number') }}

{{ $errors->first('route_code') }}

{{ $errors->first('route_leg') }}

-
Aircraft +
Aircraft + @if($read_only) +

{!! $pirep->aircraft->name !!}

+ {!! Form::hidden('aircraft_id') !!} + @else
- {!! Form::select('aircraft_id', $aircraft, null, ['class' => 'custom-select select2']) !!} + {{-- You probably don't want to change this ID if you want the fare select to work --}} + {!! Form::select('aircraft_id', $aircraft_list, null, [ + 'id' => 'aircraft_select', + 'class' => 'custom-select select2', + 'readonly' => $read_only + ]) !!}

{{ $errors->first('aircraft_id') }}

-
Origin Airport +
Origin Airport + @if($read_only) +

{!! $pirep->dpt_airport->id !!} - {!! $pirep->dpt_airport->name !!}

+ {!! Form::hidden('dpt_airport_id') !!} + @else
- {!! Form::select('dpt_airport_id', $airports, null, ['class' => 'custom-select select2']) !!} + {!! Form::select('dpt_airport_id', $airport_list, null, [ + 'class' => 'custom-select select2', + 'readonly' => $read_only + ]) !!}

{{ $errors->first('dpt_airport_id') }}

-
Arrival Airport +
Arrival Airport + @if($read_only) +

{!! $pirep->arr_airport->id !!} + - {!! $pirep->arr_airport->name !!}

+ {!! Form::hidden('arr_airport_id') !!} + @else
- {!! Form::select('arr_airport_id', $airports, null, ['class' => 'custom-select select2']) !!} + {!! Form::select('arr_airport_id', $airport_list, null, [ + 'class' => 'custom-select select2', + 'readonly' => $read_only + ]) !!}

{{ $errors->first('arr_airport_id') }}

-
Flight Time +
Flight Time + @if($read_only) +

+ {!! $pirep->hours !!} hours, {!! $pirep->minutes !!} minutes + {!! Form::hidden('hours') !!} + {!! Form::hidden('minutes') !!} +

+ @else
- {!! Form::number('hours', null, ['class' => 'form-control', 'placeholder' => 'hours']) !!} - {!! Form::number('minutes', null, ['class' => 'form-control', 'placeholder' => 'minutes']) !!} + {!! Form::number('hours', null, [ + 'class' => 'form-control', + 'placeholder' => 'hours', + 'min' => '0', + 'readonly' => $read_only + ]) !!} + + {!! Form::number('minutes', null, [ + 'class' => 'form-control', + 'placeholder' => 'minutes', + 'min' => 0, + 'readonly' => $read_only + ]) !!}

{{ $errors->first('hours') }}

{{ $errors->first('minutes') }}

-
- {!! $field->name !!} - @if($field->required === true) - * - @endif - -
- {!! Form::text($field->slug, null, [ - 'class' => 'form-control' - ]) !!} -
-

{{ $errors->first($field->slug) }}

-
Route + {!! $field->name !!} + @if($field->required === true) + * + @endif +
- {!! Form::textarea('route', null, ['class' => 'form-control', 'placeholder' => 'Route']) !!} + {!! Form::text($field->slug, null, [ + 'class' => 'form-control' + ]) !!}
-

{{ $errors->first('route') }}

+

{{ $errors->first($field->slug) }}

Notes

-
- {!! Form::textarea('notes', null, ['class' => 'form-control', 'placeholder' => 'Notes']) !!} -
-

{{ $errors->first('notes') }}

-
Route +
+ {!! Form::textarea('route', null, ['class' => 'form-control', 'placeholder' => 'Route']) !!} +
+

{{ $errors->first('route') }}

+

Notes

+
+ {!! Form::textarea('notes', null, ['class' => 'form-control', 'placeholder' => 'Notes']) !!} +
+

{{ $errors->first('notes') }}

+
+
+
+ {{-- You don't want to change this ID unless you don't want the fares form to work :) --}} +
+ @include("layouts.${SKIN_NAME}.pireps.fares") +
+
+
-
- {!! Form::submit('Submit PIREP', ['class' => 'btn btn-primary']) !!} -
+
+ {!! Form::submit('Save PIREP', ['class' => 'btn btn-primary']) !!} +
diff --git a/resources/views/layouts/default/pireps/script.blade.php b/resources/views/layouts/default/pireps/script.blade.php deleted file mode 100644 index d0e3e8c8..00000000 --- a/resources/views/layouts/default/pireps/script.blade.php +++ /dev/null @@ -1,36 +0,0 @@ -@section('scripts') - -@endsection diff --git a/resources/views/layouts/default/pireps/scripts.blade.php b/resources/views/layouts/default/pireps/scripts.blade.php new file mode 100644 index 00000000..93d9d05f --- /dev/null +++ b/resources/views/layouts/default/pireps/scripts.blade.php @@ -0,0 +1,28 @@ +@section('scripts') + +@endsection diff --git a/resources/views/layouts/default/pireps/show.blade.php b/resources/views/layouts/default/pireps/show.blade.php index b4c79762..3f53d5ab 100644 --- a/resources/views/layouts/default/pireps/show.blade.php +++ b/resources/views/layouts/default/pireps/show.blade.php @@ -10,7 +10,7 @@
- +
Status @@ -23,6 +23,7 @@ @else
@endif + {!! PirepState::label($pirep->state) !!}
@@ -71,11 +72,15 @@ + {{-- + Show the fields that have been entered + --}} + @if(count($pirep->fields) > 0)

fields

- +
@@ -93,6 +98,32 @@ @endif + {{-- + Show the fares that have been entered + --}} + + @if(count($pirep->fares) > 0) +
+
+

fares

+
Name Value
+ + + + + + @foreach($pirep->fares as $fare) + + + + + @endforeach + +
ClassCount
{!! $fare->fare->name !!} ({!! $fare->fare->code !!}){!! $fare->count !!}
+
+
+ @endif + @include("layouts.${SKIN_NAME}.pireps.map") @if(count($pirep->acars_logs) > 0) diff --git a/tests/FinanceTest.php b/tests/FinanceTest.php index 83d83e5f..a0ce4bea 100644 --- a/tests/FinanceTest.php +++ b/tests/FinanceTest.php @@ -171,4 +171,103 @@ class FinanceTest extends TestCase $this->assertEquals($new_cost, $ac_fares[0]->cost); $this->assertEquals($new_capacity, $ac_fares[0]->capacity); } + + /** + * Test getting the fares from the flight svc. Have a few base fares + * and then override some of them + */ + public function testGetFaresWithOverrides() + { + $flight = factory(App\Models\Flight::class)->create(); + $subfleet = factory(App\Models\Subfleet::class)->create(); + [$fare1, $fare2, $fare3, $fare4] = factory(App\Models\Fare::class, 4)->create(); + + # add to the subfleet, and just override one of them + $this->fareSvc->setForSubfleet($subfleet, $fare1); + $this->fareSvc->setForSubfleet($subfleet, $fare2, [ + 'price' => 100, + 'cost' => 50, + 'capacity' => 25, + ]); + + $this->fareSvc->setForSubfleet($subfleet, $fare3); + + # Now set the last one to the flight and then override stuff + $this->fareSvc->setForFlight($flight, $fare3, [ + 'price' => '300%', + 'cost' => 250, + ]); + + $fare3_price = Math::addPercent($fare3->price, 300); + + # Assign another one to the flight, that's not on the subfleet + # This one should NOT be returned in the list of fares + $this->fareSvc->setForFlight($flight, $fare4); + + $fares = $this->fareSvc->getAllFares($flight, $subfleet); + $this->assertCount(3, $fares); + + foreach($fares as $fare) { + switch($fare->id) { + case $fare1->id: + $this->assertEquals($fare->price, $fare1->price); + $this->assertEquals($fare->cost, $fare1->cost); + $this->assertEquals($fare->capacity, $fare1->capacity); + break; + + case $fare2->id: + $this->assertEquals($fare->price, 100); + $this->assertEquals($fare->cost, 50); + $this->assertEquals($fare->capacity, 25); + break; + + case $fare3->id: + $this->assertEquals($fare->price, $fare3_price); + $this->assertEquals($fare->cost, 250); + $this->assertEquals($fare->capacity, $fare3->capacity); + break; + } + } + } + + public function testGetFaresNoFlightOverrides() + { + $subfleet = factory(App\Models\Subfleet::class)->create(); + [$fare1, $fare2, $fare3] = factory(App\Models\Fare::class, 3)->create(); + + # add to the subfleet, and just override one of them + $this->fareSvc->setForSubfleet($subfleet, $fare1); + $this->fareSvc->setForSubfleet($subfleet, $fare2, [ + 'price' => 100, + 'cost' => 50, + 'capacity' => 25, + ]); + + $this->fareSvc->setForSubfleet($subfleet, $fare3); + + $fares = $this->fareSvc->getAllFares(null, $subfleet); + $this->assertCount(3, $fares); + + foreach ($fares as $fare) { + switch ($fare->id) { + case $fare1->id: + $this->assertEquals($fare->price, $fare1->price); + $this->assertEquals($fare->cost, $fare1->cost); + $this->assertEquals($fare->capacity, $fare1->capacity); + break; + + case $fare2->id: + $this->assertEquals($fare->price, 100); + $this->assertEquals($fare->cost, 50); + $this->assertEquals($fare->capacity, 25); + break; + + case $fare3->id: + $this->assertEquals($fare->price, $fare3->price); + $this->assertEquals($fare->cost, $fare3->cost); + $this->assertEquals($fare->capacity, $fare3->capacity); + break; + } + } + } }