diff --git a/app/Console/Commands/SendUpcomingAuditReport.php b/app/Console/Commands/SendUpcomingAuditReport.php new file mode 100644 index 000000000..47286b5ee --- /dev/null +++ b/app/Console/Commands/SendUpcomingAuditReport.php @@ -0,0 +1,93 @@ +audit_warning_days from today + $threshold = Carbon::now()->addDays($settings->audit_warning_days); + + + + if (($settings->alert_email != '') && ($settings->audit_warning_days) && ($settings->alerts_enabled == 1)) { + + // Send a rollup to the admin, if settings dictate + $recipients = collect(explode(',', $settings->alert_email))->map(function ($item, $key) { + return new \App\Models\Recipients\AlertRecipient($item); + }); + + // Assets due for auditing + $assets = Asset::whereDate('next_audit_date', '<=', $threshold) + ->orderBy('last_audit_date', 'asc')->get(); + + if ($assets->count() > 0) { + + $this->info(trans_choice('mail.upcoming-audits', $assets->count(), + ['count' => $assets->count(), 'threshold' => $threshold])); + \Notification::send($recipients, new SendUpcomingAuditNotification($assets, $threshold)); + $this->info('Audit report sent to '.$settings->alert_email); + } else { + $this->info('No assets to be audited. No report sent.'); + } + + + + } elseif ($settings->alert_email=='') { + $this->error('Could not send email. No alert email configured in settings'); + } elseif (!$settings->audit_warning_days) { + $this->error('No audit warning days set in Admin Notifications. No mail will be sent.'); + } elseif ($settings->alerts_enabled!=1) { + $this->info('Alerts are disabled in the settings. No mail will be sent'); + } else { + $this->error('Something went wrong. :( '); + $this->error('Admin Notifications Email Setting: '.$settings->alert_email); + $this->error('Admin Audit Warning Setting: '.$settings->audit_warning_days); + $this->error('Admin Alerts Emnabled: '.$settings->alerts_enabled); + } + + + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 371362442..f8a79a3f2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,6 +2,7 @@ namespace App\Console; +use App\Console\Commands\RestoreDeletedUsers; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -31,6 +32,7 @@ class Kernel extends ConsoleKernel Commands\RegenerateAssetTags::class, Commands\SyncAssetCounters::class, Commands\RestoreDeletedUsers::class, + Commands\SendUpcomingAuditReport::class, ]; /** diff --git a/app/Notifications/SendUpcomingAuditNotification.php b/app/Notifications/SendUpcomingAuditNotification.php new file mode 100644 index 000000000..eb68df654 --- /dev/null +++ b/app/Notifications/SendUpcomingAuditNotification.php @@ -0,0 +1,65 @@ +assets = $params; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return $notifyBy = ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + $message = (new MailMessage)->markdown('notifications.markdown.upcoming-audits', + [ + 'assets' => $this->assets, + ]) + ->subject(trans_choice('mail.upcoming-audits', $this->assets->count(), ['count' => $this->assets->count()])); + + return $message; + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/resources/lang/en/mail.php b/resources/lang/en/mail.php index de1bafb3c..5fd0dab1e 100644 --- a/resources/lang/en/mail.php +++ b/resources/lang/en/mail.php @@ -64,9 +64,11 @@ return array( 'license_expiring_alert' => 'There is :count license expiring in the next :threshold days.|There are :count licenses expiring in the next :threshold days.', 'to_reset' => 'To reset your :web password, complete this form:', 'type' => 'Type', + 'upcoming-audits' => 'There is :count asset that is coming up for audit within :threshold days.|There are :count assets that are coming up for audit within :threshold days.', 'user' => 'User', 'username' => 'Username', 'welcome' => 'Welcome :name', 'welcome_to' => 'Welcome to :web!', 'your_credentials' => 'Your Snipe-IT credentials', + ); diff --git a/resources/views/notifications/markdown/upcoming-audits.blade.php b/resources/views/notifications/markdown/upcoming-audits.blade.php new file mode 100644 index 000000000..1d633a693 --- /dev/null +++ b/resources/views/notifications/markdown/upcoming-audits.blade.php @@ -0,0 +1,20 @@ +@component('mail::message') + +### {{ trans_choice('mail.upcoming-audits', $assets->count(), ['count' => $assets->count()]) }} + +@component('mail::table') +| |{{ trans('mail.name') }}|{{ trans('general.last_audit') }}|{{ trans('general.next_audit_date') }}|{{ trans('mail.Days') }}|{{ trans('mail.supplier') }} | {{ trans('mail.assigned_to') }} +| |:------------- |:-------------|:---------|:---------|:---------|:---------| +@foreach ($assets as $asset) +@php +$next_audit_date = \App\Helpers\Helper::getFormattedDateObject($asset->next_audit_date, 'date', false); +$last_audit_date = \App\Helpers\Helper::getFormattedDateObject($asset->last_audit_date, 'date', false); +$diff = Carbon::parse($last_audit_date)->diffInDays(Carbon::now()); +$icon = ($diff <= 7) ? '🚨' : (($diff <= 14) ? '⚠️' : ' '); +@endphp +|{{ $icon }}| [{{ $asset->present()->name }}]({{ route('hardware.show', $asset->id) }}) | {{ $last_audit_date }}| {{ $next_audit_date }} | {{ $diff }} | {{ ($asset->supplier ? e($asset->supplier->name) : '') }}|{{ ($asset->assignedTo ? $asset->assignedTo->present()->name() : '') }} +@endforeach +@endcomponent + + +@endcomponent