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 @@ -
| + | Count | + + + + @foreach($aircraft->subfleet->fares as $fare) +
|---|---|
| {!! $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 + |
+
| Name | +Value | Source | -Actions | @foreach($pirep->fields as $field)||
|---|---|---|---|---|---|
| {!! $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) !!}
|
|
{{ $errors->first('flight_number') }}
+ @if($read_only) +{!! $pirep->ident !!} + {!! Form::hidden('flight_number') !!} + {!! Form::hidden('flight_code') !!} + {!! Form::hidden('flight_leg') !!} +
+ @else +{{ $errors->first('flight_number') }}
+{{ $errors->first('route_code') }}
+{{ $errors->first('route_leg') }}
+{{ $errors->first('route_code') }}
-{{ $errors->first('route_leg') }}
-Filed Using: +
Filed Via:
{!! PirepSource::label($pirep->source) !!} @if(filled($pirep->source_name)) ({!! $pirep->source_name !!}) @@ -27,44 +50,82 @@{{ $errors->first('airline_id') }}
-{!! $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{{ $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{{ $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{{ $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+ {!! $pirep->hours !!} hours, {!! $pirep->minutes !!} minutes + {!! Form::hidden('hours') !!} + {!! Form::hidden('minutes') !!} +
+ @else +{{ $errors->first('hours') }}
+{{ $errors->first('minutes') }}
{{ $errors->first('hours') }}
-{{ $errors->first('minutes') }}
-{{ $errors->first('level') }}
{{ $errors->first('notes') }}
+ + + +
++ {{ $slot }} +
+| + | Count | + + + + @foreach($aircraft->subfleet->fares as $fare) +
|---|---|
| {!! $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
+ |
+
| 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') }} + |
+
| 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-
|