diff --git a/app/Console/Commands/CheckoutLicenseToAllUsers.php b/app/Console/Commands/CheckoutLicenseToAllUsers.php new file mode 100644 index 000000000..f87a9cfa2 --- /dev/null +++ b/app/Console/Commands/CheckoutLicenseToAllUsers.php @@ -0,0 +1,103 @@ +option('license_id'); + $notify = $this->option('notify'); + + if (!$license_id) { + $this->error('ERROR: License ID is required.'); + return false; + } + + + if (!$license = License::find($license_id)->first()) { + $this->error('Invalid license ID'); + return false; + } + $users = User::whereNull('deleted_at')->get(); + + if ($users->count() > $license->getAvailSeatsCountAttribute()) { + $this->info('You do not have enough free seats to complete this task, so we will check out as many as we can. '); + } + + $this->info('Checking out '.$users->count().' of '.$license->getAvailSeatsCountAttribute().' seats for '.$license->name); + + if (!$notify) { + $this->info('No mail will be sent.'); + } + + foreach ($users as $user) { + // If the license is valid, check that there is an available seat + if ($license->getAvailSeatsCountAttribute() < 1) { + $this->error('ERROR: No available seats'); + return false; + } + + // Get the seat ID + $next = $license->freeSeat(); + if (!$licenseSeat = LicenseSeat::where('id', '=', $next->id)->first()) { + $this->error('ERROR: No available seats'); + return false; + } + + // Update the seat with checkout info, + $licenseSeat->assigned_to = $user->id; + if ($licenseSeat->save()) { + + // Temporarily null the user's email address so we don't send mail if we're not supposed to + if (!$notify) { + $user->email = null; + } + + // Log the checkout + $licenseSeat->logCheckout('Checked out via cli tool', $user); + $this->info('License '.$license_id.' checked out to '.$user->username); + } + + } + + + + } +}