phpvms/app/Models/Pirep.php

499 lines
13 KiB
PHP
Raw Normal View History

2017-06-29 08:54:05 +08:00
<?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;
2018-02-11 11:16:32 +08:00
use App\Support\Units\Distance;
use App\Support\Units\Fuel;
use Carbon\Carbon;
use Illuminate\Support\Collection;
2017-06-29 08:54:05 +08:00
/**
* @property string id
* @property string flight_number
* @property string route_code
* @property string route_leg
2018-08-27 02:50:08 +08:00
* @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
2018-08-27 02:50:08 +08:00
* @property int block_time
* @property int flight_time In minutes
* @property int planned_flight_time
* @property float block_fuel
* @property float fuel_used
2018-08-27 02:50:08 +08:00
* @property float distance
* @property float planned_distance
* @property int level
2018-05-12 01:08:55 +08:00
* @property string route
2018-08-27 02:50:08 +08:00
* @property int score
* @property User user
* @property Flight|null flight
* @property Collection fields
2018-05-30 00:04:28 +08:00
* @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
2018-05-03 04:14:18 +08:00
* @property Acars position
2018-05-05 02:59:47 +08:00
* @property Acars[] acars
2018-08-27 02:50:08 +08:00
* @property mixed cancelled
2017-06-29 08:54:05 +08:00
*/
class Pirep extends Model
2017-06-29 08:54:05 +08:00
{
use HashIdTrait;
2017-06-29 08:54:05 +08:00
public $table = 'pireps';
protected $keyType = 'string';
2017-07-05 02:57:08 +08:00
public $incrementing = false;
2017-06-29 08:54:05 +08:00
/** The form wants this */
2018-08-27 00:40:04 +08:00
public $hours;
public $minutes;
2018-03-21 08:40:19 +08:00
protected $fillable = [
'id',
'user_id',
2018-01-31 01:15:07 +08:00
'airline_id',
'aircraft_id',
'flight_number',
'route_code',
'route_leg',
2018-01-31 01:15:07 +08:00
'dpt_airport_id',
'arr_airport_id',
2018-09-09 00:38:30 +08:00
'alt_airport_id',
2018-01-31 01:15:07 +08:00
'level',
'distance',
'planned_distance',
'block_time',
'flight_time',
'planned_flight_time',
2018-01-31 01:07:48 +08:00
'zfw',
2018-01-31 01:05:14 +08:00
'block_fuel',
2018-01-31 01:27:39 +08:00
'fuel_used',
'landing_rate',
'route',
'notes',
2018-04-26 00:56:05 +08:00
'score',
2018-01-31 01:15:07 +08:00
'source',
'source_name',
'flight_type',
'state',
'status',
'block_off_time',
'block_on_time',
'submitted_at',
'created_at',
'updated_at',
];
2017-06-29 08:54:05 +08:00
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',
2018-04-26 00:56:05 +08:00
'score' => 'integer',
'source' => 'integer',
'state' => 'integer',
];
2017-06-29 08:54:05 +08:00
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' => 'required|numeric',
'fuel_used' => 'required|numeric',
'level' => 'nullable|numeric',
'notes' => 'nullable',
'route' => 'nullable',
];
2017-06-29 08:54:05 +08:00
/*
* 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,
]);
}
2017-12-04 05:29:34 +08:00
/**
* Get the flight ident, e.,g JBU1900
2018-08-27 00:40:04 +08:00
*
2017-12-04 05:29:34 +08:00
* @return string
*/
public function getIdentAttribute(): string
2017-12-04 05:29:34 +08:00
{
2018-08-27 00:40:04 +08:00
//$flight_id = $this->airline->code;
2018-04-04 11:44:11 +08:00
$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;
2017-12-04 05:29:34 +08:00
}
return $flight_id;
}
2018-05-02 09:58:05 +08:00
/**
* Return the block off time in carbon format
2018-08-27 00:40:04 +08:00
*
2018-08-27 02:50:08 +08:00
* @return Carbon|null
2018-05-02 09:58:05 +08:00
*/
public function getBlockOffTimeAttribute()
{
if (array_key_exists('block_off_time', $this->attributes)) {
return new Carbon($this->attributes['block_off_time']);
}
2018-05-02 09:58:05 +08:00
}
/**
* Return the block on time
2018-08-27 00:40:04 +08:00
*
2018-08-27 02:50:08 +08:00
* @return Carbon|null
2018-05-02 09:58:05 +08:00
*/
public function getBlockOnTimeAttribute()
{
if (array_key_exists('block_on_time', $this->attributes)) {
return new Carbon($this->attributes['block_on_time']);
}
2018-05-02 09:58:05 +08:00
}
2018-05-03 04:14:18 +08:00
/**
* Return the block on time
2018-08-27 00:40:04 +08:00
*
2018-05-03 04:14:18 +08:00
* @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
2018-08-27 00:40:04 +08:00
*
* @param $value
*/
public function setDistanceAttribute($value): void
{
if ($value instanceof Distance) {
$this->attributes['distance'] = $value->toUnit(
2018-09-09 00:38:30 +08:00
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);
}
2018-04-05 06:42:43 +08:00
/**
* Return the flight progress in a percent.
*/
public function getProgressPercentAttribute()
{
$distance = $this->distance;
2018-04-05 06:42:43 +08:00
$upper_bound = $distance;
if ($this->planned_distance) {
$upper_bound = $this->planned_distance;
2018-04-05 06:42:43 +08:00
}
$upper_bound = empty($upper_bound) ? 1 : $upper_bound;
$distance = empty($distance) ? $upper_bound : $distance;
return round(($distance / $upper_bound) * 100, 0);
2018-04-05 06:42:43 +08:00
}
/**
* 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();
2018-08-27 00:40:04 +08:00
// Merge the field values into $fields
foreach ($custom_fields as $field) {
$has_value = $field_values->firstWhere('slug', $field->slug);
2018-08-27 00:40:04 +08:00
if (!$has_value) {
$field_values->push(new PirepFieldValue([
'pirep_id' => $this->id,
2018-08-27 00:40:04 +08:00
'name' => $field->name,
'slug' => $field->slug,
'value' => '',
'source' => PirepFieldSource::MANUAL,
]));
}
}
return $field_values->sortBy('source');
}
/**
* Set the amount of fuel used
2018-08-27 00:40:04 +08:00
*
* @param $value
*/
2018-09-09 00:38:30 +08:00
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
2018-08-27 00:40:04 +08:00
*
* @param $value
*/
2018-09-09 00:38:30 +08:00
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
2018-08-27 00:40:04 +08:00
*
* @param $route
*/
2018-03-03 07:29:11 +08:00
public function setRouteAttribute($route): void
{
$route = strtoupper(trim($route));
$this->attributes['route'] = $route;
}
2018-05-12 01:08:55 +08:00
/**
* Return if this is cancelled or not
*/
2018-08-27 02:50:08 +08:00
public function getCancelledAttribute(): bool
2018-05-12 01:08:55 +08:00
{
return $this->state === PirepState::CANCELLED;
}
/**
* Check if this PIREP is allowed to be updated
2018-08-27 00:40:04 +08:00
*
* @return bool
*/
2018-03-03 07:29:11 +08:00
public function allowedUpdates(): bool
{
2018-08-27 00:40:04 +08:00
return !$this->getReadOnlyAttribute();
}
/**
* Return a custom field value
2018-08-27 00:40:04 +08:00
*
* @param $field_name
2018-08-27 00:40:04 +08:00
*
* @return string
*/
public function field($field_name): string
{
$field = $this->fields->where('name', $field_name)->first();
if ($field) {
return $field['value'];
}
return '';
}
2017-06-29 08:54:05 +08:00
/**
* Foreign Keys
*/
public function acars()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(Acars::class, 'pirep_id')
->where('type', AcarsType::FLIGHT_PATH)
2018-05-29 07:15:51 +08:00
->orderBy('created_at', 'asc')
->orderBy('sim_time', 'asc');
}
public function acars_logs()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(Acars::class, 'pirep_id')
->where('type', AcarsType::LOG)
2018-05-29 07:15:51 +08:00
->orderBy('created_at', 'desc')
->orderBy('sim_time', 'asc');
}
2018-01-02 06:01:01 +08:00
public function acars_route()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(Acars::class, 'pirep_id')
->where('type', AcarsType::ROUTE)
->orderBy('order', 'asc');
2018-01-02 06:01:01 +08:00
}
2017-07-03 10:25:48 +08:00
public function aircraft()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Aircraft::class, 'aircraft_id');
2017-07-03 10:25:48 +08:00
}
2017-06-29 08:54:05 +08:00
2017-07-23 22:45:33 +08:00
public function airline()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airline::class, 'airline_id');
2017-07-23 22:45:33 +08:00
}
public function flight()
{
return $this->belongsTo(Flight::class, 'flight_id');
}
2017-06-30 04:50:16 +08:00
public function arr_airport()
2017-06-29 08:54:05 +08:00
{
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;
});
2017-06-29 08:54:05 +08:00
}
2018-09-09 00:38:30 +08:00
public function alt_airport()
{
return $this->belongsTo(Airport::class, 'alt_airport_id');
}
2017-06-30 04:50:16 +08:00
public function dpt_airport()
2017-06-29 08:54:05 +08:00
{
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;
});
2017-06-29 08:54:05 +08:00
}
2017-08-02 23:17:54 +08:00
public function comments()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(PirepComment::class, 'pirep_id')
->orderBy('created_at', 'desc');
2017-07-24 02:07:41 +08:00
}
public function fares()
{
return $this->hasMany(PirepFare::class, 'pirep_id');
}
public function field_values()
2017-06-29 08:54:05 +08:00
{
return $this->hasMany(PirepFieldValue::class, 'pirep_id');
2017-06-29 08:54:05 +08:00
}
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()
{
2018-01-08 23:22:12 +08:00
return $this->hasOne(Acars::class, 'pirep_id')
->where('type', AcarsType::FLIGHT_PATH)
->latest();
}
2018-03-03 06:09:48 +08:00
public function transactions()
{
2018-04-02 03:32:01 +08:00
return $this->hasMany(JournalTransaction::class, 'ref_model_id')
->where('ref_model', __CLASS__)
->orderBy('credit', 'desc')
->orderBy('debit', 'desc');
2018-03-03 06:09:48 +08:00
}
2017-06-30 04:50:16 +08:00
public function user()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(User::class, 'user_id');
2017-06-29 08:54:05 +08:00
}
}