Fixes #606 - email notifications for expected checkins

This commit is contained in:
snipe 2017-08-31 21:18:05 -07:00
parent c92eed2b3e
commit 3518ea7e7d
4 changed files with 154 additions and 1 deletions

View file

@ -0,0 +1,64 @@
<?php
namespace App\Console\Commands;
use App\Models\Asset;
use Illuminate\Console\Command;
use App\Notifications\ExpectedCheckinNotification;
use Carbon\Carbon;
class SendExpectedCheckinAlerts extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'snipeit:expected-checkin';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for overdue or upcoming expected checkins.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$whenNotify = Carbon::now()->addDays(7);
$assets = Asset::with('assigneduser')->whereNotNull('expected_checkin')->where('expected_checkin', '<=', $whenNotify)->get();
$this->info($whenNotify.' is deadline');
$this->info($assets->count().' assets');
foreach ($assets as $asset) {
if ($asset->assigneduser) {
$asset->assigneduser->notify((new ExpectedCheckinNotification($asset)));
//$this->info($asset);
}
}
}
}

View file

@ -17,6 +17,7 @@ class Kernel extends ConsoleKernel
Commands\CreateAdmin::class, Commands\CreateAdmin::class,
Commands\SendExpirationAlerts::class, Commands\SendExpirationAlerts::class,
Commands\SendInventoryAlerts::class, Commands\SendInventoryAlerts::class,
Commands\SendExpectedCheckinAlerts::class,
Commands\ObjectImportCommand::class, Commands\ObjectImportCommand::class,
Commands\Versioning::class, Commands\Versioning::class,
Commands\SystemBackup::class, Commands\SystemBackup::class,
@ -38,6 +39,7 @@ class Kernel extends ConsoleKernel
$schedule->command('snipeit:inventory-alerts')->daily(); $schedule->command('snipeit:inventory-alerts')->daily();
$schedule->command('snipeit:expiring-alerts')->daily(); $schedule->command('snipeit:expiring-alerts')->daily();
$schedule->command('snipeit:expected-checkins')->daily();
$schedule->command('snipeit:backup')->weekly(); $schedule->command('snipeit:backup')->weekly();
$schedule->command('backup:clean')->daily(); $schedule->command('backup:clean')->daily();
} }

View file

@ -11,6 +11,7 @@ use Config;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Log; use Log;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
use Illuminate\Notifications\Notifiable;
/** /**
* Model for Assets. * Model for Assets.
@ -20,7 +21,7 @@ use Watson\Validating\ValidatingTrait;
class Asset extends Depreciable class Asset extends Depreciable
{ {
protected $presenter = 'App\Presenters\AssetPresenter'; protected $presenter = 'App\Presenters\AssetPresenter';
use Loggable, Requestable, Presentable; use Loggable, Requestable, Presentable, Notifiable;
use SoftDeletes; use SoftDeletes;
const LOCATION = 'location'; const LOCATION = 'location';

View file

@ -0,0 +1,86 @@
<?php
namespace App\Notifications;
use App\Models\Setting;
use App\Models\SnipeModel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
class ExpectedCheckinNotification extends Notification
{
use Queueable;
/**
* @var
*/
private $params;
/**
* Create a new notification instance.
*
* @param $params
*/
public function __construct($params)
{
$this->params = $params;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
$notifyBy = [];
$item = $this->params['item'];
$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)
{
$formatted_due = Carbon::parse($this->params->expected_checkin)->format('D, M j, Y');
return (new MailMessage)
->error()
->subject('Reminder: '.$this->params->present()->name().' checkin deadline approaching')
->line('Hi, '.$this->params->assignedto->first_name)
->greeting('An asset checked out to you is due to be checked back in on '.$formatted_due.'.')
->line('Asset: '.$this->params->present()->name())
->line('Serial: '.$this->params->serial)
->line('Asset Tag: '.$this->params->asset_tag)
->action('View Your Assets', route('view-assets'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}