Skip to content

A free open source IT asset/license management system

Refactored scope

snipedfd05e8b5b89
diff --git a/app/Models/License.php b/app/Models/License.php
index cf59ac9aec..09bc0b3038 100755
--- a/app/Models/License.php
+++ b/app/Models/License.php
@@ -157,6 +157,21 @@ class License extends Depreciable
);
}
+
+ protected function terminatesFormattedDate(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $attributes['termination_date'] ? Helper::getFormattedDateObject($attributes['termination_date'], 'date', false) : null,
+ );
+ }
+
+ protected function terminatesDiffForHumans(): Attribute
+ {
+ return Attribute:: make(
+ get: fn(mixed $value, array $attributes) => $attributes['termination_date'] ? Carbon::parse($attributes['termination_date'])->diffForHumans() : null,
+ );
+ }
+
public function prepareLimitChangeRule($parameters, $field)
{
$actual_seat_count = $this->licenseseats()->count(); //we use the *actual* seat count here, in case your license has gone wonky
@@ -707,8 +722,24 @@ class License extends Depreciable
return $this->hasMany(\App\Models\LicenseSeat::class)->whereNull('assigned_to')->whereNull('deleted_at')->whereNull('asset_id');
}
+ public function scopeActiveLicenses($query)
+ {
+
+ return $query->whereNull('licenses.deleted_at')
+
+ // The termination date is null or within range
+ ->where(function ($query) {
+ $query->whereNull('termination_date')
+ ->orWhereDate('termination_date', '>', [Carbon::now()]);
+ })
+ ->where(function ($query) {
+ $query->whereNull('expiration_date')
+ ->orWhereDate('expiration_date', '>', [Carbon::now()]);
+ });
+ }
+
/**
- * Returns expiring licenses.
+ * Expiring/terminating licenses scope
*
* This checks if:
*
@@ -722,16 +753,13 @@ class License extends Depreciable
* @return \Illuminate\Database\Eloquent\Relations\Relation
* @see \App\Console\Commands\SendExpiringLicenseNotifications
*/
- public static function getExpiringLicenses($days = 60)
+ public function scopeExpiredLicenses($query, $days = 60)
{
-
- return self::whereNull('licenses.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)]);
- })
+ return $query// 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
@@ -746,40 +774,7 @@ class License extends Depreciable
});
})
->orderBy('expiration_date', 'ASC')
- ->orderBy('termination_date', 'ASC')
- ->get();
- }
-
- public function scopeActiveLicenses($query)
- {
-
- return $query->whereNull('licenses.deleted_at')
-
- // The termination date is null or within range
- ->where(function ($query) {
- $query->whereNull('termination_date')
- ->orWhereDate('termination_date', '>', [Carbon::now()]);
- })
- ->where(function ($query) {
- $query->whereNull('expiration_date')
- ->orWhereDate('expiration_date', '>', [Carbon::now()]);
- });
- }
-
- public function scopeExpiredLicenses($query)
- {
-
- return $query->whereNull('licenses.deleted_at')
-
- // The termination date is null or within range
- ->where(function ($query) {
- $query->whereNull('termination_date')
- ->orWhereDate('termination_date', '<=', [Carbon::now()]);
- })
- ->orWhere(function ($query) {
- $query->whereNull('expiration_date')
- ->orWhereDate('expiration_date', '<=', [Carbon::now()]);
- });
+ ->orderBy('termination_date', 'ASC');
}
/**