|
14 | 14 | use Illuminate\Support\Facades\Session; |
15 | 15 | use Watson\Validating\ValidatingTrait; |
16 | 16 |
|
| 17 | + |
17 | 18 | class License extends Depreciable |
18 | 19 | { |
19 | 20 | use HasFactory; |
@@ -296,6 +297,18 @@ public function setTerminationDateAttribute($value) |
296 | 297 | } |
297 | 298 | $this->attributes['termination_date'] = $value; |
298 | 299 | } |
| 300 | + |
| 301 | + public function isInactive(): bool |
| 302 | +{ |
| 303 | + $day = now()->startOfDay(); |
| 304 | + |
| 305 | + $expired = $this->expiration_date && $this->asDateTime($this->expiration_date)->startofDay()->lessThanOrEqualTo($day); |
| 306 | + |
| 307 | + $terminated = $this->termination_date && $this->asDateTime($this->termination_date)->startofDay()->lessThanOrEqualTo($day); |
| 308 | + |
| 309 | + |
| 310 | + return $expired || $terminated; |
| 311 | +} |
299 | 312 | /** |
300 | 313 | * Sets free_seat_count attribute |
301 | 314 | * |
@@ -596,7 +609,7 @@ public static function unReassignableCount($license) : int |
596 | 609 | { |
597 | 610 | $count = 0; |
598 | 611 | if (!$license->reassignable) { |
599 | | - $count = licenseSeat::query()->where('unreassignable_seat', '=', true) |
| 612 | + $count = LicenseSeat::query()->where('unreassignable_seat', '=', true) |
600 | 613 | ->where('license_id', '=', $license->id) |
601 | 614 | ->count(); |
602 | 615 | } |
@@ -695,23 +708,45 @@ public function freeSeats() |
695 | 708 | } |
696 | 709 |
|
697 | 710 | /** |
698 | | - * Returns expiring licenses |
| 711 | + * Returns expiring licenses. |
| 712 | + * |
| 713 | + * This checks if: |
699 | 714 | * |
700 | | - * @todo should refactor. I don't like get() in model methods |
| 715 | + * 1) The license has not been deleted |
| 716 | + * 2) The expiration date is between now and the number of days specified |
| 717 | + * 3) There is an expiration date set and the termination date has not passed |
| 718 | + * 4) The license termination date is null or has not passed |
701 | 719 | * |
702 | 720 | * @author A. Gianotto <[email protected]> |
703 | 721 | * @since [v1.0] |
704 | 722 | * @return \Illuminate\Database\Eloquent\Relations\Relation |
| 723 | + * @see \App\Console\Commands\SendExpiringLicenseNotifications |
705 | 724 | */ |
706 | 725 | public static function getExpiringLicenses($days = 60) |
707 | 726 | { |
708 | | - $days = (is_null($days)) ? 60 : $days; |
709 | 727 |
|
710 | | - return self::whereNotNull('expiration_date') |
711 | | - ->whereNull('deleted_at') |
712 | | - ->whereRaw('DATE_SUB(`expiration_date`,INTERVAL '.$days.' DAY) <= DATE(NOW()) ') |
713 | | - ->where('expiration_date', '>', date('Y-m-d')) |
| 728 | + return self::whereNull('deleted_at') |
| 729 | + |
| 730 | + // The termination date is null or within range |
| 731 | + ->where(function ($query) use ($days) { |
| 732 | + $query->whereNull('termination_date') |
| 733 | + ->orWhereBetween('termination_date', [Carbon::now(), Carbon::now()->addDays($days)]); |
| 734 | + }) |
| 735 | + ->where(function ($query) use ($days) { |
| 736 | + $query->whereNotNull('expiration_date') |
| 737 | + // Handle expired licenses without termination dates |
| 738 | + ->where(function ($query) use ($days) { |
| 739 | + $query->whereNull('termination_date') |
| 740 | + ->whereBetween('expiration_date', [Carbon::now(), Carbon::now()->addDays($days)]); |
| 741 | + }) |
| 742 | + |
| 743 | + // Handle expired licenses with termination dates in the future |
| 744 | + ->orWhere(function ($query) use ($days) { |
| 745 | + $query->whereBetween('termination_date', [Carbon::now(), Carbon::now()->addDays($days)]); |
| 746 | + }); |
| 747 | + }) |
714 | 748 | ->orderBy('expiration_date', 'ASC') |
| 749 | + ->orderBy('termination_date', 'ASC') |
715 | 750 | ->get(); |
716 | 751 | } |
717 | 752 |
|
|
0 commit comments