diff --git a/app/Console/Commands/SendExpirationAlerts.php b/app/Console/Commands/SendExpirationAlerts.php index 7a97d39375a9..29c642ba5087 100644 --- a/app/Console/Commands/SendExpirationAlerts.php +++ b/app/Console/Commands/SendExpirationAlerts.php @@ -14,11 +14,11 @@ class SendExpirationAlerts extends Command { /** - * The console command name. - * + * The name and signature of the console command. + * * @var string */ - protected $name = 'snipeit:expiring-alerts'; + protected $signature = 'snipeit:expiring-alerts {--expired-licenses}'; /** * The console command description. @@ -85,7 +85,7 @@ public function handle() } // Expiring licenses - $licenses = License::query()->ExpiringLicenses($alert_interval) + $licenses = License::query()->ExpiringLicenses($alert_interval, $this->option('expired-licenses')) ->with('manufacturer','category') ->orderBy('expiration_date', 'ASC') ->orderBy('termination_date', 'ASC') diff --git a/app/Models/License.php b/app/Models/License.php index e241d5fd84d9..3f6bcf1c1346 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -784,21 +784,24 @@ public function scopeExpiredLicenses($query) * @return \Illuminate\Database\Eloquent\Relations\Relation * @see \App\Console\Commands\SendExpiringLicenseNotifications */ - public function scopeExpiringLicenses($query, $days = 60) + public function scopeExpiringLicenses($query, $days = 60, $includeExpired = false) { return $query// The termination date is null or within range ->where(function ($query) use ($days) { $query->whereNull('termination_date') ->orWhereBetween('termination_date', [Carbon::now(), Carbon::now()->addDays($days)]); }) - ->where(function ($query) use ($days) { + ->where(function ($query) use ($days, $includeExpired) { $query->whereNotNull('expiration_date') // Handle expiring licenses without termination dates - ->where(function ($query) use ($days) { + ->where(function ($query) use ($days, $includeExpired) { $query->whereNull('termination_date') - ->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addDays($days)]); + ->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addDays($days)]) + //include expired licenses if requested + ->when($includeExpired, function ($query) use ($days) { + $query->orwhereDate('expiration_date', '<=', Carbon::now()); + }); }) - // Handle expiring licenses with termination dates in the future ->orWhere(function ($query) use ($days) { $query->whereBetween('termination_date', [Carbon::now(), Carbon::now()->addDays($days)]);