Skip to content

A free open source IT asset/license management system

rewrite query for expired warranties on assets, concat queries"

Godfrey M99549ce80595
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index f68e797d5f..3d5971d5b8 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -939,6 +939,7 @@ class Asset extends Depreciable
{
$now = now();
$end = now()->addDays($days);
+
$expired_assets = self::query()
->where('archived', '=', '0')
->NotArchived()
@@ -947,27 +948,26 @@ class Asset extends Depreciable
->whereBetween('asset_eol_date', [$now, $end])
->get();
-// return self::where('archived', '=', '0')
-// ->NotArchived()
-// ->whereNull('deleted_at')
-// ->where(function ($query) use ($days) {
-// // Check for manual asset EOL first
-// $query->where(function ($query) use ($days) {
-// $query->whereNotNull('asset_eol_date')
-// ->whereBetween('asset_eol_date', [Carbon::now(), Carbon::now()->addDays($days)]);
-// // Otherwise use the warranty months + purchase date + threshold
-// })->orWhere(function ($query) use ($days) {
-// $query->whereNotNull('purchase_date')
-// ->whereNotNull('warranty_months')
-// ->whereRaw(
-// 'DATE_ADD(purchase_date, INTERVAL warranty_months MONTH) BETWEEN ? AND ?',
-// [now(), now()->addDays($days)]
-// );
-// });
-// })
-// ->orderBy('asset_eol_date', 'ASC')
-// ->orderBy('purchase_date', 'ASC')
-// ->get();
+ $assets_with_warranties = self::query()
+ ->where('archived', '=', '0')
+ ->NotArchived()
+ ->whereNull('deleted_at')
+ ->whereNotNull('purchase_date')
+ ->whereNotNull('warranty_months')
+ ->get();
+
+ $expired_warranties = $assets_with_warranties->filter(function ($asset) use ($now, $end) {
+ $expiration_window = Carbon::parse($asset->purchase_date)->addMonths((int) $asset->warranty_months);
+
+ return $expiration_window->betweenIncluded($now, $end);
+ });
+ return $expired_assets->concat($expired_warranties)
+ ->unique('id')
+ ->sortBy([
+ ['asset_eol_date', 'ASC'],
+ ['purchase_date', 'ASC']
+ ])
+ ->values();
}