fix deprecation on asset obs get unaccept report to populate

This commit is contained in:
Godfrey M 2025-01-06 11:26:45 -08:00
parent 331fbb66bd
commit bb03e00279
3 changed files with 37 additions and 20 deletions

View file

@ -9,6 +9,7 @@ use App\Models\Setting;
use App\Notifications\ExpiringAssetsNotification; use App\Notifications\ExpiringAssetsNotification;
use App\Notifications\ExpiringLicenseNotification; use App\Notifications\ExpiringLicenseNotification;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Notification;
class SendExpirationAlerts extends Command class SendExpirationAlerts extends Command
{ {
@ -45,7 +46,7 @@ class SendExpirationAlerts extends Command
$threshold = $settings->alert_interval; $threshold = $settings->alert_interval;
if (($settings->alert_email != '') && ($settings->alerts_enabled == 1)) { if (($settings->alert_email != '') && ($settings->alerts_enabled == 1)) {
$this->info('alerts');
// Send a rollup to the admin, if settings dictate // Send a rollup to the admin, if settings dictate
$recipients = collect(explode(',', $settings->alert_email))->map(function ($item, $key) { $recipients = collect(explode(',', $settings->alert_email))->map(function ($item, $key) {
return new AlertRecipient($item); return new AlertRecipient($item);
@ -54,15 +55,17 @@ class SendExpirationAlerts extends Command
// Expiring Assets // Expiring Assets
$assets = Asset::getExpiringWarrantee($threshold); $assets = Asset::getExpiringWarrantee($threshold);
if ($assets->count() > 0) { if ($assets->count() > 0) {
$this->info('expiring warrantees');
$this->info(trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count' => $assets->count(), 'threshold' => $threshold])); $this->info(trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count' => $assets->count(), 'threshold' => $threshold]));
\Notification::send($recipients, new ExpiringAssetsNotification($assets, $threshold)); Notification::send($recipients, new ExpiringAssetsNotification($assets, $threshold));
} }
// Expiring licenses // Expiring licenses
$licenses = License::getExpiringLicenses($threshold); $licenses = License::getExpiringLicenses($threshold);
if ($licenses->count() > 0) { if ($licenses->count() > 0) {
$this->info('expiring licenses');
$this->info(trans_choice('mail.license_expiring_alert', $licenses->count(), ['count' => $licenses->count(), 'threshold' => $threshold])); $this->info(trans_choice('mail.license_expiring_alert', $licenses->count(), ['count' => $licenses->count(), 'threshold' => $threshold]));
\Notification::send($recipients, new ExpiringLicenseNotification($licenses, $threshold)); Notification::send($recipients, new ExpiringLicenseNotification($licenses, $threshold));
} }
} else { } else {
if ($settings->alert_email == '') { if ($settings->alert_email == '') {
@ -71,5 +74,6 @@ class SendExpirationAlerts extends Command
$this->info('Alerts are disabled in the settings. No mail will be sent'); $this->info('Alerts are disabled in the settings. No mail will be sent');
} }
} }
$this->info('nothing here.');
} }
} }

View file

@ -1093,28 +1093,41 @@ class ReportsController extends Controller
$this->authorize('reports.view'); $this->authorize('reports.view');
$showDeleted = $deleted == 'deleted'; $showDeleted = $deleted == 'deleted';
/** $assetsForReport = collect();
* Get all assets with pending checkout acceptances
*/ $query = CheckoutAcceptance::pending()
if($showDeleted) { ->where('checkoutable_type', 'App\Models\Asset')
$acceptances = CheckoutAcceptance::pending()->where('checkoutable_type', 'App\Models\Asset')->withTrashed()->with(['assignedTo' , 'checkoutable.assignedTo', 'checkoutable.model'])->get(); ->with(['checkoutable.assignedTo', 'checkoutable.model']); // Eager load common relationships
if ($showDeleted) {
$query->withTrashed()->with(['assignedTo']);
} else { } else {
$acceptances = CheckoutAcceptance::pending()->where('checkoutable_type', 'App\Models\Asset')->with(['assignedTo' => function ($query) { $query->with(['assignedTo' => function ($query) {
$query->withTrashed(); $query->withTrashed();
}, 'checkoutable.assignedTo', 'checkoutable.model'])->get(); }]);
} }
$assetsForReport = $acceptances // Process records in chunks
->filter(function ($acceptance) { $query->chunk(100, function ($chunk) use (&$assetsForReport) {
$filtered = $chunk->filter(function ($acceptance) {
$acceptance_checkoutable_flag = false; $acceptance_checkoutable_flag = false;
if ($acceptance->checkoutable){
$acceptance_checkoutable_flag = $acceptance->checkoutable->checkedOutToUser();
}
return $acceptance->checkoutable_type == 'App\Models\Asset' && $acceptance_checkoutable_flag; if($acceptance->checkoutable) {
}) $acceptance_checkoutable_flag = $acceptance->assignedTo;
->map(function($acceptance) { }
return ['assetItem' => $acceptance->checkoutable, 'acceptance' => $acceptance]; // Return true if criteria match
return $acceptance->checkoutable_type === 'App\Models\Asset' && $acceptance_checkoutable_flag;
})->map(function ($acceptance) {
return [
'assetItem' => $acceptance->checkoutable,
'acceptance' => $acceptance,
];
});
// Merge results into the main collection
$assetsForReport = $assetsForReport->merge($filtered);
}); });
return view('reports/unaccepted_assets', compact('assetsForReport','showDeleted' )); return view('reports/unaccepted_assets', compact('assetsForReport','showDeleted' ));

View file

@ -80,7 +80,7 @@ class AssetObserver
{ {
if ($settings = Setting::getSettings()) { if ($settings = Setting::getSettings()) {
$tag = $asset->asset_tag; $tag = $asset->asset_tag;
$prefix = $settings->auto_increment_prefix; $prefix = (string)($settings->auto_increment_prefix ?? '');
$number = substr($tag, strlen($prefix)); $number = substr($tag, strlen($prefix));
// IF - auto_increment_assets is on, AND (there is no prefix OR the prefix matches the start of the tag) // IF - auto_increment_assets is on, AND (there is no prefix OR the prefix matches the start of the tag)
// AND the rest of the string after the prefix is all digits, THEN... // AND the rest of the string after the prefix is all digits, THEN...