2018-07-24 19:35:26 -07:00
< ? php
namespace App\Http\Controllers\Licenses ;
2018-09-10 07:40:26 -07:00
use App\Events\CheckoutableCheckedOut ;
2019-03-13 20:12:03 -07:00
use App\Http\Controllers\Controller ;
2018-07-24 19:35:26 -07:00
use App\Http\Requests\LicenseCheckoutRequest ;
2023-11-23 08:18:28 -08:00
use App\Models\Accessory ;
2018-07-24 19:35:26 -07:00
use App\Models\Asset ;
use App\Models\License ;
use App\Models\LicenseSeat ;
use App\Models\User ;
use Illuminate\Support\Facades\Auth ;
class LicenseCheckoutController extends Controller
{
/**
* Provides the form view for checking out a license to a user .
* Here we pass the license seat ID instead of the license ID ,
* because licenses themselves are never checked out to anyone ,
* only the seats associated with them .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
2023-11-23 08:18:28 -08:00
* @ param $id
2018-07-24 19:35:26 -07:00
* @ return \Illuminate\Contracts\View\View
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2023-11-23 08:18:28 -08:00
public function create ( $id )
2018-07-24 19:35:26 -07:00
{
2023-11-23 08:18:28 -08:00
if ( $license = License :: find ( $id )) {
2018-07-24 19:35:26 -07:00
2023-06-29 13:15:50 -07:00
$this -> authorize ( 'checkout' , $license );
2023-11-23 08:18:28 -08:00
if ( $license -> category ) {
// Make sure there is at least one available to checkout
if ( $license -> availCount () -> count () < 1 ){
return redirect () -> route ( 'licenses.index' ) -> with ( 'error' , trans ( 'admin/licenses/message.checkout.not_enough_seats' ));
}
// Return the checkout view
return view ( 'licenses/checkout' , compact ( 'license' ));
2018-07-24 19:35:26 -07:00
}
2023-11-23 08:18:28 -08:00
// Invalid category
return redirect () -> route ( 'licenses.edit' , [ 'license' => $license -> id ])
-> with ( 'error' , trans ( 'general.invalid_item_category_single' , [ 'type' => trans ( 'general.license' )]));
2018-07-24 19:35:26 -07:00
}
2023-11-23 08:18:28 -08:00
// Not found
2023-06-29 13:15:50 -07:00
return redirect () -> route ( 'licenses.index' ) -> with ( 'error' , trans ( 'admin/licenses/message.not_found' ));
2021-06-10 13:15:52 -07:00
2018-07-24 19:35:26 -07:00
}
/**
* Validates and stores the license checkout action .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ param LicenseCheckoutRequest $request
* @ param $licenseId
* @ return \Illuminate\Http\RedirectResponse
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function store ( LicenseCheckoutRequest $request , $licenseId , $seatId = null )
{
2021-06-10 13:15:52 -07:00
if ( ! $license = License :: find ( $licenseId )) {
2018-07-24 19:35:26 -07:00
return redirect () -> route ( 'licenses.index' ) -> with ( 'error' , trans ( 'admin/licenses/message.not_found' ));
}
$this -> authorize ( 'checkout' , $license );
2018-07-27 12:03:04 -07:00
$licenseSeat = $this -> findLicenseSeatToCheckout ( $license , $seatId );
2018-07-24 19:35:26 -07:00
$licenseSeat -> user_id = Auth :: id ();
2023-09-28 07:40:18 -07:00
$licenseSeat -> notes = $request -> input ( 'notes' );
2022-09-20 16:55:53 -07:00
2018-07-24 19:35:26 -07:00
2018-07-27 12:03:04 -07:00
$checkoutMethod = 'checkoutTo' . ucwords ( request ( 'checkout_to_type' ));
2018-07-24 19:35:26 -07:00
if ( $this -> $checkoutMethod ( $licenseSeat )) {
2021-06-10 13:15:52 -07:00
return redirect () -> route ( 'licenses.index' ) -> with ( 'success' , trans ( 'admin/licenses/message.checkout.success' ));
2018-07-24 19:35:26 -07:00
}
2018-07-27 12:03:04 -07:00
2021-06-10 13:15:52 -07:00
return redirect () -> route ( 'licenses.index' ) -> with ( 'error' , trans ( 'Something went wrong handling this checkout.' ));
2018-07-24 19:35:26 -07:00
}
2018-07-27 12:03:04 -07:00
protected function findLicenseSeatToCheckout ( $license , $seatId )
{
$licenseSeat = LicenseSeat :: find ( $seatId ) ? ? $license -> freeSeat ();
2021-06-10 13:15:52 -07:00
if ( ! $licenseSeat ) {
2018-07-27 12:03:04 -07:00
if ( $seatId ) {
2022-09-20 16:55:53 -07:00
throw new \Illuminate\Http\Exceptions\HttpResponseException ( redirect () -> route ( 'licenses.index' ) -> with ( 'error' , 'This Seat is not available for checkout.' ));
2018-07-27 12:03:04 -07:00
}
2022-09-20 16:55:53 -07:00
throw new \Illuminate\Http\Exceptions\HttpResponseException ( redirect () -> route ( 'licenses.index' ) -> with ( 'error' , 'There are no available seats for this license.' ));
2018-07-27 12:03:04 -07:00
}
2021-06-10 13:15:52 -07:00
if ( ! $licenseSeat -> license -> is ( $license )) {
2022-09-20 16:55:53 -07:00
throw new \Illuminate\Http\Exceptions\HttpResponseException ( redirect () -> route ( 'licenses.index' ) -> with ( 'error' , 'The license seat provided does not match the license.' ));
2018-07-27 12:03:04 -07:00
}
return $licenseSeat ;
}
2018-07-24 19:35:26 -07:00
protected function checkoutToAsset ( $licenseSeat )
{
if ( is_null ( $target = Asset :: find ( request ( 'asset_id' )))) {
return redirect () -> route ( 'licenses.index' ) -> with ( 'error' , trans ( 'admin/licenses/message.asset_does_not_exist' ));
}
$licenseSeat -> asset_id = request ( 'asset_id' );
// Override asset's assigned user if available
if ( $target -> checkedOutToUser ()) {
2021-06-10 13:15:52 -07:00
$licenseSeat -> assigned_to = $target -> assigned_to ;
2018-07-24 19:35:26 -07:00
}
2018-07-27 12:03:04 -07:00
if ( $licenseSeat -> save ()) {
2023-11-06 12:20:13 -08:00
event ( new CheckoutableCheckedOut ( $licenseSeat , $target , Auth :: user (), request ( 'notes' )));
2018-07-27 15:05:45 -07:00
2018-07-24 19:35:26 -07:00
return true ;
}
2021-06-10 13:15:52 -07:00
2018-07-24 19:35:26 -07:00
return false ;
}
protected function checkoutToUser ( $licenseSeat )
{
// Fetch the target and set the license user
if ( is_null ( $target = User :: find ( request ( 'assigned_to' )))) {
return redirect () -> route ( 'licenses.index' ) -> with ( 'error' , trans ( 'admin/licenses/message.user_does_not_exist' ));
}
$licenseSeat -> assigned_to = request ( 'assigned_to' );
if ( $licenseSeat -> save ()) {
2023-11-06 12:20:13 -08:00
event ( new CheckoutableCheckedOut ( $licenseSeat , $target , Auth :: user (), request ( 'notes' )));
2018-07-27 15:05:45 -07:00
2018-07-24 19:35:26 -07:00
return true ;
}
2021-06-10 13:15:52 -07:00
2018-07-24 19:35:26 -07:00
return false ;
}
2023-04-16 15:26:59 -07:00
/**
* Bulk checkin all license seats
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ 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 ) {
2023-04-18 01:07:26 -07:00
\Log :: debug ( 'Checking out ' . $licenseId . ' via bulk' );
2023-04-16 15:26:59 -07:00
$license = License :: findOrFail ( $licenseId );
$this -> authorize ( 'checkin' , $license );
2023-04-18 01:07:26 -07:00
$avail_count = $license -> getAvailSeatsCountAttribute ();
2023-04-16 15:26:59 -07:00
$users = User :: whereNull ( 'deleted_at' ) -> where ( 'autoassign_licenses' , '=' , 1 ) -> with ( 'licenses' ) -> get ();
2023-04-18 01:07:26 -07:00
\Log :: debug ( $avail_count . ' will be assigned' );
2023-04-16 15:26:59 -07:00
2023-04-18 01:07:26 -07:00
if ( $users -> count () > $avail_count ) {
2023-04-16 15:26:59 -07:00
\Log :: debug ( 'You do not have enough free seats to complete this task, so we will check out as many as we can. ' );
}
2023-04-16 16:08:06 -07:00
// If the license is valid, check that there is an available seat
if ( $license -> availCount () -> count () < 1 ) {
2023-04-18 01:07:26 -07:00
return redirect () -> back () -> with ( 'error' , trans ( 'admin/licenses/general.bulk.checkout_all.error_no_seats' ));
2023-04-16 16:08:06 -07:00
}
2023-04-16 15:26:59 -07:00
2023-04-18 01:07:26 -07:00
2023-04-16 16:08:06 -07:00
$assigned_count = 0 ;
2023-04-16 15:26:59 -07:00
2023-04-16 16:08:06 -07:00
foreach ( $users as $user ) {
// Check to make sure this user doesn't already have this license checked out to them
2023-04-16 15:26:59 -07:00
if ( $user -> licenses -> where ( 'id' , '=' , $licenseId ) -> count ()) {
\Log :: debug ( $user -> username . ' already has this license checked out to them. Skipping... ' );
continue ;
}
$licenseSeat = $license -> freeSeat ();
2023-04-16 16:08:06 -07:00
// Update the seat with checkout info
2023-04-16 15:26:59 -07:00
$licenseSeat -> assigned_to = $user -> id ;
if ( $licenseSeat -> save ()) {
2023-04-16 16:08:06 -07:00
$avail_count -- ;
2023-04-18 01:07:26 -07:00
$assigned_count ++ ;
$licenseSeat -> logCheckout ( trans ( 'admin/licenses/general.bulk.checkout_all.log_msg' ), $user );
\Log :: debug ( 'License ' . $license -> name . ' seat ' . $licenseSeat -> id . ' checked out to ' . $user -> username );
2023-04-16 15:26:59 -07:00
}
2023-04-16 16:08:06 -07:00
if ( $avail_count == 0 ) {
2023-04-18 01:07:26 -07:00
return redirect () -> back () -> with ( 'warning' , trans ( 'admin/licenses/general.bulk.checkout_all.warn_not_enough_seats' , [ 'count' => $assigned_count ]));
2023-04-16 16:08:06 -07:00
}
2023-04-16 15:26:59 -07:00
}
2023-04-18 02:07:17 -07:00
if ( $assigned_count == 0 ) {
return redirect () -> back () -> with ( 'warning' , trans ( 'admin/licenses/general.bulk.checkout_all.warn_no_avail_users' , [ 'count' => $assigned_count ]));
}
2023-04-18 01:07:26 -07:00
return redirect () -> back () -> with ( 'success' , trans_choice ( 'admin/licenses/general.bulk.checkout_all.success' , 2 , [ 'count' => $assigned_count ] ));
2023-04-16 15:26:59 -07:00
}
2018-07-24 19:35:26 -07:00
}