2017-06-29 08:54:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Model;
|
2018-01-02 00:38:45 +08:00
|
|
|
use App\Models\Enums\AcarsType;
|
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;
|
2018-03-21 00:46:48 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2018-02-11 11:16:32 +08:00
|
|
|
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
|
|
|
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Pirep
|
2018-03-30 03:55:25 +08:00
|
|
|
* @property string id
|
2018-03-20 09:50:40 +08:00
|
|
|
* @property string flight_number
|
|
|
|
* @property string route_code
|
|
|
|
* @property string route_leg
|
|
|
|
* @property integer airline_id
|
|
|
|
* @property integer user_id
|
2018-03-31 00:08:53 +08:00
|
|
|
* @property integer 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-03-30 02:42:16 +08:00
|
|
|
* @property integer block_time
|
2018-03-20 09:50:40 +08:00
|
|
|
* @property integer flight_time In minutes
|
2018-04-05 06:42:43 +08:00
|
|
|
* @property integer planned_flight_time
|
2018-05-02 09:58:05 +08:00
|
|
|
* @property mixed planned_distance
|
|
|
|
* @property mixed distance
|
2018-04-26 00:56:05 +08:00
|
|
|
* @property integer 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
|
2018-04-05 02:03:10 +08:00
|
|
|
* @property Carbon submitted_at
|
|
|
|
* @property Carbon created_at
|
|
|
|
* @property Carbon updated_at
|
2018-04-10 11:04:59 +08:00
|
|
|
* @property bool state
|
2018-05-03 04:14:18 +08:00
|
|
|
* @property Acars position
|
2018-05-05 02:59:47 +08:00
|
|
|
* @property Acars[] acars
|
2017-06-29 08:54:05 +08:00
|
|
|
* @package App\Models
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class Pirep extends Model
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2018-03-08 22:51:36 +08:00
|
|
|
use HashIdTrait;
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
public $table = 'pireps';
|
2017-07-05 02:57:08 +08:00
|
|
|
public $incrementing = false;
|
2017-06-29 08:54:05 +08:00
|
|
|
|
2018-02-28 03:37:29 +08:00
|
|
|
/** The form wants this */
|
|
|
|
public $hours, $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',
|
2018-01-31 01:15:07 +08:00
|
|
|
'dpt_airport_id',
|
|
|
|
'arr_airport_id',
|
|
|
|
'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
|
|
|
];
|
2017-06-29 08:54:05 +08:00
|
|
|
|
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',
|
|
|
|
'distance' => 'float',
|
|
|
|
'planned_distance' => 'float',
|
2018-03-30 02:42:16 +08:00
|
|
|
'block_time' => 'integer',
|
2018-03-20 09:50:40 +08:00
|
|
|
'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',
|
2018-03-20 09:50:40 +08:00
|
|
|
'source' => 'integer',
|
|
|
|
'state' => 'integer',
|
2017-12-10 11:21:49 +08:00
|
|
|
];
|
2017-06-29 08:54:05 +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',
|
|
|
|
'notes' => 'nullable',
|
|
|
|
'route' => 'nullable',
|
2017-12-20 10:19:36 +08:00
|
|
|
];
|
2017-06-29 08:54:05 +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::PENDING,
|
|
|
|
PirepState::CANCELLED,
|
|
|
|
];
|
2018-05-10 01:45:24 +08:00
|
|
|
|
2017-12-04 05:29:34 +08:00
|
|
|
/**
|
2017-12-20 10:19:36 +08:00
|
|
|
* Get the flight ident, e.,g JBU1900
|
2017-12-04 05:29:34 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-19 09:37:35 +08:00
|
|
|
public function getIdentAttribute(): string
|
2017-12-04 05:29:34 +08:00
|
|
|
{
|
2018-04-04 11:44:11 +08:00
|
|
|
#$flight_id = $this->airline->code;
|
|
|
|
$flight_id = $this->flight_number;
|
2018-01-24 05:48:30 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (filled($this->route_code)) {
|
2018-01-24 05:48:30 +08:00
|
|
|
$flight_id .= '/C'.$this->route_code;
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (filled($this->route_leg)) {
|
2018-01-24 05:48:30 +08:00
|
|
|
$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
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function getBlockOffTimeAttribute()
|
|
|
|
{
|
2018-05-02 11:52:31 +08:00
|
|
|
if (array_key_exists('block_off_time', $this->attributes)) {
|
|
|
|
return new Carbon($this->attributes['block_off_time']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-05-02 09:58:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the block on time
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function getBlockOnTimeAttribute()
|
|
|
|
{
|
2018-05-02 11:52:31 +08:00
|
|
|
if (array_key_exists('block_on_time', $this->attributes)) {
|
|
|
|
return new Carbon($this->attributes['block_on_time']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-05-02 09:58:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-03 04:14:18 +08:00
|
|
|
/**
|
|
|
|
* 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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:16:32 +08:00
|
|
|
/**
|
|
|
|
* Return a new Length unit so conversions can be made
|
|
|
|
* @return int|Distance
|
|
|
|
*/
|
|
|
|
public function getDistanceAttribute()
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!array_key_exists('distance', $this->attributes)) {
|
2018-02-11 12:49:08 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:16:32 +08:00
|
|
|
try {
|
2018-02-11 12:04:30 +08:00
|
|
|
$distance = (float) $this->attributes['distance'];
|
2018-04-08 09:52:12 +08:00
|
|
|
if ($this->skip_mutator) {
|
|
|
|
return $distance;
|
|
|
|
}
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-12 10:19:02 +08:00
|
|
|
return new Distance($distance, config('phpvms.internal_units.distance'));
|
2018-02-11 11:16:32 +08:00
|
|
|
} catch (NonNumericValue $e) {
|
|
|
|
return 0;
|
|
|
|
} catch (NonStringUnitName $e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 12:04:30 +08:00
|
|
|
/**
|
|
|
|
* Set the distance unit, convert to our internal default unit
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setDistanceAttribute($value): void
|
|
|
|
{
|
|
|
|
if ($value instanceof Distance) {
|
2018-02-12 10:19:02 +08:00
|
|
|
$this->attributes['distance'] = $value->toUnit(
|
|
|
|
config('phpvms.internal_units.distance'));
|
2018-02-11 12:04:30 +08:00
|
|
|
} else {
|
|
|
|
$this->attributes['distance'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 11:04:59 +08:00
|
|
|
/**
|
|
|
|
* Return if this PIREP can be edited or not
|
|
|
|
*/
|
|
|
|
public function getReadOnlyAttribute(): bool
|
|
|
|
{
|
2018-05-10 23:35:10 +08:00
|
|
|
return \in_array($this->state, static::$read_only_states, true);
|
2018-04-10 11:04:59 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 04:07:33 +08:00
|
|
|
/**
|
|
|
|
* Return a new Fuel unit so conversions can be made
|
|
|
|
* @return int|Fuel
|
|
|
|
*/
|
|
|
|
public function getFuelUsedAttribute()
|
|
|
|
{
|
|
|
|
if (!array_key_exists('fuel_used', $this->attributes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$fuel_used = (float) $this->attributes['fuel_used'];
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-21 04:07:33 +08:00
|
|
|
return new Fuel($fuel_used, config('phpvms.internal_units.fuel'));
|
|
|
|
} catch (NonNumericValue $e) {
|
|
|
|
return 0;
|
|
|
|
} catch (NonStringUnitName $e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:16:32 +08:00
|
|
|
/**
|
|
|
|
* Return the planned_distance in a converter class
|
|
|
|
* @return int|Distance
|
|
|
|
*/
|
|
|
|
public function getPlannedDistanceAttribute()
|
|
|
|
{
|
2018-02-11 12:49:08 +08:00
|
|
|
if (!array_key_exists('planned_distance', $this->attributes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:16:32 +08:00
|
|
|
try {
|
|
|
|
$distance = (float) $this->attributes['planned_distance'];
|
2018-04-08 09:52:12 +08:00
|
|
|
if ($this->skip_mutator) {
|
|
|
|
return $distance;
|
|
|
|
}
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-12 10:19:02 +08:00
|
|
|
return new Distance($distance, config('phpvms.internal_units.distance'));
|
2018-02-11 11:16:32 +08:00
|
|
|
} catch (NonNumericValue $e) {
|
|
|
|
return 0;
|
|
|
|
} catch (NonStringUnitName $e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 06:42:43 +08:00
|
|
|
/**
|
|
|
|
* Return the flight progress in a percent.
|
|
|
|
*/
|
|
|
|
public function getProgressPercentAttribute()
|
|
|
|
{
|
2018-05-02 09:58:05 +08:00
|
|
|
$upper_bound = $this->distance['nmi'];
|
|
|
|
if($this->planned_distance) {
|
|
|
|
$upper_bound = $this->planned_distance['nmi'];
|
2018-04-05 06:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!$upper_bound) {
|
|
|
|
$upper_bound = 1;
|
|
|
|
}
|
|
|
|
|
2018-05-02 09:58:05 +08:00
|
|
|
return round(($this->distance['nmi'] / $upper_bound) * 100, 0);
|
2018-04-05 06:42:43 +08:00
|
|
|
}
|
|
|
|
|
2018-03-16 07:20:07 +08:00
|
|
|
/**
|
|
|
|
* Look up the flight, based on the PIREP flight info
|
|
|
|
* @return Flight|null
|
|
|
|
*/
|
2018-03-19 09:37:35 +08:00
|
|
|
public function getFlightAttribute(): ?Flight
|
2018-03-16 07:20:07 +08:00
|
|
|
{
|
|
|
|
$where = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'airline_id' => $this->airline_id,
|
2018-03-16 07:20:07 +08:00
|
|
|
'flight_number' => $this->flight_number,
|
2018-03-20 09:50:40 +08:00
|
|
|
'active' => true,
|
2018-03-16 07:20:07 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if (filled($this->route_code)) {
|
|
|
|
$where['route_code'] = $this->route_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filled($this->route_leg)) {
|
|
|
|
$where['route_leg'] = $this->route_leg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Flight::where($where)->first();
|
|
|
|
}
|
|
|
|
|
2018-02-21 04:07:33 +08:00
|
|
|
/**
|
|
|
|
* Set the amount of fuel used
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setFuelUsedAttribute($value)
|
|
|
|
{
|
|
|
|
if ($value instanceof Fuel) {
|
|
|
|
$this->attributes['fuel_used'] = $value->toUnit(
|
|
|
|
config('phpvms.internal_units.fuel')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->attributes['fuel_used'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 12:04:30 +08:00
|
|
|
/**
|
|
|
|
* Set the distance unit, convert to our internal default unit
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setPlannedDistanceAttribute($value)
|
|
|
|
{
|
|
|
|
if ($value instanceof Distance) {
|
2018-02-12 10:19:02 +08:00
|
|
|
$this->attributes['planned_distance'] = $value->toUnit(
|
|
|
|
config('phpvms.internal_units.distance')
|
|
|
|
);
|
2018-02-11 12:04:30 +08:00
|
|
|
} else {
|
2018-02-11 12:30:42 +08:00
|
|
|
$this->attributes['planned_distance'] = $value;
|
2018-02-11 12:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 02:58:48 +08:00
|
|
|
/**
|
|
|
|
* Do some cleanup on the route
|
|
|
|
* @param $route
|
|
|
|
*/
|
2018-03-03 07:29:11 +08:00
|
|
|
public function setRouteAttribute($route): void
|
2018-02-07 02:58:48 +08:00
|
|
|
{
|
|
|
|
$route = strtoupper(trim($route));
|
|
|
|
$this->attributes['route'] = $route;
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
/**
|
|
|
|
* Check if this PIREP is allowed to be updated
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-03-03 07:29:11 +08:00
|
|
|
public function allowedUpdates(): bool
|
2018-01-29 01:12:13 +08:00
|
|
|
{
|
2018-05-10 23:35:10 +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
|
|
|
|
* @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 '';
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
/**
|
|
|
|
* Foreign Keys
|
|
|
|
*/
|
2017-12-27 05:26:12 +08:00
|
|
|
|
|
|
|
public function acars()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->hasMany(Acars::class, 'pirep_id')
|
2018-03-20 09:50:40 +08:00
|
|
|
->where('type', AcarsType::FLIGHT_PATH)
|
|
|
|
->orderBy('created_at', 'desc');
|
2017-12-27 05:26:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-04 12:19:06 +08:00
|
|
|
public function acars_logs()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->hasMany(Acars::class, 'pirep_id')
|
2018-03-20 09:50:40 +08:00
|
|
|
->where('type', AcarsType::LOG)
|
|
|
|
->orderBy('created_at', 'asc');
|
2018-01-04 12:19:06 +08:00
|
|
|
}
|
|
|
|
|
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')
|
2018-03-20 09:50:40 +08:00
|
|
|
->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
|
|
|
}
|
|
|
|
|
2017-06-30 04:50:16 +08:00
|
|
|
public function arr_airport()
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Airport::class, 'arr_airport_id');
|
2017-06-29 08:54:05 +08:00
|
|
|
}
|
|
|
|
|
2017-06-30 04:50:16 +08:00
|
|
|
public function dpt_airport()
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Airport::class, 'dpt_airport_id');
|
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')
|
2018-03-20 09:50:40 +08:00
|
|
|
->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');
|
|
|
|
}
|
|
|
|
|
2017-06-30 04:50:16 +08:00
|
|
|
public function fields()
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->hasMany(PirepFieldValues::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
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->hasOne(Acars::class, 'pirep_id')
|
2018-03-20 09:50:40 +08:00
|
|
|
->where('type', AcarsType::FLIGHT_PATH)
|
|
|
|
->latest();
|
2017-12-21 01:28:21 +08:00
|
|
|
}
|
|
|
|
|
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__)
|
2018-03-20 09:50:40 +08:00
|
|
|
->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
|
|
|
}
|
|
|
|
}
|