2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\Asset;
|
2016-09-15 19:58:27 -07:00
|
|
|
use App\Models\AssetModel;
|
2016-03-25 01:18:05 -07:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
2018-04-04 17:33:02 -07:00
|
|
|
use App\Notifications\RequestAssetNotification;
|
|
|
|
use App\Notifications\RequestAssetCancelationNotification;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Redirect;
|
2016-10-31 21:00:30 -07:00
|
|
|
use Illuminate\Http\Request;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to the ability for users
|
|
|
|
* to view their own assets in the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
class ViewAssetsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Redirect to the profile page.
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function getIndex()
|
|
|
|
{
|
|
|
|
|
2016-09-29 22:20:49 -07:00
|
|
|
$user = User::with(
|
2017-03-31 13:48:31 -07:00
|
|
|
'assets.model',
|
2016-09-29 22:20:49 -07:00
|
|
|
'consumables',
|
|
|
|
'accessories',
|
|
|
|
'licenses',
|
|
|
|
'userloc',
|
|
|
|
'userlog'
|
2018-07-24 19:35:26 -07:00
|
|
|
)->withTrashed()->find(Auth::id());
|
2016-09-29 22:20:49 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
$userlog = $user->userlog->load('item', 'user', 'target');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if (isset($user->id)) {
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('account/view-assets', compact('user', 'userlog'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
// Redirect to the user management page
|
|
|
|
return redirect()->route('users.index')
|
|
|
|
->with('error', trans('admin/users/message.user_not_found', $user->id));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
/**
|
|
|
|
* Returns view of requestable items for a user.
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getRequestableIndex()
|
|
|
|
{
|
|
|
|
|
2017-10-28 02:58:38 -07:00
|
|
|
$assets = Asset::with('model', 'defaultLoc', 'location', 'assignedTo', 'requests')->Hardware()->RequestableAssets()->get();
|
2017-09-05 17:54:58 -07:00
|
|
|
$models = AssetModel::with('category', 'requests', 'assets')->RequestableModels()->get();
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2019-01-10 13:20:43 -08:00
|
|
|
return view('account/requestable-assets', compact('assets', 'models'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
public function getRequestItem($itemType, $itemId = null)
|
|
|
|
{
|
|
|
|
$item = null;
|
|
|
|
$fullItemType = 'App\\Models\\' . studly_case($itemType);
|
2018-04-04 17:33:02 -07:00
|
|
|
|
2016-09-29 22:20:49 -07:00
|
|
|
if ($itemType == "asset_model") {
|
2016-09-15 19:58:27 -07:00
|
|
|
$itemType = "model";
|
|
|
|
}
|
|
|
|
$item = call_user_func(array($fullItemType, 'find'), $itemId);
|
2018-04-04 17:33:02 -07:00
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
$user = Auth::user();
|
2018-04-04 17:33:02 -07:00
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
|
|
|
|
$logaction = new Actionlog();
|
|
|
|
$logaction->item_id = $data['asset_id'] = $item->id;
|
|
|
|
$logaction->item_type = $fullItemType;
|
2016-09-26 22:35:51 -07:00
|
|
|
$logaction->created_at = $data['requested_date'] = date("Y-m-d H:i:s");
|
2018-04-04 17:33:02 -07:00
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
if ($user->location_id) {
|
|
|
|
$logaction->location_id = $user->location_id;
|
|
|
|
}
|
|
|
|
$logaction->target_id = $data['user_id'] = Auth::user()->id;
|
|
|
|
$logaction->target_type = User::class;
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$data['item_quantity'] = Input::has('request-quantity') ? e(Input::get('request-quantity')) : 1;
|
2016-12-23 17:52:00 -08:00
|
|
|
$data['requested_by'] = $user->present()->fullName();
|
2018-04-04 17:33:02 -07:00
|
|
|
$data['item'] = $item;
|
2016-09-15 19:58:27 -07:00
|
|
|
$data['item_type'] = $itemType;
|
2018-04-04 17:33:02 -07:00
|
|
|
$data['target'] = Auth::user();
|
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
|
|
|
|
if ($fullItemType == Asset::class) {
|
2016-12-15 18:18:13 -08:00
|
|
|
$data['item_url'] = route('hardware.show', $item->id);
|
2016-09-15 19:58:27 -07:00
|
|
|
} else {
|
|
|
|
$data['item_url'] = route("view/${itemType}", $item->id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$settings = Setting::getSettings();
|
2016-09-15 19:58:27 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
if ($item_request = $item->isRequestedBy($user)) {
|
|
|
|
$item->cancelRequest();
|
|
|
|
$data['item_quantity'] = $item_request->qty;
|
|
|
|
$logaction->logaction('request_canceled');
|
2016-09-15 19:58:27 -07:00
|
|
|
|
|
|
|
if (($settings->alert_email!='') && ($settings->alerts_enabled=='1') && (!config('app.lock_passwords'))) {
|
2018-04-04 17:33:02 -07:00
|
|
|
$settings->notify(new RequestAssetCancelationNotification($data));
|
2016-09-15 19:58:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.canceled'));
|
|
|
|
|
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
$item->request();
|
|
|
|
if (($settings->alert_email!='') && ($settings->alerts_enabled=='1') && (!config('app.lock_passwords'))) {
|
|
|
|
$logaction->logaction('requested');
|
|
|
|
$settings->notify(new RequestAssetNotification($data));
|
|
|
|
}
|
2018-04-04 17:33:02 -07:00
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success'));
|
|
|
|
}
|
2018-04-04 17:33:02 -07:00
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
/**
|
|
|
|
* Process a specific requested asset
|
|
|
|
* @param null $assetId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
public function getRequestAsset($assetId = null)
|
|
|
|
{
|
|
|
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
|
|
// Check if the asset exists and is requestable
|
|
|
|
if (is_null($asset = Asset::RequestableAssets()->find($assetId))) {
|
2018-04-04 17:33:02 -07:00
|
|
|
return redirect()->route('requestable-assets')
|
|
|
|
->with('error', trans('admin/hardware/message.does_not_exist_or_not_requestable'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
if (!Company::isCurrentUserHasAccess($asset)) {
|
2018-04-04 17:33:02 -07:00
|
|
|
return redirect()->route('requestable-assets')
|
|
|
|
->with('error', trans('general.insufficient_permissions'));
|
2016-09-15 19:58:27 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$data['item'] = $asset;
|
|
|
|
$data['target'] = Auth::user();
|
|
|
|
$data['item_quantity'] = 1;
|
|
|
|
$settings = Setting::getSettings();
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$logaction = new Actionlog();
|
|
|
|
$logaction->item_id = $data['asset_id'] = $asset->id;
|
|
|
|
$logaction->item_type = $data['item_type'] = Asset::class;
|
|
|
|
$logaction->created_at = $data['requested_date'] = date("Y-m-d H:i:s");
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
if ($user->location_id) {
|
|
|
|
$logaction->location_id = $user->location_id;
|
|
|
|
}
|
|
|
|
$logaction->target_id = $data['user_id'] = Auth::user()->id;
|
|
|
|
$logaction->target_type = User::class;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
// If it's already requested, cancel the request.
|
|
|
|
if ($asset->isRequestedBy(Auth::user())) {
|
|
|
|
$asset->cancelRequest();
|
2018-05-16 19:20:43 -07:00
|
|
|
$asset->decrement('requests_counter', 1);
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$logaction->logaction('request canceled');
|
|
|
|
$settings->notify(new RequestAssetCancelationNotification($data));
|
|
|
|
return redirect()->route('requestable-assets')
|
|
|
|
->with('success')->with('success', trans('admin/hardware/message.requests.cancel-success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$logaction->logaction('requested');
|
|
|
|
$asset->request();
|
|
|
|
$asset->increment('requests_counter', 1);
|
|
|
|
$settings->notify(new RequestAssetNotification($data));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
public function getRequestedAssets()
|
|
|
|
{
|
2018-04-04 17:33:02 -07:00
|
|
|
return view('account/requested');
|
2016-09-15 19:58:27 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Get the acceptance screen
|
|
|
|
public function getAcceptAsset($logID = null)
|
|
|
|
{
|
2018-09-10 08:13:16 -07:00
|
|
|
return redirect()->route('account.accept');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
}
|