* Fix pilot leave calculation #1022 * Remove unused imports * Add logging where fares are being saved
This commit is contained in:
@@ -4,10 +4,8 @@ namespace App\Cron\Nightly;
|
||||
|
||||
use App\Contracts\Listener;
|
||||
use App\Events\CronNightly;
|
||||
use App\Models\Enums\UserState;
|
||||
use App\Models\User;
|
||||
use App\Services\UserService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Determine if any pilots should be set to ON LEAVE status
|
||||
@@ -18,6 +16,8 @@ class PilotLeave extends Listener
|
||||
|
||||
/**
|
||||
* PilotLeave constructor.
|
||||
*
|
||||
* @param UserService $userSvc
|
||||
*/
|
||||
public function __construct(UserService $userSvc)
|
||||
{
|
||||
@@ -34,14 +34,7 @@ class PilotLeave extends Listener
|
||||
*/
|
||||
public function handle(CronNightly $event): void
|
||||
{
|
||||
if (setting('pilots.auto_leave_days') === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$date = Carbon::now()->subDay(setting('pilots.auto_leave_days'));
|
||||
$users = User::where('status', UserState::ACTIVE)
|
||||
->whereDate('updated_at', '<', $date);
|
||||
|
||||
$users = $this->userSvc->findUsersOnLeave();
|
||||
foreach ($users as $user) {
|
||||
Log::info('Setting user '.$user->ident.' to ON LEAVE status');
|
||||
$this->userSvc->setStatusOnLeave($user);
|
||||
|
||||
@@ -109,6 +109,7 @@ class SimBriefController
|
||||
|
||||
$loadmax = $lfactor + $lfactorv;
|
||||
$loadmax = $loadmax > 100 ? 100 : $loadmax;
|
||||
|
||||
// Failsafe for admins not defining load values for their flights
|
||||
// and also leave the general settings empty, set loadmax to 100
|
||||
if ($loadmax === 0) {
|
||||
|
||||
@@ -4,6 +4,14 @@ namespace App\Models;
|
||||
|
||||
use App\Contracts\Model;
|
||||
|
||||
/**
|
||||
* @property int id
|
||||
* @property string pirep_id
|
||||
* @property int fare_id
|
||||
* @property int count
|
||||
* @property Pirep pirep
|
||||
* @property Fare fare
|
||||
*/
|
||||
class PirepFare extends Model
|
||||
{
|
||||
public $table = 'pirep_fares';
|
||||
|
||||
@@ -286,9 +286,7 @@ class FareService extends Service
|
||||
*/
|
||||
public function getForPirep(Pirep $pirep)
|
||||
{
|
||||
$found_fares = PirepFare::where('pirep_id', $pirep->id)->get();
|
||||
|
||||
return $found_fares;
|
||||
return PirepFare::where('pirep_id', $pirep->id)->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,6 +311,8 @@ class FareService extends Service
|
||||
$fare['pirep_id'] = $pirep->id;
|
||||
// other fields: ['fare_id', 'count']
|
||||
|
||||
Log::info('Saving fare pirep='.$pirep->id.', fare='.$fare['count']);
|
||||
|
||||
$field = new PirepFare($fare);
|
||||
$field->save();
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ use App\Repositories\SubfleetRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
use App\Support\Units\Time;
|
||||
use App\Support\Utils;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use function is_array;
|
||||
@@ -254,6 +255,41 @@ class UserService extends Service
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the users that are determined to be on leave. Only goes through the
|
||||
* currently active users. If the user doesn't have a PIREP, then the creation date
|
||||
* of the user record is used to determine the difference
|
||||
*/
|
||||
public function findUsersOnLeave(): array
|
||||
{
|
||||
$leave_days = setting('pilots.auto_leave_days');
|
||||
if ($leave_days === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$return_users = [];
|
||||
|
||||
$date = Carbon::now('UTC');
|
||||
$users = User::with(['last_pirep'])->where('status', UserState::ACTIVE)->get();
|
||||
|
||||
/** @var User $user */
|
||||
foreach ($users as $user) {
|
||||
// If they haven't submitted a PIREP, use the date that the user was created
|
||||
if (!$user->last_pirep) {
|
||||
$diff_date = $user->created_at;
|
||||
} else {
|
||||
$diff_date = $user->last_pirep->submitted_at;
|
||||
}
|
||||
|
||||
// See if the difference is larger than what the setting calls for
|
||||
if ($date->diffInDays($diff_date) > $leave_days) {
|
||||
$return_users[] = $user;
|
||||
}
|
||||
}
|
||||
|
||||
return $return_users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the subfleets this user is allowed access to,
|
||||
* based on their current rank
|
||||
|
||||
Reference in New Issue
Block a user