Skip to content

A free open source IT asset/license management system

Refactor alert query

snipe13c0d335d3aa
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 13a721c136..37c8f34f33 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -825,19 +825,24 @@ class Asset extends Depreciable
*/
public static function getExpiringWarrantee($days = 30)
{
- $days = (is_null($days)) ? 30 : $days;
- return self::where('archived', '=', '0') // this can stay for right now, as `archived` defaults to 0 at the db level, but should probably be replaced with assetstatus->archived?
- ->whereNotNull('warranty_months')
- ->whereNotNull('purchase_date')
- ->whereNull('deleted_at')
+ return self::where('archived', '=', '0')
->NotArchived()
- ->whereRaw(
- 'DATE_ADD(`purchase_date`, INTERVAL `warranty_months` MONTH) <= DATE_ADD(NOW(), INTERVAL '
- . $days
- . ' DAY) AND DATE_ADD(`purchase_date`, INTERVAL `warranty_months` MONTH) > NOW()'
- )
- ->orderByRaw('DATE_ADD(`purchase_date`,INTERVAL `warranty_months` MONTH)')
+ ->whereNull('deleted_at')
+
+ // Check for manual asset EOL first
+ ->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')
+ ->whereDate('purchase_date', '<=', Carbon::now()->addMonths('assets.warranty_months')->addDays($days));
+ })
+ ->orderBy('asset_eol_date', 'ASC')
+ ->orderBy('purchase_date', 'ASC')
->get();
}
diff --git a/app/Models/License.php b/app/Models/License.php
index d6621a5277..d0a0faf8d9 100755
--- a/app/Models/License.php
+++ b/app/Models/License.php
@@ -708,24 +708,45 @@ class License extends Depreciable
}
/**
- * Returns expiring licenses
+ * Returns expiring licenses.
*
- * @todo should refactor. I don't like get() in model methods
+ * This checks if:
+ *
+ * 1) The license has not been deleted
+ * 2) The expiration date is between now and the number of days specified
+ * 3) There is an expiration date set and the termination date has not passed
+ * 4) The license termination date is null or has not passed
*
* @author A. Gianotto <[email protected]>
* @since [v1.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
+ * @see \App\Console\Commands\SendExpiringLicenseNotifications
*/
public static function getExpiringLicenses($days = 60)
{
- $days = (is_null($days)) ? 60 : $days;
- return self::whereNotNull('expiration_date')
- ->whereNull('deleted_at')
- ->whereRaw('DATE_SUB(`expiration_date`,INTERVAL '.$days.' DAY) <= DATE(NOW()) ')
- ->where('expiration_date', '>', date('Y-m-d'))
- ->where('termination_date', '>', date('Y-m-d'))
+ return self::whereNull('deleted_at')
+
+ // The termination date is null or within range
+ ->where(function ($query) use ($days) {
+ $query->whereNull('termination_date')
+ ->orWhereBetween('termination_date', [Carbon::now(), Carbon::now()->addDays($days)]);
+ })
+ ->where(function ($query) use ($days) {
+ $query->whereNotNull('expiration_date')
+ // Handle expired licenses without termination dates
+ ->where(function ($query) use ($days) {
+ $query->whereNull('termination_date')
+ ->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addDays($days)]);
+ })
+
+ // Handle expired licenses with termination dates in the future
+ ->orWhere(function ($query) use ($days) {
+ $query->whereBetween('termination_date', [Carbon::now(), Carbon::now()->addDays($days)]);
+ });
+ })
->orderBy('expiration_date', 'ASC')
+ ->orderBy('termination_date', 'ASC')
->get();
}