2019-05-05 19:32:52 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
2024-04-26 13:26:00 -07:00
|
|
|
use App\Models\Recipients\AlertRecipient;
|
2019-05-05 19:32:52 -07:00
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Notifications\SendUpcomingAuditNotification;
|
|
|
|
use Carbon\Carbon;
|
2021-06-10 13:15:52 -07:00
|
|
|
use DB;
|
|
|
|
use Illuminate\Console\Command;
|
2019-05-05 19:32:52 -07:00
|
|
|
|
|
|
|
class SendUpcomingAuditReport extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'snipeit:upcoming-audits';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Send email/slack notifications for upcoming asset audits.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-05-06 16:44:14 -07:00
|
|
|
$settings = Setting::getSettings();
|
2024-04-26 13:26:00 -07:00
|
|
|
$interval = $settings->audit_warning_days ?? 0;
|
|
|
|
$today = Carbon::now();
|
|
|
|
$interval_date = $today->copy()->addDays($interval);
|
|
|
|
|
2019-05-05 19:32:52 -07:00
|
|
|
$settings = Setting::getSettings();
|
2024-04-26 13:26:00 -07:00
|
|
|
$assets = Asset::whereNull('deleted_at')->DueOrOverdueForAudit($settings)->orderBy('assets.next_audit_date', 'desc')->get();
|
|
|
|
$this->info($assets->count().' assets must be audited in on or before '.$interval_date.' is deadline');
|
2019-05-05 19:32:52 -07:00
|
|
|
|
|
|
|
|
2024-04-26 13:26:00 -07:00
|
|
|
if (($assets) && ($assets->count() > 0) && ($settings->alert_email != '')) {
|
2019-05-05 19:32:52 -07:00
|
|
|
// Send a rollup to the admin, if settings dictate
|
2024-04-26 13:26:00 -07:00
|
|
|
$recipients = collect(explode(',', $settings->alert_email))->map(function ($item) {
|
|
|
|
return new AlertRecipient($item);
|
2019-05-05 19:32:52 -07:00
|
|
|
});
|
|
|
|
|
2024-04-26 13:26:00 -07:00
|
|
|
$this->info('Sending Admin SendUpcomingAuditNotification to: '.$settings->alert_email);
|
|
|
|
\Notification::send($recipients, new SendUpcomingAuditNotification($assets, $settings->audit_warning_days));
|
2019-05-05 19:32:52 -07:00
|
|
|
|
|
|
|
}
|
2024-04-26 13:26:00 -07:00
|
|
|
|
2019-05-05 19:32:52 -07:00
|
|
|
}
|
|
|
|
}
|