23 protected $name =
'asset-import:csv';
39 parent::__construct();
49 $filename = $this->argument(
'filename');
52 if (!$this->option(
'testrun')==
'true') {
53 $this->comment(
'======= Importing Assets from '.$filename.
' =========');
55 $this->comment(
'====== TEST ONLY Asset Import for '.$filename.
' ====');
56 $this->comment(
'============== NO DATA WILL BE WRITTEN ==============');
59 if (! ini_get(
"auto_detect_line_endings")) {
60 ini_set(
"auto_detect_line_endings",
'1');
63 $csv = Reader::createFromPath($this->argument(
'filename'));
64 $csv->setNewline(
"\r\n");
69 $nbInsert = $csv->each(
function ($row) use ($duplicates) {
75 if (array_key_exists(
'0',$row)) {
76 $user_name = trim($row[0]);
82 if (array_key_exists(
'1',$row)) {
83 $user_email = trim($row[1]);
89 if (array_key_exists(
'2',$row)) {
90 $user_username = trim($row[2]);
96 if (array_key_exists(
'3',$row)) {
97 $user_asset_asset_name = trim($row[3]);
99 $user_asset_asset_name =
'';
103 if (array_key_exists(
'4',$row)) {
104 $user_asset_category = trim($row[4]);
106 $user_asset_category =
'';
110 if (array_key_exists(
'5',$row)) {
111 $user_asset_name = trim($row[5]);
113 $user_asset_name =
'';
117 if (array_key_exists(
'6',$row)) {
118 $user_asset_mfgr = trim($row[6]);
120 $user_asset_mfgr =
'';
124 if (array_key_exists(
'7',$row)) {
125 $user_asset_modelno = trim($row[7]);
127 $user_asset_modelno =
'';
131 if (array_key_exists(
'8',$row)) {
132 $user_asset_serial = trim($row[8]);
134 $user_asset_serial =
'';
138 if (array_key_exists(
'9',$row)) {
139 $user_asset_tag = trim($row[9]);
141 $user_asset_tag =
'';
145 if (array_key_exists(
'10',$row)) {
146 $user_asset_location = trim($row[10]);
148 $user_asset_location =
'';
152 if (array_key_exists(
'11',$row)) {
153 $user_asset_notes = trim($row[11]);
155 $user_asset_notes =
'';
159 if (array_key_exists(
'12',$row)) {
161 $user_asset_purchase_date = date(
"Y-m-d 00:00:01", strtotime($row[12]));
163 $user_asset_purchase_date =
'';
166 $user_asset_purchase_date =
'';
170 if (array_key_exists(
'13',$row)) {
172 $user_asset_purchase_cost = trim($row[13]);
174 $user_asset_purchase_cost =
'';
177 $user_asset_purchase_cost =
'';
181 if (array_key_exists(
'14',$row)) {
183 $user_asset_company_name = trim($row[14]);
185 $user_asset_company_name=
'';
188 $user_asset_company_name =
'';
193 if (is_numeric($user_name)) {
194 $this->comment(
'User '.$user_name.
' is not a name - assume this user already exists');
200 } elseif ($user_name==
'') {
201 $this->comment(
'No user data provided - skipping user creation, just adding asset');
208 $first_name = $user_email_array[
'first_name'];
209 $last_name = $user_email_array[
'last_name'];
211 if ($user_email==
'') {
212 $user_email = $user_email_array[
'username'].
'@'.config(
'app.domain');
215 if ($user_username==
'') {
216 if ($this->option(
'username_format')==
'email') {
217 $user_username = $user_email;
220 $user_username = $user_name_array[
'username'];
227 $this->comment(
'Full Name: '.$user_name);
228 $this->comment(
'First Name: '.$first_name);
229 $this->comment(
'Last Name: '.$last_name);
230 $this->comment(
'Username: '.$user_username);
231 $this->comment(
'Email: '.$user_email);
232 $this->comment(
'Category Name: '.$user_asset_category);
233 $this->comment(
'Item: '.$user_asset_name);
234 $this->comment(
'Manufacturer ID: '.$user_asset_mfgr);
235 $this->comment(
'Model No: '.$user_asset_modelno);
236 $this->comment(
'Serial No: '.$user_asset_serial);
237 $this->comment(
'Asset Tag: '.$user_asset_tag);
238 $this->comment(
'Location: '.$user_asset_location);
239 $this->comment(
'Purchase Date: '.$user_asset_purchase_date);
240 $this->comment(
'Purchase Cost: '.$user_asset_purchase_cost);
241 $this->comment(
'Notes: '.$user_asset_notes);
242 $this->comment(
'Company Name: '.$user_asset_company_name);
244 $this->comment(
'------------- Action Summary ----------------');
246 if ($user_username!=
'') {
247 if ($user = User::MatchEmailOrUsername($user_username, $user_email)
248 ->whereNotNull(
'username')->first()) {
249 $this->comment(
'User '.$user_username.
' already exists');
251 $user = new \App\Models\User;
252 $password = substr(str_shuffle(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
254 $user->first_name = $first_name;
255 $user->last_name = $last_name;
256 $user->username = $user_username;
257 $user->email = $user_email;
258 $user->permissions =
'{user":1}';
259 $user->password = bcrypt($password);
260 $user->activated = 1;
262 $this->comment(
'User '.$first_name.
' created');
264 $this->comment(
'ERROR CREATING User '.$first_name.
' '.$last_name);
265 $this->comment($user->getErrors());
274 if ($location = Location::where(
'name', e($user_asset_location))->first()) {
275 $this->comment(
'Location '.$user_asset_location.
' already exists');
280 if ($user_asset_location!=
'')
282 $location->name = e($user_asset_location);
283 $location->address =
'';
284 $location->city =
'';
285 $location->state =
'';
286 $location->country =
'';
287 $location->user_id = 1;
289 if (!$this->option(
'testrun')==
'true') {
291 if ($location->save()) {
292 $this->comment(
'Location '.$user_asset_location.
' was created');
294 $this->comment(
'Something went wrong! Location '.$user_asset_location.
' was NOT created');
295 $this->comment($location->getErrors());
301 $this->comment(
'Location '.$user_asset_location.
' was (not) created - test run only');
306 $this->comment(
'No location given, so none created.');
311 if (e($user_asset_category)==
'') {
312 $category_name =
'Unnamed Category';
314 $category_name = e($user_asset_category);
318 if ($category = Category::where(
'name', e($category_name))->where(
'category_type',
'asset')->first()) {
319 $this->comment(
'Category '.$category_name.
' already exists');
323 $category->name = e($category_name);
324 $category->category_type =
'asset';
325 $category->user_id = 1;
327 if ($category->save()) {
328 $this->comment(
'Category '.$user_asset_category.
' was created');
330 $this->comment(
'Something went wrong! Category '.$user_asset_category.
' was NOT created');
331 $this->comment($category->getErrors());
337 if ($manufacturer = Manufacturer::where(
'name', e($user_asset_mfgr))->first()) {
338 $this->comment(
'Manufacturer '.$user_asset_mfgr.
' already exists');
341 $manufacturer->name = e($user_asset_mfgr);
342 $manufacturer->user_id = 1;
344 if ($manufacturer->save()) {
345 $this->comment(
'Manufacturer '.$user_asset_mfgr.
' was created');
347 $this->comment(
'Something went wrong! Manufacturer '.$user_asset_mfgr.
' was NOT created: '. $manufacturer->getErrors()->first());
353 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()) {
354 $this->comment(
'The Asset Model '.$user_asset_name.
' with model number '.$user_asset_modelno.
' already exists');
357 $asset_model->name = e($user_asset_name);
358 $asset_model->manufacturer_id = $manufacturer->id;
359 $asset_model->modelno = e($user_asset_modelno);
360 $asset_model->category_id = $category->id;
361 $asset_model->user_id = 1;
363 if ($asset_model->save()) {
364 $this->comment(
'Asset Model '.$user_asset_name.
' with model number '.$user_asset_modelno.
' was created');
366 $this->comment(
'Something went wrong! Asset Model '.$user_asset_name.
' was NOT created: '.$asset_model->getErrors()->first());
372 if ($user_asset_company_name!=
'') {
373 if ($company = Company::where(
'name', e($user_asset_company_name))->first()) {
374 $this->comment(
'Company '.$user_asset_company_name.
' already exists');
377 $company->name = e($user_asset_company_name);
379 if ($company->save()) {
380 $this->comment(
'Company '.$user_asset_company_name.
' was created');
382 $this->comment(
'Something went wrong! Company '.$user_asset_company_name.
' was NOT created: '.$company->getErrors()->first());
391 if ($asset = Asset::where(
'asset_tag', e($user_asset_tag))->first()) {
392 $this->comment(
'The Asset with asset tag '.$user_asset_tag.
' already exists');
394 $asset =
new Asset();
395 $asset->name = e($user_asset_asset_name);
396 if ($user_asset_purchase_date!=
'') {
397 $asset->purchase_date = $user_asset_purchase_date;
399 $asset->purchase_date = NULL;
401 if ($user_asset_purchase_cost!=
'') {
402 $asset->purchase_cost = ParseFloat(e($user_asset_purchase_cost));
404 $asset->purchase_cost = 0.00;
406 $asset->serial = e($user_asset_serial);
407 $asset->asset_tag = e($user_asset_tag);
408 $asset->model_id = $asset_model->id;
409 $asset->assigned_to = $user->id;
410 $asset->rtd_location_id = $location->id;
412 $asset->status_id = $status_id;
413 $asset->company_id = $company->id;
414 if ($user_asset_purchase_date!=
'') {
415 $asset->purchase_date = $user_asset_purchase_date;
417 $asset->purchase_date = NULL;
419 $asset->notes = e($user_asset_notes);
421 if ($asset->save()) {
422 $this->comment(
'Asset '.$user_asset_name.
' with serial number '.$user_asset_serial.
' was created');
424 $this->comment(
'Something went wrong! Asset '.$user_asset_name.
' was NOT created: '.$asset->getErrors()->first());
431 $this->comment(
'=====================================');
448 array(
'filename', InputArgument::REQUIRED,
'File for the CSV import.'),
461 array(
'email_format', null, InputOption::VALUE_REQUIRED,
'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname', null),
462 array(
'username_format', null, InputOption::VALUE_REQUIRED,
'The format of the username that should be generated. Options are firstname.lastname, firstname, filastname, email', null),
463 array(
'testrun', null, InputOption::VALUE_REQUIRED,
'Test the output without writing to the database or not.', null),
getArguments()
Get the console command arguments.
static generateFormattedNameFromFullName($format= 'filastname', $users_name)
getOptions()
Get the console command options.
fire()
Execute the console command.
__construct()
Create a new command instance.