phpvms/app/Models/Pirep.php

362 lines
9.1 KiB
PHP
Raw Normal View History

2017-06-29 08:54:05 +08:00
<?php
namespace App\Models;
use App\Models\Enums\AcarsType;
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;
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-03 07:29:11 +08:00
* @property string flight_number
* @property string route_code
* @property string route_leg
* @property integer airline_id
2018-03-03 07:29:11 +08:00
* @property Aircraft aircraft
* @property Airline airline
* @property Airport arr_airport
* @property Airport dep_airport
2018-03-03 07:29:11 +08:00
* @property integer flight_time In minutes
* @property User user
* @property Flight|null flight
* @property int user_id
2017-06-29 08:54:05 +08:00
* @package App\Models
*/
class Pirep extends BaseModel
2017-06-29 08:54:05 +08:00
{
use HashIdTrait;
2017-06-29 08:54:05 +08:00
public const ID_MAX_LENGTH = 12;
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
/** The form wants this */
public $hours, $minutes;
public $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',
'level',
'distance',
'planned_distance',
'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-01-31 01:15:07 +08:00
'source',
'source_name',
'flight_type',
'state',
'status',
'created_at',
'updated_at',
];
2017-06-29 08:54:05 +08:00
protected $casts = [
'user_id' => 'integer',
2018-01-31 01:15:07 +08:00
'airline_id' => 'integer',
'aircraft_id' => 'integer',
'level' => 'integer',
2018-01-30 06:29:02 +08:00
'distance' => 'float',
'planned_distance' => 'float',
'flight_time' => 'integer',
'planned_flight_time' => 'integer',
2018-01-31 01:09:59 +08:00
'zfw' => 'float',
'block_fuel' => 'float',
2018-01-31 01:27:39 +08:00
'fuel_used' => 'float',
'landing_rate' => 'float',
'source' => 'integer',
2018-01-31 01:15:07 +08:00
'flight_type' => 'integer',
'state' => 'integer',
'status' => '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',
'notes' => 'nullable',
'route' => 'nullable',
];
2017-06-29 08:54:05 +08:00
2017-12-04 05:29:34 +08:00
/**
* Get the flight ident, e.,g JBU1900
2017-12-04 05:29:34 +08:00
* @return string
*/
public function getIdentAttribute()
2017-12-04 05:29:34 +08:00
{
$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;
2017-12-04 05:29:34 +08:00
}
return $flight_id;
}
2018-02-11 11:16:32 +08:00
/**
* Return a new Length unit so conversions can be made
* @return int|Distance
*/
public function getDistanceAttribute()
{
if(!array_key_exists('distance', $this->attributes)) {
return null;
}
2018-02-11 11:16:32 +08:00
try {
$distance = (float) $this->attributes['distance'];
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;
}
}
/**
* 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 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'];
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()
{
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'];
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;
}
}
/**
* Look up the flight, based on the PIREP flight info
* @return Flight|null
*/
public function getFlightAttribute()
{
$where = [
'airline_id' => $this->airline_id,
'flight_number' => $this->flight_number,
'active' => true,
];
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();
}
/**
* 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;
}
}
/**
* Set the distance unit, convert to our internal default unit
* @param $value
*/
public function setPlannedDistanceAttribute($value)
{
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
*/
2018-03-03 07:29:11 +08:00
public function setRouteAttribute($route): void
{
$route = strtoupper(trim($route));
$this->attributes['route'] = $route;
}
/**
* 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-31 01:09:59 +08:00
if($this->state === PirepState::CANCELLED) {
return false;
}
return true;
}
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)
->orderBy('created_at', 'desc');
}
public function acars_logs()
{
2018-01-08 23:22:12 +08:00
return $this->hasMany(Acars::class, 'pirep_id')
->where('type', AcarsType::LOG)
->orderBy('created_at', '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')
2018-01-02 06:01:01 +08:00
->where('type', AcarsType::ROUTE)
2018-01-05 09:33:23 +08:00
->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')
->orderBy('created_at', 'desc');
2017-07-24 02:07:41 +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
}
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()
{
return $this->hasMany(JournalTransaction::class, 'ref_class_id')
2018-03-03 07:29:11 +08:00
->where('ref_class', __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
}
}