* 391 Notification refactorings (#441) * Refactor notifications to allow easier plugins * Notification refactoring * Formatting * Move news to NewsService; cleanup of events * More refactoring; added send email out for news item and the template * Formatting * Formatting * Fix missing newsRepo (#445) * Refactor and add importer to Installer module #443 (#444) * Refactor and add importer to Installer module #443 * Refactor for finances to use in import * Import groups into roles * Formatting * Formatting * Add interface in installer for import * Notes about importing * Check for installer folder * Formatting * Fix pirep->user mapping * Unused import * Formatting * Replace importer with AJAX powered; better error handling #443 (#447) * Replace importer with AJAX powered; better error handling #443 * Formatting * Fix command line importer * Remove bootstrap cache (#448) * Cleanup the bootstrap/cache directory when packaging * Fix removal of bootstrap cache * Formatting * Stricter checks on ACARS API data (#451) * Stricter checks on ACARS API data * More checks * Fix for flight_number check forcing to exist * Allow nullable on flight_id * Avoid proc_open use #455 (#456) * Use PhpExecutableFinder() closes #457 #458 (#460) * Use DateTimeZone instead of int for creating datetime closes #461 * Fix CSV imports giving Storage class not found #454 (#462) * Fix CSV imports giving Storage class not found #454 * Update yarn files for security alert * Add PHP 7.4 support (#464) * Add PHP 7.4 to build matrix * DB fix * YAML parser fix in test data * Show versions * Package updates * Track used ICAOs * 7.4 METAR parsing fix * METAR parser fix * Formatting * Add meters to response units * Call instance for unit conversion * Return value * Catch exception for unknown quantity * Comment fix * Formatting * METAR parsing fixes on PHP 7.4 * Package updates * More random airport ID * More random airport ID * Properly disable toolbar * Semver written out to version file * Use dev as default identifier * Fix BindingResolutionError when debug toolbar isn't present (#465) * Fix BindingResolutionError when debug toolbar isn't present * Formatting * Split the importer module out from the installer module (#468) * Split the importer module out from the installer module * Cleanup of unused imports * Move updater into separate module #453 * Remove unused imports/formatting * Disable the install and importer modules at the end of the setup * Unused imports; update IJ style * test explicit stage for php+mysql * add more to matrix * Add different MariaDB versions * undo * Cleanup Model doc * Pilots cannot use the dashboard or flights without admin rights (#481) * Use auth middleware instead of specific groups for logged in state * Auth check for admin access * Check user admin access for updates * Formatting * Allow nullable field and calculate distance if nulled for flight import #478 (#482) * Check for no roles being attached #480 (#483) * Return the flight fares if there are no subfleet fares #488 (#489) * Return the flight fares if there are no subfleet fares #488 * Formatting * Formatting * Account for units when entering fuel amounts #493 * Search for ICAO not working properly (#496) * /flights and /flights/search direct to the same endpoint * Properly set the distance/planned_distance on save (#497) * 491 Installation Error (#495) * Disable CSRF token * Add error handling around looking up the theme and set a default * Note about logs in issue template * Formatting * Fix GeoService errors when viewing PIREP #498 (#499) * Add new command to export a specific PIREP for debugging (#501) * Set a default model value for airports on PIREP (#500) * Set a default model value for airports on PIREP * Fix airport icao reference * Default airport models * Catch broader exception writing out config files #491 * style * Add reference to docs on doc site (#502) * Properly create/update rows importing #486 (#503) * Add base Dockerfile for Dockerhub builds (#504) * New subfleet not being attached to an airline on import #479 (#505) * Fix subfleet not being attached to an airline on creation in import #479 * Call airline name with optional() around subfleet * Minor cleanup * Search flights by subfleet #484 (#506) * API level search of flights #484 * Add Subfleet to flights page for search * Make the fuel used optional (#512) * Add make to Docker container * Add getRootDomain() to Utils (#514) * Show admin dropdown for admin-access ability (#515) * Show admin dropdown for admin-access ability closes #509 * Formatting * Check user permissions on the routes #508 (#516) * Check user permissions on the routes #508 * Formatting * Return default value on exception for setting() * Correct text for no subfleets #507 (#518) * Add a public_url() helper #513 (#519) * Reduce number of queries for update check (#520) * Try to clear caches before updating (#522) * Try to clear caches before updating * Add clear-compiled to maintenance cache list * Formatting * Set PIREPs page to public (#526) Set PIREPs page to public * Fix live and route map errors #527 (#528) * Add menu bar for mobile (#529) * Format all blade templates to 2 spaces #530 (#531) * Fix PIREP edit endpoint closes #533 (#534) * Fix import during flight cron #532 (#535) * PIREPS resource except for show (#536) * Use optional() around the airport fields (#537) * Use optional() around the airport fields * Add null-coalesce around full_name * Add link to download ACARS config from profile (#539) * Add link to download ACARS config from profile * Formatting * Update xml config file template (#540)
499 lines
13 KiB
PHP
499 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Contracts\Model;
|
|
use App\Models\Enums\AcarsType;
|
|
use App\Models\Enums\PirepFieldSource;
|
|
use App\Models\Enums\PirepState;
|
|
use App\Models\Traits\HashIdTrait;
|
|
use App\Support\Units\Distance;
|
|
use App\Support\Units\Fuel;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* @property string id
|
|
* @property string flight_number
|
|
* @property string route_code
|
|
* @property string route_leg
|
|
* @property int airline_id
|
|
* @property int user_id
|
|
* @property int aircraft_id
|
|
* @property Aircraft aircraft
|
|
* @property Airline airline
|
|
* @property Airport arr_airport
|
|
* @property string arr_airport_id
|
|
* @property Airport dpt_airport
|
|
* @property string dpt_airport_id
|
|
* @property Carbon block_off_time
|
|
* @property Carbon block_on_time
|
|
* @property int block_time
|
|
* @property int flight_time In minutes
|
|
* @property int planned_flight_time
|
|
* @property float block_fuel
|
|
* @property float fuel_used
|
|
* @property float distance
|
|
* @property float planned_distance
|
|
* @property int level
|
|
* @property string route
|
|
* @property int score
|
|
* @property User user
|
|
* @property Flight|null flight
|
|
* @property Collection fields
|
|
* @property int status
|
|
* @property bool state
|
|
* @property string source
|
|
* @property Carbon submitted_at
|
|
* @property Carbon created_at
|
|
* @property Carbon updated_at
|
|
* @property bool read_only
|
|
* @property Acars position
|
|
* @property Acars[] acars
|
|
* @property mixed cancelled
|
|
*/
|
|
class Pirep extends Model
|
|
{
|
|
use HashIdTrait;
|
|
|
|
public $table = 'pireps';
|
|
|
|
protected $keyType = 'string';
|
|
public $incrementing = false;
|
|
|
|
/** The form wants this */
|
|
public $hours;
|
|
public $minutes;
|
|
|
|
protected $fillable = [
|
|
'id',
|
|
'user_id',
|
|
'airline_id',
|
|
'aircraft_id',
|
|
'flight_number',
|
|
'route_code',
|
|
'route_leg',
|
|
'dpt_airport_id',
|
|
'arr_airport_id',
|
|
'alt_airport_id',
|
|
'level',
|
|
'distance',
|
|
'planned_distance',
|
|
'block_time',
|
|
'flight_time',
|
|
'planned_flight_time',
|
|
'zfw',
|
|
'block_fuel',
|
|
'fuel_used',
|
|
'landing_rate',
|
|
'route',
|
|
'notes',
|
|
'score',
|
|
'source',
|
|
'source_name',
|
|
'flight_type',
|
|
'state',
|
|
'status',
|
|
'block_off_time',
|
|
'block_on_time',
|
|
'submitted_at',
|
|
'created_at',
|
|
'updated_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'user_id' => 'integer',
|
|
'airline_id' => 'integer',
|
|
'aircraft_id' => 'integer',
|
|
'level' => 'integer',
|
|
'distance' => 'float',
|
|
'planned_distance' => 'float',
|
|
'block_time' => 'integer',
|
|
'flight_time' => 'integer',
|
|
'planned_flight_time' => 'integer',
|
|
'zfw' => 'float',
|
|
'block_fuel' => 'float',
|
|
'fuel_used' => 'float',
|
|
'landing_rate' => 'float',
|
|
'score' => 'integer',
|
|
'source' => 'integer',
|
|
'state' => 'integer',
|
|
];
|
|
|
|
public static $rules = [
|
|
'airline_id' => 'required|exists:airlines,id',
|
|
'aircraft_id' => 'required|exists:aircraft,id',
|
|
'flight_number' => 'required',
|
|
'dpt_airport_id' => 'required',
|
|
'arr_airport_id' => 'required',
|
|
'block_fuel' => 'sometimes|numeric',
|
|
'fuel_used' => 'sometimes|numeric',
|
|
'level' => 'nullable|numeric',
|
|
'notes' => 'nullable',
|
|
'route' => 'nullable',
|
|
];
|
|
|
|
/*
|
|
* If a PIREP is in these states, then it can't be changed.
|
|
*/
|
|
public static $read_only_states = [
|
|
PirepState::ACCEPTED,
|
|
PirepState::REJECTED,
|
|
PirepState::CANCELLED,
|
|
];
|
|
|
|
/*
|
|
* If a PIREP is in one of these states, it can't be cancelled
|
|
*/
|
|
public static $cancel_states = [
|
|
PirepState::ACCEPTED,
|
|
PirepState::REJECTED,
|
|
PirepState::CANCELLED,
|
|
PirepState::DELETED,
|
|
];
|
|
|
|
/**
|
|
* Create a new PIREP model from a given flight. Pre-populates the fields
|
|
*
|
|
* @param \App\Models\Flight $flight
|
|
*
|
|
* @return \App\Models\Pirep
|
|
*/
|
|
public static function fromFlight(Flight $flight)
|
|
{
|
|
return new self([
|
|
'flight_id' => $flight->id,
|
|
'airline_id' => $flight->airline_id,
|
|
'flight_number' => $flight->flight_number,
|
|
'route_code' => $flight->route_code,
|
|
'route_leg' => $flight->route_leg,
|
|
'dpt_airport_id' => $flight->dpt_airport_id,
|
|
'arr_airport_id' => $flight->arr_airport_id,
|
|
'route' => $flight->route,
|
|
'level' => $flight->level,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the flight ident, e.,g JBU1900
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getIdentAttribute(): string
|
|
{
|
|
//$flight_id = $this->airline->code;
|
|
$flight_id = $this->flight_number;
|
|
|
|
if (filled($this->route_code)) {
|
|
$flight_id .= '/C'.$this->route_code;
|
|
}
|
|
|
|
if (filled($this->route_leg)) {
|
|
$flight_id .= '/L'.$this->route_leg;
|
|
}
|
|
|
|
return $flight_id;
|
|
}
|
|
|
|
/**
|
|
* Return the block off time in carbon format
|
|
*
|
|
* @return Carbon|null
|
|
*/
|
|
public function getBlockOffTimeAttribute()
|
|
{
|
|
if (array_key_exists('block_off_time', $this->attributes)) {
|
|
return new Carbon($this->attributes['block_off_time']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return the block on time
|
|
*
|
|
* @return Carbon|null
|
|
*/
|
|
public function getBlockOnTimeAttribute()
|
|
{
|
|
if (array_key_exists('block_on_time', $this->attributes)) {
|
|
return new Carbon($this->attributes['block_on_time']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return the block on time
|
|
*
|
|
* @return Carbon
|
|
*/
|
|
public function getSubmittedAtAttribute()
|
|
{
|
|
if (array_key_exists('submitted_at', $this->attributes)) {
|
|
return new Carbon($this->attributes['submitted_at']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the distance unit, convert to our internal default unit
|
|
*
|
|
* @param $value
|
|
*/
|
|
public function setDistanceAttribute($value): void
|
|
{
|
|
if ($value instanceof Distance) {
|
|
$this->attributes['distance'] = $value->toUnit(
|
|
config('phpvms.internal_units.distance')
|
|
);
|
|
} else {
|
|
$this->attributes['distance'] = $value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return if this PIREP can be edited or not
|
|
*/
|
|
public function getReadOnlyAttribute(): bool
|
|
{
|
|
return \in_array($this->state, static::$read_only_states, true);
|
|
}
|
|
|
|
/**
|
|
* Return the flight progress in a percent.
|
|
*/
|
|
public function getProgressPercentAttribute()
|
|
{
|
|
$distance = $this->distance;
|
|
|
|
$upper_bound = $distance;
|
|
if ($this->planned_distance) {
|
|
$upper_bound = $this->planned_distance;
|
|
}
|
|
|
|
$upper_bound = empty($upper_bound) ? 1 : $upper_bound;
|
|
$distance = empty($distance) ? $upper_bound : $distance;
|
|
|
|
return round(($distance / $upper_bound) * 100, 0);
|
|
}
|
|
|
|
/**
|
|
* Get the pirep_fields and then the pirep_field_values and
|
|
* merge them together. If a field value doesn't exist then add in a fake one
|
|
*/
|
|
public function getFieldsAttribute()
|
|
{
|
|
$custom_fields = PirepField::all();
|
|
$field_values = PirepFieldValue::where('pirep_id', $this->id)->get();
|
|
|
|
// Merge the field values into $fields
|
|
foreach ($custom_fields as $field) {
|
|
$has_value = $field_values->firstWhere('slug', $field->slug);
|
|
if (!$has_value) {
|
|
$field_values->push(new PirepFieldValue([
|
|
'pirep_id' => $this->id,
|
|
'name' => $field->name,
|
|
'slug' => $field->slug,
|
|
'value' => '',
|
|
'source' => PirepFieldSource::MANUAL,
|
|
]));
|
|
}
|
|
}
|
|
|
|
return $field_values->sortBy('source');
|
|
}
|
|
|
|
/**
|
|
* Set the amount of fuel used
|
|
*
|
|
* @param $value
|
|
*/
|
|
public function setFuelUsedAttribute($value): void
|
|
{
|
|
if ($value instanceof Fuel) {
|
|
$this->attributes['fuel_used'] = $value->toUnit(
|
|
config('phpvms.internal_units.fuel')
|
|
);
|
|
} else {
|
|
$this->attributes['fuel_used'] = $value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the distance unit, convert to our internal default unit
|
|
*
|
|
* @param $value
|
|
*/
|
|
public function setPlannedDistanceAttribute($value): void
|
|
{
|
|
if ($value instanceof Distance) {
|
|
$this->attributes['planned_distance'] = $value->toUnit(
|
|
config('phpvms.internal_units.distance')
|
|
);
|
|
} else {
|
|
$this->attributes['planned_distance'] = $value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Do some cleanup on the route
|
|
*
|
|
* @param $route
|
|
*/
|
|
public function setRouteAttribute($route): void
|
|
{
|
|
$route = strtoupper(trim($route));
|
|
$this->attributes['route'] = $route;
|
|
}
|
|
|
|
/**
|
|
* Return if this is cancelled or not
|
|
*/
|
|
public function getCancelledAttribute(): bool
|
|
{
|
|
return $this->state === PirepState::CANCELLED;
|
|
}
|
|
|
|
/**
|
|
* Check if this PIREP is allowed to be updated
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function allowedUpdates(): bool
|
|
{
|
|
return !$this->getReadOnlyAttribute();
|
|
}
|
|
|
|
/**
|
|
* Return a custom field value
|
|
*
|
|
* @param $field_name
|
|
*
|
|
* @return string
|
|
*/
|
|
public function field($field_name): string
|
|
{
|
|
$field = $this->fields->where('name', $field_name)->first();
|
|
if ($field) {
|
|
return $field['value'];
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Foreign Keys
|
|
*/
|
|
public function acars()
|
|
{
|
|
return $this->hasMany(Acars::class, 'pirep_id')
|
|
->where('type', AcarsType::FLIGHT_PATH)
|
|
->orderBy('created_at', 'asc')
|
|
->orderBy('sim_time', 'asc');
|
|
}
|
|
|
|
public function acars_logs()
|
|
{
|
|
return $this->hasMany(Acars::class, 'pirep_id')
|
|
->where('type', AcarsType::LOG)
|
|
->orderBy('created_at', 'desc')
|
|
->orderBy('sim_time', 'asc');
|
|
}
|
|
|
|
public function acars_route()
|
|
{
|
|
return $this->hasMany(Acars::class, 'pirep_id')
|
|
->where('type', AcarsType::ROUTE)
|
|
->orderBy('order', 'asc');
|
|
}
|
|
|
|
public function aircraft()
|
|
{
|
|
return $this->belongsTo(Aircraft::class, 'aircraft_id');
|
|
}
|
|
|
|
public function airline()
|
|
{
|
|
return $this->belongsTo(Airline::class, 'airline_id');
|
|
}
|
|
|
|
public function flight()
|
|
{
|
|
return $this->belongsTo(Flight::class, 'flight_id');
|
|
}
|
|
|
|
public function arr_airport()
|
|
{
|
|
return $this->belongsTo(Airport::class, 'arr_airport_id')
|
|
->withDefault(function ($model) {
|
|
if (!empty($this->attributes['arr_airport_id'])) {
|
|
$model->id = $this->attributes['arr_airport_id'];
|
|
$model->icao = $this->attributes['arr_airport_id'];
|
|
$model->name = $this->attributes['arr_airport_id'];
|
|
}
|
|
|
|
return $model;
|
|
});
|
|
}
|
|
|
|
public function alt_airport()
|
|
{
|
|
return $this->belongsTo(Airport::class, 'alt_airport_id');
|
|
}
|
|
|
|
public function dpt_airport()
|
|
{
|
|
return $this->belongsTo(Airport::class, 'dpt_airport_id')
|
|
->withDefault(function ($model) {
|
|
if (!empty($this->attributes['dpt_airport_id'])) {
|
|
$model->id = $this->attributes['dpt_airport_id'];
|
|
$model->icao = $this->attributes['dpt_airport_id'];
|
|
$model->name = $this->attributes['dpt_airport_id'];
|
|
}
|
|
|
|
return $model;
|
|
});
|
|
}
|
|
|
|
public function comments()
|
|
{
|
|
return $this->hasMany(PirepComment::class, 'pirep_id')
|
|
->orderBy('created_at', 'desc');
|
|
}
|
|
|
|
public function fares()
|
|
{
|
|
return $this->hasMany(PirepFare::class, 'pirep_id');
|
|
}
|
|
|
|
public function field_values()
|
|
{
|
|
return $this->hasMany(PirepFieldValue::class, 'pirep_id');
|
|
}
|
|
|
|
public function pilot()
|
|
{
|
|
return $this->user();
|
|
}
|
|
|
|
/**
|
|
* Relationship that holds the current position, but limits the ACARS
|
|
* relationship to only one row (the latest), to prevent an N+! problem
|
|
*/
|
|
public function position()
|
|
{
|
|
return $this->hasOne(Acars::class, 'pirep_id')
|
|
->where('type', AcarsType::FLIGHT_PATH)
|
|
->latest();
|
|
}
|
|
|
|
public function transactions()
|
|
{
|
|
return $this->hasMany(JournalTransaction::class, 'ref_model_id')
|
|
->where('ref_model', __CLASS__)
|
|
->orderBy('credit', 'desc')
|
|
->orderBy('debit', 'desc');
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
}
|