2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Model;
|
2018-03-23 10:21:35 +08:00
|
|
|
use App\Models\Enums\Days;
|
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-04-13 05:12:32 +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
|
|
|
|
2018-03-19 09:37:35 +08:00
|
|
|
/**
|
2018-03-21 02:06:06 +08:00
|
|
|
* @property string id
|
2018-07-25 04:58:11 +08:00
|
|
|
* @property mixed ident
|
2018-03-21 02:06:06 +08:00
|
|
|
* @property Airline airline
|
2019-08-05 20:27:53 +08:00
|
|
|
* @property int airline_id
|
2018-03-21 02:06:06 +08:00
|
|
|
* @property mixed flight_number
|
|
|
|
* @property mixed route_code
|
2018-07-25 03:50:50 +08:00
|
|
|
* @property int route_leg
|
|
|
|
* @property bool has_bid
|
2018-03-21 02:06:06 +08:00
|
|
|
* @property Collection field_values
|
2018-03-22 06:07:30 +08:00
|
|
|
* @property Collection fares
|
2018-03-23 01:43:58 +08:00
|
|
|
* @property Collection subfleets
|
2019-08-05 20:27:53 +08:00
|
|
|
* @property int days
|
2019-11-06 00:44:31 +08:00
|
|
|
* @property int distance
|
|
|
|
* @property int flight_time
|
2019-08-05 20:27:53 +08:00
|
|
|
* @property string route
|
2019-11-06 00:44:31 +08:00
|
|
|
* @property int level
|
2019-08-05 20:27:53 +08:00
|
|
|
* @property Airport dpt_airport
|
2018-03-31 09:57:30 +08:00
|
|
|
* @property Airport arr_airport
|
|
|
|
* @property Airport alt_airport
|
2018-03-24 01:27:28 +08:00
|
|
|
* @property string dpt_airport_id
|
|
|
|
* @property string arr_airport_id
|
|
|
|
* @property string alt_airport_id
|
2018-04-13 05:12:32 +08:00
|
|
|
* @property int active
|
|
|
|
* @property Carbon start_date
|
|
|
|
* @property Carbon end_date
|
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';
|
|
|
|
|
2018-02-28 03:37:29 +08:00
|
|
|
/** The form wants this */
|
2018-08-27 00:40:04 +08:00
|
|
|
public $hours;
|
|
|
|
public $minutes;
|
2018-02-28 03:37:29 +08:00
|
|
|
|
2019-09-13 20:05:02 +08:00
|
|
|
protected $keyType = 'string';
|
|
|
|
public $incrementing = false;
|
|
|
|
|
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-03-23 10:21:35 +08:00
|
|
|
'days',
|
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',
|
2018-04-10 08:32:38 +08:00
|
|
|
'start_date',
|
|
|
|
'end_date',
|
2017-12-13 03:59:05 +08:00
|
|
|
'has_bid',
|
|
|
|
'active',
|
2018-07-13 11:23:45 +08:00
|
|
|
'visible',
|
2017-12-13 03:59:05 +08:00
|
|
|
];
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2017-12-13 03:59:05 +08:00
|
|
|
protected $casts = [
|
|
|
|
'flight_number' => 'integer',
|
2018-03-23 10:21:35 +08:00
|
|
|
'days' => '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-04-10 08:32:38 +08:00
|
|
|
'start_date' => 'date',
|
|
|
|
'end_date' => 'date',
|
2017-12-13 03:59:05 +08:00
|
|
|
'has_bid' => 'boolean',
|
2018-07-13 11:23:45 +08:00
|
|
|
'route_leg' => 'integer',
|
2017-12-13 03:59:05 +08:00
|
|
|
'active' => 'boolean',
|
2018-07-13 11:23:45 +08:00
|
|
|
'visible' => 'boolean',
|
2017-12-13 03:59:05 +08:00
|
|
|
];
|
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',
|
2019-12-03 07:27:58 +08:00
|
|
|
'dpt_airport_id' => 'required|size:4|exists:airports,id',
|
|
|
|
'arr_airport_id' => 'required|size:4|exists:airports,id',
|
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
|
|
|
|
2018-03-23 10:21:35 +08:00
|
|
|
/**
|
|
|
|
* Return all of the flights on any given day(s) of the week
|
|
|
|
* Search using bitmasks
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 10:21:35 +08:00
|
|
|
* @param Days[] $days List of the enumerated values
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 10:21:35 +08:00
|
|
|
* @return Flight
|
|
|
|
*/
|
|
|
|
public static function findByDays(array $days)
|
|
|
|
{
|
2018-08-27 02:50:08 +08:00
|
|
|
/** @noinspection DynamicInvocationViaScopeResolutionInspection */
|
2018-08-27 00:40:04 +08:00
|
|
|
$flights = self::where('active', true);
|
|
|
|
foreach ($days as $day) {
|
2018-03-23 10:21:35 +08:00
|
|
|
$flights = $flights->where('days', '&', $day);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $flights;
|
|
|
|
}
|
|
|
|
|
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-05-16 14:47:31 +08:00
|
|
|
if (filled($this->route_code)) {
|
|
|
|
$flight_id .= '-'.$this->route_code;
|
2018-02-11 11:16:32 +08:00
|
|
|
}
|
2017-12-21 09:12:39 +08:00
|
|
|
|
2018-05-16 14:47:31 +08:00
|
|
|
if (filled($this->route_leg)) {
|
|
|
|
$flight_id .= '-'.$this->route_leg;
|
2018-05-14 05:59:35 +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
|
|
|
/**
|
|
|
|
* Set the distance unit, convert to our internal default unit
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 11:16:32 +08:00
|
|
|
* @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-23 10:21:35 +08:00
|
|
|
/**
|
|
|
|
* @param $day
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 10:21:35 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function on_day($day): bool
|
|
|
|
{
|
|
|
|
return ($this->days & $day) === $day;
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:36:28 +08:00
|
|
|
/**
|
|
|
|
* Return a custom field value
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 00:36:28 +08:00
|
|
|
* @param $field_name
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 00:36:28 +08:00
|
|
|
* @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-08-27 00:40:04 +08:00
|
|
|
if ($field) {
|
2018-03-21 00:36:28 +08:00
|
|
|
return $field['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:12:32 +08:00
|
|
|
/**
|
|
|
|
* Set the days parameter. If an array is passed, it's
|
|
|
|
* AND'd together to create the mask value
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-13 05:12:32 +08:00
|
|
|
* @param array|int $val
|
|
|
|
*/
|
|
|
|
public function setDaysAttribute($val): void
|
|
|
|
{
|
|
|
|
if (\is_array($val)) {
|
|
|
|
$val = Days::getDaysMask($val);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->attributes['days'] = $val;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|