diff --git a/app/Database/factories/FlightFactory.php b/app/Database/factories/FlightFactory.php index 83776e99..b4d81fb1 100644 --- a/app/Database/factories/FlightFactory.php +++ b/app/Database/factories/FlightFactory.php @@ -26,6 +26,8 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) use ($airline }, 'distance' => $faker->numberBetween(0, 3000), 'route' => null, + 'days' => 0, + 'level' => 0, 'dpt_time' => $faker->time(), 'arr_time' => $faker->time(), 'flight_time' => $faker->numberBetween(60, 360), diff --git a/app/Database/migrations/2017_06_17_214650_create_flight_tables.php b/app/Database/migrations/2017_06_17_214650_create_flight_tables.php index b6c016e2..ed8c729d 100644 --- a/app/Database/migrations/2017_06_17_214650_create_flight_tables.php +++ b/app/Database/migrations/2017_06_17_214650_create_flight_tables.php @@ -24,6 +24,7 @@ class CreateFlightTables extends Migration $table->string('alt_airport_id', 5)->nullable(); $table->string('dpt_time', 10)->nullable(); $table->string('arr_time', 10)->nullable(); + $table->unsignedTinyInteger('days')->nullable(); $table->unsignedInteger('level')->nullable()->default(0); $table->unsignedDecimal('distance')->nullable()->default(0.0); $table->unsignedInteger('flight_time')->nullable(); diff --git a/app/Http/Controllers/Admin/FlightController.php b/app/Http/Controllers/Admin/FlightController.php index 052082e0..571e0e64 100644 --- a/app/Http/Controllers/Admin/FlightController.php +++ b/app/Http/Controllers/Admin/FlightController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Requests\CreateFlightRequest; use App\Http\Requests\UpdateFlightRequest; use App\Interfaces\Controller; +use App\Models\Enums\Days; use App\Models\Enums\FlightType; use App\Models\Flight; use App\Models\FlightField; @@ -143,6 +144,7 @@ class FlightController extends Controller { return view('admin.flights.create', [ 'flight' => null, + 'days' => [], 'flight_fields' => $this->flightFieldRepo->all(), 'airlines' => $this->airlineRepo->selectBoxList(), 'airports' => $this->airportRepo->selectBoxList(true, false), @@ -179,6 +181,7 @@ class FlightController extends Controller return redirect()->back()->withInput($request->all()); } + $input['days'] = Days::getDaysMask($input['days']); $input['active'] = get_truth_state($input['active']); $time = new Time($input['minutes'], $input['hours']); @@ -232,6 +235,7 @@ class FlightController extends Controller return view('admin.flights.edit', [ 'flight' => $flight, + 'days' => $flight->days, 'flight_fields' => $this->flightFieldRepo->all(), 'airlines' => $this->airlineRepo->selectBoxList(), 'airports' => $this->airportRepo->selectBoxList(), @@ -279,6 +283,8 @@ class FlightController extends Controller return redirect()->back()->withInput($request->all()); } + $input['days'] = Days::getDaysMask($input['days']); + $input['flight_time'] = Time::init( $input['minutes'], $input['hours'])->getMinutes(); diff --git a/app/Interfaces/Enum.php b/app/Interfaces/Enum.php index eaa93773..fc82547e 100644 --- a/app/Interfaces/Enum.php +++ b/app/Interfaces/Enum.php @@ -67,12 +67,7 @@ abstract class Enum */ public static function getFromCode($code) { - $code = strtoupper($code); - if(!array_key_exists($code, static::$codes)) { - return null; - } - - return static::$codes[$code]; + return array_search($code, static::$codes, true); } /** @@ -82,7 +77,12 @@ abstract class Enum */ public static function convertToCode($value) { - return array_search($value, static::$codes, true); + $value = (int) $value; + if (!array_key_exists($value, static::$codes)) { + return null; + } + + return static::$codes[$value]; } /** diff --git a/app/Models/Enums/AircraftStatus.php b/app/Models/Enums/AircraftStatus.php index 1cd3af33..16d3f8bb 100644 --- a/app/Models/Enums/AircraftStatus.php +++ b/app/Models/Enums/AircraftStatus.php @@ -25,10 +25,10 @@ class AircraftStatus extends Enum ]; public static $codes = [ - 'S' => AircraftStatus::STORED, - 'A' => AircraftStatus::ACTIVE, - 'R' => AircraftStatus::RETIRED, - 'C' => AircraftStatus::SCRAPPED, - 'W' => AircraftStatus::WRITTEN_OFF, + AircraftStatus::STORED => 'S', + AircraftStatus::ACTIVE => 'A' , + AircraftStatus::RETIRED => 'R', + AircraftStatus::SCRAPPED => 'C', + AircraftStatus::WRITTEN_OFF => 'W', ]; } diff --git a/app/Models/Enums/Days.php b/app/Models/Enums/Days.php index a38213ba..92ce6848 100644 --- a/app/Models/Enums/Days.php +++ b/app/Models/Enums/Days.php @@ -6,6 +6,7 @@ use App\Interfaces\Enum; /** * Class Days + * Start on Monday - ISO8601 * @package App\Models\Enums */ class Days extends Enum @@ -18,7 +19,7 @@ class Days extends Enum public const SATURDAY = 1 << 5; public const SUNDAY = 1 << 6; - protected static $labels = [ + public static $labels = [ Days::MONDAY => 'system.days.mon', Days::TUESDAY => 'system.days.tues', Days::WEDNESDAY => 'system.days.wed', @@ -27,4 +28,40 @@ class Days extends Enum Days::SATURDAY => 'system.days.sat', Days::SUNDAY => 'system.days.sun', ]; + + public static $codes = [ + 'M' => Days::MONDAY, + 'T' => Days::TUESDAY, + 'W' => Days::WEDNESDAY, + 'Th' => Days::THURSDAY, + 'F' => Days::FRIDAY, + 'S' => Days::SATURDAY, + 'Su' => Days::SUNDAY, + ]; + + /** + * Create the masked value for the days of week + * @param array $days + * @return int|mixed + */ + public static function getDaysMask(array $days) + { + $mask = 0; + foreach($days as $day) { + $mask |= $day; + } + + return $mask; + } + + /** + * See if the given mask has a day + * @param $mask + * @param $day + * @return bool + */ + public static function in($mask, $day): bool + { + return ($mask & $day) === $day; + } } diff --git a/app/Models/Enums/ExpenseType.php b/app/Models/Enums/ExpenseType.php index 9fdb26dc..0330fd0a 100644 --- a/app/Models/Enums/ExpenseType.php +++ b/app/Models/Enums/ExpenseType.php @@ -21,8 +21,8 @@ class ExpenseType extends Enum ]; protected static $codes = [ - 'F' => ExpenseType::FLIGHT, - 'D' => ExpenseType::DAILY, - 'M' => ExpenseType::MONTHLY, + ExpenseType::FLIGHT => 'F', + ExpenseType::DAILY =>'D', + ExpenseType::MONTHLY => 'M', ]; } diff --git a/app/Models/Enums/FlightType.php b/app/Models/Enums/FlightType.php index 96db4ad0..4dd04b57 100644 --- a/app/Models/Enums/FlightType.php +++ b/app/Models/Enums/FlightType.php @@ -21,8 +21,8 @@ class FlightType extends Enum ]; protected static $codes = [ - 'P' => FlightType::PASSENGER, - 'C' => FlightType::CARGO, - 'H' => FlightType::CHARTER, + FlightType::PASSENGER => 'P', + FlightType::CARGO => 'C', + FlightType::CHARTER => 'H', ]; } diff --git a/app/Models/Enums/FuelType.php b/app/Models/Enums/FuelType.php index 3e8cf130..5a8ac332 100644 --- a/app/Models/Enums/FuelType.php +++ b/app/Models/Enums/FuelType.php @@ -14,7 +14,7 @@ class FuelType extends Enum public const JET_A = 1; public const MOGAS = 2; - protected static $labels = [ + public static $labels = [ FuelType::LOW_LEAD => '100LL', FuelType::JET_A => 'JET A', FuelType::MOGAS => 'MOGAS', diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 2b439ef6..c3fa3018 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Interfaces\Model; +use App\Models\Enums\Days; use App\Models\Traits\HashIdTrait; use App\Support\Units\Distance; use Illuminate\Support\Collection; @@ -18,6 +19,7 @@ use PhpUnitsOfMeasure\Exception\NonStringUnitName; * @property Collection field_values * @property Collection fares * @property Collection subfleets + * @property integer days */ class Flight extends Model { @@ -40,6 +42,7 @@ class Flight extends Model 'alt_airport_id', 'dpt_time', 'arr_time', + 'days', 'level', 'distance', 'flight_time', @@ -52,6 +55,7 @@ class Flight extends Model protected $casts = [ 'flight_number' => 'integer', + 'days' => 'integer', 'level' => 'integer', 'distance' => 'float', 'flight_time' => 'integer', @@ -70,6 +74,22 @@ class Flight extends Model 'level' => 'nullable', ]; + /** + * Return all of the flights on any given day(s) of the week + * Search using bitmasks + * @param Days[] $days List of the enumerated values + * @return Flight + */ + public static function findByDays(array $days) + { + $flights = Flight::where('active', true); + foreach($days as $day) { + $flights = $flights->where('days', '&', $day); + } + + return $flights; + } + /** * Get the flight ident, e.,g JBU1900 */ @@ -125,6 +145,15 @@ class Flight extends Model } } + /** + * @param $day + * @return bool + */ + public function on_day($day): bool + { + return ($this->days & $day) === $day; + } + /** * Return a custom field value * @param $field_name diff --git a/app/Services/ImportExport/FlightExporter.php b/app/Services/ImportExport/FlightExporter.php index 68ecb18c..cb9c992c 100644 --- a/app/Services/ImportExport/FlightExporter.php +++ b/app/Services/ImportExport/FlightExporter.php @@ -3,6 +3,7 @@ namespace App\Services\ImportExport; use App\Interfaces\ImportExport; +use App\Models\Enums\Days; use App\Models\Enums\FlightType; use App\Models\Flight; @@ -39,6 +40,7 @@ class FlightExporter extends ImportExport $ret['airline'] = $ret['airline']->icao; $ret['distance'] = $ret['distance']->toNumber(); + $ret['days'] = $this->getDays($flight); $ret['flight_type'] = FlightType::convertToCode($ret['flight_type']); $ret['fares'] = $this->getFares($flight); @@ -48,6 +50,46 @@ class FlightExporter extends ImportExport return $ret; } + /** + * Return the days string + * @param Flight $flight + * @return string + */ + protected function getDays(Flight &$flight) + { + $days_str = ''; + + if($flight->on_day(Days::MONDAY)) { + $days_str .= '1'; + } + + if ($flight->on_day(Days::TUESDAY)) { + $days_str .= '2'; + } + + if ($flight->on_day(Days::WEDNESDAY)) { + $days_str .= '3'; + } + + if ($flight->on_day(Days::THURSDAY)) { + $days_str .= '4'; + } + + if ($flight->on_day(Days::FRIDAY)) { + $days_str .= '5'; + } + + if ($flight->on_day(Days::SATURDAY)) { + $days_str .= '6'; + } + + if ($flight->on_day(Days::SUNDAY)) { + $days_str .= '7'; + } + + return $days_str; + } + /** * Return any custom fares that have been made to this flight * @param Flight $flight diff --git a/app/Services/ImportExport/FlightImporter.php b/app/Services/ImportExport/FlightImporter.php index 388c34f8..d12e4dc7 100644 --- a/app/Services/ImportExport/FlightImporter.php +++ b/app/Services/ImportExport/FlightImporter.php @@ -4,6 +4,7 @@ namespace App\Services\ImportExport; use App\Interfaces\ImportExport; use App\Models\Airport; +use App\Models\Enums\Days; use App\Models\Enums\FlightType; use App\Models\Fare; use App\Models\Flight; @@ -83,6 +84,7 @@ class FlightImporter extends ImportExport ], $row); // Airport atttributes + $flight->setAttribute('days', $this->setDays($row['days'])); $flight->setAttribute('dpt_airport_id', $row['dpt_airport']); $flight->setAttribute('arr_airport_id', $row['arr_airport']); if ($row['alt_airport']) { @@ -116,6 +118,49 @@ class FlightImporter extends ImportExport return true; } + /** + * Return the mask of the days + * @param $day_str + * @return int|mixed + */ + protected function setDays($day_str) + { + if(!$day_str) { + return 0; + } + + $days = []; + if(strpos($day_str, '1') !== false) { + $days[] = Days::MONDAY; + } + + if (strpos($day_str, '2') !== false) { + $days[] = Days::TUESDAY; + } + + if (strpos($day_str, '3') !== false) { + $days[] = Days::WEDNESDAY; + } + + if (strpos($day_str, '4') !== false) { + $days[] = Days::THURSDAY; + } + + if (strpos($day_str, '5') !== false) { + $days[] = Days::FRIDAY; + } + + if (strpos($day_str, '6') !== false) { + $days[] = Days::SATURDAY; + } + + if (strpos($day_str, '7') !== false) { + $days[] = Days::SUNDAY; + } + + return Days::getDaysMask($days); + } + /** * Process the airport * @param $airport diff --git a/app/helpers.php b/app/helpers.php index 3c51f1d2..c32b029b 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -1,5 +1,18 @@ +