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;
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
$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 = [
|
2020-05-12 11:36:14 -07:00
|
|
|
'note' => '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;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
// Was the asset updated?
|
|
|
|
if ($licenseSeat->save()) {
|
2019-01-17 20:36:40 -08:00
|
|
|
event(new CheckoutableCheckedIn($licenseSeat, $return_to, Auth::user(), $request->input('note')));
|
2018-07-25 01:02:06 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($backTo == 'user') {
|
|
|
|
return redirect()->route('users.show', $return_to->id)->with('success', trans('admin/licenses/message.checkin.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
|
|
|
return redirect()->route('licenses.show', $licenseSeat->license_id)->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
|
|
|
}
|
|
|
|
}
|