specify fares, js to dynamically change fare form; get applicable fares for the flight/pirep #125
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
|
||||
@@ -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]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
41
app/Models/PirepFare.php
Normal file
41
app/Models/PirepFare.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
/**
|
||||
* Class PirepFare
|
||||
* @package App\Models
|
||||
*/
|
||||
class PirepFare extends BaseModel
|
||||
{
|
||||
public $table = 'pirep_fares';
|
||||
public $timestamps = false;
|
||||
|
||||
public $fillable = [
|
||||
'pirep_id',
|
||||
'fare_id',
|
||||
'count',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'count' => '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');
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
25
app/Services/FinanceService.php
Normal file
25
app/Services/FinanceService.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class FinanceService extends BaseService
|
||||
{
|
||||
private $fareSvc,
|
||||
$flightSvc;
|
||||
|
||||
/**
|
||||
* FinanceService constructor.
|
||||
* @param FareService $fareSvc
|
||||
* @param FlightService $flightSvc
|
||||
*/
|
||||
public function __construct(
|
||||
FareService $fareSvc,
|
||||
FlightService $flightSvc
|
||||
) {
|
||||
$this->fareSvc = $fareSvc;
|
||||
$this->flightSvc = $flightSvc;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user