/** * Execute the console command. * * @return mixed */ public function fire() { $filename = $this->argument('filename'); if (!$this->option('web-importer')) { $logFile = $this->option('logfile'); \Log::useFiles($logFile); if ($this->option('testrun')) { $this->comment('====== TEST ONLY Asset Import for ' . $filename . ' ===='); $this->comment('============== NO DATA WILL BE WRITTEN =============='); } else { $this->comment('======= Importing Assets from ' . $filename . ' ========='); } } if (!ini_get("auto_detect_line_endings")) { ini_set("auto_detect_line_endings", '1'); } $csv = Reader::createFromPath($this->argument('filename')); $csv->setNewline("\r\n"); $results = $csv->fetchAssoc(); $newarray = null; foreach ($results as $index => $arraytoNormalize) { $internalnewarray = array_change_key_case($arraytoNormalize); $newarray[$index] = $internalnewarray; } $this->locations = Location::All(['name', 'id']); $this->categories = Category::All(['name', 'category_type', 'id']); $this->manufacturers = Manufacturer::All(['name', 'id']); $this->asset_models = AssetModel::All(['name', 'modelno', 'category_id', 'manufacturer_id', 'id']); $this->companies = Company::All(['name', 'id']); $this->status_labels = Statuslabel::All(['name', 'id']); $this->suppliers = Supplier::All(['name', 'id']); $this->assets = Asset::all(['asset_tag']); $this->accessories = Accessory::All(['name']); $this->consumables = Consumable::All(['name']); $this->customfields = CustomField::All(['name']); $bar = null; if (!$this->option('web-importer')) { $bar = $this->output->createProgressBar(count($newarray)); } // Loop through the records DB::transaction(function () use(&$newarray, $bar) { Model::unguard(); $item_type = strtolower($this->option('item-type')); foreach ($newarray as $row) { // Let's just map some of these entries to more user friendly words // Fetch general items here, fetch item type specific items in respective methods /** @var Asset, License, Accessory, or Consumable $item_type */ $item_category = $this->array_smart_fetch($row, "category"); $item_company_name = $this->array_smart_fetch($row, "company"); $item_location = $this->array_smart_fetch($row, "location"); $item_status_name = $this->array_smart_fetch($row, "status"); $item["item_name"] = $this->array_smart_fetch($row, "item name"); if ($this->array_smart_fetch($row, "purchase date") != '') { $item["purchase_date"] = date("Y-m-d 00:00:01", strtotime($this->array_smart_fetch($row, "purchase date"))); } else { $item["purchase_date"] = null; } $item["purchase_cost"] = $this->array_smart_fetch($row, "purchase cost"); $item["order_number"] = $this->array_smart_fetch($row, "order number"); $item["notes"] = $this->array_smart_fetch($row, "notes"); $item["quantity"] = $this->array_smart_fetch($row, "quantity"); $item["requestable"] = $this->array_smart_fetch($row, "requestable"); $item["asset_tag"] = $this->array_smart_fetch($row, "asset tag"); $this->current_assetId = $item["item_name"]; if ($item["asset_tag"] != '') { $this->current_assetId = $item["asset_tag"]; } $this->log('Category: ' . $item_category); $this->log('Location: ' . $item_location); $this->log('Purchase Date: ' . $item["purchase_date"]); $this->log('Purchase Cost: ' . $item["purchase_cost"]); $this->log('Company Name: ' . $item_company_name); $this->log('Status: ' . $item_status_name); $item["user"] = $this->createOrFetchUser($row); $item["location"] = $this->createOrFetchLocation($item_location); $item["category"] = $this->createOrFetchCategory($item_category, $item_type); $item["manufacturer"] = $this->createOrFetchManufacturer($row); $item["company"] = $this->createOrFetchCompany($item_company_name); $item["status_label"] = $this->createOrFetchStatusLabel($item_status_name); switch ($item_type) { case "asset": // ----------------------------- // CUSTOM FIELDS // ----------------------------- // Loop through custom fields in the database and see if we have any matches in the CSV foreach ($this->customfields as $customfield) { if ($item['custom_fields'][$customfield->db_column_name()] = $this->array_smart_custom_field_fetch($row, $customfield)) { $this->log('Custom Field ' . $customfield->name . ': ' . $this->array_smart_custom_field_fetch($row, $customfield)); } } $this->createAssetIfNotExists($row, $item); break; case "accessory": $this->createAccessoryIfNotExists($item); break; case 'consumable': $this->createConsumableIfNotExists($item); break; } if (!$this->option('web-importer')) { $bar->advance(); } $this->log('------------- Action Summary ----------------'); } }); if (!$this->option('web-importer')) { $bar->finish(); } $this->log('====================================='); if (!$this->option('web-importer')) { if (!empty($this->errors)) { $this->comment("The following Errors were encountered."); foreach ($this->errors as $asset => $error) { $this->comment('Error: Item: ' . $asset . 'failed validation: ' . json_encode($error)); } } else { $this->comment("All Items imported successfully!"); } } else { if (empty($this->errors)) { return 0; } else { $this->comment(json_encode($this->errors)); //Send a big string to the return 1; } } $this->comment(""); return 2; }