88use App \Models \Traits \Searchable ;
99use App \Presenters \Presentable ;
1010use Carbon \Carbon ;
11+ use Illuminate \Database \Eloquent \Casts \Attribute ;
1112use Illuminate \Database \Eloquent \Factories \HasFactory ;
1213use Illuminate \Database \Eloquent \SoftDeletes ;
1314use Illuminate \Support \Facades \DB ;
@@ -156,6 +157,29 @@ function ($license) {
156157 );
157158 }
158159
160+
161+ protected function terminatesFormattedDate (): Attribute
162+ {
163+ return Attribute:: make (
164+ get: fn (mixed $ value , array $ attributes ) => $ attributes ['termination_date ' ] ? Helper::getFormattedDateObject ($ attributes ['termination_date ' ], 'date ' , false ) : null ,
165+ );
166+ }
167+
168+ protected function terminatesDiffInDays (): Attribute
169+ {
170+ return Attribute:: make (
171+ get: fn (mixed $ value , array $ attributes ) => $ attributes ['termination_date ' ] ? Carbon::now ()->diffInDays ($ attributes ['termination_date ' ]) : null ,
172+ );
173+ }
174+
175+ protected function terminatesDiffForHumans (): Attribute
176+ {
177+ return Attribute:: make (
178+ get: fn (mixed $ value , array $ attributes ) => $ attributes ['termination_date ' ] ? Carbon::parse ($ attributes ['termination_date ' ])->diffForHumans () : null ,
179+ );
180+ }
181+
182+
159183 public function prepareLimitChangeRule ($ parameters , $ field )
160184 {
161185 $ actual_seat_count = $ this ->licenseseats ()->count (); //we use the *actual* seat count here, in case your license has gone wonky
@@ -706,8 +730,41 @@ public function freeSeats()
706730 return $ this ->hasMany (\App \Models \LicenseSeat::class)->whereNull ('assigned_to ' )->whereNull ('deleted_at ' )->whereNull ('asset_id ' );
707731 }
708732
733+ public function scopeActiveLicenses ($ query )
734+ {
735+
736+ return $ query ->whereNull ('licenses.deleted_at ' )
737+
738+ // The termination date is null or within range
739+ ->where (function ($ query ) {
740+ $ query ->whereNull ('termination_date ' )
741+ ->orWhereDate ('termination_date ' , '> ' , [Carbon::now ()]);
742+ })
743+ ->where (function ($ query ) {
744+ $ query ->whereNull ('expiration_date ' )
745+ ->orWhereDate ('expiration_date ' , '> ' , [Carbon::now ()]);
746+ });
747+ }
748+
749+ /**
750+ * Expiried/terminated licenses scope
751+ *
752+ * @author A. Gianotto <[email protected] > 753+ * @since [v1.0]
754+ * @return \Illuminate\Database\Eloquent\Relations\Relation
755+ * @see \App\Console\Commands\SendExpiringLicenseNotifications
756+ */
757+ public function scopeExpiredLicenses ($ query )
758+ {
759+ return $ query ->whereDate ('termination_date ' , '<= ' , Carbon::now ())// The termination date is null or within range
760+ ->orWhere (function ($ query ) {
761+ $ query ->whereDate ('expiration_date ' , '<= ' , Carbon::now ());
762+ })
763+ ->whereNull ('deleted_at ' );
764+ }
765+
709766 /**
710- * Returns expiring licenses.
767+ * Expiring/terminating licenses scope
711768 *
712769 * This checks if:
713770 *
@@ -721,63 +778,25 @@ public function freeSeats()
721778 * @return \Illuminate\Database\Eloquent\Relations\Relation
722779 * @see \App\Console\Commands\SendExpiringLicenseNotifications
723780 */
724- public static function getExpiringLicenses ( $ days = 60 )
781+ public function scopeExpiringLicenses ( $ query , $ days = 60 )
725782 {
726-
727- return self ::whereNull ('licenses.deleted_at ' )
728-
729- // The termination date is null or within range
730- ->where (function ($ query ) use ($ days ) {
731- $ query ->whereNull ('termination_date ' )
732- ->orWhereBetween ('termination_date ' , [Carbon::now (), Carbon::now ()->addDays ($ days )]);
733- })
783+ return $ query// The termination date is null or within range
784+ ->where (function ($ query ) use ($ days ) {
785+ $ query ->whereNull ('termination_date ' )
786+ ->orWhereBetween ('termination_date ' , [Carbon::now (), Carbon::now ()->addDays ($ days )]);
787+ })
734788 ->where (function ($ query ) use ($ days ) {
735789 $ query ->whereNotNull ('expiration_date ' )
736- // Handle expired licenses without termination dates
790+ // Handle expiring licenses without termination dates
737791 ->where (function ($ query ) use ($ days ) {
738792 $ query ->whereNull ('termination_date ' )
739793 ->whereBetween ('expiration_date ' , [Carbon::now (), Carbon::now ()->addDays ($ days )]);
740794 })
741795
742- // Handle expired licenses with termination dates in the future
796+ // Handle expiring licenses with termination dates in the future
743797 ->orWhere (function ($ query ) use ($ days ) {
744798 $ query ->whereBetween ('termination_date ' , [Carbon::now (), Carbon::now ()->addDays ($ days )]);
745799 });
746- })
747- ->orderBy ('expiration_date ' , 'ASC ' )
748- ->orderBy ('termination_date ' , 'ASC ' )
749- ->get ();
750- }
751-
752- public function scopeActiveLicenses ($ query )
753- {
754-
755- return $ query ->whereNull ('licenses.deleted_at ' )
756-
757- // The termination date is null or within range
758- ->where (function ($ query ) {
759- $ query ->whereNull ('termination_date ' )
760- ->orWhereDate ('termination_date ' , '> ' , [Carbon::now ()]);
761- })
762- ->where (function ($ query ) {
763- $ query ->whereNull ('expiration_date ' )
764- ->orWhereDate ('expiration_date ' , '> ' , [Carbon::now ()]);
765- });
766- }
767-
768- public function scopeExpiredLicenses ($ query )
769- {
770-
771- return $ query ->whereNull ('licenses.deleted_at ' )
772-
773- // The termination date is null or within range
774- ->where (function ($ query ) {
775- $ query ->whereNull ('termination_date ' )
776- ->orWhereDate ('termination_date ' , '<= ' , [Carbon::now ()]);
777- })
778- ->orWhere (function ($ query ) {
779- $ query ->whereNull ('expiration_date ' )
780- ->orWhereDate ('expiration_date ' , '<= ' , [Carbon::now ()]);
781800 });
782801 }
783802
0 commit comments