Features/nicer notifications (#5886)

* Improved expiring licenses notification

* Improved expiring assets notification

* Nicee low inventory notification

* Refactored stupid language strings

* Oops

* Use settings variable
This commit is contained in:
snipe 2018-07-18 19:15:45 -07:00 committed by GitHub
parent 92671823d8
commit 9daeeeb851
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 333 additions and 171 deletions

View file

@ -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'] .= '<tr style="background-color: #fcffa3;">';
} else {
$asset_data['email_content'] .= '<tr style="background-color:#d9534f;">';
}
$asset_data['email_content'] .= '<td><a href="'.config('app.url').'/hardware/'.e($asset->id).'/view">';
$asset_data['email_content'] .= $asset->present()->name().'</a></td><td>'.e($asset->asset_tag).'</td>';
$asset_data['email_content'] .= '<td>'.e($asset->present()->warrantee_expires()).'</td>';
$asset_data['email_content'] .= '<td>'.$difference.' '.trans('mail.days').'</td>';
$asset_data['email_content'] .= '<td>'.($asset->supplier ? e($asset->supplier->name) : '').'</td>';
$asset_data['email_content'] .= '<td>'.($asset->assignedTo ? e($asset->assignedTo->present()->name()) : '').'</td>';
$asset_data['email_content'] .= '</tr>';
}
$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 ((Setting::getSettings()->alert_email!='') && ($settings->alerts_enabled==1)) {
if ($difference > 30) {
$license_data['email_content'] .= '<tr style="background-color: #fcffa3;">';
} else {
$license_data['email_content'] .= '<tr style="background-color:#d9534f;">';
}
$license_data['email_content'] .= '<td><a href="'.route('licenses.show', $license->id).'">';
$license_data['email_content'] .= $license->name.'</a></td>';
$license_data['email_content'] .= '<td>'.$license->expiration_date.'</td>';
$license_data['email_content'] .= '<td>'.$difference.' days</td>';
$license_data['email_content'] .= '</tr>';
if ($assets->count() > 0) {
// Send a rollup to the admin, if settings dictate
$recipient->notify(new ExpiringAssetsNotification($assets, $threshold));
}
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 (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');
}
}

View file

@ -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');
}
}

View file

@ -0,0 +1,79 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ExpiringAssetsNotification extends Notification
{
use Queueable;
/**
* @var
*/
private $params;
/**
* Create a new notification instance.
*
* @param $params
*/
public function __construct($params, $threshold)
{
$this->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 [
//
];
}
}

View file

@ -0,0 +1,80 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ExpiringLicenseNotification extends Notification
{
use Queueable;
/**
* @var
*/
private $params;
/**
* Create a new notification instance.
*
* @param $params
*/
public function __construct($params, $threshold)
{
$this->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 [
//
];
}
}

View file

@ -0,0 +1,79 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class InventoryAlert extends Notification
{
use Queueable;
/**
* @var
*/
private $params;
/**
* Create a new notification instance.
*
* @param $params
*/
public function __construct($params, $threshold)
{
$this->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 [
//
];
}
}

View file

@ -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',

View file

@ -1,26 +0,0 @@
@extends('emails/layouts/default')
@section('content')
<p>{{ trans_choice('mail.There_are',$count) }} {{ $count }} {{ trans_choice('mail.assets_warrantee_expiring',$count) }}</p>
<table style="border: 1px solid black; padding: 5px;" width="100%" cellspacing="0" cellpadding="3">
<tr>
<td><strong>{{ trans('mail.name') }}</strong></td>
<td><strong>{{ trans('mail.tag') }}</strong></td>
<td><strong>{{ trans('mail.expires') }}</strong></td>
<td><strong>{{ trans('mail.Days') }}</strong></td>
<td><strong>{{ trans('mail.supplier') }}</strong></td>
<td><strong>{{ trans('mail.assigned_to') }}</strong></td>
</tr>
{!! $email_content !!}
</table>
@if ($snipeSettings->show_url_in_emails=='1')
<p><a href="{{ url('/') }}">{{ $snipeSettings->site_name }}</a></p>
@else
<p>{{ $snipeSettings->site_name }}</p>
@endif
@stop

View file

@ -1,23 +0,0 @@
@extends('emails/layouts/default')
@section('content')
<p>{{ trans_choice('mail.There_are',$count) }} {{ $count }} {{ trans_choice('mail.licenses_expiring',$count) }}</p>
<table style="border: 1px solid black; padding: 5px;" width="100%" cellspacing="0" cellpadding="3">
<tr>
<td><strong>{{ trans('mail.name') }}</strong></td>
<td><strong>{{ trans('mail.expires') }}</strong></td>
<td><strong>{{ trans('mail.Days') }}</strong></td>
</tr>
{!! $email_content !!}
</table>
@if ($snipeSettings->show_url_in_emails=='1')
<p><a href="{{ url('/') }}">{{ $snipeSettings->site_name }}</a></p>
@else
<p>{{ $snipeSettings->site_name }}</p>
@endif
@stop

View file

@ -1,36 +0,0 @@
@extends('emails/layouts/default')
@section('content')
<p>{{ trans_choice('mail.There_are',$count) }} {{ $count }} {{ trans_choice('mail.items_below_minimum',$count) }}</p>
<table style="border: 1px solid black; padding: 5px;" width="100%" cellspacing="0" cellpadding="3">
<tr>
<td><strong>{{ trans('mail.name') }}</strong></td>
<td><strong>{{ trans('mail.type') }}</strong></td>
<td><strong>{{ trans('mail.current_QTY') }}</strong></td>
<td><strong>{{ trans('mail.min_QTY') }}</strong></td>
</tr>
@for($i=0; $count > $i; $i++)
<tr>
<td>
<a href="{{ route($data[$i]['type'].'.show', $data[$i]['id']) }}">{{ $data[$i]['name'] }}</a>
</td>
<td>{{ $data[$i]['type'] }}</td>
<td>{{ $data[$i]['remaining'] }}</td>
<td>{{ $data[$i]['min_amt'] }}</td>
</tr>
@endfor
</table>
@if ($snipeSettings->show_url_in_emails=='1')
<p><a href="{{ url('/') }}">{{ $snipeSettings->site_name }}</a></p>
@else
<p>{{ $snipeSettings->site_name }}</p>
@endif
@stop

View file

@ -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

View file

@ -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

View file

@ -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++)
|<a href="({{ route($items[$i]['type'].'.show', $items[$i]['id']) }}">{{ $items[$i]['name'] }}</a>|{{ $items[$i]['type'] }} |{{ $items[$i]['remaining'] }} |{{ $items[$i]['min_amt'] }} |
@endfor
@endcomponent
@endcomponent

View file

@ -36,7 +36,7 @@
<!-- Email Body -->
<tr>
<td class="body" width="100%" cellpadding="0" cellspacing="0">
<table class="inner-body" align="center" width="570" cellpadding="0" cellspacing="0">
<table class="inner-body" align="center" width="90%" cellpadding="0" cellspacing="0">
<!-- Body content -->
<tr>
<td class="content-cell">