rewrite query for expired warranties on assets, concat queries"

This commit is contained in:
Godfrey M
2025-10-23 11:01:51 -07:00
parent e2019a13ab
commit 99549ce805

View File

@@ -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();
}