2018-07-16 17:44:03 -07:00
|
|
|
<?php
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
namespace App\Http\Controllers\Assets;
|
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
use App\Exceptions\CheckoutNotAllowed;
|
2021-02-03 01:29:54 -08:00
|
|
|
use App\Helpers\Helper;
|
2018-07-16 17:44:03 -07:00
|
|
|
use App\Http\Controllers\CheckInOutRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-07-16 17:44:03 -07:00
|
|
|
use App\Http\Requests\AssetCheckoutRequest;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class AssetCheckoutController extends Controller
|
|
|
|
{
|
|
|
|
use CheckInOutRequest;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Returns a view that presents a form to check an asset out to a
|
|
|
|
* user.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $assetId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2018-07-16 17:44:03 -07:00
|
|
|
public function create($assetId)
|
|
|
|
{
|
|
|
|
// Check if the asset exists
|
2023-02-06 10:59:51 -08:00
|
|
|
if (is_null($asset = Asset::with('company')->find(e($assetId)))) {
|
2018-07-16 17:44:03 -07:00
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('checkout', $asset);
|
|
|
|
|
|
|
|
if ($asset->availableForCheckout()) {
|
2021-02-03 01:29:54 -08:00
|
|
|
return view('hardware/checkout', compact('asset'))
|
|
|
|
->with('statusLabel_list', Helper::deployableStatusLabelList());
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and process the form data to check out an asset to a user.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param AssetCheckoutRequest $request
|
|
|
|
* @param int $assetId
|
|
|
|
* @return Redirect
|
|
|
|
* @since [v1.0]
|
|
|
|
*/
|
|
|
|
public function store(AssetCheckoutRequest $request, $assetId)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// Check if the asset exists
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $asset = Asset::find($assetId)) {
|
2018-07-16 17:44:03 -07:00
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
2021-06-10 13:15:52 -07:00
|
|
|
} elseif (! $asset->availableForCheckout()) {
|
2018-07-16 17:44:03 -07:00
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available'));
|
|
|
|
}
|
|
|
|
$this->authorize('checkout', $asset);
|
|
|
|
$admin = Auth::user();
|
|
|
|
|
|
|
|
$target = $this->determineCheckoutTarget($asset);
|
2020-04-29 07:59:00 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
$asset = $this->updateAssetLocation($asset, $target);
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$checkout_at = date('Y-m-d H:i:s');
|
|
|
|
if (($request->filled('checkout_at')) && ($request->get('checkout_at') != date('Y-m-d'))) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$checkout_at = $request->get('checkout_at');
|
|
|
|
}
|
|
|
|
|
|
|
|
$expected_checkin = '';
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('expected_checkin')) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$expected_checkin = $request->get('expected_checkin');
|
|
|
|
}
|
|
|
|
|
2021-02-03 01:29:54 -08:00
|
|
|
if ($request->filled('status_id')) {
|
|
|
|
$asset->status_id = $request->get('status_id');
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:06:43 -07:00
|
|
|
if(!empty($asset->licenseseats->all())){
|
|
|
|
if(request('checkout_to_type') == 'user') {
|
|
|
|
foreach ($asset->licenseseats as $seat){
|
|
|
|
$seat->assigned_to = $target->id;
|
|
|
|
$seat->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 18:23:28 -07:00
|
|
|
$settings = \App\Models\Setting::getSettings();
|
|
|
|
|
2023-09-26 06:33:42 -07:00
|
|
|
// We have to check whether $target->company_id is null here since locations don't have a company yet
|
|
|
|
if (($settings->full_multiple_companies_support) && ((!is_null($target->company_id)) && (!is_null($asset->company_id)))) {
|
2023-08-02 18:23:28 -07:00
|
|
|
if ($target->company_id != $asset->company_id){
|
|
|
|
return redirect()->to("hardware/$assetId/checkout")->with('error', trans('general.error_user_company'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 05:26:41 -08:00
|
|
|
if ($asset->checkOut($target, $admin, $checkout_at, $expected_checkin, $request->get('note'), $request->get('name'))) {
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.checkout.success'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the asset management page with error
|
2020-10-23 14:00:04 -07:00
|
|
|
return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error').$asset->getErrors());
|
2018-07-16 17:44:03 -07:00
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors());
|
|
|
|
} catch (CheckoutNotAllowed $e) {
|
|
|
|
return redirect()->back()->with('error', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|