/** * Import history * * This needs a LOT of love. It's done very inelegantly right now, and there are * a ton of optimizations that could (and should) be done. * * @author [A. Gianotto] [<*****@*****.**>] * @since [v3.3] * @return View */ public function postImportHistory(Request $request) { if (!ini_get("auto_detect_line_endings")) { ini_set("auto_detect_line_endings", '1'); } $assets = Asset::all(['asset_tag']); $csv = Reader::createFromPath(Input::file('user_import_csv')); $csv->setNewline("\r\n"); //get the first row, usually the CSV header //$headers = $csv->fetchOne(); $results = $csv->fetchAssoc(); $item = array(); $status = array(); foreach ($results as $row) { if (is_array($row)) { $row = array_change_key_case($row, CASE_LOWER); $asset_tag = Helper::array_smart_fetch($row, "asset tag"); if (!array_key_exists($asset_tag, $item)) { $item[$asset_tag] = array(); } $batch_counter = count($item[$asset_tag]); $item[$asset_tag][$batch_counter]['checkout_date'] = Carbon::parse(Helper::array_smart_fetch($row, "date"))->format('Y-m-d H:i:s'); $item[$asset_tag][$batch_counter]['asset_tag'] = Helper::array_smart_fetch($row, "asset tag"); $item[$asset_tag][$batch_counter]['name'] = Helper::array_smart_fetch($row, "name"); $item[$asset_tag][$batch_counter]['email'] = Helper::array_smart_fetch($row, "email"); $asset = Asset::where('asset_tag', '=', $asset_tag)->first(); $item[$asset_tag][$batch_counter]['asset_id'] = $asset->id; $base_username = User::generateFormattedNameFromFullName(Setting::getSettings()->username_format, $item[$asset_tag][$batch_counter]['name']); $user = User::where('username', '=', $base_username['username']); $user_query = ' on username ' . $base_username['username']; if ($request->input('match_firstnamelastname') == '1') { $firstnamedotlastname = User::generateFormattedNameFromFullName('firstname.lastname', $item[$asset_tag][$batch_counter]['name']); $item[$asset_tag][$batch_counter]['username'][] = $firstnamedotlastname['username']; $user->orWhere('username', '=', $firstnamedotlastname['username']); $user_query .= ', or on username ' . $firstnamedotlastname['username']; } if ($request->input('match_flastname') == '1') { $flastname = User::generateFormattedNameFromFullName('filastname', $item[$asset_tag][$batch_counter]['name']); $item[$asset_tag][$batch_counter]['username'][] = $flastname['username']; $user->orWhere('username', '=', $flastname['username']); $user_query .= ', or on username ' . $flastname['username']; } if ($request->input('match_firstname') == '1') { $firstname = User::generateFormattedNameFromFullName('firstname', $item[$asset_tag][$batch_counter]['name']); $item[$asset_tag][$batch_counter]['username'][] = $firstname['username']; $user->orWhere('username', '=', $firstname['username']); $user_query .= ', or on username ' . $firstname['username']; } if ($request->input('match_email') == '1') { if ($item[$asset_tag][$batch_counter]['email'] == '') { $item[$asset_tag][$batch_counter]['username'][] = $user_email = User::generateEmailFromFullName($item[$asset_tag][$batch_counter]['name']); $user->orWhere('username', '=', $user_email); $user_query .= ', or on username ' . $user_email; } } // A matching user was found if ($user = $user->first()) { $item[$asset_tag][$batch_counter]['checkedout_to'] = $user->id; $status['success'][] = 'Found user ' . Helper::array_smart_fetch($row, "name") . $user_query; if ($asset) { $item[$asset_tag][$batch_counter]['user_id'] = $user->id; Actionlog::firstOrCreate(array('asset_id' => $asset->id, 'asset_type' => 'hardware', 'user_id' => Auth::user()->id, 'note' => 'Checkout imported by ' . Auth::user()->fullName() . ' from history importer', 'checkedout_to' => $item[$asset_tag][$batch_counter]['user_id'], 'created_at' => $item[$asset_tag][$batch_counter]['checkout_date'], 'action_type' => 'checkout')); $asset->assigned_to = $user->id; $asset->save(); } else { $status['error'][] = 'Asset does not exist so no checkin log was created.'; } } else { $item[$asset_tag][$batch_counter]['checkedout_to'] = null; $status['error'][] = 'No matching user for ' . Helper::array_smart_fetch($row, "name"); } } } // Loop through and backfill the checkins foreach ($item as $key => $asset_batch) { $total_in_batch = count($asset_batch); for ($x = 0; $x < $total_in_batch; $x++) { $next = $x + 1; // Only do this if a matching user was found if ($asset_batch[$x]['checkedout_to'] != '') { if ($total_in_batch > 1 && $x < $total_in_batch && array_key_exists($next, $asset_batch)) { $checkin_date = Carbon::parse($asset_batch[$next]['checkout_date'])->subDay(1)->format('Y-m-d H:i:s'); $asset_batch[$x]['real_checkin'] = $checkin_date; Actionlog::firstOrCreate(array('asset_id' => $asset_batch[$x]['asset_id'], 'asset_type' => 'hardware', 'user_id' => Auth::user()->id, 'note' => 'Checkin imported by ' . Auth::user()->fullName() . ' from history importer', 'checkedout_to' => null, 'created_at' => $checkin_date, 'action_type' => 'checkin')); } } } } return View::make('hardware/history')->with('status', $status); }
public static function getExpiringWarrantee($days = 30) { return Asset::where('archived', '=', '0')->whereNotNull('warranty_months')->whereNotNull('purchase_date')->whereNull('deleted_at')->whereRaw(\DB::raw('DATE_ADD(`purchase_date`,INTERVAL `warranty_months` MONTH) <= DATE(NOW() + INTERVAL ' . $days . ' DAY) AND DATE_ADD(`purchase_date`,INTERVAL `warranty_months` MONTH) > NOW()'))->orderBy('purchase_date', 'ASC')->get(); }
/** * Searches the assets table by asset tag, and redirects if it finds one * * @author [A. Gianotto] [<*****@*****.**>] * @since [v3.0] * @return Redirect */ public function getAssetByTag() { if (Input::get('topsearch') == "true") { $topsearch = true; } else { $topsearch = false; } if ($asset = Asset::where('asset_tag', '=', Input::get('assetTag'))->first()) { return redirect()->route('view/hardware', $asset->id)->with('topsearch', $topsearch); } return redirect()->to('hardware')->with('error', trans('admin/hardware/message.does_not_exist')); }
/** * Execute the console command. * * @return mixed */ public function fire() { $filename = $this->argument('filename'); if (!$this->option('testrun') == 'true') { $this->comment('======= Importing Assets from ' . $filename . ' ========='); } else { $this->comment('====== TEST ONLY Asset Import for ' . $filename . ' ===='); $this->comment('============== NO DATA WILL BE WRITTEN =============='); } if (!ini_get("auto_detect_line_endings")) { ini_set("auto_detect_line_endings", '1'); } $csv = Reader::createFromPath($this->argument('filename')); $csv->setNewline("\r\n"); $csv->setOffset(1); $duplicates = ''; // Loop through the records $nbInsert = $csv->each(function ($row) use($duplicates) { $status_id = 1; // Let's just map some of these entries to more user friendly words // User's name if (array_key_exists('0', $row)) { $user_name = trim($row[0]); } else { $user_name = ''; } // User's email if (array_key_exists('1', $row)) { $user_email = trim($row[1]); } else { $user_email = ''; } // User's email if (array_key_exists('2', $row)) { $user_username = trim($row[2]); } else { $user_username = ''; } // Asset Name if (array_key_exists('3', $row)) { $user_asset_asset_name = trim($row[3]); } else { $user_asset_asset_name = ''; } // Asset Category if (array_key_exists('4', $row)) { $user_asset_category = trim($row[4]); } else { $user_asset_category = ''; } // Asset Name if (array_key_exists('5', $row)) { $user_asset_name = trim($row[5]); } else { $user_asset_name = ''; } // Asset Manufacturer if (array_key_exists('6', $row)) { $user_asset_mfgr = trim($row[6]); } else { $user_asset_mfgr = ''; } // Asset model number if (array_key_exists('7', $row)) { $user_asset_modelno = trim($row[7]); } else { $user_asset_modelno = ''; } // Asset serial number if (array_key_exists('8', $row)) { $user_asset_serial = trim($row[8]); } else { $user_asset_serial = ''; } // Asset tag if (array_key_exists('9', $row)) { $user_asset_tag = trim($row[9]); } else { $user_asset_tag = ''; } // Asset location if (array_key_exists('10', $row)) { $user_asset_location = trim($row[10]); } else { $user_asset_location = ''; } // Asset notes if (array_key_exists('11', $row)) { $user_asset_notes = trim($row[11]); } else { $user_asset_notes = ''; } // Asset purchase date if (array_key_exists('12', $row)) { if ($row[12] != '') { $user_asset_purchase_date = date("Y-m-d 00:00:01", strtotime($row[12])); } else { $user_asset_purchase_date = ''; } } else { $user_asset_purchase_date = ''; } // Asset purchase cost if (array_key_exists('13', $row)) { if ($row[13] != '') { $user_asset_purchase_cost = trim($row[13]); } else { $user_asset_purchase_cost = ''; } } else { $user_asset_purchase_cost = ''; } // Asset Company Name if (array_key_exists('14', $row)) { if ($row[14] != '') { $user_asset_company_name = trim($row[14]); } else { $user_asset_company_name = ''; } } else { $user_asset_company_name = ''; } // A number was given instead of a name if (is_numeric($user_name)) { $this->comment('User ' . $user_name . ' is not a name - assume this user already exists'); $user_username = ''; $first_name = ''; $last_name = ''; // No name was given } elseif ($user_name == '') { $this->comment('No user data provided - skipping user creation, just adding asset'); $first_name = ''; $last_name = ''; //$user_username = ''; } else { $user_email_array = User::generateFormattedNameFromFullName($this->option('email_format'), $user_name); $first_name = $user_email_array['first_name']; $last_name = $user_email_array['last_name']; if ($user_email == '') { $user_email = $user_email_array['username'] . '@' . config('app.domain'); } if ($user_username == '') { if ($this->option('username_format') == 'email') { $user_username = $user_email; } else { $user_name_array = User::generateFormattedNameFromFullName($this->option('username_format'), $user_name); $user_username = $user_name_array['username']; } } } $this->comment('Full Name: ' . $user_name); $this->comment('First Name: ' . $first_name); $this->comment('Last Name: ' . $last_name); $this->comment('Username: '******'Email: ' . $user_email); $this->comment('Category Name: ' . $user_asset_category); $this->comment('Item: ' . $user_asset_name); $this->comment('Manufacturer ID: ' . $user_asset_mfgr); $this->comment('Model No: ' . $user_asset_modelno); $this->comment('Serial No: ' . $user_asset_serial); $this->comment('Asset Tag: ' . $user_asset_tag); $this->comment('Location: ' . $user_asset_location); $this->comment('Purchase Date: ' . $user_asset_purchase_date); $this->comment('Purchase Cost: ' . $user_asset_purchase_cost); $this->comment('Notes: ' . $user_asset_notes); $this->comment('Company Name: ' . $user_asset_company_name); $this->comment('------------- Action Summary ----------------'); if ($user_username != '') { if ($user = User::MatchEmailOrUsername($user_username, $user_email)->whereNotNull('username')->first()) { $this->comment('User ' . $user_username . ' already exists'); } else { $user = new \App\Models\User(); $password = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20); $user->first_name = $first_name; $user->last_name = $last_name; $user->username = $user_username; $user->email = $user_email; $user->permissions = '{user":1}'; $user->password = bcrypt($password); $user->activated = 1; if ($user->save()) { $this->comment('User ' . $first_name . ' created'); } else { $this->error('ERROR CREATING User ' . $first_name . ' ' . $last_name); $this->error($user->getErrors()); } } } else { $user = new User(); } // Check for the location match and create it if it doesn't exist if ($location = Location::where('name', e($user_asset_location))->first()) { $this->comment('Location ' . $user_asset_location . ' already exists'); } else { $location = new Location(); if ($user_asset_location != '') { $location->name = e($user_asset_location); $location->address = ''; $location->city = ''; $location->state = ''; $location->country = ''; $location->user_id = 1; if (!$this->option('testrun') == 'true') { if ($location->save()) { $this->comment('Location ' . $user_asset_location . ' was created'); } else { $this->error('Something went wrong! Location ' . $user_asset_location . ' was NOT created'); $this->error($location->getErrors()); } } else { $this->comment('Location ' . $user_asset_location . ' was (not) created - test run only'); } } else { $this->comment('No location given, so none created.'); } } if (e($user_asset_category) == '') { $category_name = 'Unnamed Category'; } else { $category_name = e($user_asset_category); } // Check for the category match and create it if it doesn't exist if ($category = Category::where('name', e($category_name))->where('category_type', 'asset')->first()) { $this->comment('Category ' . $category_name . ' already exists'); } else { $category = new Category(); $category->name = e($category_name); $category->category_type = 'asset'; $category->user_id = 1; if ($category->save()) { $this->comment('Category ' . $user_asset_category . ' was created'); } else { $this->error('Something went wrong! Category ' . $user_asset_category . ' was NOT created'); $this->error($category->getErrors()); } } // Check for the manufacturer match and create it if it doesn't exist if ($manufacturer = Manufacturer::where('name', e($user_asset_mfgr))->first()) { $this->comment('Manufacturer ' . $user_asset_mfgr . ' already exists'); } else { $manufacturer = new Manufacturer(); $manufacturer->name = e($user_asset_mfgr); $manufacturer->user_id = 1; if ($manufacturer->save()) { $this->comment('Manufacturer ' . $user_asset_mfgr . ' was created'); } else { $this->error('Something went wrong! Manufacturer ' . $user_asset_mfgr . ' was NOT created: ' . $manufacturer->getErrors()->first()); } } // Check for the asset model match and create it if it doesn't exist if ($asset_model = AssetModel::where('name', e($user_asset_name))->where('modelno', e($user_asset_modelno))->where('category_id', $category->id)->where('manufacturer_id', $manufacturer->id)->first()) { $this->comment('The Asset Model ' . $user_asset_name . ' with model number ' . $user_asset_modelno . ' already exists'); } else { $asset_model = new AssetModel(); $asset_model->name = e($user_asset_name); $asset_model->manufacturer_id = $manufacturer->id; $asset_model->modelno = e($user_asset_modelno); $asset_model->category_id = $category->id; $asset_model->user_id = 1; if ($asset_model->save()) { $this->comment('Asset Model ' . $user_asset_name . ' with model number ' . $user_asset_modelno . ' was created'); } else { $this->error('Something went wrong! Asset Model ' . $user_asset_name . ' was NOT created: ' . $asset_model->getErrors()->first()); } } // Check for the asset company match and create it if it doesn't exist if ($user_asset_company_name != '') { if ($company = Company::where('name', e($user_asset_company_name))->first()) { $this->comment('Company ' . $user_asset_company_name . ' already exists'); } else { $company = new Company(); $company->name = e($user_asset_company_name); if ($company->save()) { $this->comment('Company ' . $user_asset_company_name . ' was created'); } else { $this->error('Something went wrong! Company ' . $user_asset_company_name . ' was NOT created: ' . $company->getErrors()->first()); } } } else { $company = new Company(); } // Check for the asset match and create it if it doesn't exist if ($asset = Asset::where('asset_tag', e($user_asset_tag))->first()) { $this->comment('The Asset with asset tag ' . $user_asset_tag . ' already exists'); } else { $asset = new Asset(); $asset->name = e($user_asset_asset_name); if ($user_asset_purchase_date != '') { $asset->purchase_date = $user_asset_purchase_date; } else { $asset->purchase_date = null; } if ($user_asset_purchase_cost != '') { $asset->purchase_cost = ParseFloat(e($user_asset_purchase_cost)); } else { $asset->purchase_cost = 0.0; } $asset->serial = e($user_asset_serial); $asset->asset_tag = e($user_asset_tag); $asset->model_id = $asset_model->id; $asset->assigned_to = $user->id; $asset->rtd_location_id = $location->id; $asset->user_id = 1; $asset->status_id = $status_id; $asset->company_id = $company->id; if ($user_asset_purchase_date != '') { $asset->purchase_date = $user_asset_purchase_date; } else { $asset->purchase_date = null; } $asset->notes = e($user_asset_notes); if ($asset->save()) { $this->comment('Asset ' . $user_asset_name . ' with serial number ' . $user_asset_serial . ' was created'); } else { $this->error('Something went wrong! Asset ' . $user_asset_name . ' was NOT created: ' . $asset->getErrors()->first()); } } $this->comment('====================================='); return true; }); }
/** * Return JSON containing a list of assets assigned to a user. * * @author [A. Gianotto] [<*****@*****.**>] * @since [v3.0] * @return string JSON */ public function getAssetList($userId) { $assets = Asset::where('assigned_to', '=', $userId)->with('model')->get(); return response()->json($assets); }
/** * Get the asset information to present to the model view detail page * * @author [A. Gianotto] [<*****@*****.**>] * @since [v2.0] * @param int $modelId * @return String JSON */ public function getDataView($modelID) { $assets = Asset::where('model_id', '=', $modelID)->with('company'); if (Input::has('search')) { $assets = $assets->TextSearch(e(Input::get('search'))); } if (Input::has('offset')) { $offset = e(Input::get('offset')); } else { $offset = 0; } if (Input::has('limit')) { $limit = e(Input::get('limit')); } else { $limit = 50; } $allowed_columns = ['name', 'serial', 'asset_tag']; $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at'; $assets = $assets->orderBy($sort, $order); $assetsCount = $assets->count(); $assets = $assets->skip($offset)->take($limit)->get(); $rows = array(); foreach ($assets as $asset) { $actions = ''; if ($asset->assetstatus) { if ($asset->assetstatus->deployable != 0) { if ($asset->assigned_to != '' && $asset->assigned_to > 0) { $actions = '<a href="' . route('checkin/hardware', $asset->id) . '" class="btn btn-primary btn-sm">' . trans('general.checkin') . '</a>'; } else { $actions = '<a href="' . route('checkout/hardware', $asset->id) . '" class="btn btn-info btn-sm">' . trans('general.checkout') . '</a>'; } } } $rows[] = array('id' => $asset->id, 'name' => (string) link_to('/hardware/' . $asset->id . '/view', $asset->showAssetName()), 'asset_tag' => (string) link_to('hardware/' . $asset->id . '/view', $asset->asset_tag), 'serial' => $asset->serial, 'assigned_to' => $asset->assigned_to ? (string) link_to('/admin/users/' . $asset->assigned_to . '/view', $asset->assigneduser->fullName()) : '', 'actions' => $actions, 'companyName' => Company::getName($asset)); } $data = array('total' => $assetsCount, 'rows' => $rows); return $data; }
/** * Return JSON containing a list of assets assigned to a user. * * @author [A. Gianotto] [<*****@*****.**>] * @since [v3.0] * @return string JSON */ public function getAssetList($userId) { $assets = Asset::where('assigned_to', '=', $userId)->get(); return response()->json($assets); //$foo = Asset::where('assigned_to','=',$userId)->get(); //print_r($foo); }