@@ -937,25 +937,37 @@ public function model()
937937 */
938938 public static function getExpiringWarrantyOrEol ($ days = 30 )
939939 {
940-
941- return self ::where ('archived ' , '= ' , '0 ' )
940+ $ now = now ();
941+ $ end = now ()->addDays ($ days );
942+
943+ $ expired_assets = self ::query ()
944+ ->where ('archived ' , '= ' , '0 ' )
945+ ->NotArchived ()
946+ ->whereNull ('deleted_at ' )
947+ ->whereNotNull ('asset_eol_date ' )
948+ ->whereBetween ('asset_eol_date ' , [$ now , $ end ])
949+ ->get ();
950+
951+ $ assets_with_warranties = self ::query ()
952+ ->where ('archived ' , '= ' , '0 ' )
942953 ->NotArchived ()
943954 ->whereNull ('deleted_at ' )
944- ->where (function ($ query ) use ($ days ) {
945- // Check for manual asset EOL first
946- $ query ->where (function ($ query ) use ($ days ) {
947- $ query ->whereNotNull ('asset_eol_date ' )
948- ->whereBetween ('asset_eol_date ' , [Carbon::now (), Carbon::now ()->addDays ($ days )]);
949- // Otherwise use the warranty months + purchase date + threshold
950- })->orWhere (function ($ query ) use ($ days ) {
951- $ query ->whereNotNull ('purchase_date ' )
952- ->whereNotNull ('warranty_months ' )
953- ->whereBetween ('purchase_date ' , [Carbon::now (), Carbon::now ()->addMonths ('assets.warranty_months ' )->addDays ($ days )]);
954- });
955- })
956- ->orderBy ('asset_eol_date ' , 'ASC ' )
957- ->orderBy ('purchase_date ' , 'ASC ' )
955+ ->whereNotNull ('purchase_date ' )
956+ ->whereNotNull ('warranty_months ' )
958957 ->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 ();
959971 }
960972
961973
0 commit comments