Skip to content

A free open source IT asset/license management system

Added console command to send inventory reports to users

snipe77cdb2f409cc
diff --git a/app/Console/Commands/SendCurrentInventoryToUsers.php b/app/Console/Commands/SendCurrentInventoryToUsers.php
new file mode 100644
index 0000000000..2ea8ac3782
--- /dev/null
+++ b/app/Console/Commands/SendCurrentInventoryToUsers.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\Asset;
+use App\Models\User;
+use App\Notifications\CurrentInventory;
+
+class SendCurrentInventoryToUsers extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'snipeit:user-inventory';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'This will send users a report of all of the items currently checked out to them.';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+
+ $users = User::whereNull('deleted_at')->whereNotNull('email')->with('assets', 'accessories', 'consumables', 'licenses')->get();
+
+ foreach ($users as $user) {
+ $this->info($user->email);
+ $user->notify((new CurrentInventory($user)));
+ }
+
+
+ }
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index fed6c0398c..750369df55 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -31,6 +31,7 @@ class Kernel extends ConsoleKernel
Commands\RegenerateAssetTags::class,
Commands\SyncAssetCounters::class,
Commands\RestoreDeletedUsers::class,
+ Commands\SendCurrentInventoryToUsers::class,
];
/**
diff --git a/app/Notifications/CurrentInventory.php b/app/Notifications/CurrentInventory.php
new file mode 100644
index 0000000000..b39c7245c6
--- /dev/null
+++ b/app/Notifications/CurrentInventory.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Notifications;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Notifications\Notification;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Notifications\Messages\MailMessage;
+
+class CurrentInventory extends Notification
+{
+ use Queueable;
+
+ /**
+ * Create a new notification instance.
+ *
+ * @return void
+ */
+ public function __construct($user)
+ {
+ $this->user = $user;
+ }
+
+ /**
+ * Get the notification's delivery channels.
+ *
+ * @param mixed $notifiable
+ * @return array
+ */
+ public function via($notifiable)
+ {
+ return ['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.user-inventory',
+ [
+ 'assets' => $this->user->assets,
+ 'accessories' => $this->user->accessories,
+ 'licenses' => $this->user->licenses,
+ ])
+ ->subject('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/views/notifications/markdown/user-inventory.blade.php b/resources/views/notifications/markdown/user-inventory.blade.php
new file mode 100644
index 0000000000..4efe69e01f
--- /dev/null
+++ b/resources/views/notifications/markdown/user-inventory.blade.php
@@ -0,0 +1,39 @@
+@component('mail::message')
+
+This is a reminder of the items currently checked out to you. If you feel this list is inaccurate (something is missing, or something appears here that you believe you never received), please email {{ config('mail.reply_to.name') }} at {{ config('mail.reply_to.address') }}.
+
+
+@component('mail::table')
+
+@if ($assets->count() > 0)
+## {{ $assets->count() }} Assets
+|{{ trans('mail.name') }} |{{ trans('mail.asset_tag') }} |
+|:------------- |:-------------|:---------|
+@foreach($assets as $asset)
+|{{ $asset->present()->name }} |{{ $asset->asset_tag }} |
+@endforeach
+@endif
+
+@if ($accessories->count() > 0)
+## {{ $accessories->count() }} Accessories
+|{{ trans('mail.name') }} |
+| |:------------- |
+@foreach($accessories as $accessory)
+|{{ $accessory->name }} |
+@endforeach
+@endif
+
+@if ($licenses->count() > 0)
+## {{ $licenses->count() }} Licenses
+
+| |:------------- |
+@foreach($licenses as $license)
+|{{ $asset->$license }} |
+@endforeach
+@endif
+
+
+@endcomponent
+
+
+@endcomponent