Skip to content

Commit 99549ce

Browse files
committed
rewrite query for expired warranties on assets, concat queries"
1 parent e2019a1 commit 99549ce

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

app/Models/Asset.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ public static function getExpiringWarrantyOrEol($days = 30)
939939
{
940940
$now = now();
941941
$end = now()->addDays($days);
942+
942943
$expired_assets = self::query()
943944
->where('archived', '=', '0')
944945
->NotArchived()
@@ -947,27 +948,26 @@ public static function getExpiringWarrantyOrEol($days = 30)
947948
->whereBetween('asset_eol_date', [$now, $end])
948949
->get();
949950

950-
// return self::where('archived', '=', '0')
951-
// ->NotArchived()
952-
// ->whereNull('deleted_at')
953-
// ->where(function ($query) use ($days) {
954-
// // Check for manual asset EOL first
955-
// $query->where(function ($query) use ($days) {
956-
// $query->whereNotNull('asset_eol_date')
957-
// ->whereBetween('asset_eol_date', [Carbon::now(), Carbon::now()->addDays($days)]);
958-
// // Otherwise use the warranty months + purchase date + threshold
959-
// })->orWhere(function ($query) use ($days) {
960-
// $query->whereNotNull('purchase_date')
961-
// ->whereNotNull('warranty_months')
962-
// ->whereRaw(
963-
// 'DATE_ADD(purchase_date, INTERVAL warranty_months MONTH) BETWEEN ? AND ?',
964-
// [now(), now()->addDays($days)]
965-
// );
966-
// });
967-
// })
968-
// ->orderBy('asset_eol_date', 'ASC')
969-
// ->orderBy('purchase_date', 'ASC')
970-
// ->get();
951+
$assets_with_warranties = self::query()
952+
->where('archived', '=', '0')
953+
->NotArchived()
954+
->whereNull('deleted_at')
955+
->whereNotNull('purchase_date')
956+
->whereNotNull('warranty_months')
957+
->get();
958+
959+
$expired_warranties = $assets_with_warranties->filter(function ($asset) use ($now, $end) {
960+
$expiration_window = Carbon::parse($asset->purchase_date)->addMonths((int) $asset->warranty_months);
961+
962+
return $expiration_window->betweenIncluded($now, $end);
963+
});
964+
return $expired_assets->concat($expired_warranties)
965+
->unique('id')
966+
->sortBy([
967+
['asset_eol_date', 'ASC'],
968+
['purchase_date', 'ASC']
969+
])
970+
->values();
971971
}
972972

973973

0 commit comments

Comments
 (0)