From 9d8f251fc44d4cd3eebaab306dd1757c77ba9e6d Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 28 Oct 2025 14:15:15 +0000 Subject: [PATCH] Limit the email to 30 records, added optional --with-output --- .../Commands/SendUpcomingAuditReport.php | 84 ++++++++++++------- app/Mail/SendUpcomingAuditMail.php | 6 +- resources/lang/en-US/general.php | 1 + resources/lang/en-US/mail.php | 3 +- .../markdown/upcoming-audits.blade.php | 11 ++- 5 files changed, 69 insertions(+), 36 deletions(-) diff --git a/app/Console/Commands/SendUpcomingAuditReport.php b/app/Console/Commands/SendUpcomingAuditReport.php index 4e422a1dcd..657cf095b4 100644 --- a/app/Console/Commands/SendUpcomingAuditReport.php +++ b/app/Console/Commands/SendUpcomingAuditReport.php @@ -16,7 +16,7 @@ class SendUpcomingAuditReport extends Command * * @var string */ - protected $signature = 'snipeit:upcoming-audits'; + protected $signature = 'snipeit:upcoming-audits {--with-output : Display the results in a table in your console in addition to sending the email}'; /** * The console command description. @@ -47,43 +47,67 @@ class SendUpcomingAuditReport extends Command $today = Carbon::now(); $interval_date = $today->copy()->addDays($interval); - $assets = Asset::whereNull('deleted_at')->dueOrOverdueForAudit($settings)->orderBy('assets.next_audit_date', 'asc')->get(); - $this->info($assets->count() . ' assets must be audited in on or before ' . $interval_date . ' is deadline'); + $assets_query = Asset::whereNull('deleted_at')->dueOrOverdueForAudit($settings)->orderBy('assets.next_audit_date', 'asc')->with('supplier'); + $asset_count = $assets_query->count(); + $this->info(number_format($asset_count) . ' assets must be audited in on or before ' . $interval_date); - if ((count($assets) !== 0) && ($assets->count() > 0) && ($settings->alert_email != '')) { + + if ($asset_count > 0) { + + $assets_for_email = $assets_query->limit(30)->get(); + // Send a rollup to the admin, if settings dictate - $recipients = collect(explode(',', $settings->alert_email)) - ->map(fn($item) => trim($item)) - ->filter(fn($item) => !empty($item)) - ->all(); + if ($settings->alert_email != '') { + + $recipients = collect(explode(',', $settings->alert_email)) + ->map(fn($item) => trim($item)) + ->filter(fn($item) => !empty($item)) + ->all(); + + Mail::to($recipients)->send(new SendUpcomingAuditMail($assets_for_email, $settings->audit_warning_days, $asset_count)); + $this->info('Audit notification sent to: ' . $settings->alert_email); + + } else { + $this->info('There is no admin alert email set so no email will be sent.'); + } - $this->info('Sending Admin SendUpcomingAuditNotification to: ' . $settings->alert_email); - Mail::to($recipients)->send(new SendUpcomingAuditMail($assets, $settings->audit_warning_days)); - $this->table( - [ - trans('general.id'), - trans('general.name'), - trans('general.last_audit'), - trans('general.next_audit_date'), - trans('mail.Days'), - trans('mail.supplier'), - trans('mail.assigned_to'), + if ($this->option('with-output')) { - ], - $assets->map(fn($item) => [ - trans('general.id') => $item->id, - trans('general.name') => $item->display_name, - trans('general.last_audit') => $item->last_audit_formatted_date, - trans('general.next_audit_date') => $item->next_audit_formatted_date, - trans('mail.Days') => round($item->next_audit_diff_in_days), - trans('mail.supplier') => $item->supplier ? $item->supplier->name : '', - trans('mail.assigned_to') => $item->assignedTo ? $item->assignedTo->display_name : '', - ]) - ); + + // Get the full list if the user wants output in the console + $assets_for_output = $assets_query->limit(null)->get(); + + $this->table( + [ + trans('general.id'), + trans('general.name'), + trans('general.last_audit'), + trans('general.next_audit_date'), + trans('mail.Days'), + trans('mail.supplier'), + trans('mail.assigned_to'), + + ], + $assets_for_output->map(fn($item) => [ + trans('general.id') => $item->id, + trans('general.name') => $item->display_name, + trans('general.last_audit') => $item->last_audit_formatted_date, + trans('general.next_audit_date') => $item->next_audit_formatted_date, + trans('mail.Days') => round($item->next_audit_diff_in_days), + trans('mail.supplier') => $item->supplier ? $item->supplier->name : '', + trans('mail.assigned_to') => $item->assignedTo ? $item->assignedTo->display_name : '', + ]) + ); + } + + } else { + $this->info('There are no assets due for audit in the next ' . $interval . ' days.'); } + + } } diff --git a/app/Mail/SendUpcomingAuditMail.php b/app/Mail/SendUpcomingAuditMail.php index 03faa081eb..d0b511771a 100644 --- a/app/Mail/SendUpcomingAuditMail.php +++ b/app/Mail/SendUpcomingAuditMail.php @@ -17,10 +17,11 @@ class SendUpcomingAuditMail extends Mailable /** * Create a new message instance. */ - public function __construct($params, $threshold) + public function __construct($params, $threshold, $total) { $this->assets = $params; $this->threshold = $threshold; + $this->total = $total; } /** @@ -32,7 +33,7 @@ class SendUpcomingAuditMail extends Mailable return new Envelope( from: $from, - subject: trans_choice('mail.upcoming-audits', $this->assets->count(), ['count' => $this->assets->count(), 'threshold' => $this->threshold]), + subject: trans_choice('mail.upcoming-audits', $this->total, ['count' => $this->total, 'threshold' => $this->threshold]), ); } @@ -49,6 +50,7 @@ class SendUpcomingAuditMail extends Mailable with: [ 'assets' => $this->assets, 'threshold' => $this->threshold, + 'total' => $this->total, ], ); } diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php index e18eca65ce..8722586054 100644 --- a/resources/lang/en-US/general.php +++ b/resources/lang/en-US/general.php @@ -350,6 +350,7 @@ return [ 'login_disabled' => 'Login Disabled', 'audit_due' => 'Due for Audit', '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', + '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', 'checkin_due' => 'Due for Checkin', 'checkin_overdue' => 'Overdue for Checkin', 'checkin_due_days' => '{}Due for Checkin|[1]Assets Due for Checkin Within :days Day|[2,*]Assets Due for Checkin Within :days Days', diff --git a/resources/lang/en-US/mail.php b/resources/lang/en-US/mail.php index 70ee6ba42f..1fd02d5c07 100644 --- a/resources/lang/en-US/mail.php +++ b/resources/lang/en-US/mail.php @@ -100,7 +100,8 @@ return [ 'the_following_item' => 'The following item has been checked in: ', 'to_reset' => 'To reset your :web password, complete this form:', 'type' => 'Type', - '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.', + '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. ', + '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.', 'user' => 'User', 'username' => 'Username', 'unaccepted_asset_reminder' => 'Reminder: You have Unaccepted Assets.', diff --git a/resources/views/notifications/markdown/upcoming-audits.blade.php b/resources/views/notifications/markdown/upcoming-audits.blade.php index 1c1cd3a107..c21e2767f2 100644 --- a/resources/views/notifications/markdown/upcoming-audits.blade.php +++ b/resources/views/notifications/markdown/upcoming-audits.blade.php @@ -1,12 +1,17 @@ @component('mail::message') -{{ trans_choice('mail.upcoming-audits', $assets->count(), ['count' => $assets->count(), 'threshold' => $threshold]) }} +{{ trans_choice('mail.upcoming-audits', $total, ['count' => $total, 'threshold' => $threshold]) }} +{{ trans('mail.upcoming-audits_click') }} + + + {{ trans_choice('general.audit_due_days_view_all', $threshold, ['days' => $threshold]) }} + | | | | | ------------- | ------------- | ------------- | @foreach ($assets as $asset) -| {{ ($asset->next_audit_diff_in_days <= 7) ? '🚨' : (($asset->next_audit_diff_in_days <= 14) ? '⚠️' : '⚠️') }} **{{ trans('mail.name') }}** | {{ $asset->display_name }} | +| {{ ($asset->next_audit_diff_in_days <= 7) ? '🚨' : (($asset->next_audit_diff_in_days <= 14) ? '⚠️' : '⚠️') }} **{{ trans('mail.name') }}** | {{ $asset->display_name }} ({{ trans('general.audit') }})| @if ($asset->serial) | **{{ trans('general.serial_number') }}** | {{ $asset->serial }} | @endif @@ -33,6 +38,6 @@ - {{ trans_choice('general.audit_due_days', $threshold, ['days' => $threshold]) }} + {{ trans_choice('general.audit_due_days_view_all', $threshold, ['days' => $threshold]) }} @endcomponent