2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\Accessory;
|
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\Asset;
|
2016-09-15 19:58:27 -07:00
|
|
|
use App\Models\AssetModel;
|
|
|
|
use App\Models\CheckoutRequest;
|
2016-03-25 01:18:05 -07:00
|
|
|
use App\Models\Company;
|
2016-06-10 08:29:43 -07:00
|
|
|
use App\Models\Component;
|
2016-09-15 19:58:27 -07:00
|
|
|
use App\Models\Consumable;
|
|
|
|
use App\Models\License;
|
2016-03-25 01:18:05 -07:00
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
2018-04-04 17:33:02 -07:00
|
|
|
use App\Notifications\RequestAssetNotification;
|
|
|
|
use App\Notifications\RequestAssetCancelationNotification;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Auth;
|
|
|
|
use Config;
|
|
|
|
use DB;
|
|
|
|
use Input;
|
|
|
|
use Lang;
|
|
|
|
use Mail;
|
|
|
|
use Redirect;
|
|
|
|
use Slack;
|
|
|
|
use Validator;
|
|
|
|
use View;
|
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'
|
|
|
|
)->withTrashed()->find(Auth::user()->id);
|
|
|
|
|
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
|
|
|
} else {
|
|
|
|
// Prepare the error message
|
2016-04-07 13:39:35 -07:00
|
|
|
$error = trans('admin/users/message.user_not_found', compact('id'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Redirect to the user management page
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->route('users')->with('error', $error);
|
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
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('account/requestable-assets', compact('user', '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'));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$item->request();
|
|
|
|
if (($settings->alert_email!='') && ($settings->alerts_enabled=='1') && (!config('app.lock_passwords'))) {
|
2018-04-04 17:33:02 -07:00
|
|
|
$logaction->logaction('requested');
|
|
|
|
$settings->notify(new RequestAssetNotification($data));
|
2016-09-15 19:58:27 -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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} elseif (!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();
|
|
|
|
$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'));
|
|
|
|
} else {
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$logaction->logaction('requested');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$asset->request();
|
|
|
|
$settings->notify(new RequestAssetNotification($data));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
2016-04-28 21:06:41 -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)
|
|
|
|
{
|
|
|
|
|
2017-11-07 22:25:24 -08:00
|
|
|
$findlog = Actionlog::where('id', $logID)->first();
|
|
|
|
|
|
|
|
if (!$findlog) {
|
|
|
|
return redirect()->to('account/view-assets')->with('error', 'No matching record.');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-10-31 21:00:30 -07:00
|
|
|
if ($findlog->accepted_id!='') {
|
|
|
|
return redirect()->to('account/view-assets')->with('error', trans('admin/users/message.error.asset_already_accepted'));
|
|
|
|
}
|
2016-06-10 08:29:43 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2016-10-31 21:00:30 -07:00
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
// TODO - Fix this for non-assets
|
|
|
|
if (($findlog->item_type==Asset::class) && ($user->id != $findlog->item->assigned_to)) {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->to('account/view-assets')->with('error', trans('admin/users/message.error.incorrect_user_accepted'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
$item = $findlog->item;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Check if the asset exists
|
|
|
|
if (is_null($item)) {
|
|
|
|
// Redirect to the asset management page
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->to('account')->with('error', trans('admin/hardware/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} elseif (!Company::isCurrentUserHasAccess($item)) {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->route('requestable-assets')->with('error', trans('general.insufficient_permissions'));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('account/accept-asset', compact('item'))->with('findlog', $findlog)->with('item', $item);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the acceptance
|
2016-10-31 21:00:30 -07:00
|
|
|
public function postAcceptAsset(Request $request, $logID = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
// Check if the asset exists
|
2016-09-06 19:39:42 -07:00
|
|
|
if (is_null($findlog = Actionlog::where('id', $logID)->first())) {
|
2016-03-25 01:18:05 -07:00
|
|
|
// Redirect to the asset management page
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->to('account/view-assets')->with('error', trans('admin/hardware/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-09-20 07:20:10 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if ($findlog->accepted_id!='') {
|
|
|
|
// Redirect to the asset management page
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->to('account/view-assets')->with('error', trans('admin/users/message.error.asset_already_accepted'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Input::has('asset_acceptance')) {
|
2016-10-31 21:00:30 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.error.accept_or_decline'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
if (($findlog->item_type==Asset::class) && ($user->id != $findlog->item->assigned_to)) {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->to('account/view-assets')->with('error', trans('admin/users/message.error.incorrect_user_accepted'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-10-31 21:00:30 -07:00
|
|
|
if ($request->has('signature_output')) {
|
|
|
|
$path = config('app.private_uploads').'/signatures';
|
|
|
|
$sig_filename = "siglog-".$findlog->id.'-'.date('Y-m-d-his').".png";
|
|
|
|
$data_uri = e($request->get('signature_output'));
|
|
|
|
$encoded_image = explode(",", $data_uri);
|
|
|
|
$decoded_image = base64_decode($encoded_image[1]);
|
|
|
|
file_put_contents($path."/".$sig_filename, $decoded_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
$logaction = new Actionlog();
|
|
|
|
|
|
|
|
if (Input::get('asset_acceptance')=='accepted') {
|
|
|
|
$logaction_msg = 'accepted';
|
|
|
|
$accepted="accepted";
|
2016-04-07 13:39:35 -07:00
|
|
|
$return_msg = trans('admin/users/message.accepted');
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
$logaction_msg = 'declined';
|
|
|
|
$accepted="rejected";
|
2016-04-07 13:39:35 -07:00
|
|
|
$return_msg = trans('admin/users/message.declined');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-09-06 19:39:42 -07:00
|
|
|
$logaction->item_id = $findlog->item_id;
|
|
|
|
$logaction->item_type = $findlog->item_type;
|
2016-10-31 21:00:30 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Asset
|
2016-09-06 19:39:42 -07:00
|
|
|
if (($findlog->item_id!='') && ($findlog->item_type==Asset::class)) {
|
2016-03-25 01:18:05 -07:00
|
|
|
if (Input::get('asset_acceptance')!='accepted') {
|
|
|
|
DB::table('assets')
|
2016-09-06 19:39:42 -07:00
|
|
|
->where('id', $findlog->item_id)
|
2016-03-25 01:18:05 -07:00
|
|
|
->update(array('assigned_to' => null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:00:30 -07:00
|
|
|
$logaction->target_id = $findlog->target_id;
|
2016-11-24 12:48:14 -08:00
|
|
|
$logaction->target_type = User::class;
|
2016-03-25 01:18:05 -07:00
|
|
|
$logaction->note = e(Input::get('note'));
|
2016-10-31 21:00:30 -07:00
|
|
|
$logaction->updated_at = date("Y-m-d H:i:s");
|
|
|
|
|
|
|
|
|
2016-11-21 18:40:26 -08:00
|
|
|
if (isset($sig_filename)) {
|
2016-10-31 21:00:30 -07:00
|
|
|
$logaction->accept_signature = $sig_filename;
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
$log = $logaction->logaction($logaction_msg);
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
$update_checkout = DB::table('action_logs')
|
2016-03-25 01:18:05 -07:00
|
|
|
->where('id', $findlog->id)
|
|
|
|
->update(array('accepted_id' => $logaction->id));
|
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
if (($findlog->item_id!='') && ($findlog->item_type==Asset::class)) {
|
2016-10-31 21:00:30 -07:00
|
|
|
$affected_asset = $logaction->item;
|
|
|
|
$affected_asset->accepted = $accepted;
|
2016-03-25 01:18:05 -07:00
|
|
|
$affected_asset->save();
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if ($update_checkout) {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->to('account/view-assets')->with('success', $return_msg);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
} else {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->to('account/view-assets')->with('error', 'Something went wrong ');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|