Skip to content

Commit 9d8f251

Browse files
committed
Limit the email to 30 records, added optional --with-output
1 parent 2b49865 commit 9d8f251

File tree

5 files changed

+73
-40
lines changed

5 files changed

+73
-40
lines changed

app/Console/Commands/SendUpcomingAuditReport.php

Lines changed: 58 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,67 @@ 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 in on or before ' . $interval_date);
5253

5354

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

110+
111+
88112
}
89113
}

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)