From 17447c6903f9cfc1fd5bc501cb81ca5fdeead197 Mon Sep 17 00:00:00 2001 From: "B.Fatih KOZ" Date: Thu, 3 Jun 2021 23:17:16 +0300 Subject: [PATCH] Block aircraft with Simbrief (#1213) * Block Aircraft with SimBrief Changes aim to have the ability to block an aircraft's usage if it is used to generate a SimBrief OFP. Unused/Expired briefings will be deleted by cron like before but will now be checked by HourlyCron, so admins can define more precise restrictions for them (and the blockage period of their aircraft) Owner of the SimBrief OFP will be able to start a flight with acars using that particular aircraft, but pilots will get an Aircraft Not Available error (similar to Aircraft State check) To prevent SimBrief OFP packs being marked as expired/unused, during pirep prefile, pirep_id will be saved to SimBrief model along with flight_id. And when a flight is finished (pirep file), flight_id will be removed from SimBrief model as before. Only pirep_id will remain and aircraft will be available for another OFP generation. * Update PirepController In case a pirep is being saved/submitted with manual entry (but the va is using simbrief effectively) same logic should be applied during save/submit button selection. Save will act like a pirep prefile , Submit will be pirep file. * Manual Pirep Checks Manual pireps, prefiled from a generated simbrief should be checked too. Also pirep.show blade's submit button should provide the same simbrief checks. * Update PirepService.php * Change settings and move sb cron to hourly * StyleFix (SimBriefService) * Another StyleFix (SimBriefService) * Update SimBriefController Removed null check of pirep_id for aircraft list generation to prevent live flights' aircraft being listed for another ofp generation. ( Active acars flights will have both flight_id and pirep_id at simbrief table) * Update PirepService.php Co-authored-by: Nabeel S --- .../ClearExpiredSimbrief.php | 8 ++-- ...5608_remove_setting_simbriefexpiredays.php | 19 +++++++++ app/Database/seeds/settings.yml | 13 ++++-- .../Controllers/Frontend/PirepController.php | 8 +++- .../Frontend/SimBriefController.php | 7 ++++ app/Providers/CronServiceProvider.php | 4 +- app/Services/PirepService.php | 40 ++++++++++++++++++- app/Services/SimBriefService.php | 22 +++++----- 8 files changed, 99 insertions(+), 22 deletions(-) rename app/Cron/{Nightly => Hourly}/ClearExpiredSimbrief.php (74%) create mode 100644 app/Database/migrations/2021_05_28_165608_remove_setting_simbriefexpiredays.php diff --git a/app/Cron/Nightly/ClearExpiredSimbrief.php b/app/Cron/Hourly/ClearExpiredSimbrief.php similarity index 74% rename from app/Cron/Nightly/ClearExpiredSimbrief.php rename to app/Cron/Hourly/ClearExpiredSimbrief.php index 37925fe6..8a770bb6 100644 --- a/app/Cron/Nightly/ClearExpiredSimbrief.php +++ b/app/Cron/Hourly/ClearExpiredSimbrief.php @@ -1,9 +1,9 @@ simbriefSvc->removeExpiredEntries(); } } diff --git a/app/Database/migrations/2021_05_28_165608_remove_setting_simbriefexpiredays.php b/app/Database/migrations/2021_05_28_165608_remove_setting_simbriefexpiredays.php new file mode 100644 index 00000000..f8106998 --- /dev/null +++ b/app/Database/migrations/2021_05_28_165608_remove_setting_simbriefexpiredays.php @@ -0,0 +1,19 @@ +where(['key' => 'simbrief.expire_days']) + ->delete(); + } +} diff --git a/app/Database/seeds/settings.yml b/app/Database/seeds/settings.yml index e353a517..b2478394 100644 --- a/app/Database/seeds/settings.yml +++ b/app/Database/seeds/settings.yml @@ -207,13 +207,13 @@ options: '' type: boolean description: 'Only allow briefs to be created for bidded flights' -- key: simbrief.expire_days +- key: simbrief.expire_hours name: 'Simbrief Expire Time' group: simbrief - value: 5 + value: 6 options: '' type: number - description: 'Days after how long to remove unused briefs' + description: 'Hours after how long to remove unused briefs' - key: simbrief.noncharter_pax_weight name: 'Non-Charter Passenger Weight' group: simbrief @@ -256,6 +256,13 @@ options: '' type: boolean description: 'Use privatized user name as SimBrief OFP captain name' +- key: simbrief.block_aircraft + name: 'Restrict Aircraft' + group: simbrief + value: false + options: '' + type: boolean + description: 'When enabled, an aircraft can only be used for one active SimBrief OFP and Flight/Pirep' - key: pireps.duplicate_check_time name: 'PIREP duplicate time check' group: pireps diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index 3759ac05..00697cef 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -399,7 +399,13 @@ class PirepController extends Controller if ($brief !== null) { /** @var SimBriefService $sbSvc */ $sbSvc = app(SimBriefService::class); - $sbSvc->attachSimbriefToPirep($pirep, $brief); + // Keep the flight_id with SimBrief depending on the button selected + // Save = Keep the flight_id , Submit = Remove the flight_id + if ($attrs['submit'] === 'save') { + $sbSvc->attachSimbriefToPirep($pirep, $brief, true); + } elseif ($attrs['submit'] === 'submit') { + $sbSvc->attachSimbriefToPirep($pirep, $brief); + } } } diff --git a/app/Http/Controllers/Frontend/SimBriefController.php b/app/Http/Controllers/Frontend/SimBriefController.php index 32497d1d..f91629b6 100644 --- a/app/Http/Controllers/Frontend/SimBriefController.php +++ b/app/Http/Controllers/Frontend/SimBriefController.php @@ -100,6 +100,13 @@ class SimBriefController $aircrafts = $aircrafts->where('airport_id', $flight->dpt_airport_id); } + if (setting('simbrief.block_aircraft')) { + // Build a list of aircraft_id's being used for active sb packs + $sb_aircraft = SimBrief::whereNotNull('flight_id')->pluck('aircraft_id'); + // Filter aircraft list to non used/blocked ones + $aircrafts = $aircrafts->whereNotIn('id', $sb_aircraft); + } + return view('flights.simbrief_aircraft', [ 'flight' => $flight, 'aircrafts' => $aircrafts, diff --git a/app/Providers/CronServiceProvider.php b/app/Providers/CronServiceProvider.php index af3934ce..0d97f546 100644 --- a/app/Providers/CronServiceProvider.php +++ b/app/Providers/CronServiceProvider.php @@ -2,11 +2,11 @@ namespace App\Providers; +use App\Cron\Hourly\ClearExpiredSimbrief; use App\Cron\Hourly\DeletePireps; use App\Cron\Hourly\RemoveExpiredBids; use App\Cron\Hourly\RemoveExpiredLiveFlights; use App\Cron\Nightly\ApplyExpenses; -use App\Cron\Nightly\ClearExpiredSimbrief; use App\Cron\Nightly\NewVersionCheck; use App\Cron\Nightly\PilotLeave; use App\Cron\Nightly\RecalculateBalances; @@ -31,7 +31,6 @@ class CronServiceProvider extends ServiceProvider SetActiveFlights::class, RecalculateStats::class, NewVersionCheck::class, - ClearExpiredSimbrief::class, ], CronWeekly::class => [ @@ -45,6 +44,7 @@ class CronServiceProvider extends ServiceProvider DeletePireps::class, RemoveExpiredBids::class, RemoveExpiredLiveFlights::class, + ClearExpiredSimbrief::class, ], ]; } diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index e43b680e..1696645c 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -148,6 +148,18 @@ class PirepService extends Service throw new AircraftNotAvailable($pirep->aircraft); } + // See if this aircraft is being used by another user's active simbrief ofp + if (setting('simbrief.block_aircraft', false)) { + $sb_aircraft = SimBrief::select('aircraft_id') + ->where('aircraft_id', $pirep->aircraft_id) + ->where('user_id', '!=', $pirep->user_id) + ->whereNotNull('flight_id') + ->count(); + if ($sb_aircraft > 0) { + throw new AircraftNotAvailable($pirep->aircraft); + } + } + // See if this aircraft is at the departure airport /* @noinspection NotOptimalIfConditionsInspection */ if (setting('pireps.only_aircraft_at_dpt_airport') && $aircraft->airport_id !== $pirep->dpt_airport_id) { @@ -164,9 +176,20 @@ class PirepService extends Service } } - event(new PirepPrefiled($pirep)); - $pirep->save(); + $pirep->refresh(); + + // Check if there is a simbrief_id, update it to have the pirep_id + // Keep the flight_id until the end of flight (pirep file) + if (array_key_exists('simbrief_id', $attrs)) { + /** @var SimBrief $simbrief */ + $simbrief = SimBrief::find($attrs['simbrief_id']); + if ($simbrief) { + $this->simBriefSvc->attachSimbriefToPirep($pirep, $simbrief, true); + } + } + + event(new PirepPrefiled($pirep)); return $pirep; } @@ -427,6 +450,19 @@ class PirepService extends Service } } + // Check if there is a simbrief_id, change it to be set to the PIREP + // at the end of the flight when it's been submitted finally. + // Prefile, Save (as draft) and File already have this but the Submit button + // visible at pireps.show blade uses this function so Simbrief also needs to + // checked here too (to remove the flight_id and release the aircraft) + if (!empty($pirep->simbrief)) { + /** @var SimBrief $simbrief */ + $simbrief = SimBrief::find($pirep->simbrief->id); + if ($simbrief) { + $this->simBriefSvc->attachSimbriefToPirep($pirep, $simbrief); + } + } + Log::info('New PIREP filed', [$pirep]); event(new PirepFiled($pirep)); diff --git a/app/Services/SimBriefService.php b/app/Services/SimBriefService.php index f09880c5..3ccc2a87 100644 --- a/app/Services/SimBriefService.php +++ b/app/Services/SimBriefService.php @@ -132,21 +132,23 @@ class SimBriefService extends Service * * 1. Read from the XML the basic PIREP info (dep, arr), and then associate the PIREP * to the flight ID - * 2. Remove the flight ID from the SimBrief field and assign the pirep_id to the row + * 2. Remove the flight ID from the SimBrief model and assign the pirep ID to the row + * at the end of the flight. Keep flight ID until the flight ends (pirep file). * 3. Update the planned flight route in the acars table * 4. Add additional flight fields (ones which match ACARS) * * @param $pirep - * @param SimBrief $simBrief The briefing to create the PIREP from + * @param SimBrief $simBrief The briefing to create the PIREP from + * @param bool $keep_flight True keeps the flight_id, default is false * * @return \App\Models\Pirep */ - public function attachSimbriefToPirep($pirep, SimBrief $simBrief): Pirep + public function attachSimbriefToPirep($pirep, SimBrief $simBrief, $keep_flight = false): Pirep { $this->addRouteToPirep($pirep, $simBrief); $simBrief->pirep_id = $pirep->id; - $simBrief->flight_id = null; + $simBrief->flight_id = !empty($keep_flight) ? $pirep->flight_id : null; $simBrief->save(); return $pirep; @@ -185,14 +187,14 @@ class SimBriefService extends Service } /** - * Remove any expired entries from the SimBrief table. Expired means there's - * a flight_id attached to it, but no pirep_id (meaning it was never used for - * an actual flight) + * Remove any expired entries from the SimBrief table. + * Expired means there's a flight_id attached to it, but no pirep_id + * (meaning it was never used for an actual flight) */ public function removeExpiredEntries(): void { - $expire_days = setting('simbrief.expire_days', 5); - $expire_time = Carbon::now('UTC')->subDays($expire_days); + $expire_hours = setting('simbrief.expire_hours', 6); + $expire_time = Carbon::now('UTC')->subHours($expire_hours); $briefs = SimBrief::where([ ['pirep_id', null], @@ -202,7 +204,7 @@ class SimBriefService extends Service foreach ($briefs as $brief) { $brief->delete(); - // TODO: Delete any assets + // TODO: Delete any assets (Which assets ?) } } }