public function createLogRecord($action, $asset, $admin, $user, $expected_checkin = null, $note = null, $checkout_at = null) { $logaction = new Actionlog(); $logaction->asset_id = $this->id; $logaction->checkedout_to = $this->assigned_to; $logaction->asset_type = 'hardware'; $logaction->note = $note; if ($checkout_at != '') { $logaction->created_at = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s', strtotime($checkout_at))); } else { $logaction->created_at = \Carbon\Carbon::now(); } if ($action == "checkout") { if ($user) { $logaction->location_id = $user->location_id; } } else { // Update the asset data to null, since it's being checked in $logaction->checkedout_to = $asset->assigned_to; $logaction->checkedout_to = ''; $logaction->asset_id = $asset->id; $logaction->location_id = null; $logaction->asset_type = 'hardware'; $logaction->note = $note; $logaction->user_id = $admin->id; } $logaction->adminlog()->associate($admin); $log = $logaction->logaction($action); return $logaction; }
/** * Saves the checkout information * * @author [A. Gianotto] [<*****@*****.**>] * @see ConsumablesController::getCheckout() method that returns the form. * @since [v1.0] * @param int $consumableId * @return Redirect */ public function postCheckout($consumableId) { // Check if the consumable exists if (is_null($consumable = Consumable::find($consumableId))) { // Redirect to the consumable management page with error return redirect()->to('consumables')->with('error', trans('admin/consumables/message.not_found')); } elseif (!Company::isCurrentUserHasAccess($consumable)) { return redirect()->to('admin/consumables')->with('error', trans('general.insufficient_permissions')); } $admin_user = Auth::user(); $assigned_to = e(Input::get('assigned_to')); // Check if the user exists if (is_null($user = User::find($assigned_to))) { // Redirect to the consumable management page with error return redirect()->to('admin/consumables')->with('error', trans('admin/consumables/message.user_does_not_exist')); } // Update the consumable data $consumable->assigned_to = e(Input::get('assigned_to')); $consumable->users()->attach($consumable->id, array('consumable_id' => $consumable->id, 'user_id' => $admin_user->id, 'assigned_to' => e(Input::get('assigned_to')))); $logaction = new Actionlog(); $logaction->consumable_id = $consumable->id; $logaction->checkedout_to = $consumable->assigned_to; $logaction->asset_type = 'consumable'; $logaction->asset_id = 0; $logaction->location_id = $user->location_id; $logaction->user_id = Auth::user()->id; $logaction->note = e(Input::get('note')); $settings = Setting::getSettings(); if ($settings->slack_endpoint) { $slack_settings = ['username' => $settings->botname, 'channel' => $settings->slack_channel, 'link_names' => true]; $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings); try { $client->attach(['color' => 'good', 'fields' => [['title' => 'Checked Out:', 'value' => strtoupper($logaction->asset_type) . ' <' . config('app.url') . '/admin/consumables/' . $consumable->id . '/view' . '|' . $consumable->name . '> checked out to <' . config('app.url') . '/admin/users/' . $user->id . '/view|' . $user->fullName() . '> by <' . config('app.url') . '/admin/users/' . $admin_user->id . '/view' . '|' . $admin_user->fullName() . '>.'], ['title' => 'Note:', 'value' => e($logaction->note)]]])->send('Consumable Checked Out'); } catch (Exception $e) { } } $log = $logaction->logaction('checkout'); $consumable_user = DB::table('consumables_users')->where('assigned_to', '=', $consumable->assigned_to)->where('consumable_id', '=', $consumable->id)->first(); $data['log_id'] = $logaction->id; $data['eula'] = $consumable->getEula(); $data['first_name'] = $user->first_name; $data['item_name'] = $consumable->name; $data['checkout_date'] = $logaction->created_at; $data['note'] = $logaction->note; $data['require_acceptance'] = $consumable->requireAcceptance(); if ($consumable->requireAcceptance() == '1' || $consumable->getEula()) { Mail::send('emails.accept-asset', $data, function ($m) use($user) { $m->to($user->email, $user->first_name . ' ' . $user->last_name); $m->subject('Confirm consumable delivery'); }); } // Redirect to the new consumable page return redirect()->to("admin/consumables")->with('success', trans('admin/consumables/message.checkout.success')); }
/** * Save bulk deleted. * * @author [A. Gianotto] [<*****@*****.**>] * @param array $assets * @since [v2.0] * @return View */ public function postBulkDelete($assets = null) { if (!Company::isCurrentUserAuthorized()) { return redirect()->to('hardware')->with('error', trans('general.insufficient_permissions')); } elseif (Input::has('bulk_edit')) { //$assets = Input::get('bulk_edit'); $assets = Asset::find(Input::get('bulk_edit')); //print_r($assets); foreach ($assets as $asset) { //echo '<li>'.$asset; $update_array['deleted_at'] = date('Y-m-d h:i:s'); $update_array['assigned_to'] = null; if (DB::table('assets')->where('id', $asset->id)->update($update_array)) { $logaction = new Actionlog(); $logaction->asset_id = $asset->id; $logaction->asset_type = 'hardware'; $logaction->created_at = date("Y-m-d H:i:s"); $logaction->user_id = Auth::user()->id; $log = $logaction->logaction('deleted'); } } // endforeach return redirect()->to("hardware")->with('success', trans('admin/hardware/message.delete.success')); // no values given, nothing to update } else { return redirect()->to("hardware")->with('info', trans('admin/hardware/message.delete.nothing_updated')); } // Something weird happened here - default to hardware return redirect()->to("hardware"); }
/** * Validate and store checkout data. * * @author [A. Gianotto] [<*****@*****.**>] * @see ComponentsController::getCheckout() method that returns the form. * @since [v3.0] * @param int $componentId * @return Redirect */ public function postCheckout(Request $request, $componentId) { // Check if the component exists if (is_null($component = Component::find($componentId))) { // Redirect to the component management page with error return redirect()->to('components')->with('error', trans('admin/components/message.not_found')); } elseif (!Company::isCurrentUserHasAccess($component)) { return redirect()->to('admin/components')->with('error', trans('general.insufficient_permissions')); } $max_to_checkout = $component->numRemaining(); $validator = Validator::make($request->all(), ["asset_id" => "required", "assigned_qty" => "required|numeric|between:1,{$max_to_checkout}"]); if ($validator->fails()) { return redirect()->back()->withErrors($validator)->withInput(); } $admin_user = Auth::user(); $asset_id = e(Input::get('asset_id')); // Check if the user exists if (is_null($asset = Asset::find($asset_id))) { // Redirect to the component management page with error return redirect()->to('admin/components')->with('error', trans('admin/components/message.asset_does_not_exist')); } // Update the component data $component->asset_id = $asset_id; $component->assets()->attach($component->id, array('component_id' => $component->id, 'user_id' => $admin_user->id, 'created_at' => date('Y-m-d h:i:s'), 'assigned_qty' => e(Input::get('assigned_qty')), 'asset_id' => $asset_id)); $logaction = new Actionlog(); $logaction->component_id = $component->id; $logaction->asset_id = $asset_id; $logaction->asset_type = 'component'; $logaction->location_id = $asset->location_id; $logaction->user_id = Auth::user()->id; $logaction->note = e(Input::get('note')); $settings = Setting::getSettings(); if ($settings->slack_endpoint) { $slack_settings = ['username' => $settings->botname, 'channel' => $settings->slack_channel, 'link_names' => true]; $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings); try { $client->attach(['color' => 'good', 'fields' => [['title' => 'Checked Out:', 'value' => strtoupper($logaction->asset_type) . ' <' . config('app.url') . '/admin/components/' . $component->id . '/view' . '|' . $component->name . '> checked out to <' . config('app.url') . '/hardware/' . $asset->id . '/view|' . $asset->showAssetName() . '> by <' . config('app.url') . '/admin/users/' . $admin_user->id . '/view' . '|' . $admin_user->fullName() . '>.'], ['title' => 'Note:', 'value' => e($logaction->note)]]])->send('Component Checked Out'); } catch (Exception $e) { } } $log = $logaction->logaction('checkout'); // Redirect to the new component page return redirect()->to("admin/components")->with('success', trans('admin/components/message.checkout.success')); }
/** * Validates and stores files associated with a license. * * @todo Switch to using the AssetFileRequest form request validator. * @author [A. Gianotto] [<*****@*****.**>] * @since [v1.0] * @param int $licenseId * @return Redirect */ public function postUpload($licenseId = null) { $license = License::find($licenseId); // the license is valid $destinationPath = config('app.private_uploads') . '/licenses'; if (isset($license->id)) { if (!Company::isCurrentUserHasAccess($license)) { return redirect()->to('admin/licenses')->with('error', trans('general.insufficient_permissions')); } if (Input::hasFile('licensefile')) { foreach (Input::file('licensefile') as $file) { $rules = array('licensefile' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar|max:2000'); $validator = Validator::make(array('licensefile' => $file), $rules); if ($validator->passes()) { $extension = $file->getClientOriginalExtension(); $filename = 'license-' . $license->id . '-' . str_random(8); $filename .= '-' . str_slug($file->getClientOriginalName()) . '.' . $extension; $upload_success = $file->move($destinationPath, $filename); //Log the deletion of seats to the log $logaction = new Actionlog(); $logaction->asset_id = $license->id; $logaction->asset_type = 'software'; $logaction->user_id = Auth::user()->id; $logaction->note = e(Input::get('notes')); $logaction->checkedout_to = null; $logaction->created_at = date("Y-m-d h:i:s"); $logaction->filename = $filename; $log = $logaction->logaction('uploaded'); } else { return redirect()->back()->with('error', trans('admin/licenses/message.upload.invalidfiles')); } } if ($upload_success) { return redirect()->back()->with('success', trans('admin/licenses/message.upload.success')); } else { return redirect()->back()->with('success', trans('admin/licenses/message.upload.error')); } } else { return redirect()->back()->with('error', trans('admin/licenses/message.upload.nofiles')); } } else { // Prepare the error message $error = trans('admin/licenses/message.does_not_exist', compact('id')); // Redirect to the licence management page return redirect()->route('licenses')->with('error', $error); } }
/** * Check in the item so that it can be checked out again to someone else * * @uses Accessory::checkin_email() to determine if an email can and should be sent * @author [A. Gianotto] [<*****@*****.**>] * @param int $accessoryId * @return Redirect **/ public function postCheckin(Request $request, $accessoryUserId = null, $backto = null) { // Check if the accessory exists if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) { // Redirect to the accessory management page with error return redirect()->to('admin/accessories')->with('error', trans('admin/accessories/message.not_found')); } $accessory = Accessory::find($accessory_user->accessory_id); if (!Company::isCurrentUserHasAccess($accessory)) { return redirect()->to('admin/accessories')->with('error', trans('general.insufficient_permissions')); } $logaction = new Actionlog(); $logaction->checkedout_to = e($accessory_user->assigned_to); $return_to = e($accessory_user->assigned_to); $admin_user = Auth::user(); // Was the accessory updated? if (DB::table('accessories_users')->where('id', '=', $accessory_user->id)->delete()) { $logaction->accessory_id = e($accessory->id); $logaction->location_id = null; $logaction->asset_type = 'accessory'; $logaction->user_id = e($admin_user->id); $logaction->note = e(Input::get('note')); $settings = Setting::getSettings(); if ($settings->slack_endpoint) { $slack_settings = ['username' => e($settings->botname), 'channel' => e($settings->slack_channel), 'link_names' => true]; $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings); try { $client->attach(['color' => 'good', 'fields' => [['title' => 'Checked In:', 'value' => strtoupper($logaction->asset_type) . ' <' . config('app.url') . '/admin/accessories/' . e($accessory->id) . '/view' . '|' . e($accessory->name) . '> checked in by <' . config('app.url') . '/admin/users/' . e($admin_user->id) . '/view' . '|' . e($admin_user->fullName()) . '>.'], ['title' => 'Note:', 'value' => e($logaction->note)]]])->send('Accessory Checked In'); } catch (Exception $e) { } } $log = $logaction->logaction('checkin from'); if (!is_null($accessory_user->assigned_to)) { $user = User::find($accessory_user->assigned_to); } $data['log_id'] = $logaction->id; $data['first_name'] = e($user->first_name); $data['item_name'] = e($accessory->name); $data['checkin_date'] = e($logaction->created_at); $data['item_tag'] = ''; $data['note'] = e($logaction->note); if ($accessory->checkin_email() == '1') { Mail::send('emails.checkin-asset', $data, function ($m) use($user) { $m->to($user->email, $user->first_name . ' ' . $user->last_name); $m->subject('Confirm Accessory Checkin'); }); } if ($backto == 'user') { return redirect()->to("admin/users/" . $return_to . '/view')->with('success', trans('admin/accessories/message.checkin.success')); } else { return redirect()->to("admin/accessories/" . $accessory->id . "/view")->with('success', trans('admin/accessories/message.checkin.success')); } } // Redirect to the accessory management page with error return redirect()->to("admin/accessories")->with('error', trans('admin/accessories/message.checkin.error')); }
/** * Soft-delete bulk users * * @author [A. Gianotto] [<*****@*****.**>] * @since [v1.0] * @return Redirect */ public function postBulkSave() { if (!Input::has('edit_user') || count(Input::has('edit_user')) == 0) { return redirect()->back()->with('error', 'No users selected'); } elseif (!Input::has('status_id') || count(Input::has('status_id')) == 0) { return redirect()->route('users')->with('error', 'No status selected'); } else { $user_raw_array = Input::get('edit_user'); $asset_array = array(); if (($key = array_search(Auth::user()->id, $user_raw_array)) !== false) { unset($user_raw_array[$key]); } if (!Auth::user()->isSuperUser()) { return redirect()->route('users')->with('error', trans('admin/users/message.insufficient_permissions')); } if (!config('app.lock_passwords')) { $users = User::whereIn('id', $user_raw_array)->get(); $assets = Asset::whereIn('assigned_to', $user_raw_array)->get(); $accessories = DB::table('accessories_users')->whereIn('assigned_to', $user_raw_array)->get(); $licenses = DB::table('license_seats')->whereIn('assigned_to', $user_raw_array)->get(); $license_array = array(); $accessory_array = array(); foreach ($assets as $asset) { $asset_array[] = $asset->id; // Update the asset log $logaction = new Actionlog(); $logaction->asset_id = $asset->id; $logaction->checkedout_to = $asset->assigned_to; $logaction->asset_type = 'hardware'; $logaction->user_id = Auth::user()->id; $logaction->note = 'Bulk checkin asset and delete user'; $logaction->logaction('checkin from'); Asset::whereIn('id', $asset_array)->update(array('status_id' => e(Input::get('status_id')), 'assigned_to' => null)); } foreach ($accessories as $accessory) { $accessory_array[] = $accessory->accessory_id; // Update the asset log $logaction = new Actionlog(); $logaction->accessory_id = $accessory->id; $logaction->checkedout_to = $accessory->assigned_to; $logaction->asset_type = 'accessory'; $logaction->user_id = Auth::user()->id; $logaction->note = 'Bulk checkin accessory and delete user'; $logaction->logaction('checkin from'); } foreach ($licenses as $license) { $license_array[] = $license->id; // Update the asset log $logaction = new Actionlog(); $logaction->asset_id = $license->id; $logaction->checkedout_to = $license->assigned_to; $logaction->asset_type = 'software'; $logaction->user_id = Auth::user()->id; $logaction->note = 'Bulk checkin license and delete user'; $logaction->logaction('checkin from'); } LicenseSeat::whereIn('id', $license_array)->update(['assigned_to' => null]); foreach ($users as $user) { $user->accessories()->sync(array()); $user->delete(); } return redirect()->route('users')->with('success', 'Your selected users have been deleted and their assets have been updated.'); } else { return redirect()->route('users')->with('error', 'Bulk delete is not enabled in this installation'); } } }
public function postAcceptAsset($logID = null) { // Check if the asset exists if (is_null($findlog = DB::table('asset_logs')->where('id', '=', $logID)->first())) { // Redirect to the asset management page return redirect()->to('account/view-assets')->with('error', trans('admin/hardware/message.does_not_exist')); } if ($findlog->accepted_id != '') { // Redirect to the asset management page return redirect()->to('account/view-assets')->with('error', trans('admin/users/message.error.asset_already_accepted')); } if (!Input::has('asset_acceptance')) { return redirect()->to('account/view-assets')->with('error', trans('admin/users/message.error.accept_or_decline')); } $user = Auth::user(); if ($user->id != $findlog->checkedout_to) { return redirect()->to('account/view-assets')->with('error', trans('admin/users/message.error.incorrect_user_accepted')); } $logaction = new Actionlog(); if (Input::get('asset_acceptance') == 'accepted') { $logaction_msg = 'accepted'; $accepted = "accepted"; $return_msg = trans('admin/users/message.accepted'); } else { $logaction_msg = 'declined'; $accepted = "rejected"; $return_msg = trans('admin/users/message.declined'); } // Asset if ($findlog->asset_id != '' && $findlog->asset_type == 'hardware') { $logaction->asset_id = $findlog->asset_id; $logaction->accessory_id = null; $logaction->asset_type = 'hardware'; if (Input::get('asset_acceptance') != 'accepted') { DB::table('assets')->where('id', $findlog->asset_id)->update(array('assigned_to' => null)); } // software } elseif ($findlog->asset_id != '' && $findlog->asset_type == 'software') { $logaction->asset_id = $findlog->asset_id; $logaction->accessory_id = null; $logaction->component_id = null; $logaction->asset_type = 'software'; // accessories } elseif ($findlog->accessory_id != '') { $logaction->asset_id = null; $logaction->component_id = null; $logaction->accessory_id = $findlog->accessory_id; $logaction->asset_type = 'accessory'; // accessories } elseif ($findlog->consumable_id != '') { $logaction->asset_id = null; $logaction->accessory_id = null; $logaction->component_id = null; $logaction->consumable_id = $findlog->consumable_id; $logaction->asset_type = 'consumable'; } elseif ($findlog->component_id != '') { $logaction->asset_id = null; $logaction->accessory_id = null; $logaction->consumable_id = null; $logaction->component_id = $findlog->component_id; $logaction->asset_type = 'component'; } $logaction->checkedout_to = $findlog->checkedout_to; $logaction->note = e(Input::get('note')); $logaction->user_id = $user->id; $logaction->accepted_at = date("Y-m-d h:i:s"); $log = $logaction->logaction($logaction_msg); $update_checkout = DB::table('asset_logs')->where('id', $findlog->id)->update(array('accepted_id' => $logaction->id)); $affected_asset = $logaction->assetlog; $affected_asset->accepted = $accepted; $affected_asset->save(); if ($update_checkout) { return redirect()->to('account/view-assets')->with('success', $return_msg); } else { return redirect()->to('account/view-assets')->with('error', 'Something went wrong '); } }