phpvms/app/Models/Flight.php

254 lines
6.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use App\Interfaces\Model;
use App\Models\Enums\Days;
use App\Models\Traits\HashIdTrait;
2018-02-11 11:16:32 +08:00
use App\Support\Units\Distance;
use Carbon\Carbon;
use Illuminate\Support\Collection;
2018-02-11 11:16:32 +08:00
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
/**
* @property string id
* @property mixed ident
* @property Airline airline
2018-08-27 00:40:04 +08:00
* @property int airline_id
* @property mixed flight_number
* @property mixed route_code
* @property int route_leg
* @property bool has_bid
* @property Collection field_values
2018-03-22 06:07:30 +08:00
* @property Collection fares
* @property Collection subfleets
2018-08-27 00:40:04 +08:00
* @property int days
2018-03-31 09:57:30 +08:00
* @property Airport dep_airport
* @property Airport arr_airport
* @property Airport alt_airport
* @property string dpt_airport_id
* @property string arr_airport_id
* @property string alt_airport_id
* @property int active
* @property Carbon start_date
* @property Carbon end_date
*/
class Flight extends Model
{
use HashIdTrait;
2017-06-25 02:20:24 +08:00
public $table = 'flights';
2017-06-25 02:20:24 +08:00
public $incrementing = false;
/** The form wants this */
2018-08-27 00:40:04 +08:00
public $hours;
public $minutes;
2018-03-21 08:40:19 +08:00
protected $fillable = [
'id',
'airline_id',
'flight_number',
'route_code',
'route_leg',
'dpt_airport_id',
'arr_airport_id',
'alt_airport_id',
'dpt_time',
'arr_time',
'days',
'level',
'distance',
'flight_time',
'flight_type',
'route',
'notes',
'start_date',
'end_date',
'has_bid',
'active',
'visible',
];
protected $casts = [
'flight_number' => 'integer',
'days' => 'integer',
'level' => 'integer',
'distance' => 'float',
'flight_time' => 'integer',
'start_date' => 'date',
'end_date' => 'date',
'has_bid' => 'boolean',
'route_leg' => 'integer',
'active' => 'boolean',
'visible' => 'boolean',
];
public static $rules = [
'airline_id' => 'required|exists:airlines,id',
'flight_number' => 'required',
'route_code' => 'nullable',
'route_leg' => 'nullable',
'dpt_airport_id' => 'required',
'arr_airport_id' => 'required',
'level' => 'nullable',
];
/**
* Return all of the flights on any given day(s) of the week
* Search using bitmasks
2018-08-27 00:40:04 +08:00
*
* @param Days[] $days List of the enumerated values
2018-08-27 00:40:04 +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) {
$flights = $flights->where('days', '&', $day);
}
return $flights;
}
/**
* Get the flight ident, e.,g JBU1900
*/
public function getIdentAttribute(): string
{
$flight_id = $this->airline->code;
$flight_id .= $this->flight_number;
if (filled($this->route_code)) {
$flight_id .= '-'.$this->route_code;
2018-02-11 11:16:32 +08:00
}
if (filled($this->route_leg)) {
$flight_id .= '-'.$this->route_leg;
}
return $flight_id;
}
2018-02-11 11:16:32 +08:00
/**
* Return a new Length unit so conversions can be made
2018-08-27 00:40:04 +08:00
*
2018-02-11 11:16:32 +08:00
* @return int|Distance
*/
public function getDistanceAttribute()
{
if (!array_key_exists('distance', $this->attributes)) {
2018-08-27 02:50:08 +08:00
return 0;
}
2018-02-11 11:16:32 +08:00
try {
$distance = (float) $this->attributes['distance'];
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
2018-08-27 00:40:04 +08:00
*
2018-02-11 11:16:32 +08:00
* @param $value
*/
public function setDistanceAttribute($value): void
2018-02-11 11:16:32 +08:00
{
if ($value instanceof Distance) {
$this->attributes['distance'] = $value->toUnit(
config('phpvms.internal_units.distance')
);
2018-02-11 11:16:32 +08:00
} else {
$this->attributes['distance'] = $value;
}
}
/**
* @param $day
2018-08-27 00:40:04 +08:00
*
* @return bool
*/
public function on_day($day): bool
{
return ($this->days & $day) === $day;
}
/**
* Return a custom field value
2018-08-27 00:40:04 +08:00
*
* @param $field_name
2018-08-27 00:40:04 +08:00
*
* @return string
*/
public function field($field_name): string
{
$field = $this->field_values->where('name', $field_name)->first();
2018-08-27 00:40:04 +08:00
if ($field) {
return $field['value'];
}
return '';
}
/**
* 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
*
* @param array|int $val
*/
public function setDaysAttribute($val): void
{
if (\is_array($val)) {
$val = Days::getDaysMask($val);
}
$this->attributes['days'] = $val;
}
/**
* 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
}
public function dpt_airport()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'dpt_airport_id');
}
public function arr_airport()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'arr_airport_id');
}
public function alt_airport()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'alt_airport_id');
}
public function fares()
{
2018-01-08 23:22:12 +08:00
return $this->belongsToMany(Fare::class, 'flight_fare')
->withPivot('price', 'cost', 'capacity');
}
public function field_values()
{
return $this->hasMany(FlightFieldValue::class, 'flight_id');
}
public function subfleets()
{
2018-03-21 05:11:24 +08:00
return $this->belongsToMany(Subfleet::class, 'flight_subfleet');
}
}