|
3 | 3 | namespace App\Console\Commands; |
4 | 4 |
|
5 | 5 | use App\Mail\UnacceptedAssetReminderMail; |
| 6 | +use App\Models\Accessory; |
6 | 7 | use App\Models\Asset; |
7 | 8 | use App\Models\CheckoutAcceptance; |
| 9 | +use App\Models\Component; |
| 10 | +use App\Models\Consumable; |
| 11 | +use App\Models\LicenseSeat; |
8 | 12 | use App\Models\Setting; |
9 | 13 | use App\Models\User; |
10 | 14 | use App\Notifications\CheckoutAssetNotification; |
11 | 15 | use App\Notifications\CurrentInventory; |
12 | 16 | use Illuminate\Console\Command; |
| 17 | +use Illuminate\Database\Eloquent\Relations\MorphTo; |
13 | 18 | use Illuminate\Support\Facades\Mail; |
14 | 19 |
|
15 | 20 | class SendAcceptanceReminder extends Command |
@@ -45,19 +50,30 @@ public function __construct() |
45 | 50 | */ |
46 | 51 | public function handle() |
47 | 52 | { |
48 | | - $pending = CheckoutAcceptance::pending()->where('checkoutable_type', 'App\Models\Asset') |
49 | | - ->whereHas('checkoutable', function($query) { |
50 | | - $query->where('accepted_at', null) |
51 | | - ->where('declined_at', null); |
52 | | - }) |
53 | | - ->with(['assignedTo', 'checkoutable.assignedTo', 'checkoutable.model', 'checkoutable.adminuser']) |
54 | | - ->get(); |
| 53 | + $pending = CheckoutAcceptance::query() |
| 54 | + ->with([ |
| 55 | + 'checkoutable' => function (MorphTo $morph) { |
| 56 | + $morph->morphWith([ |
| 57 | + Asset::class => ['model.category', 'assignedTo', 'adminuser', 'company', 'checkouts'], |
| 58 | + Accessory::class => ['category', 'company', 'checkouts'], |
| 59 | + LicenseSeat::class => ['user', 'license', 'checkouts'], |
| 60 | + Component::class => ['assignedTo', 'company', 'checkouts'], |
| 61 | + Consumable::class => ['company', 'checkouts'], |
| 62 | + ]); |
| 63 | + }, |
| 64 | + 'assignedTo', |
| 65 | + ]) |
| 66 | + ->whereHasMorph( |
| 67 | + 'checkoutable', |
| 68 | + [Asset::class, Accessory::class, LicenseSeat::class, Component::class, Consumable::class], |
| 69 | + fn ($q) => $q->whereNull('accepted_at') |
| 70 | + ->whereNull('declined_at') |
| 71 | + ) |
| 72 | + ->pending() |
| 73 | + ->get(); |
55 | 74 |
|
56 | 75 | $count = 0; |
57 | 76 | $unacceptedAssetGroups = $pending |
58 | | - ->filter(function($acceptance) { |
59 | | - return $acceptance->checkoutable_type == 'App\Models\Asset'; |
60 | | - }) |
61 | 77 | ->map(function($acceptance) { |
62 | 78 | return ['assetItem' => $acceptance->checkoutable, 'acceptance' => $acceptance]; |
63 | 79 | }) |
|
0 commit comments