2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Model;
|
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-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;
|
|
|
|
|
2018-03-19 09:37:35 +08:00
|
|
|
/**
|
2018-03-21 02:06:06 +08:00
|
|
|
* @property string id
|
|
|
|
* @property Airline airline
|
|
|
|
* @property mixed flight_number
|
|
|
|
* @property mixed route_code
|
|
|
|
* @property mixed route_leg
|
|
|
|
* @property Collection field_values
|
2018-03-22 06:07:30 +08:00
|
|
|
* @property Collection fares
|
2018-03-19 09:37:35 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class Flight extends Model
|
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
|
|
|
|
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;
|
|
|
|
|
2018-03-21 08:40:19 +08:00
|
|
|
protected $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
|
|
|
|
*/
|
2018-03-19 09:37:35 +08:00
|
|
|
public function getIdentAttribute(): string
|
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)) {
|
2018-03-20 09:50:40 +08:00
|
|
|
$flight_id .= '/C'.$this->route_code;
|
2018-02-11 11:16:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (filled($this->route_leg)) {
|
2018-03-20 09:50:40 +08:00
|
|
|
$flight_id .= '/L'.$this->route_leg;
|
2018-02-11 11:16:32 +08:00
|
|
|
}
|
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-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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the distance unit, convert to our internal default unit
|
|
|
|
* @param $value
|
|
|
|
*/
|
2018-03-19 09:37:35 +08:00
|
|
|
public function setDistanceAttribute($value): void
|
2018-02-11 11:16:32 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
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-03-21 00:36:28 +08:00
|
|
|
/**
|
|
|
|
* Return a custom field value
|
|
|
|
* @param $field_name
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-21 00:46:48 +08:00
|
|
|
public function field($field_name): string
|
2018-03-21 00:36:28 +08:00
|
|
|
{
|
2018-03-21 02:06:06 +08:00
|
|
|
$field = $this->field_values->where('name', $field_name)->first();
|
2018-03-21 00:36:28 +08:00
|
|
|
if($field) {
|
|
|
|
return $field['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
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')
|
2018-03-20 09:50:40 +08:00
|
|
|
->withPivot('price', 'cost', 'capacity');
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:06:06 +08:00
|
|
|
public function field_values()
|
2017-07-11 08:06:06 +08:00
|
|
|
{
|
2018-03-21 02:06:06 +08:00
|
|
|
return $this->hasMany(FlightFieldValue::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-03-21 05:11:24 +08:00
|
|
|
return $this->belongsToMany(Subfleet::class, 'flight_subfleet');
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
}
|