2017-06-29 08:54:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
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;
|
2017-12-13 02:43:58 +08:00
|
|
|
use App\Models\Traits\HashId;
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Pirep
|
|
|
|
*
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
2017-12-26 05:19:34 +08:00
|
|
|
class Pirep extends BaseModel
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2017-12-13 02:43:58 +08:00
|
|
|
use HashId;
|
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
|
|
|
|
2017-12-10 11:21:49 +08:00
|
|
|
public $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_id',
|
|
|
|
'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',
|
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-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',
|
|
|
|
'raw_data',
|
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-01-29 01:12:13 +08:00
|
|
|
'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',
|
2017-12-26 05:19:34 +08:00
|
|
|
'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',
|
2018-01-30 08:14:55 +08:00
|
|
|
'landing_rate' => 'float',
|
2017-12-26 05:19:34 +08:00
|
|
|
'source' => 'integer',
|
2018-01-31 01:15:07 +08:00
|
|
|
'flight_type' => 'integer',
|
2017-12-26 05:19:34 +08:00
|
|
|
'state' => 'integer',
|
|
|
|
'status' => '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-01-24 05:48:30 +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
|
|
|
|
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
|
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
public function getIdentAttribute()
|
2017-12-04 05:29:34 +08:00
|
|
|
{
|
|
|
|
$flight_id = $this->airline->code;
|
2018-01-24 05:48:30 +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-02-07 02:58:48 +08:00
|
|
|
/**
|
|
|
|
* Do some cleanup on the route
|
|
|
|
* @param $route
|
|
|
|
*/
|
|
|
|
public function setRouteAttribute($route)
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function allowedUpdates()
|
|
|
|
{
|
2018-01-31 01:09:59 +08:00
|
|
|
if($this->state === PirepState::CANCELLED) {
|
2018-01-29 01:12:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-01-02 00:38:45 +08:00
|
|
|
->where('type', AcarsType::FLIGHT_PATH)
|
|
|
|
->orderBy('created_at', 'asc');
|
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-01-04 12:19:06 +08:00
|
|
|
->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')
|
2017-12-28 06:47:22 +08:00
|
|
|
->orderBy('created_at', 'desc');
|
2017-07-24 02:07:41 +08:00
|
|
|
}
|
|
|
|
|
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-06-30 04:50:16 +08:00
|
|
|
public function flight()
|
2017-06-29 08:54:05 +08:00
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Flight::class, 'flight_id');
|
2017-06-30 04:50:16 +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-01-02 00:38:45 +08:00
|
|
|
->where('type', AcarsType::FLIGHT_PATH)
|
|
|
|
->latest();
|
2017-12-21 01:28:21 +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
|
|
|
}
|
|
|
|
}
|