2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Model;
|
2022-03-14 23:45:18 +08:00
|
|
|
use App\Models\Casts\DistanceCast;
|
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;
|
2022-03-14 23:45:18 +08:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
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
|
2021-04-06 06:00:50 +08:00
|
|
|
* @property mixed callsign
|
2018-03-21 02:06:06 +08:00
|
|
|
* @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
|
2022-03-14 23:45:18 +08:00
|
|
|
* @property Distance distance
|
|
|
|
* @property Distance planned_distance
|
2019-11-06 00:44:31 +08:00
|
|
|
* @property int flight_time
|
2019-08-05 20:27:53 +08:00
|
|
|
* @property string route
|
2020-10-28 06:46:15 +08:00
|
|
|
* @property string dpt_time
|
|
|
|
* @property string arr_time
|
|
|
|
* @property string flight_type
|
|
|
|
* @property string notes
|
2019-11-06 00:44:31 +08:00
|
|
|
* @property int level
|
2020-03-06 09:19:12 +08:00
|
|
|
* @property float load_factor
|
|
|
|
* @property float load_factor_variance
|
2020-03-07 00:36:02 +08:00
|
|
|
* @property float pilot_pay
|
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;
|
2022-03-14 23:45:18 +08:00
|
|
|
use HasFactory;
|
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;
|
2022-03-14 23:45:18 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
public $minutes;
|
2018-02-28 03:37:29 +08:00
|
|
|
|
2019-09-13 20:05:02 +08:00
|
|
|
protected $keyType = 'string';
|
2022-03-14 23:45:18 +08:00
|
|
|
|
2019-09-13 20:05:02 +08:00
|
|
|
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',
|
2021-04-06 06:00:50 +08:00
|
|
|
'callsign',
|
2017-12-13 03:59:05 +08:00
|
|
|
'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',
|
2020-03-06 09:19:12 +08:00
|
|
|
'load_factor',
|
|
|
|
'load_factor_variance',
|
2020-03-07 00:36:02 +08:00
|
|
|
'pilot_pay',
|
2018-01-30 08:14:55 +08:00
|
|
|
'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 = [
|
2020-03-06 09:19:12 +08:00
|
|
|
'flight_number' => 'integer',
|
|
|
|
'days' => 'integer',
|
|
|
|
'level' => 'integer',
|
2022-03-14 23:45:18 +08:00
|
|
|
'distance' => DistanceCast::class,
|
2020-03-06 09:19:12 +08:00
|
|
|
'flight_time' => 'integer',
|
|
|
|
'start_date' => 'date',
|
|
|
|
'end_date' => 'date',
|
|
|
|
'load_factor' => 'double',
|
|
|
|
'load_factor_variance' => 'double',
|
2020-03-07 00:36:02 +08:00
|
|
|
'pilot_pay' => 'float',
|
2020-03-06 09:19:12 +08:00
|
|
|
'has_bid' => 'boolean',
|
|
|
|
'route_leg' => 'integer',
|
|
|
|
'active' => 'boolean',
|
|
|
|
'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 = [
|
2020-03-06 09:19:12 +08:00
|
|
|
'airline_id' => 'required|exists:airlines,id',
|
|
|
|
'flight_number' => 'required',
|
2021-04-06 06:00:50 +08:00
|
|
|
'callsign' => 'string|max:4|nullable',
|
2020-03-06 09:19:12 +08:00
|
|
|
'route_code' => 'nullable',
|
|
|
|
'route_leg' => 'nullable',
|
|
|
|
'dpt_airport_id' => 'required|exists:airports,id',
|
|
|
|
'arr_airport_id' => 'required|exists:airports,id',
|
|
|
|
'load_factor' => 'nullable|numeric',
|
|
|
|
'load_factor_variance' => 'nullable|numeric',
|
|
|
|
'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
|
|
|
/**
|
2021-11-02 21:23:52 +08:00
|
|
|
* Get the flight ident, e.,g JBU1900/C.nn/L.yy
|
2017-12-20 10:19:36 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function ident(): Attribute
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
return Attribute::make(
|
|
|
|
get: function ($_, $attrs) {
|
|
|
|
$flight_id = optional($this->airline)->code;
|
|
|
|
$flight_id .= $this->flight_number;
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
if (filled($this->route_code)) {
|
|
|
|
$flight_id .= '/C.'.$this->route_code;
|
|
|
|
}
|
2017-12-21 09:12:39 +08:00
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
if (filled($this->route_leg)) {
|
|
|
|
$flight_id .= '/L.'.$this->route_leg;
|
|
|
|
}
|
2018-05-14 05:59:35 +08:00
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
return $flight_id;
|
|
|
|
}
|
|
|
|
);
|
2018-02-11 11:16:32 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return Attribute
|
2018-04-13 05:12:32 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function days(): Attribute
|
2018-04-13 05:12:32 +08:00
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
return Attribute::make(
|
|
|
|
set: function ($value) {
|
|
|
|
if (\is_array($value)) {
|
|
|
|
$value = Days::getDaysMask($value);
|
|
|
|
}
|
2018-04-13 05:12:32 +08:00
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
);
|
2018-04-13 05:12:32 +08:00
|
|
|
}
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
/*
|
|
|
|
* Relationships
|
2017-07-11 08:06:06 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
public function simbrief()
|
|
|
|
{
|
|
|
|
// id = key from table, flight_id = reference key
|
|
|
|
return $this->belongsTo(SimBrief::class, 'id', 'flight_id');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|