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\CheckoutableCheckedIn;
|
2024-07-26 04:48:07 -07:00
|
|
|
use App\Helpers\Helper;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Models\License;
|
|
|
|
use App\Models\LicenseSeat;
|
|
|
|
use App\Models\User;
|
2021-08-25 14:38:34 -07:00
|
|
|
use App\Models\Asset;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Http\Request;
|
2018-07-27 15:23:58 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
class LicenseCheckinController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Makes the form view to check a license seat back into inventory.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $seatId
|
|
|
|
* @param string $backTo
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function create($seatId = null, $backTo = null)
|
|
|
|
{
|
|
|
|
// Check if the asset exists
|
|
|
|
if (is_null($licenseSeat = LicenseSeat::find($seatId)) || is_null($license = License::find($licenseSeat->license_id))) {
|
|
|
|
// Redirect to the asset management page with error
|
|
|
|
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('checkout', $license);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return view('licenses/checkin', compact('licenseSeat'))->with('backto', $backTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and stores the license checkin action.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LicenseCheckinController::create() method that provides the form view
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $seatId
|
|
|
|
* @param string $backTo
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2018-07-25 01:02:06 -07:00
|
|
|
public function store(Request $request, $seatId = null, $backTo = null)
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
|
|
|
// Check if the asset exists
|
|
|
|
if (is_null($licenseSeat = LicenseSeat::find($seatId))) {
|
|
|
|
// Redirect to the asset management page with error
|
|
|
|
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$license = License::find($licenseSeat->license_id);
|
2023-02-27 16:57:38 -08:00
|
|
|
|
|
|
|
// LicenseSeat is not assigned, it can't be checked in
|
2023-03-02 17:33:32 -08:00
|
|
|
if (is_null($licenseSeat->assigned_to) && is_null($licenseSeat->asset_id)) {
|
2023-02-27 16:57:38 -08:00
|
|
|
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkin.error'));
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$this->authorize('checkout', $license);
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $license->reassignable) {
|
2018-07-24 19:35:26 -07:00
|
|
|
// Not allowed to checkin
|
|
|
|
Session::flash('error', 'License not reassignable.');
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->back()->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declare the rules for the form validation
|
|
|
|
$rules = [
|
2023-09-28 07:40:18 -07:00
|
|
|
'notes' => 'string|nullable',
|
2018-07-24 19:35:26 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
// Create a new validator instance from our validation rules
|
2019-12-10 19:32:50 -08:00
|
|
|
$validator = Validator::make($request->all(), $rules);
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
// If validation fails, we'll exit the operation now.
|
|
|
|
if ($validator->fails()) {
|
|
|
|
// Ooops.. something went wrong
|
|
|
|
return redirect()->back()->withInput()->withErrors($validator);
|
|
|
|
}
|
2021-08-25 14:38:34 -07:00
|
|
|
|
|
|
|
if($licenseSeat->assigned_to != null){
|
|
|
|
$return_to = User::find($licenseSeat->assigned_to);
|
|
|
|
} else {
|
|
|
|
$return_to = Asset::find($licenseSeat->asset_id);
|
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
// Update the asset data
|
2021-06-10 13:15:52 -07:00
|
|
|
$licenseSeat->assigned_to = null;
|
|
|
|
$licenseSeat->asset_id = null;
|
2023-09-28 07:40:18 -07:00
|
|
|
$licenseSeat->notes = $request->input('notes');
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2024-07-26 04:48:07 -07:00
|
|
|
session()->put(['redirect_option' => $request->get('redirect_option')]);
|
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
// Was the asset updated?
|
|
|
|
if ($licenseSeat->save()) {
|
2024-07-04 12:49:22 -07:00
|
|
|
event(new CheckoutableCheckedIn($licenseSeat, $return_to, auth()->user(), $request->input('notes')));
|
2018-07-25 01:02:06 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2024-07-26 04:48:07 -07:00
|
|
|
return redirect()->to(Helper::getRedirectOption($request, $license->id, 'Licenses'))->with('success', trans('admin/licenses/message.checkin.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the license page with error
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkin.error'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2023-04-16 12:30:02 -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
|
|
|
|
*/
|
|
|
|
|
2023-04-16 15:27:15 -07:00
|
|
|
public function bulkCheckin(Request $request, $licenseId) {
|
|
|
|
|
|
|
|
$license = License::findOrFail($licenseId);
|
|
|
|
$this->authorize('checkin', $license);
|
|
|
|
|
2023-08-23 15:22:40 -07:00
|
|
|
if (! $license->reassignable) {
|
|
|
|
// Not allowed to checkin
|
|
|
|
Session::flash('error', 'License not reassignable.');
|
|
|
|
|
|
|
|
return redirect()->back()->withInput();
|
|
|
|
}
|
|
|
|
|
2023-04-18 01:07:26 -07:00
|
|
|
$licenseSeatsByUser = LicenseSeat::where('license_id', '=', $licenseId)
|
2023-04-16 15:27:15 -07:00
|
|
|
->whereNotNull('assigned_to')
|
|
|
|
->with('user')
|
|
|
|
->get();
|
|
|
|
|
2023-04-18 01:07:26 -07:00
|
|
|
foreach ($licenseSeatsByUser as $user_seat) {
|
|
|
|
$user_seat->assigned_to = null;
|
2023-04-16 15:27:15 -07:00
|
|
|
|
2023-04-18 01:07:26 -07:00
|
|
|
if ($user_seat->save()) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('Checking in '.$license->name.' from user '.$user_seat->username);
|
2023-04-18 01:07:26 -07:00
|
|
|
$user_seat->logCheckin($user_seat->user, trans('admin/licenses/general.bulk.checkin_all.log_msg'));
|
2023-04-16 15:27:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 01:07:26 -07:00
|
|
|
$licenseSeatsByAsset = LicenseSeat::where('license_id', '=', $licenseId)
|
|
|
|
->whereNotNull('asset_id')
|
|
|
|
->with('asset')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
foreach ($licenseSeatsByAsset as $asset_seat) {
|
|
|
|
$asset_seat->asset_id = null;
|
|
|
|
|
|
|
|
if ($asset_seat->save()) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('Checking in '.$license->name.' from asset '.$asset_seat->asset_tag);
|
2023-04-18 01:07:26 -07:00
|
|
|
$asset_seat->logCheckin($asset_seat->asset, trans('admin/licenses/general.bulk.checkin_all.log_msg'));
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', trans_choice('admin/licenses/general.bulk.checkin_all.success', 2, ['count' => $count] ));
|
2023-04-16 12:30:02 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|