2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
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-12 10:38:56 +08:00
|
|
|
use App\Support\Units\Time;
|
2018-02-11 11:16:32 +08:00
|
|
|
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
|
|
|
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
class Flight extends BaseModel
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
2018-03-08 22:51:36 +08:00
|
|
|
use HashIdTrait;
|
2017-06-25 02:20:24 +08:00
|
|
|
|
2018-02-21 05:57:31 +08:00
|
|
|
public const ID_MAX_LENGTH = 12;
|
|
|
|
|
2017-06-18 06:25:36 +08:00
|
|
|
public $table = 'flights';
|
2017-06-25 02:20:24 +08:00
|
|
|
public $incrementing = false;
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2018-02-28 03:37:29 +08:00
|
|
|
/** The form wants this */
|
|
|
|
public $hours, $minutes;
|
|
|
|
|
2017-12-13 03:59:05 +08:00
|
|
|
public $fillable = [
|
2018-01-30 08:14:55 +08:00
|
|
|
'id',
|
2017-12-13 03:59:05 +08:00
|
|
|
'airline_id',
|
|
|
|
'flight_number',
|
|
|
|
'route_code',
|
|
|
|
'route_leg',
|
|
|
|
'dpt_airport_id',
|
|
|
|
'arr_airport_id',
|
|
|
|
'alt_airport_id',
|
|
|
|
'dpt_time',
|
|
|
|
'arr_time',
|
2018-01-04 11:40:05 +08:00
|
|
|
'level',
|
|
|
|
'distance',
|
2018-02-12 10:38:56 +08:00
|
|
|
'flight_time',
|
2018-01-30 08:14:55 +08:00
|
|
|
'flight_type',
|
|
|
|
'route',
|
2017-12-13 03:59:05 +08:00
|
|
|
'notes',
|
|
|
|
'has_bid',
|
|
|
|
'active',
|
|
|
|
];
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2017-12-13 03:59:05 +08:00
|
|
|
protected $casts = [
|
|
|
|
'flight_number' => 'integer',
|
2018-01-04 11:40:05 +08:00
|
|
|
'level' => 'integer',
|
|
|
|
'distance' => 'float',
|
2018-02-12 10:38:56 +08:00
|
|
|
'flight_time' => 'integer',
|
2018-01-30 08:14:55 +08:00
|
|
|
'flight_type' => 'integer',
|
2017-12-13 03:59:05 +08:00
|
|
|
'has_bid' => 'boolean',
|
|
|
|
'active' => 'boolean',
|
|
|
|
];
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2017-12-13 03:59:05 +08:00
|
|
|
public static $rules = [
|
2018-01-24 11:58:13 +08:00
|
|
|
'airline_id' => 'required|exists:airlines,id',
|
2017-12-13 03:59:05 +08:00
|
|
|
'flight_number' => 'required',
|
2018-01-24 11:58:13 +08:00
|
|
|
'route_code' => 'nullable',
|
|
|
|
'route_leg' => 'nullable',
|
2017-12-13 03:59:05 +08:00
|
|
|
'dpt_airport_id' => 'required',
|
|
|
|
'arr_airport_id' => 'required',
|
2018-01-24 11:58:13 +08:00
|
|
|
'level' => 'nullable',
|
2017-12-13 03:59:05 +08:00
|
|
|
];
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
/**
|
|
|
|
* Get the flight ident, e.,g JBU1900
|
|
|
|
*/
|
|
|
|
public function getIdentAttribute()
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
2017-12-21 09:12:39 +08:00
|
|
|
$flight_id = $this->airline->code;
|
|
|
|
$flight_id .= $this->flight_number;
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-02-11 11:16:32 +08:00
|
|
|
if (filled($this->route_code)) {
|
|
|
|
$flight_id .= '/C' . $this->route_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filled($this->route_leg)) {
|
|
|
|
$flight_id .= '/L' . $this->route_leg;
|
|
|
|
}
|
2017-12-21 09:12:39 +08:00
|
|
|
|
|
|
|
return $flight_id;
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
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-02-11 12:49:08 +08:00
|
|
|
if (!array_key_exists('distance', $this->attributes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:16:32 +08:00
|
|
|
try {
|
|
|
|
$distance = (float) $this->attributes['distance'];
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the distance unit, convert to our internal default unit
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setDistanceAttribute($value)
|
|
|
|
{
|
|
|
|
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 11:16:32 +08:00
|
|
|
} else {
|
|
|
|
$this->attributes['distance'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:38:56 +08:00
|
|
|
/**
|
|
|
|
* @return Time
|
|
|
|
*/
|
2018-02-12 10:50:48 +08:00
|
|
|
/*public function getFlightTimeAttribute()
|
2018-02-12 10:38:56 +08:00
|
|
|
{
|
|
|
|
if (!array_key_exists('flight_time', $this->attributes)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Time($this->attributes['flight_time']);
|
2018-02-12 10:50:48 +08:00
|
|
|
}*/
|
2018-02-12 10:38:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*/
|
2018-02-12 10:50:48 +08:00
|
|
|
/*public function setFlightTimeAttribute($value)
|
2018-02-12 10:38:56 +08:00
|
|
|
{
|
|
|
|
if ($value instanceof Time) {
|
|
|
|
$this->attributes['flight_time'] = $value->getMinutes();
|
|
|
|
} else {
|
|
|
|
$this->attributes['flight_time'] = $value;
|
|
|
|
}
|
2018-02-12 10:50:48 +08:00
|
|
|
}*/
|
2018-02-12 10:38:56 +08:00
|
|
|
|
2017-07-11 08:06:06 +08:00
|
|
|
/**
|
|
|
|
* Relationship
|
|
|
|
*/
|
|
|
|
|
2017-06-20 00:50:25 +08:00
|
|
|
public function airline()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Airline::class, 'airline_id');
|
2017-06-20 00:50:25 +08:00
|
|
|
}
|
|
|
|
|
2017-06-18 06:25:36 +08:00
|
|
|
public function dpt_airport()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Airport::class, 'dpt_airport_id');
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function arr_airport()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Airport::class, 'arr_airport_id');
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function alt_airport()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Airport::class, 'alt_airport_id');
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
public function fares()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsToMany(Fare::class, 'flight_fare')
|
|
|
|
->withPivot('price', 'cost', 'capacity');
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|
|
|
|
|
2017-07-11 08:06:06 +08:00
|
|
|
public function fields()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->hasMany(FlightFields::class, 'flight_id');
|
2017-07-11 08:06:06 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
public function subfleets()
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsToMany(Subfleet::class, 'subfleet_flight');
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
}
|