2017-06-29 08:54:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Model;
|
2021-06-04 22:51:59 +08:00
|
|
|
use App\Events\PirepStateChange;
|
|
|
|
use App\Events\PirepStatusChange;
|
2022-03-14 23:45:18 +08:00
|
|
|
use App\Models\Casts\CarbonCast;
|
|
|
|
use App\Models\Casts\DistanceCast;
|
|
|
|
use App\Models\Casts\FuelCast;
|
2018-01-02 00:38:45 +08:00
|
|
|
use App\Models\Enums\AcarsType;
|
2018-07-13 10:20:10 +08:00
|
|
|
use App\Models\Enums\PirepFieldSource;
|
2018-01-29 01:12:13 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
2018-03-08 22:51:36 +08:00
|
|
|
use App\Models\Traits\HashIdTrait;
|
2018-02-11 11:16:32 +08:00
|
|
|
use App\Support\Units\Distance;
|
2018-02-21 04:07:33 +08:00
|
|
|
use App\Support\Units\Fuel;
|
2018-04-05 02:03:10 +08:00
|
|
|
use Carbon\Carbon;
|
2022-03-14 23:45:18 +08:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-06-04 22:51:59 +08:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2018-03-21 00:46:48 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2021-06-04 22:51:59 +08:00
|
|
|
use Kleemans\AttributeEvents;
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-30 03:55:25 +08:00
|
|
|
* @property string id
|
2020-03-30 01:33:14 +08:00
|
|
|
* @property string ident
|
2018-03-20 09:50:40 +08:00
|
|
|
* @property string flight_number
|
|
|
|
* @property string route_code
|
|
|
|
* @property string route_leg
|
2020-02-12 23:40:52 +08:00
|
|
|
* @property string flight_type
|
2018-08-27 02:50:08 +08:00
|
|
|
* @property int airline_id
|
|
|
|
* @property int user_id
|
|
|
|
* @property int aircraft_id
|
2018-03-20 09:50:40 +08:00
|
|
|
* @property Aircraft aircraft
|
|
|
|
* @property Airline airline
|
|
|
|
* @property Airport arr_airport
|
2018-03-31 00:08:53 +08:00
|
|
|
* @property string arr_airport_id
|
|
|
|
* @property Airport dpt_airport
|
|
|
|
* @property string dpt_airport_id
|
2018-04-05 02:03:10 +08:00
|
|
|
* @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
|
2022-03-14 23:45:18 +08:00
|
|
|
* @property Fuel block_fuel
|
|
|
|
* @property Fuel fuel_used
|
|
|
|
* @property Distance distance
|
|
|
|
* @property Distance planned_distance
|
2022-03-19 04:43:34 +08:00
|
|
|
* @property float progress_percent
|
2019-11-19 23:06:07 +08:00
|
|
|
* @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
|
2018-03-20 09:50:40 +08:00
|
|
|
* @property User user
|
2018-03-16 07:20:07 +08:00
|
|
|
* @property Flight|null flight
|
2018-03-21 00:46:48 +08:00
|
|
|
* @property Collection fields
|
2020-08-13 10:02:20 +08:00
|
|
|
* @property string status
|
2021-06-04 22:51:59 +08:00
|
|
|
* @property int state
|
|
|
|
* @property int source
|
2021-05-03 22:00:10 +08:00
|
|
|
* @property string source_name
|
2018-04-05 02:03:10 +08:00
|
|
|
* @property Carbon submitted_at
|
|
|
|
* @property Carbon created_at
|
|
|
|
* @property Carbon updated_at
|
2018-05-31 03:00:20 +08:00
|
|
|
* @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
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class Pirep extends Model
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2021-06-04 22:51:59 +08:00
|
|
|
use AttributeEvents;
|
2018-03-08 22:51:36 +08:00
|
|
|
use HashIdTrait;
|
2022-03-14 23:45:18 +08:00
|
|
|
use HasFactory;
|
2021-06-04 22:51:59 +08:00
|
|
|
use Notifiable;
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
public $table = 'pireps';
|
2019-09-13 20:05:02 +08:00
|
|
|
protected $keyType = 'string';
|
2017-07-05 02:57:08 +08:00
|
|
|
public $incrementing = false;
|
2018-02-28 03:37:29 +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 = [
|
2018-01-30 08:14:55 +08:00
|
|
|
'id',
|
2017-12-10 11:21:49 +08:00
|
|
|
'user_id',
|
2018-01-31 01:15:07 +08:00
|
|
|
'airline_id',
|
|
|
|
'aircraft_id',
|
2017-12-10 11:21:49 +08:00
|
|
|
'flight_number',
|
|
|
|
'route_code',
|
|
|
|
'route_leg',
|
2020-11-03 00:57:25 +08:00
|
|
|
'flight_id',
|
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',
|
2018-01-30 06:27:55 +08:00
|
|
|
'distance',
|
|
|
|
'planned_distance',
|
2018-03-30 02:42:16 +08:00
|
|
|
'block_time',
|
2017-12-10 11:21:49 +08:00
|
|
|
'flight_time',
|
2017-12-26 05:19:34 +08:00
|
|
|
'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',
|
2018-01-30 08:14:55 +08:00
|
|
|
'landing_rate',
|
2017-12-10 11:21:49 +08:00
|
|
|
'route',
|
|
|
|
'notes',
|
2018-04-26 00:56:05 +08:00
|
|
|
'score',
|
2018-01-31 01:15:07 +08:00
|
|
|
'source',
|
|
|
|
'source_name',
|
2018-01-30 08:14:55 +08:00
|
|
|
'flight_type',
|
2017-12-20 10:19:36 +08:00
|
|
|
'state',
|
2017-12-10 11:21:49 +08:00
|
|
|
'status',
|
2018-03-30 03:10:49 +08:00
|
|
|
'block_off_time',
|
|
|
|
'block_on_time',
|
2018-04-05 02:03:10 +08:00
|
|
|
'submitted_at',
|
2018-01-30 08:14:55 +08:00
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
2017-12-10 11:21:49 +08:00
|
|
|
];
|
|
|
|
protected $casts = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user_id' => 'integer',
|
|
|
|
'airline_id' => 'integer',
|
|
|
|
'aircraft_id' => 'integer',
|
|
|
|
'level' => 'integer',
|
2022-03-14 23:45:18 +08:00
|
|
|
'distance' => DistanceCast::class,
|
|
|
|
'planned_distance' => DistanceCast::class,
|
2018-03-30 02:42:16 +08:00
|
|
|
'block_time' => 'integer',
|
2022-03-14 23:45:18 +08:00
|
|
|
'block_off_time' => CarbonCast::class,
|
|
|
|
'block_on_time' => CarbonCast::class,
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight_time' => 'integer',
|
|
|
|
'planned_flight_time' => 'integer',
|
|
|
|
'zfw' => 'float',
|
2022-03-14 23:45:18 +08:00
|
|
|
'block_fuel' => FuelCast::class,
|
|
|
|
'fuel_used' => FuelCast::class,
|
2018-03-20 09:50:40 +08:00
|
|
|
'landing_rate' => 'float',
|
2018-04-26 00:56:05 +08:00
|
|
|
'score' => 'integer',
|
2018-03-20 09:50:40 +08:00
|
|
|
'source' => 'integer',
|
|
|
|
'state' => 'integer',
|
2022-03-14 23:45:18 +08:00
|
|
|
'submitted_at' => CarbonCast::class,
|
2017-12-10 11:21:49 +08:00
|
|
|
];
|
2017-12-20 10:19:36 +08:00
|
|
|
public static $rules = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'airline_id' => 'required|exists:airlines,id',
|
|
|
|
'aircraft_id' => 'required|exists:aircraft,id',
|
|
|
|
'flight_number' => 'required',
|
|
|
|
'dpt_airport_id' => 'required',
|
|
|
|
'arr_airport_id' => 'required',
|
2020-02-24 05:01:09 +08:00
|
|
|
'block_fuel' => 'nullable|numeric',
|
|
|
|
'fuel_used' => 'nullable|numeric',
|
2019-11-19 23:06:07 +08:00
|
|
|
'level' => 'nullable|numeric',
|
2018-03-20 09:50:40 +08:00
|
|
|
'notes' => 'nullable',
|
|
|
|
'route' => 'nullable',
|
2017-12-20 10:19:36 +08:00
|
|
|
];
|
2021-06-04 22:51:59 +08:00
|
|
|
/**
|
|
|
|
* Auto-dispatch events for lifecycle state changes
|
|
|
|
*/
|
|
|
|
protected $dispatchesEvents = [
|
|
|
|
'status:*' => PirepStatusChange::class,
|
|
|
|
'state:*' => PirepStateChange::class,
|
|
|
|
];
|
2019-09-17 01:08:26 +08:00
|
|
|
/*
|
2018-05-10 23:35:10 +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,
|
|
|
|
];
|
2019-09-17 01:08:26 +08:00
|
|
|
/*
|
|
|
|
* 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,
|
|
|
|
];
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
/**
|
|
|
|
* Create a new PIREP model from a given flight. Pre-populates the fields
|
|
|
|
*
|
|
|
|
* @param \App\Models\Flight $flight
|
|
|
|
*
|
|
|
|
* @return \App\Models\Pirep
|
|
|
|
*/
|
2020-03-23 21:31:35 +08:00
|
|
|
public static function fromFlight(Flight $flight): self
|
2019-11-06 00:44:31 +08:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
/**
|
|
|
|
* Create a new PIREP from a SimBrief instance
|
|
|
|
*
|
2021-08-10 00:01:27 +08:00
|
|
|
* @param \App\Models\SimBrief $simbrief
|
2020-03-23 21:31:35 +08:00
|
|
|
*
|
|
|
|
* @return \App\Models\Pirep
|
|
|
|
*/
|
2021-08-10 00:01:27 +08:00
|
|
|
public static function fromSimBrief(SimBrief $simbrief): self
|
2020-03-23 21:31:35 +08:00
|
|
|
{
|
|
|
|
return new self([
|
2021-08-10 00:01:27 +08:00
|
|
|
'flight_id' => $simbrief->flight->id,
|
|
|
|
'airline_id' => $simbrief->flight->airline_id,
|
|
|
|
'flight_number' => $simbrief->flight->flight_number,
|
|
|
|
'route_code' => $simbrief->flight->route_code,
|
|
|
|
'route_leg' => $simbrief->flight->route_leg,
|
|
|
|
'dpt_airport_id' => $simbrief->flight->dpt_airport_id,
|
|
|
|
'arr_airport_id' => $simbrief->flight->arr_airport_id,
|
|
|
|
'route' => $simbrief->xml->getRouteString(),
|
|
|
|
'level' => $simbrief->xml->getFlightLevel(),
|
2020-03-23 21:31:35 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:29:34 +08:00
|
|
|
/**
|
2021-11-02 21:23:52 +08:00
|
|
|
* Get the flight ident, e.,g JBU1900/C.nn/L.yy
|
2017-12-04 05:29:34 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function ident(): Attribute
|
2017-12-04 05:29:34 +08:00
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return Attribute::make(get: function ($value, $attrs) {
|
|
|
|
$flight_id = optional($this->airline)->code;
|
|
|
|
$flight_id .= $this->flight_number;
|
2018-05-02 09:58:05 +08:00
|
|
|
|
2022-03-19 04:57:48 +08:00
|
|
|
if (filled($this->route_code)) {
|
2022-03-19 04:43:34 +08:00
|
|
|
$flight_id .= '/C.'.$this->route_code;
|
|
|
|
}
|
2018-05-03 04:14:18 +08:00
|
|
|
|
2022-03-19 04:57:48 +08:00
|
|
|
if (filled($this->route_leg)) {
|
2022-03-19 04:43:34 +08:00
|
|
|
$flight_id .= '/L.'.$this->route_leg;
|
2022-03-14 23:45:18 +08:00
|
|
|
}
|
2022-03-19 04:43:34 +08:00
|
|
|
|
|
|
|
return $flight_id;
|
|
|
|
});
|
2018-02-11 12:04:30 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 11:04:59 +08:00
|
|
|
/**
|
|
|
|
* Return if this PIREP can be edited or not
|
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function readOnly(): Attribute
|
2018-04-10 11:04:59 +08:00
|
|
|
{
|
2022-03-19 04:57:48 +08:00
|
|
|
return Attribute::make(get: fn ($_, $attrs) => \in_array($this->state, static::$read_only_states, true));
|
2018-04-10 11:04:59 +08:00
|
|
|
}
|
|
|
|
|
2018-04-05 06:42:43 +08:00
|
|
|
/**
|
|
|
|
* Return the flight progress in a percent.
|
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function progressPercent(): Attribute
|
2018-04-05 06:42:43 +08:00
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return Attribute::make(get: function ($_, $attrs) {
|
|
|
|
$distance = $attrs['distance'];
|
2018-04-05 06:42:43 +08:00
|
|
|
|
2022-03-19 04:43:34 +08:00
|
|
|
$upper_bound = $distance;
|
2022-03-19 04:57:48 +08:00
|
|
|
if (!empty($attrs['planned_distance']) && $attrs['planned_distance'] > 0) {
|
2022-03-19 04:43:34 +08:00
|
|
|
$upper_bound = $attrs['planned_distance'];
|
|
|
|
}
|
2018-04-05 06:42:43 +08:00
|
|
|
|
2022-03-19 04:43:34 +08:00
|
|
|
$upper_bound = empty($upper_bound) ? 1 : $upper_bound;
|
|
|
|
$distance = empty($distance) ? $upper_bound : $distance;
|
2020-01-15 22:09:58 +08:00
|
|
|
|
2022-03-19 04:43:34 +08:00
|
|
|
return round(($distance / $upper_bound) * 100);
|
|
|
|
});
|
2018-04-05 06:42:43 +08:00
|
|
|
}
|
|
|
|
|
2018-07-13 10:20:10 +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
|
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function fields(): Attribute
|
2018-07-13 10:20:10 +08:00
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return Attribute::make(get: function ($_, $attrs) {
|
|
|
|
$custom_fields = PirepField::all();
|
|
|
|
$field_values = PirepFieldValue::where('pirep_id', $this->id)->orderBy('created_at', 'asc')->get();
|
|
|
|
|
|
|
|
// Merge the field values into $fields
|
|
|
|
foreach ($custom_fields as $field) {
|
|
|
|
$has_value = $field_values->firstWhere('slug', $field->slug);
|
2022-03-19 04:57:48 +08:00
|
|
|
if (!$has_value) {
|
2022-03-19 04:43:34 +08:00
|
|
|
$field_values->push(new PirepFieldValue([
|
|
|
|
'pirep_id' => $this->id,
|
|
|
|
'name' => $field->name,
|
|
|
|
'slug' => $field->slug,
|
|
|
|
'value' => '',
|
|
|
|
'source' => PirepFieldSource::MANUAL,
|
|
|
|
]));
|
2022-03-14 23:45:18 +08:00
|
|
|
}
|
2018-07-13 10:20:10 +08:00
|
|
|
}
|
2022-03-19 04:43:34 +08:00
|
|
|
|
|
|
|
return $field_values;
|
|
|
|
});
|
2018-02-11 12:04:30 +08:00
|
|
|
}
|
|
|
|
|
2018-02-07 02:58:48 +08:00
|
|
|
/**
|
|
|
|
* Do some cleanup on the route
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return Attribute
|
2018-02-07 02:58:48 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function route(): Attribute
|
2018-02-07 02:58:48 +08:00
|
|
|
{
|
2022-03-19 04:57:48 +08:00
|
|
|
return Attribute::make(set: fn ($route) => strtoupper(trim($route)));
|
2018-02-07 02:58:48 +08:00
|
|
|
}
|
|
|
|
|
2018-05-12 01:08:55 +08:00
|
|
|
/**
|
|
|
|
* Return if this is cancelled or not
|
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function cancelled(): Attribute
|
2018-05-12 01:08:55 +08:00
|
|
|
{
|
2022-03-19 04:57:48 +08:00
|
|
|
return Attribute::make(get: fn ($_, $attrs) => $this->state === PirepState::CANCELLED);
|
2018-05-12 01:08:55 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
/**
|
|
|
|
* Check if this PIREP is allowed to be updated
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-29 01:12:13 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-03-03 07:29:11 +08:00
|
|
|
public function allowedUpdates(): bool
|
2018-01-29 01:12:13 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
return !$this->getReadOnlyAttribute();
|
2018-01-29 01:12:13 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:46:48 +08:00
|
|
|
/**
|
|
|
|
* Return a custom field value
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 00:46:48 +08:00
|
|
|
* @param $field_name
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 00:46:48 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function field($field_name): string
|
|
|
|
{
|
|
|
|
$field = $this->fields->where('name', $field_name)->first();
|
2022-03-19 04:57:48 +08:00
|
|
|
if ($field) {
|
2018-03-21 00:46:48 +08:00
|
|
|
return $field['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
/**
|
|
|
|
* Foreign Keys
|
|
|
|
*/
|
2017-12-27 05:26:12 +08:00
|
|
|
public function acars()
|
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return $this->hasMany(Acars::class, 'pirep_id')->where('type', AcarsType::FLIGHT_PATH)->orderBy('created_at', 'asc')->orderBy('sim_time', 'asc');
|
2017-12-27 05:26:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-04 12:19:06 +08:00
|
|
|
public function acars_logs()
|
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return $this->hasMany(Acars::class, 'pirep_id')->where('type', AcarsType::LOG)->orderBy('created_at', 'desc')->orderBy('sim_time', 'asc');
|
2018-01-04 12:19:06 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 06:01:01 +08:00
|
|
|
public function acars_route()
|
|
|
|
{
|
2022-03-19 04:43:34 +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
|
|
|
}
|
|
|
|
|
2019-11-06 00:44:31 +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
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return $this->belongsTo(Airport::class, 'arr_airport_id')->withDefault(function ($model) {
|
2022-03-19 04:57:48 +08:00
|
|
|
if (!empty($this->attributes['arr_airport_id'])) {
|
2022-03-19 04:43:34 +08:00
|
|
|
$model->id = $this->attributes['arr_airport_id'];
|
|
|
|
$model->icao = $this->attributes['arr_airport_id'];
|
|
|
|
$model->name = $this->attributes['arr_airport_id'];
|
|
|
|
}
|
2020-01-16 02:00:58 +08:00
|
|
|
|
2022-03-19 04:43:34 +08:00
|
|
|
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
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return $this->belongsTo(Airport::class, 'dpt_airport_id')->withDefault(function ($model) {
|
2022-03-19 04:57:48 +08:00
|
|
|
if (!empty($this->attributes['dpt_airport_id'])) {
|
2022-03-19 04:43:34 +08:00
|
|
|
$model->id = $this->attributes['dpt_airport_id'];
|
|
|
|
$model->icao = $this->attributes['dpt_airport_id'];
|
|
|
|
$model->name = $this->attributes['dpt_airport_id'];
|
|
|
|
}
|
2020-01-16 02:00:58 +08:00
|
|
|
|
2022-03-19 04:43:34 +08:00
|
|
|
return $model;
|
|
|
|
});
|
2017-06-29 08:54:05 +08:00
|
|
|
}
|
|
|
|
|
2017-08-02 23:17:54 +08:00
|
|
|
public function comments()
|
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return $this->hasMany(PirepComment::class, 'pirep_id')->orderBy('created_at', 'desc');
|
2017-07-24 02:07:41 +08:00
|
|
|
}
|
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
public function fares()
|
|
|
|
{
|
|
|
|
return $this->hasMany(PirepFare::class, 'pirep_id');
|
|
|
|
}
|
|
|
|
|
2018-07-13 10:20:10 +08:00
|
|
|
public function field_values()
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2018-07-13 10:20:10 +08:00
|
|
|
return $this->hasMany(PirepFieldValue::class, 'pirep_id');
|
2017-06-29 08:54:05 +08:00
|
|
|
}
|
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
public function pilot()
|
|
|
|
{
|
|
|
|
return $this->user();
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:47:22 +08:00
|
|
|
/**
|
|
|
|
* 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()
|
2017-12-21 01:28:21 +08:00
|
|
|
{
|
2022-03-19 04:43:34 +08:00
|
|
|
return $this->hasOne(Acars::class, 'pirep_id')->where('type', AcarsType::FLIGHT_PATH)->latest();
|
2017-12-21 01:28:21 +08:00
|
|
|
}
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
public function simbrief()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(SimBrief::class, 'id', 'pirep_id');
|
|
|
|
}
|
|
|
|
|
2018-03-03 06:09:48 +08:00
|
|
|
public function transactions()
|
|
|
|
{
|
2022-03-19 04:43:34 +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
|
|
|
}
|
|
|
|
}
|