diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index cbf1ab25..34f75ab7 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -13,7 +13,11 @@ use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** - * Define the application's command schedule. + * Define the application's command schedule. How this works... according to the command + * time, an event gets send out with the appropriate time (e.g, hourly sends an hourly event) + * + * Then the CronServiceProvider has the list of cronjobs which then run according to the events + * and then calls those at the proper times. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @@ -31,6 +35,7 @@ class Kernel extends ConsoleKernel $schedule->command('backup:run')->daily()->at('02:00'); // Update the last time the cron was run + /** @var CronService $cronSvc */ $cronSvc = app(CronService::class); $cronSvc->updateLastRunTime(); } diff --git a/app/Cron/Hourly/DeletePireps.php b/app/Cron/Hourly/DeletePireps.php new file mode 100644 index 00000000..0ac449ff --- /dev/null +++ b/app/Cron/Hourly/DeletePireps.php @@ -0,0 +1,52 @@ +deletePireps(setting('pireps.delete_rejected_hours'), PirepState::REJECTED); + $this->deletePireps(setting('pireps.delete_cancelled_hours'), PirepState::CANCELLED); + } + + /** + * Look for and delete PIREPs which match the criteria + * + * @param int $expire_time_hours The time in hours to look for PIREPs + * @param int $state The PirepState enum value + */ + protected function deletePireps(int $expire_time_hours, int $state) + { + $dt = Carbon::now()->subHours($expire_time_hours); + $pireps = Pirep::whereDate('created_at', '<', $dt)->where(['state' => $state])->get(); + + /** @var PirepService $pirepSvc */ + $pirepSvc = app(PirepService::class); + + /** @var Pirep $pirep */ + foreach ($pireps as $pirep) { + Log::info('Cron: Deleting PIREP id='.$pirep->id.', state='.PirepState::label($state)); + $pirepSvc->delete($pirep); + } + } +} diff --git a/app/Cron/Monthly/ApplyExpenses.php b/app/Cron/Monthly/ApplyExpenses.php index ce0b39c3..d8ccbc4b 100644 --- a/app/Cron/Monthly/ApplyExpenses.php +++ b/app/Cron/Monthly/ApplyExpenses.php @@ -6,6 +6,7 @@ use App\Contracts\Listener; use App\Events\CronMonthly; use App\Models\Enums\ExpenseType; use App\Services\Finance\RecurringFinanceService; +use Illuminate\Support\Facades\Log; /** * Go through and apply any finances that are daily @@ -35,6 +36,7 @@ class ApplyExpenses extends Listener */ public function handle(CronMonthly $event): void { + Log::info('Monthly: Applying monthly expenses'); $this->financeSvc->processExpenses(ExpenseType::MONTHLY); } } diff --git a/app/Cron/Nightly/ApplyExpenses.php b/app/Cron/Nightly/ApplyExpenses.php index 244e61a5..7b371eca 100644 --- a/app/Cron/Nightly/ApplyExpenses.php +++ b/app/Cron/Nightly/ApplyExpenses.php @@ -6,6 +6,7 @@ use App\Contracts\Listener; use App\Events\CronNightly; use App\Models\Enums\ExpenseType; use App\Services\Finance\RecurringFinanceService; +use Illuminate\Support\Facades\Log; /** * Go through and apply any finances that are daily @@ -35,6 +36,7 @@ class ApplyExpenses extends Listener */ public function handle(CronNightly $event): void { + Log::info('Nightly: Applying daily expenses'); $this->financeSvc->processExpenses(ExpenseType::DAILY); } } diff --git a/app/Cron/Nightly/ClearExpiredSimbrief.php b/app/Cron/Nightly/ClearExpiredSimbrief.php index 56ae8156..37925fe6 100644 --- a/app/Cron/Nightly/ClearExpiredSimbrief.php +++ b/app/Cron/Nightly/ClearExpiredSimbrief.php @@ -5,6 +5,7 @@ namespace App\Cron\Nightly; use App\Contracts\Listener; use App\Events\CronNightly; use App\Services\SimBriefService; +use Illuminate\Support\Facades\Log; /** * Clear any expired SimBrief flight briefs that aren't attached to a PIREP @@ -23,6 +24,7 @@ class ClearExpiredSimbrief extends Listener */ public function handle(CronNightly $event): void { + Log::info('Nightly: Removing expired Simbrief entries'); $this->simbriefSvc->removeExpiredEntries(); } } diff --git a/app/Cron/Nightly/NewVersionCheck.php b/app/Cron/Nightly/NewVersionCheck.php index fb0b205f..dba49923 100644 --- a/app/Cron/Nightly/NewVersionCheck.php +++ b/app/Cron/Nightly/NewVersionCheck.php @@ -5,6 +5,7 @@ namespace App\Cron\Nightly; use App\Contracts\Listener; use App\Events\CronNightly; use App\Services\VersionService; +use Illuminate\Support\Facades\Log; /** * Determine if any pilots should be set to ON LEAVE status @@ -28,6 +29,7 @@ class NewVersionCheck extends Listener */ public function handle(CronNightly $event): void { + Log::info('Nightly: Checking for new version'); $this->versionSvc->isNewVersionAvailable(); } } diff --git a/app/Cron/Nightly/PilotLeave.php b/app/Cron/Nightly/PilotLeave.php index f49606c1..32882fe0 100644 --- a/app/Cron/Nightly/PilotLeave.php +++ b/app/Cron/Nightly/PilotLeave.php @@ -34,7 +34,10 @@ class PilotLeave extends Listener */ public function handle(CronNightly $event): void { + Log::info('Cron: Running pilot leave check'); $users = $this->userSvc->findUsersOnLeave(); + Log::info('Found '.count($users).' users on leave'); + foreach ($users as $user) { Log::info('Setting user '.$user->ident.' to ON LEAVE status'); $this->userSvc->setStatusOnLeave($user); diff --git a/app/Cron/Nightly/RecalculateBalances.php b/app/Cron/Nightly/RecalculateBalances.php index 3161453a..76bb99d5 100644 --- a/app/Cron/Nightly/RecalculateBalances.php +++ b/app/Cron/Nightly/RecalculateBalances.php @@ -35,7 +35,7 @@ class RecalculateBalances extends Listener */ public function handle(CronNightly $event): void { - Log::info('Recalculating balances'); + Log::info('Nightly: Recalculating balances'); $journals = Journal::all(); foreach ($journals as $journal) { diff --git a/app/Cron/Nightly/RecalculateStats.php b/app/Cron/Nightly/RecalculateStats.php index e4eb2320..1fd1fd2b 100644 --- a/app/Cron/Nightly/RecalculateStats.php +++ b/app/Cron/Nightly/RecalculateStats.php @@ -32,10 +32,10 @@ class RecalculateStats extends Listener */ public function handle(CronNightly $event): void { - Log::info('Recalculating user stats'); + Log::info('Nightly: Recalculating user stats'); $this->userSvc->recalculateAllUserStats(); - Log::info('Recalcuating aircraft status'); + Log::info('Nightly: Recalcuating aircraft status'); $this->aircraftSvc->recalculateStats(); } } diff --git a/app/Cron/Nightly/SetActiveFlights.php b/app/Cron/Nightly/SetActiveFlights.php index 9915d38c..9b6b10bb 100644 --- a/app/Cron/Nightly/SetActiveFlights.php +++ b/app/Cron/Nightly/SetActiveFlights.php @@ -19,6 +19,8 @@ class SetActiveFlights extends Listener */ public function handle(CronNightly $event): void { + Log::info('Nightly: Setting active flights'); + $this->checkFlights(); } diff --git a/app/Database/seeds/settings.yml b/app/Database/seeds/settings.yml index 080e88c3..4ac3adbd 100644 --- a/app/Database/seeds/settings.yml +++ b/app/Database/seeds/settings.yml @@ -180,12 +180,12 @@ type: number description: 'How much the load factor can vary per-flight' - key: simbrief.api_key - name: 'SimBrief API Key' + name: 'Simbrief API Key' group: simbrief value: '' options: '' type: string - description: 'Your SimBrief API key' + description: 'Your Simbrief API key' - key: simbrief.only_bids name: 'Only allow for bids' group: simbrief @@ -194,47 +194,47 @@ type: boolean description: 'Only allow briefs to be created for bidded flights' - key: simbrief.expire_days - name: 'SimBrief Expire Time' + name: 'Simbrief Expire Time' group: simbrief value: 5 options: '' type: number description: 'Days after how long to remove unused briefs' - key: simbrief.noncharter_pax_weight - name: 'SimBrief Passenger Weight for Non-Charter (Scheduled etc) Flights' + name: 'Non-Charter Passenger Weight' group: simbrief value: 185 options: '' type: number - description: 'Passenger weight for Non-Charter flights excluding baggage (lbs)' + description: 'Passenger weight for non-charter flights excluding baggage (lbs)' - key: simbrief.noncharter_baggage_weight - name: 'SimBrief Baggage Weight per Pax for Non-Charter (Scheduled etc) Flights' + name: 'Non-Charter Baggage Weight' group: simbrief value: 35 options: '' type: number - description: 'Passenger baggage weight for Non-Charter flights (lbs)' + description: 'Passenger baggage weight for non-charter flights (lbs)' - key: simbrief.charter_pax_weight - name: 'SimBrief Passenger Weight for Charter Flights' + name: 'Charter Passenger Weight' group: simbrief value: 168 options: '' type: number - description: 'Passenger weight for Charter flights excluding baggage (lbs)' + description: 'Passenger weight for charter flights excluding baggage (lbs)' - key: simbrief.charter_baggage_weight - name: 'SimBrief Baggage Weight per Pax for Charter Flights' + name: 'Charter Baggage Weight' group: simbrief value: 28 options: '' type: number - description: 'Passenger baggage weight for Charter flights (lbs)' + description: 'Passenger baggage weight for charter flights (lbs)' - key: simbrief.callsign - name: 'SimBrief ATC Callsign' + name: 'Use ATC Callsign' group: simbrief value: false options: '' type: boolean - description: 'Use pilot ident as SimBrief ATC Callsign' + description: 'Use pilot ident as Simbrief ATC Callsign' - key: pireps.duplicate_check_time name: 'PIREP duplicate time check' group: pireps @@ -270,6 +270,20 @@ options: '' type: boolean description: 'Enables remaining fuel amounts to be considered for fuel expenses' +- key: pireps.delete_cancelled_hours + name: 'Delete cancelled PIREPs' + group: pireps + value: 12 + options: '' + type: int + description: 'The time in hours to delete a cancelled PIREP' +- key: pireps.delete_rejected_hours + name: 'Delete rejected PIREPs' + group: pireps + value: 12 + options: '' + type: int + description: 'The time in hours to delete a rejected PIREP' - key: pilots.id_length name: 'Pilot ID Length' group: pilots @@ -347,4 +361,3 @@ options: '' type: 'text' description: 'Discord public channel ID for broadcasat notifications' - diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index 20f19b40..b5cd5347 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -394,10 +394,10 @@ class PirepController extends Controller return redirect(route('admin.pireps.index')); } - $this->pirepRepo->delete($id); + $this->pirepSvc->delete($pirep); Flash::success('Pirep deleted successfully.'); - return redirect(route('admin.pireps.index')); + return redirect()->back(); } /** diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index 41f22e9c..9d6609f8 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -7,7 +7,6 @@ use App\Http\Requests\CreatePirepRequest; use App\Http\Requests\UpdatePirepRequest; use App\Models\Enums\PirepSource; use App\Models\Enums\PirepState; -use App\Models\Enums\PirepStatus; use App\Models\Fare; use App\Models\Pirep; use App\Models\SimBrief; @@ -519,12 +518,8 @@ class PirepController extends Controller $this->pirepSvc->submit($pirep); Flash::success('PIREP submitted!'); } elseif ($attrs['submit'] === 'delete' || $attrs['submit'] === 'cancel') { - $this->pirepRepo->update([ - 'state' => PirepState::CANCELLED, - 'status' => PirepStatus::CANCELLED, - ], $pirep->id); - - Flash::success('PIREP cancelled!'); + $this->pirepSvc->delete($pirep); + Flash::success('PIREP deleted!'); return redirect(route('frontend.pireps.index')); } diff --git a/app/Providers/CronServiceProvider.php b/app/Providers/CronServiceProvider.php index 0dc2f872..af3934ce 100644 --- a/app/Providers/CronServiceProvider.php +++ b/app/Providers/CronServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Cron\Hourly\DeletePireps; use App\Cron\Hourly\RemoveExpiredBids; use App\Cron\Hourly\RemoveExpiredLiveFlights; use App\Cron\Nightly\ApplyExpenses; @@ -41,6 +42,7 @@ class CronServiceProvider extends ServiceProvider ], CronHourly::class => [ + DeletePireps::class, RemoveExpiredBids::class, RemoveExpiredLiveFlights::class, ], diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 8ec68c21..86043c85 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -23,6 +23,8 @@ use App\Models\Enums\PirepState; use App\Models\Enums\PirepStatus; use App\Models\Navdata; use App\Models\Pirep; +use App\Models\PirepComment; +use App\Models\PirepFare; use App\Models\PirepFieldValue; use App\Models\SimBrief; use App\Models\User; @@ -436,6 +438,29 @@ class PirepService extends Service return $pirep; } + /** + * Delete the PIREP and all of the associated data. Does a force delete to make sure that we + * don't run into problems with foreign keys. Models/tables affected: + * + * acars + * bids + * pirep_comments + * pirep_fares + * pirep_field_values + * simbrief + * + * @param Pirep $pirep + */ + public function delete(Pirep $pirep): void + { + $w = ['pirep_id' => $pirep->id]; + PirepComment::where($w)->forceDelete(); + PirepFare::where($w)->forceDelete(); + PirepFieldValue::where($w)->forceDelete(); + SimBrief::where($w)->forceDelete(); + $pirep->forceDelete(); + } + /** * Update any custom PIREP fields * diff --git a/resources/views/admin/pireps/actions.blade.php b/resources/views/admin/pireps/actions.blade.php index 5c4a3b8a..bdee0700 100644 --- a/resources/views/admin/pireps/actions.blade.php +++ b/resources/views/admin/pireps/actions.blade.php @@ -23,11 +23,19 @@ 'pirep_id' => $pirep->id, 'new_status' => PirepState::REJECTED, 'class' => $on_edit_page ? 'pirep_change_status': 'pirep_submit_status']) }} - {{ Form::button('Reject', ['type' => 'submit', 'class' => 'btn btn-danger']) }} + {{ Form::button('Reject', ['type' => 'submit', 'class' => 'btn btn-warning']) }} {{ Form::close() }} @endif -