Skip to content

A free open source IT asset/license management system

Fixes #606 - email notifications for expected checkins

snipe3518ea7e7dd5
diff --git a/app/Console/Commands/SendExpectedCheckinAlerts.php b/app/Console/Commands/SendExpectedCheckinAlerts.php
new file mode 100644
index 0000000000..912f2975a7
--- /dev/null
+++ b/app/Console/Commands/SendExpectedCheckinAlerts.php
@@ -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);
+ }
+ }
+
+
+
+
+
+ }
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index ca86a418eb..d6de607fb8 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -17,6 +17,7 @@ class Kernel extends ConsoleKernel
Commands\CreateAdmin::class,
Commands\SendExpirationAlerts::class,
Commands\SendInventoryAlerts::class,
+ Commands\SendExpectedCheckinAlerts::class,
Commands\ObjectImportCommand::class,
Commands\Versioning::class,
Commands\SystemBackup::class,
@@ -38,6 +39,7 @@ class Kernel extends ConsoleKernel
$schedule->command('snipeit:inventory-alerts')->daily();
$schedule->command('snipeit:expiring-alerts')->daily();
+ $schedule->command('snipeit:expected-checkins')->daily();
$schedule->command('snipeit:backup')->weekly();
$schedule->command('backup:clean')->daily();
}
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 2da464c057..0cbc9b5f78 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -11,6 +11,7 @@ use Config;
use Illuminate\Database\Eloquent\SoftDeletes;
use Log;
use Watson\Validating\ValidatingTrait;
+use Illuminate\Notifications\Notifiable;
/**
* Model for Assets.
@@ -20,7 +21,7 @@ use Watson\Validating\ValidatingTrait;
class Asset extends Depreciable
{
protected $presenter = 'App\Presenters\AssetPresenter';
- use Loggable, Requestable, Presentable;
+ use Loggable, Requestable, Presentable, Notifiable;
use SoftDeletes;
const LOCATION = 'location';
diff --git a/app/Notifications/ExpectedCheckinNotification.php b/app/Notifications/ExpectedCheckinNotification.php
new file mode 100644
index 0000000000..d1562832a2
--- /dev/null
+++ b/app/Notifications/ExpectedCheckinNotification.php
@@ -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 [
+ //
+ ];
+ }
+}