2020-07-09 20:04:05 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
use App\Models\License;
|
2020-07-09 20:04:05 -07:00
|
|
|
use App\Models\LicenseSeat;
|
|
|
|
use App\Models\User;
|
2021-06-10 13:15:52 -07:00
|
|
|
use Illuminate\Console\Command;
|
2020-07-09 20:04:05 -07:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class CheckoutLicenseToAllUsers extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'snipeit:checkout-to-all {--license_id=} {--notify}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-11-15 12:50:46 -08:00
|
|
|
protected $description = 'Checks out licenses to all users';
|
2020-07-09 20:04:05 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$license_id = $this->option('license_id');
|
|
|
|
$notify = $this->option('notify');
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $license_id) {
|
|
|
|
$this->error('ERROR: License ID is required.');
|
2020-07-09 20:04:05 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-09 20:04:05 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $license = License::where('id', '=', $license_id)->with('assignedusers')->first()) {
|
2020-07-09 20:04:05 -07:00
|
|
|
$this->error('Invalid license ID');
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-07-09 20:04:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-09 21:12:50 -07:00
|
|
|
|
2023-04-24 17:26:06 -07:00
|
|
|
$users = User::whereNull('deleted_at')->where('autoassign_licenses', '=', 1)->with('licenses')->get();
|
2020-07-09 20:04:05 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $notify) {
|
2020-07-09 20:04:05 -07:00
|
|
|
$this->info('No mail will be sent.');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($users as $user) {
|
2020-07-09 21:12:50 -07:00
|
|
|
|
|
|
|
// Check to make sure this user doesn't already have this license checked out
|
|
|
|
// to them
|
|
|
|
|
|
|
|
if ($user->licenses->where('id', '=', $license_id)->count()) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->info($user->username.' already has this license checked out to them. Skipping... ');
|
2020-07-09 21:12:50 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-09 20:04:05 -07:00
|
|
|
// If the license is valid, check that there is an available seat
|
2020-07-09 20:43:13 -07:00
|
|
|
if ($license->availCount()->count() < 1) {
|
2020-07-09 20:04:05 -07:00
|
|
|
$this->error('ERROR: No available seats');
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-07-09 20:04:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-09 20:43:13 -07:00
|
|
|
$this->info($license->availCount()->count().' seats left');
|
2020-07-09 20:04:05 -07:00
|
|
|
// Get the seat ID
|
2020-07-09 20:26:02 -07:00
|
|
|
$licenseSeat = $license->freeSeat();
|
|
|
|
|
2020-07-09 20:04:05 -07:00
|
|
|
// Update the seat with checkout info,
|
2021-06-10 13:15:52 -07:00
|
|
|
$licenseSeat->assigned_to = $user->id;
|
2020-07-09 20:04:05 -07:00
|
|
|
if ($licenseSeat->save()) {
|
|
|
|
|
|
|
|
// Temporarily null the user's email address so we don't send mail if we're not supposed to
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $notify) {
|
2020-07-09 20:04:05 -07:00
|
|
|
$user->email = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log the checkout
|
|
|
|
$licenseSeat->logCheckout('Checked out via cli tool', $user);
|
2020-07-09 20:43:13 -07:00
|
|
|
$this->info('License '.$license_id.' seat '.$licenseSeat->id.' checked out to '.$user->username);
|
2020-07-09 20:04:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|