Skip to content

Commit 60a7b7f

Browse files
authored
Merge pull request #18109 from grokability/audit-improvements
Limit the upcoming audit email to 30 records, added optional --with-output
2 parents 2b49865 + 90263ea commit 60a7b7f

File tree

5 files changed

+75
-40
lines changed

5 files changed

+75
-40
lines changed

app/Console/Commands/SendUpcomingAuditReport.php

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SendUpcomingAuditReport extends Command
1616
*
1717
* @var string
1818
*/
19-
protected $signature = 'snipeit:upcoming-audits';
19+
protected $signature = 'snipeit:upcoming-audits {--with-output : Display the results in a table in your console in addition to sending the email}';
2020

2121
/**
2222
* The console command description.
@@ -47,43 +47,69 @@ public function handle()
4747
$today = Carbon::now();
4848
$interval_date = $today->copy()->addDays($interval);
4949

50-
$assets = Asset::whereNull('deleted_at')->dueOrOverdueForAudit($settings)->orderBy('assets.next_audit_date', 'asc')->get();
51-
$this->info($assets->count() . ' assets must be audited in on or before ' . $interval_date . ' is deadline');
50+
$assets_query = Asset::whereNull('deleted_at')->dueOrOverdueForAudit($settings)->orderBy('assets.next_audit_date', 'asc')->with('supplier');
51+
$asset_count = $assets_query->count();
52+
$this->info(number_format($asset_count) . ' assets must be audited on or before ' . $interval_date);
53+
if (!$this->option('with-output')) {
54+
$this->info('Run this command with the --with-output option to see the full list in the console.');
55+
}
56+
5257

58+
if ($asset_count > 0) {
59+
60+
$assets_for_email = $assets_query->limit(30)->get();
5361

54-
if ((count($assets) !== 0) && ($assets->count() > 0) && ($settings->alert_email != '')) {
5562
// Send a rollup to the admin, if settings dictate
56-
$recipients = collect(explode(',', $settings->alert_email))
57-
->map(fn($item) => trim($item))
58-
->filter(fn($item) => !empty($item))
59-
->all();
60-
61-
62-
$this->info('Sending Admin SendUpcomingAuditNotification to: ' . $settings->alert_email);
63-
Mail::to($recipients)->send(new SendUpcomingAuditMail($assets, $settings->audit_warning_days));
64-
65-
$this->table(
66-
[
67-
trans('general.id'),
68-
trans('general.name'),
69-
trans('general.last_audit'),
70-
trans('general.next_audit_date'),
71-
trans('mail.Days'),
72-
trans('mail.supplier'),
73-
trans('mail.assigned_to'),
74-
75-
],
76-
$assets->map(fn($item) => [
77-
trans('general.id') => $item->id,
78-
trans('general.name') => $item->display_name,
79-
trans('general.last_audit') => $item->last_audit_formatted_date,
80-
trans('general.next_audit_date') => $item->next_audit_formatted_date,
81-
trans('mail.Days') => round($item->next_audit_diff_in_days),
82-
trans('mail.supplier') => $item->supplier ? $item->supplier->name : '',
83-
trans('mail.assigned_to') => $item->assignedTo ? $item->assignedTo->display_name : '',
84-
])
85-
);
63+
if ($settings->alert_email != '') {
64+
65+
$recipients = collect(explode(',', $settings->alert_email))
66+
->map(fn($item) => trim($item))
67+
->filter(fn($item) => !empty($item))
68+
->all();
69+
70+
Mail::to($recipients)->send(new SendUpcomingAuditMail($assets_for_email, $settings->audit_warning_days, $asset_count));
71+
$this->info('Audit notification sent to: ' . $settings->alert_email);
72+
73+
} else {
74+
$this->info('There is no admin alert email set so no email will be sent.');
75+
}
76+
77+
78+
79+
if ($this->option('with-output')) {
80+
81+
82+
// Get the full list if the user wants output in the console
83+
$assets_for_output = $assets_query->limit(null)->get();
84+
85+
$this->table(
86+
[
87+
trans('general.id'),
88+
trans('general.name'),
89+
trans('general.last_audit'),
90+
trans('general.next_audit_date'),
91+
trans('mail.Days'),
92+
trans('mail.supplier'),
93+
trans('mail.assigned_to'),
94+
95+
],
96+
$assets_for_output->map(fn($item) => [
97+
trans('general.id') => $item->id,
98+
trans('general.name') => $item->display_name,
99+
trans('general.last_audit') => $item->last_audit_formatted_date,
100+
trans('general.next_audit_date') => $item->next_audit_formatted_date,
101+
trans('mail.Days') => round($item->next_audit_diff_in_days),
102+
trans('mail.supplier') => $item->supplier ? $item->supplier->name : '',
103+
trans('mail.assigned_to') => $item->assignedTo ? $item->assignedTo->display_name : '',
104+
])
105+
);
106+
}
107+
108+
} else {
109+
$this->info('There are no assets due for audit in the next ' . $interval . ' days.');
86110
}
87111

112+
113+
88114
}
89115
}

app/Mail/SendUpcomingAuditMail.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class SendUpcomingAuditMail extends Mailable
1717
/**
1818
* Create a new message instance.
1919
*/
20-
public function __construct($params, $threshold)
20+
public function __construct($params, $threshold, $total)
2121
{
2222
$this->assets = $params;
2323
$this->threshold = $threshold;
24+
$this->total = $total;
2425
}
2526

2627
/**
@@ -32,7 +33,7 @@ public function envelope(): Envelope
3233

3334
return new Envelope(
3435
from: $from,
35-
subject: trans_choice('mail.upcoming-audits', $this->assets->count(), ['count' => $this->assets->count(), 'threshold' => $this->threshold]),
36+
subject: trans_choice('mail.upcoming-audits', $this->total, ['count' => $this->total, 'threshold' => $this->threshold]),
3637
);
3738
}
3839

@@ -49,6 +50,7 @@ public function content(): Content
4950
with: [
5051
'assets' => $this->assets,
5152
'threshold' => $this->threshold,
53+
'total' => $this->total,
5254
],
5355
);
5456
}

resources/lang/en-US/general.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@
350350
'login_disabled' => 'Login Disabled',
351351
'audit_due' => 'Due for Audit',
352352
'audit_due_days' => '{}Assets Due or Overdue for Audit|[1]Assets Due or Overdue for Audit Within a Day|[2,*]Assets Due or Overdue for Audit Within :days Days',
353+
'audit_due_days_view_all' => '{}Assets Due or Overdue for Audit|[1]View All Assets Due or Overdue for Audit Within a Day|[2,*]View All Assets Due or Overdue for Audit Within :days Days',
353354
'checkin_due' => 'Due for Checkin',
354355
'checkin_overdue' => 'Overdue for Checkin',
355356
'checkin_due_days' => '{}Due for Checkin|[1]Assets Due for Checkin Within :days Day|[2,*]Assets Due for Checkin Within :days Days',

resources/lang/en-US/mail.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@
100100
'the_following_item' => 'The following item has been checked in: ',
101101
'to_reset' => 'To reset your :web password, complete this form:',
102102
'type' => 'Type',
103-
'upcoming-audits' => 'There is :count asset that is coming up for audit within :threshold days.|There are :count assets that are coming up for audit within :threshold days.',
103+
'upcoming-audits' => 'There is :count asset that is coming up for audit within :threshold days.|There are :count assets that are coming up for audit within :threshold days. ',
104+
'upcoming-audits_click' => 'This email may not contain the full list so as not to exceed email size limits. Click on the button below to view all assets due for audit.',
104105
'user' => 'User',
105106
'username' => 'Username',
106107
'unaccepted_asset_reminder' => 'Reminder: You have Unaccepted Assets.',

resources/views/notifications/markdown/upcoming-audits.blade.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
@component('mail::message')
22

3-
{{ trans_choice('mail.upcoming-audits', $assets->count(), ['count' => $assets->count(), 'threshold' => $threshold]) }}
3+
{{ trans_choice('mail.upcoming-audits', $total, ['count' => $total, 'threshold' => $threshold]) }}
4+
{{ trans('mail.upcoming-audits_click') }}
5+
6+
<x-mail::button :url="route('assets.audit.due')">
7+
{{ trans_choice('general.audit_due_days_view_all', $threshold, ['days' => $threshold]) }}
8+
</x-mail::button>
49

510
<x-mail::table>
611
| | | |
712
| ------------- | ------------- | ------------- |
813
@foreach ($assets as $asset)
9-
| {{ ($asset->next_audit_diff_in_days <= 7) ? '🚨' : (($asset->next_audit_diff_in_days <= 14) ? '⚠️' : '⚠️') }} **{{ trans('mail.name') }}** | <a href="{{ route('hardware.show', $asset->id) }}">{{ $asset->display_name }}</a> |
14+
| {{ ($asset->next_audit_diff_in_days <= 7) ? '🚨' : (($asset->next_audit_diff_in_days <= 14) ? '⚠️' : '⚠️') }} **{{ trans('mail.name') }}** | <a href="{{ route('hardware.show', $asset->id) }}">{{ $asset->display_name }}</a> (<a href="{{ route('asset.audit.create', $asset->id) }}">{{ trans('general.audit') }}</a>)|
1015
@if ($asset->serial)
1116
| **{{ trans('general.serial_number') }}** | {{ $asset->serial }} |
1217
@endif
@@ -33,6 +38,6 @@
3338
</x-mail::table>
3439

3540
<x-mail::button :url="route('assets.audit.due')">
36-
{{ trans_choice('general.audit_due_days', $threshold, ['days' => $threshold]) }}
41+
{{ trans_choice('general.audit_due_days_view_all', $threshold, ['days' => $threshold]) }}
3742
</x-mail::button>
3843
@endcomponent

0 commit comments

Comments
 (0)