diff --git a/app/Console/Commands/SendExpirationAlerts.php b/app/Console/Commands/SendExpirationAlerts.php index 759341477d..82a324067c 100644 --- a/app/Console/Commands/SendExpirationAlerts.php +++ b/app/Console/Commands/SendExpirationAlerts.php @@ -6,6 +6,8 @@ use App\Models\Asset; use App\Models\License; use App\Models\Setting; use DB; +use App\Notifications\ExpiringLicenseNotification; +use App\Notifications\ExpiringAssetsNotification; use Illuminate\Console\Command; @@ -44,88 +46,39 @@ class SendExpirationAlerts extends Command public function fire() { + $settings = Setting::getSettings(); + $threshold = $settings->alert_interval; + + // Expiring Assets - $expiring_assets = Asset::getExpiringWarrantee(Setting::getSettings()->alert_interval); - $this->info(count($expiring_assets).' expiring assets'); - - $asset_data['count'] = count($expiring_assets); - $asset_data['email_content'] =''; - $now = date("Y-m-d"); - - - foreach ($expiring_assets as $asset) { - - $expires = $asset->present()->warrantee_expires(); - $difference = round(abs(strtotime($expires) - strtotime($now))/86400); - - if ($difference > 30) { - $asset_data['email_content'] .= ''; - } else { - $asset_data['email_content'] .= ''; - } - $asset_data['email_content'] .= ''; - $asset_data['email_content'] .= $asset->present()->name().''.e($asset->asset_tag).''; - $asset_data['email_content'] .= ''.e($asset->present()->warrantee_expires()).''; - $asset_data['email_content'] .= ''.$difference.' '.trans('mail.days').''; - $asset_data['email_content'] .= ''.($asset->supplier ? e($asset->supplier->name) : '').''; - $asset_data['email_content'] .= ''.($asset->assignedTo ? e($asset->assignedTo->present()->name()) : '').''; - $asset_data['email_content'] .= ''; - } + $assets = Asset::getExpiringWarrantee(Setting::getSettings()->alert_interval); + $this->info(trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count'=>$assets->count(), 'threshold' => $threshold])); // Expiring licenses - $expiring_licenses = License::getExpiringLicenses(Setting::getSettings()->alert_interval); - $this->info(count($expiring_licenses).' expiring licenses'); + $licenses = License::getExpiringLicenses($threshold); + $this->info(trans_choice('mail.license_expiring_alert', $licenses->count(), ['count'=>$licenses->count(), 'threshold' => $threshold])); - $license_data['count'] = $expiring_licenses->count(); - $license_data['email_content'] = ''; + $recipient = new \App\Models\Recipients\AlertRecipient(); - foreach ($expiring_licenses as $license) { - $expires = $license->expiration_date; - $difference = round(abs(strtotime($expires) - strtotime($now))/86400); - - if ($difference > 30) { - $license_data['email_content'] .= ''; - } else { - $license_data['email_content'] .= ''; - } - $license_data['email_content'] .= ''; - $license_data['email_content'] .= $license->name.''; - $license_data['email_content'] .= ''.$license->expiration_date.''; - $license_data['email_content'] .= ''.$difference.' days'; - $license_data['email_content'] .= ''; - } - - if ((Setting::getSettings()->alert_email!='') && (Setting::getSettings()->alerts_enabled==1)) { - - - if (count($expiring_assets) > 0) { - $this->info('Report sent to '.Setting::getSettings()->alert_email); - \Mail::send('emails.expiring-assets-report', $asset_data, function ($m) { - $m->to(explode(',', Setting::getSettings()->alert_email), Setting::getSettings()->site_name); - $m->replyTo(config('mail.reply_to.address'), config('mail.reply_to.name')); - $m->subject(trans('mail.Expiring_Assets_Report')); - }); + if ((Setting::getSettings()->alert_email!='') && ($settings->alerts_enabled==1)) { + if ($assets->count() > 0) { + // Send a rollup to the admin, if settings dictate + $recipient->notify(new ExpiringAssetsNotification($assets, $threshold)); } - if (count($expiring_licenses) > 0) { - $this->info('Report sent to '.Setting::getSettings()->alert_email); - \Mail::send('emails.expiring-licenses-report', $license_data, function ($m) { - $m->to(explode(',', Setting::getSettings()->alert_email), Setting::getSettings()->site_name); - $m->replyTo(config('mail.reply_to.address'), config('mail.reply_to.name')); - $m->subject(trans('mail.Expiring_Licenses_Report')); - }); - + if ($licenses->count() > 0) { + $recipient->notify(new ExpiringLicenseNotification($licenses, $threshold)); } } else { - if (Setting::getSettings()->alert_email=='') { - echo "Could not send email. No alert email configured in settings. \n"; - } elseif (Setting::getSettings()->alerts_enabled!=1) { - echo "Alerts are disabled in the settings. No mail will be sent. \n"; + if ($settings->alert_email=='') { + $this->error('Could not send email. No alert email configured in settings'); + } elseif ($settings->alerts_enabled!=1) { + $this->info('Alerts are disabled in the settings. No mail will be sent'); } } diff --git a/app/Console/Commands/SendInventoryAlerts.php b/app/Console/Commands/SendInventoryAlerts.php index 1822733b22..1f96fca9d1 100644 --- a/app/Console/Commands/SendInventoryAlerts.php +++ b/app/Console/Commands/SendInventoryAlerts.php @@ -6,6 +6,7 @@ use App\Models\Setting; use DB; use Mail; use App\Helpers\Helper; +use App\Notifications\InventoryAlert; use Illuminate\Console\Command; @@ -42,25 +43,27 @@ class SendInventoryAlerts extends Command */ public function handle() { - if ((Setting::getSettings()->alert_email!='') && (Setting::getSettings()->alerts_enabled==1)) { + $settings = Setting::getSettings(); - $data['data'] = Helper::checkLowInventory(); - $data['count'] = count($data['data']); + if (($settings->alert_email!='') && ($settings->alerts_enabled==1)) { - if (count($data['data']) > 0) { - \Mail::send('emails.low-inventory', $data, function ($m) { - $m->to(explode(',', Setting::getSettings()->alert_email), Setting::getSettings()->site_name); - $m->replyTo(config('mail.reply_to.address'), config('mail.reply_to.name')); - $m->subject(trans('mail.Low_Inventory_Report')); - }); + $items = Helper::checkLowInventory(); + // Send a rollup to the admin, if settings dictate + $recipient = new \App\Models\Recipients\AlertRecipient(); + + if (($items) && (count($items) > 0) && ($settings->alert_email!='')) { + + $this->info( trans_choice('mail.low_inventory_alert',count($items)) ); + + $recipient->notify(new InventoryAlert($items, $settings->alert_threshold)); } } else { if (Setting::getSettings()->alert_email=='') { - echo "Could not send email. No alert email configured in settings. \n"; + $this->error('Could not send email. No alert email configured in settings'); } elseif (Setting::getSettings()->alerts_enabled!=1) { - echo "Alerts are disabled in the settings. No mail will be sent. \n"; + $this->info('Alerts are disabled in the settings. No mail will be sent'); } } diff --git a/app/Notifications/ExpiringAssetsNotification.php b/app/Notifications/ExpiringAssetsNotification.php new file mode 100644 index 0000000000..d130d26e27 --- /dev/null +++ b/app/Notifications/ExpiringAssetsNotification.php @@ -0,0 +1,79 @@ +assets = $params; + $this->threshold = $threshold; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + $notifyBy = []; + $notifyBy[]='mail'; + return $notifyBy; + } + + public function toSlack($notifiable) + { + + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $asset + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($params) + { + + $message = (new MailMessage)->markdown('notifications.markdown.report-expiring-assets', + [ + 'assets' => $this->assets, + 'threshold' => $this->threshold, + ]) + ->subject(trans('mail.Expiring_Assets_Report')); + + return $message; + + + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/app/Notifications/ExpiringLicenseNotification.php b/app/Notifications/ExpiringLicenseNotification.php new file mode 100644 index 0000000000..81798f6ca6 --- /dev/null +++ b/app/Notifications/ExpiringLicenseNotification.php @@ -0,0 +1,80 @@ +licenses = $params; + $this->threshold = $threshold; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + $notifyBy = []; + $notifyBy[]='mail'; + return $notifyBy; + } + + public function toSlack($notifiable) + { + + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $asset + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($params) + { + + $message = (new MailMessage)->markdown('notifications.markdown.report-expiring-licenses', + [ + 'licenses' => $this->licenses, + 'threshold' => $this->threshold, + ]) + ->subject(trans('mail.Expiring_Licenses_Report')); + + return $message; + + + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/app/Notifications/InventoryAlert.php b/app/Notifications/InventoryAlert.php new file mode 100644 index 0000000000..3dee49134a --- /dev/null +++ b/app/Notifications/InventoryAlert.php @@ -0,0 +1,79 @@ +items = $params; + $this->threshold = $threshold; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + $notifyBy = []; + $notifyBy[]='mail'; + return $notifyBy; + } + + public function toSlack($notifiable) + { + + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $asset + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($params) + { + + $message = (new MailMessage)->markdown('notifications.markdown.report-low-inventory', + [ + 'items' => $this->items, + 'threshold' => $this->threshold, + ]) + ->subject(trans('mail.Low_Inventory_Report')); + + 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 4b7385c520..de1bafb3cc 100644 --- a/resources/lang/en/mail.php +++ b/resources/lang/en/mail.php @@ -10,8 +10,6 @@ return array( 'asset_name' => 'Asset Name:', 'asset_requested' => 'Asset requested', 'asset_tag' => 'Asset Tag:', - 'assets_warrantee_expiring' => '{1} asset with warrantee expiring in the next 60 days.|[2,Inf] assets with warrantees - expiring in the next 60 days.', 'assigned_to' => 'Assigned To', 'best_regards' => 'Best regards,', 'canceled' => 'Canceled:', @@ -37,11 +35,8 @@ return array( 'hi' => 'Hi', 'i_have_read' => 'I have read and agree to the terms of use, and have received this item.', 'item' => 'Item:', - 'items_below_minimum' => '{1} item that is below minimum inventory or will soon be low.|[2,Inf] items that are below minimum - inventory or will soon be low.', 'Item_Request_Canceled' => 'Item Request Canceled', 'Item_Requested' => 'Item Requested', - 'licenses_expiring' => '{1} license expiring next 60 days.|[2,Inf] licenses expiring next 60 days.', 'link_to_update_password' => 'Please click on the following link to update your :web password:', 'login_first_admin' => 'Login to your new Snipe-IT installation using the credentials below:', 'login' => 'Login:', @@ -64,7 +59,9 @@ return array( 'test_email' => 'Test Email from Snipe-IT', 'test_mail_text' => 'This is a test from the Snipe-IT Asset Management System. If you got this, mail is working :)', 'the_following_item' => 'The following item has been checked in: ', - 'There_are' => '{1} There is|[2,Inf] There are', + 'low_inventory_alert' => 'There is :count item that is below minimum inventory or will soon be low.|There are :count items that are below minimum inventory or will soon be low.', + 'assets_warrantee_alert' => 'There is :count asset with a warrantee expiring in the next :threshold days.|There are :count assets with warrantees expiring in the next :threshold days.', + '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', 'user' => 'User', diff --git a/resources/views/emails/expiring-assets-report.blade.php b/resources/views/emails/expiring-assets-report.blade.php deleted file mode 100644 index ae36b143a9..0000000000 --- a/resources/views/emails/expiring-assets-report.blade.php +++ /dev/null @@ -1,26 +0,0 @@ -@extends('emails/layouts/default') - -@section('content') - -

{{ trans_choice('mail.There_are',$count) }} {{ $count }} {{ trans_choice('mail.assets_warrantee_expiring',$count) }}

- - - - - - - - - - - -{!! $email_content !!} -
{{ trans('mail.name') }}{{ trans('mail.tag') }}{{ trans('mail.expires') }}{{ trans('mail.Days') }}{{ trans('mail.supplier') }}{{ trans('mail.assigned_to') }}
- - @if ($snipeSettings->show_url_in_emails=='1') -

{{ $snipeSettings->site_name }}

- @else -

{{ $snipeSettings->site_name }}

- @endif - -@stop diff --git a/resources/views/emails/expiring-licenses-report.blade.php b/resources/views/emails/expiring-licenses-report.blade.php deleted file mode 100755 index bb5373e76d..0000000000 --- a/resources/views/emails/expiring-licenses-report.blade.php +++ /dev/null @@ -1,23 +0,0 @@ -@extends('emails/layouts/default') - -@section('content') - -

{{ trans_choice('mail.There_are',$count) }} {{ $count }} {{ trans_choice('mail.licenses_expiring',$count) }}

- - - - - - - - -{!! $email_content !!} -
{{ trans('mail.name') }}{{ trans('mail.expires') }}{{ trans('mail.Days') }}
- -@if ($snipeSettings->show_url_in_emails=='1') -

{{ $snipeSettings->site_name }}

-@else -

{{ $snipeSettings->site_name }}

-@endif - -@stop diff --git a/resources/views/emails/low-inventory.blade.php b/resources/views/emails/low-inventory.blade.php deleted file mode 100644 index 552f0ebd1c..0000000000 --- a/resources/views/emails/low-inventory.blade.php +++ /dev/null @@ -1,36 +0,0 @@ -@extends('emails/layouts/default') - -@section('content') - -

{{ trans_choice('mail.There_are',$count) }} {{ $count }} {{ trans_choice('mail.items_below_minimum',$count) }}

- - - - - - - - - - @for($i=0; $count > $i; $i++) - - - - - - - - @endfor - - -
{{ trans('mail.name') }}{{ trans('mail.type') }}{{ trans('mail.current_QTY') }}{{ trans('mail.min_QTY') }}
- {{ $data[$i]['name'] }} - {{ $data[$i]['type'] }}{{ $data[$i]['remaining'] }}{{ $data[$i]['min_amt'] }}
- - @if ($snipeSettings->show_url_in_emails=='1') -

{{ $snipeSettings->site_name }}

- @else -

{{ $snipeSettings->site_name }}

- @endif - -@stop diff --git a/resources/views/notifications/markdown/report-expiring-assets.blade.php b/resources/views/notifications/markdown/report-expiring-assets.blade.php new file mode 100644 index 0000000000..a2ff143693 --- /dev/null +++ b/resources/views/notifications/markdown/report-expiring-assets.blade.php @@ -0,0 +1,20 @@ +@component('mail::message') + +{{ trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count'=>$assets->count(), 'threshold' => $threshold]) }} + +@component('mail::table') +| |{{ trans('mail.name') }} |{{ trans('mail.expires') }} |{{ trans('mail.Days') }}|{{ trans('mail.supplier') }} | {{ trans('mail.assigned_to') }} +| |:------------- |:-------------|:---------|:---------|:---------|:---------| +@foreach ($assets as $asset) +@php +$expires = \App\Helpers\Helper::getFormattedDateObject($asset->present()->warrantee_expires, 'date'); +$diff = round(abs(strtotime($asset->present()->warrantee_expires) - strtotime(date('Y-m-d')))/86400); +$icon = ($diff <= ($threshold / 2)) ? '🚨' : (($diff <= $threshold) ? '⚠️' : ' '); + +@endphp +|{{ $icon }}| [{{ $asset->present()->name }}]({{ route('hardware.show', $asset->id) }}) | {{ $expires['formatted'] }} | {{ $diff }} {{ trans('mail.Days') }} | {{ ($asset->supplier ? e($asset->supplier->name) : '') }}|{{ ($asset->assignedTo ? e($asset->assignedTo->present()->name()) : '') }} +@endforeach +@endcomponent + + +@endcomponent diff --git a/resources/views/notifications/markdown/report-expiring-licenses.blade.php b/resources/views/notifications/markdown/report-expiring-licenses.blade.php new file mode 100644 index 0000000000..2df4dbb3a1 --- /dev/null +++ b/resources/views/notifications/markdown/report-expiring-licenses.blade.php @@ -0,0 +1,22 @@ +@component('mail::message') + + {{ trans_choice('mail.license_expiring_alert', $licenses->count(), ['count'=>$licenses->count(), 'threshold' => $threshold]) }} + +@component('mail::table') +| |{{ trans('mail.name') }} |{{ trans('mail.expires') }} |{{ trans('mail.Days') }} +| |:------------- |:-------------|:-------------| +@foreach ($licenses as $license) +@php +$expires = \App\Helpers\Helper::getFormattedDateObject($license->expiration_date, 'date'); +$diff = round(abs(strtotime($license->expiration_date->format('Y-m-d')) - strtotime(date('Y-m-d')))/86400); + +$icon = ($diff <= ($threshold / 2)) ? '🚨' : (($diff <= $threshold) ? '⚠️' : ' '); + + +@endphp +|{{ $icon }}| [{{ $license->name }}]({{ route('licenses.show', $license->id) }}) | {{ $expires['formatted'] }} | {{ $diff }} {{ trans('mail.Days') }} +@endforeach +@endcomponent + + +@endcomponent diff --git a/resources/views/notifications/markdown/report-low-inventory.blade.php b/resources/views/notifications/markdown/report-low-inventory.blade.php new file mode 100644 index 0000000000..d07899411f --- /dev/null +++ b/resources/views/notifications/markdown/report-low-inventory.blade.php @@ -0,0 +1,14 @@ +@component('mail::message') + +{{ trans_choice('mail.low_inventory_alert',count($items)) }} + +@component('mail::table') +|{{ trans('mail.name') }} |{{ trans('mail.type') }} |{{ trans('mail.current_QTY') }}|{{ trans('mail.min_QTY') }} +|:-------------|:---------|:---------:|:---------:| +@for($i=0; count($items) > $i; $i++) +|{{ $items[$i]['name'] }}|{{ $items[$i]['type'] }} |{{ $items[$i]['remaining'] }} |{{ $items[$i]['min_amt'] }} | +@endfor +@endcomponent + + +@endcomponent diff --git a/resources/views/vendor/mail/html/layout.blade.php b/resources/views/vendor/mail/html/layout.blade.php index 948afcee9d..b2af14ec2e 100644 --- a/resources/views/vendor/mail/html/layout.blade.php +++ b/resources/views/vendor/mail/html/layout.blade.php @@ -36,7 +36,7 @@ - +