diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.php index 6f2ae003ca..ef637d3ecc 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php @@ -126,4 +126,64 @@ class LicenseCheckoutController extends Controller return false; } + + /** + * Bulk checkin all license seats + * + * @author [A. Gianotto] [] + * @see LicenseCheckinController::create() method that provides the form view + * @since [v6.1.1] + * @return \Illuminate\Http\RedirectResponse + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + + public function bulkCheckout($licenseId) { + + \Log::debug('checking out '.$licenseId.' via bulk'); + $license = License::findOrFail($licenseId); + $this->authorize('checkin', $license); + + $users = User::whereNull('deleted_at')->where('autoassign_licenses', '=', 1)->with('licenses')->get(); + \Log::debug($users->count().' will be assigned'); + + if ($users->count() > $license->getAvailSeatsCountAttribute()) { + \Log::debug('You do not have enough free seats to complete this task, so we will check out as many as we can. '); + } + + $count = 0; + foreach ($users as $user) { + + // Check to make sure this user doesn't already have this license checked out + // to them + + if ($user->licenses->where('id', '=', $licenseId)->count()) { + \Log::debug($user->username.' already has this license checked out to them. Skipping... '); + continue; + } + + // If the license is valid, check that there is an available seat + if ($license->availCount()->count() < 1) { + return redirect()->back()->with('error', 'No more available seats'); + } + + $licenseSeat = $license->freeSeat(); + + // Update the seat with checkout info, + $licenseSeat->assigned_to = $user->id; + + if ($licenseSeat->save()) { + $count++; + \Log::debug('License seat '.$licenseSeat.' is now assigned to '.$licenseSeat->assigned_to); + $licenseSeat->logCheckout('Checked out bulk license checkout in license GUI', $user); + \Log::debug('License '.$licenseId.' seat '.$licenseSeat->id.' checked out to '.$user->username); + } + } + + if ($count == 0) { + return redirect()->back()->with('warning', 'No error was encountered, but there were no eligible users to checkout to.'); + } + + return redirect()->back()->with('success', 'Licenses checked out successfully to '.$count.' users!'); + + } }