public function testAssetModelAdd() { $assetmodel = factory(AssetModel::class, 'assetmodel')->make(); $values = ['name' => $assetmodel->name, 'manufacturer_id' => $assetmodel->manufacturer_id, 'category_id' => $assetmodel->category_id, 'eol' => $assetmodel->eol]; AssetModel::create($values); $this->tester->seeRecord('models', $values); }
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $rules = ['name' => 'min:2|max:255', 'model_id' => 'required|integer', 'status_id' => 'required|integer', 'company_id' => 'integer', 'warranty_months' => 'integer|min:0|max:240', 'physical' => 'integer', 'checkout_date' => 'date', 'checkin_date' => 'date', 'supplier_id' => 'integer', 'status' => 'integer', 'asset_tag' => 'required']; $model = AssetModel::find($this->request->get('model_id')); if ($model && $model->fieldset) { $rules += $model->fieldset->validation_rules(); } return $rules; }
public static function modelList() { $models = AssetModel::with('manufacturer')->get(); $model_array[''] = trans('general.select_model'); foreach ($models as $model) { $model_array[$model->id] = $model->displayModelName(); } return $model_array; }
/** * Run the migrations. * * @return void */ public function up() { DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $f2 = new \App\Models\CustomFieldset(['name' => "Asset with MAC Address"]); $f2->timestamps = false; //when this model was first created, it had no timestamps. But later on it gets them. if (!$f2->save()) { throw new Exception("couldn't save customfieldset"); } $macid = DB::table('custom_fields')->insertGetId(['name' => "MAC Address", 'format' => \App\Models\CustomField::$PredefinedFormats['MAC'], 'element' => 'text']); if (!$macid) { throw new Exception("Can't save MAC Custom field: {$macid}"); } $f2->fields()->attach($macid, ['required' => false, 'order' => 1]); \App\Models\AssetModel::where(["show_mac_address" => true])->update(["fieldset_id" => $f2->id]); Schema::table('assets', function (Blueprint $table) { $table->renameColumn('mac_address', '_snipeit_mac_address'); }); // DB::statement("ALTER TABLE assets CHANGE mac_address _snipeit_mac_address varchar(255)"); $ans = Schema::table("models", function (Blueprint $table) { $table->renameColumn('show_mac_address', 'deprecated_mac_address'); }); }
/** * Execute the console command. * * @return mixed */ public function handle() { if ($this->confirm("\n****************************************************\nTHIS WILL DELETE ALL OF THE DATA IN YOUR DATABASE. \nThere is NO undo. This WILL destroy ALL of your data. \n****************************************************\n\nDo you wish to continue? No backsies! [y|N]")) { if ($this->option('soft')) { Accessory::getQuery()->delete(); Asset::getQuery()->delete(); Category::getQuery()->delete(); Company::getQuery()->delete(); Component::getQuery()->delete(); Consumable::getQuery()->delete(); Depreciation::getQuery()->delete(); License::getQuery()->delete(); LicenseSeat::getQuery()->delete(); Location::getQuery()->delete(); Manufacturer::getQuery()->delete(); AssetModel::getQuery()->delete(); Statuslabel::getQuery()->delete(); Supplier::getQuery()->delete(); Group::getQuery()->delete(); DB::statement('delete from accessories_users'); DB::statement('delete from asset_logs'); DB::statement('delete from asset_maintenances'); DB::statement('delete from asset_uploads'); DB::statement('delete from consumables_users'); DB::statement('delete from custom_field_custom_fieldset'); DB::statement('delete from custom_fields'); DB::statement('delete from custom_fieldsets'); DB::statement('delete from components_assets'); DB::statement('delete from password_resets'); DB::statement('delete from requested_assets'); DB::statement('delete from requests'); DB::statement('delete from throttle'); DB::statement('delete from users_groups'); DB::statement('delete from users WHERE id!=1'); } else { \DB::statement('drop table IF EXISTS accessories_users'); \DB::statement('drop table IF EXISTS accessories'); \DB::statement('drop table IF EXISTS asset_logs'); \DB::statement('drop table IF EXISTS asset_maintenances'); \DB::statement('drop table IF EXISTS asset_uploads'); \DB::statement('drop table IF EXISTS assets'); \DB::statement('drop table IF EXISTS categories'); \DB::statement('drop table IF EXISTS companies'); \DB::statement('drop table IF EXISTS consumables_users'); \DB::statement('drop table IF EXISTS consumables'); \DB::statement('drop table IF EXISTS custom_field_custom_fieldset'); \DB::statement('drop table IF EXISTS custom_fields'); \DB::statement('drop table IF EXISTS custom_fieldsets'); \DB::statement('drop table IF EXISTS depreciations'); \DB::statement('drop table IF EXISTS groups'); \DB::statement('drop table IF EXISTS history'); \DB::statement('drop table IF EXISTS components'); \DB::statement('drop table IF EXISTS components_assets'); \DB::statement('drop table IF EXISTS license_seats'); \DB::statement('drop table IF EXISTS licenses'); \DB::statement('drop table IF EXISTS locations'); \DB::statement('drop table IF EXISTS manufacturers'); \DB::statement('drop table IF EXISTS models'); \DB::statement('drop table IF EXISTS migrations'); \DB::statement('drop table IF EXISTS password_resets'); \DB::statement('drop table IF EXISTS requested_assets'); \DB::statement('drop table IF EXISTS requests'); \DB::statement('drop table IF EXISTS settings'); \DB::statement('drop table IF EXISTS status_labels'); \DB::statement('drop table IF EXISTS suppliers'); \DB::statement('drop table IF EXISTS throttle'); \DB::statement('drop table IF EXISTS users_groups'); \DB::statement('drop table IF EXISTS users'); } } }
<a href='#' data-toggle="modal" data-target="#createModal" data-dependency="model" data-select="model_select_id" class="btn btn-sm btn-default">New</a> <span class="mac_spinner" style="padding-left: 10px; color: green; display:none; width: 30px;"><i class="fa fa-spinner fa-spin"></i> </span> </div> </div> <div id='custom_fields_content'> <!-- Custom Fields --> @if ($asset->model && $asset->model->fieldset) <?php $model = $asset->model; ?> @endif @if (Input::old('model_id')) <?php $model = \App\Models\AssetModel::find(Input::old('model_id')); ?> @elseif (isset($selected_model)) <?php $model = $selected_model; ?> @endif @if (isset($model) && $model) @include("models/custom_fields_form",["model" => $model]) @endif </div> <!-- Status --> <div class="form-group {{ $errors->has('status_id') ? ' has-error' : '' }}"> <label for="status_id" class="col-md-3 control-label">{{ trans('admin/hardware/form.status') }}</label> <div class="col-md-7 col-sm-11{{ (\App\Helpers\Helper::checkIfRequired($asset, 'status_id')) ? ' required' : '' }}">
/** * Validate and process asset edit form. * * @author [A. Gianotto] [<*****@*****.**>] * @param int $assetId * @since [v1.0] * @return Redirect */ public function postEdit(AssetRequest $request, $assetId = null) { // Check if the asset exists if (!($asset = Asset::find($assetId))) { // Redirect to the asset management page with error return redirect()->to('hardware')->with('error', trans('admin/hardware/message.does_not_exist')); } elseif (!Company::isCurrentUserHasAccess($asset)) { return redirect()->to('hardware')->with('error', trans('general.insufficient_permissions')); } if ($request->has('status_id')) { $asset->status_id = e($request->input('status_id')); } else { $asset->status_id = null; } if ($request->has('warranty_months')) { $asset->warranty_months = e($request->input('warranty_months')); } else { $asset->warranty_months = null; } if ($request->has('purchase_cost')) { $asset->purchase_cost = e(Helper::formatCurrencyOutput($request->input('purchase_cost'))); } else { $asset->purchase_cost = null; } if ($request->has('purchase_date')) { $asset->purchase_date = e($request->input('purchase_date')); } else { $asset->purchase_date = null; } if ($request->has('supplier_id')) { $asset->supplier_id = e($request->input('supplier_id')); } else { $asset->supplier_id = null; } // If the box isn't checked, it's not in the request at all. $asset->requestable = $request->has('requestable'); if ($request->has('rtd_location_id')) { $asset->rtd_location_id = e($request->input('rtd_location_id')); } else { $asset->rtd_location_id = null; } if ($request->has('image_delete')) { unlink(public_path() . '/uploads/assets/' . $asset->image); $asset->image = ''; } // Update the asset data $asset->name = e($request->input('name')); $asset->serial = e($request->input('serial')); $asset->company_id = Company::getIdForCurrentUser(e($request->input('company_id'))); $asset->model_id = e($request->input('model_id')); $asset->order_number = e($request->input('order_number')); $asset->asset_tag = e($request->input('asset_tag')); $asset->notes = e($request->input('notes')); $asset->physical = '1'; // Update the image if (Input::has('image')) { $image = $request->input('image'); // See postCreate for more explaination of the following. $header = explode(';', $image, 2)[0]; $extension = substr($header, strpos($header, '/') + 1); $image = substr($image, strpos($image, ',') + 1); $directory = public_path('uploads/assets/'); // Check if the uploads directory exists. If not, try to create it. if (!file_exists($directory)) { mkdir($directory, 0755); } $file_name = str_random(25) . "." . $extension; $path = public_path('uploads/assets/' . $file_name); try { Image::make($image)->resize(500, 500, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($path); $asset->image = $file_name; } catch (\Exception $e) { \Input::flash(); $messageBag = new \Illuminate\Support\MessageBag(); $messageBag->add('image', $e->getMessage()); \Session()->flash('errors', \Session::get('errors', new \Illuminate\Support\ViewErrorBag())->put('default', $messageBag)); return response()->json(['image' => $e->getMessage()], 422); } $asset->image = $file_name; } // Update custom fields in the database. // Validation for these fields is handlded through the AssetRequest form request // FIXME: No idea why this is returning a Builder error on db_column_name. // Need to investigate and fix. Using static method for now. $model = AssetModel::find($request->get('model_id')); if ($model->fieldset) { foreach ($model->fieldset->fields as $field) { if ($field->field_encrypted == '1') { if (Gate::allows('admin')) { $asset->{\App\Models\CustomField::name_to_db_name($field->name)} = \Crypt::encrypt(e($request->input(\App\Models\CustomField::name_to_db_name($field->name)))); } } else { $asset->{\App\Models\CustomField::name_to_db_name($field->name)} = e($request->input(\App\Models\CustomField::name_to_db_name($field->name))); } } } if ($asset->save()) { // Redirect to the new asset page \Session::flash('success', trans('admin/hardware/message.update.success')); return response()->json(['redirect_url' => route("view/hardware", $assetId)]); } \Input::flash(); \Session::flash('errors', $asset->getErrors()); return response()->json(['errors' => $asset->getErrors()], 500); }
public function run() { AssetModel::truncate(); factory(AssetModel::class, 'assetmodel', 5)->create(); }
/** * Select the asset model if it exists, otherwise create it. * * @author Daniel Melzter * @since 3.0 * @param array * @param $category Category * @param $manufacturer Manufacturer * @return Model * @internal param $asset_modelno string */ public function createOrFetchAssetModel(array $row, $category, $manufacturer) { $asset_model_name = $this->array_smart_fetch($row, "model name"); $asset_modelno = $this->array_smart_fetch($row, "model number"); if (empty($asset_model_name)) { $asset_model_name = 'Unknown'; } if (empty($asset_modelno)) { $asset_modelno = ''; } $this->log('Model Name: ' . $asset_model_name); $this->log('Model No: ' . $asset_modelno); foreach ($this->asset_models as $tempmodel) { if (strcasecmp($tempmodel->name, $asset_model_name) == 0 && $tempmodel->modelno == $asset_modelno && $tempmodel->category_id == $category->id && $tempmodel->manufacturer_id == $manufacturer->id) { $this->log('A matching model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' already exists'); return $tempmodel; } } $asset_model = new AssetModel(); $asset_model->name = $asset_model_name; $asset_model->manufacturer_id = $manufacturer->id; $asset_model->modelno = $asset_modelno; $asset_model->category_id = $category->id; $asset_model->user_id = $this->option('user_id'); if (!$this->option('testrun')) { if ($asset_model->save()) { $this->asset_models->add($asset_model); $this->log('Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' was created'); return $asset_model; } else { $this->jsonError('Asset Model "' . $asset_model_name . '"', $asset_model->getErrors()); return $asset_model; } } else { $this->asset_models->add($asset_model); return $asset_model; } }
/** * Execute the console command. * * @return mixed */ public function handle() { $force = $this->option('force'); if ($this->confirm("\n****************************************************\nTHIS WILL PURGE ALL SOFT-DELETED ITEMS IN YOUR SYSTEM. \nThere is NO undo. This WILL permanently destroy \nALL of your deleted data. \n****************************************************\n\nDo you wish to continue? No backsies! [y|N]") || $force == 'true') { /** * Delete assets */ $assets = Asset::whereNotNull('deleted_at')->withTrashed()->get(); $assetcount = $assets->count(); $this->info($assets->count() . ' assets purged.'); $asset_assoc = 0; $asset_maintenances = 0; foreach ($assets as $asset) { $this->info('- Asset "' . $asset->showAssetName() . '" deleted.'); $asset_assoc += $asset->assetlog()->count(); $asset->assetlog()->forceDelete(); $asset_maintenances += $asset->assetmaintenances()->count(); $asset->assetmaintenances()->forceDelete(); $asset->forceDelete(); } $this->info($asset_assoc . ' corresponding log records purged.'); $this->info($asset_maintenances . ' corresponding maintenance records purged.'); $locations = Location::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($locations->count() . ' locations purged.'); foreach ($locations as $location) { $this->info('- Location "' . $location->name . '" deleted.'); $location->forceDelete(); } $accessories = Accessory::whereNotNull('deleted_at')->withTrashed()->get(); $accessory_assoc = 0; $this->info($accessories->count() . ' accessories purged.'); foreach ($accessories as $accessory) { $this->info('- Accessory "' . $accessory->name . '" deleted.'); $accessory_assoc += $accessory->assetlog()->count(); $accessory->assetlog()->forceDelete(); $accessory->forceDelete(); } $this->info($accessory_assoc . ' corresponding log records purged.'); $consumables = Consumable::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($consumables->count() . ' consumables purged.'); foreach ($consumables as $consumable) { $this->info('- Consumable "' . $consumable->name . '" deleted.'); $consumable->assetlog()->forceDelete(); $consumable->forceDelete(); } $components = Component::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($components->count() . ' components purged.'); foreach ($components as $component) { $this->info('- Component "' . $component->name . '" deleted.'); $component->assetlog()->forceDelete(); $component->forceDelete(); } $licenses = License::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($licenses->count() . ' licenses purged.'); foreach ($licenses as $license) { $this->info('- License "' . $license->name . '" deleted.'); $license->assetlog()->forceDelete(); $license->licenseseats()->forceDelete(); $license->forceDelete(); } $models = AssetModel::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($models->count() . ' asset models purged.'); foreach ($models as $model) { $this->info('- Asset Model "' . $model->name . '" deleted.'); $model->forceDelete(); } $categories = Category::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($categories->count() . ' categories purged.'); foreach ($categories as $category) { $this->info('- Category "' . $category->name . '" deleted.'); $category->forceDelete(); } $suppliers = Supplier::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($suppliers->count() . ' suppliers purged.'); foreach ($suppliers as $supplier) { $this->info('- Supplier "' . $supplier->name . '" deleted.'); $supplier->forceDelete(); } $users = User::whereNotNull('deleted_at')->where('show_in_list', '!=', '0')->withTrashed()->get(); $this->info($users->count() . ' users purged.'); $user_assoc = 0; foreach ($users as $user) { $this->info('- User "' . $user->username . '" deleted.'); $user_assoc += $user->userlog()->count(); $user->userlog()->forceDelete(); $user->forceDelete(); } $this->info($user_assoc . ' corresponding user log records purged.'); $manufacturers = Manufacturer::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($manufacturers->count() . ' manufacturers purged.'); foreach ($manufacturers as $manufacturer) { $this->info('- Manufacturer "' . $manufacturer->name . '" deleted.'); $manufacturer->forceDelete(); } $status_labels = Statuslabel::whereNotNull('deleted_at')->withTrashed()->get(); $this->info($status_labels->count() . ' status labels purged.'); foreach ($status_labels as $status_label) { $this->info('- Status Label "' . $status_label->name . '" deleted.'); $status_label->forceDelete(); } } else { $this->info('Action canceled. Nothing was purged.'); } }
/** * 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; }); }
/** * Validates a custom fieldset and then deletes if it has no models associated. * * @author [Brady Wetherington] [<*****@*****.**>] * @param int $id * @since [v1.8] * @return View */ public function destroy($id) { // $fieldset = CustomFieldset::find($id); $models = AssetModel::where("fieldset_id", "=", $id); if ($models->count() == 0) { $fieldset->delete(); return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.fieldset.delete.success')); } else { return redirect()->route("admin.custom_fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use')); //->with("models",$models); } }
/** * Get the JSON response to populate the data tables on the * Asset Model listing page. * * @author [A. Gianotto] [<*****@*****.**>] * @since [v2.0] * @param string $status * @return String JSON */ public function getDatatable($status = null) { $models = AssetModel::with('category', 'assets', 'depreciation'); switch ($status) { case 'Deleted': $models->withTrashed()->Deleted(); break; } if (Input::has('search')) { $models = $models->TextSearch(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 = ['id', 'name', 'modelno']; $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at'; $models = $models->orderBy($sort, $order); $modelCount = $models->count(); $models = $models->skip($offset)->take($limit)->get(); $rows = array(); foreach ($models as $model) { if ($model->deleted_at == '') { $actions = '<div style=" white-space: nowrap;"><a href="' . route('clone/model', $model->id) . '" class="btn btn-info btn-sm" title="Clone Model" data-toggle="tooltip"><i class="fa fa-clone"></i></a> <a href="' . route('update/model', $model->id) . '" class="btn btn-warning btn-sm" style="margin-right:5px;"><i class="fa fa-pencil icon-white"></i></a><a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="' . route('delete/model', $model->id) . '" data-content="' . trans('admin/models/message.delete.confirm') . '" data-title="' . trans('general.delete') . ' ' . htmlspecialchars($model->name) . '?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></div>'; } else { $actions = '<a href="' . route('restore/model', $model->id) . '" class="btn btn-warning btn-sm"><i class="fa fa-recycle icon-white"></i></a>'; } $rows[] = array('id' => $model->id, 'manufacturer' => (string) link_to('/admin/settings/manufacturers/' . $model->manufacturer->id . '/view', $model->manufacturer->name), 'name' => (string) link_to('/hardware/models/' . $model->id . '/view', $model->name), 'image' => $model->image != '' ? '<img src="' . config('app.url') . '/uploads/models/' . $model->image . '" height=50 width=50>' : '', 'modelnumber' => $model->modelno, 'numassets' => $model->assets->count(), 'depreciation' => $model->depreciation && $model->depreciation->id > 0 ? $model->depreciation->name . ' (' . $model->depreciation->months . ')' : trans('general.no_depreciation'), 'category' => $model->category ? $model->category->name : '', 'eol' => $model->eol ? $model->eol . ' ' . trans('general.months') : '', 'note' => $model->getNote(), 'actions' => $actions); } $data = array('total' => $modelCount, 'rows' => $rows); return $data; }