Import status labels.

This commit is contained in:
Daniel Meltzer 2016-05-24 18:14:01 -04:00
parent 22c6f32e92
commit fd0d04eba4

View file

@ -14,6 +14,7 @@ use App\Models\Company;
use App\Models\Consumable; use App\Models\Consumable;
use App\Models\Location; use App\Models\Location;
use App\Models\Manufacturer; use App\Models\Manufacturer;
use App\Models\Statuslabel;
use App\Models\Supplier; use App\Models\Supplier;
use App\Models\User; use App\Models\User;
use DB; use DB;
@ -90,6 +91,7 @@ class ObjectImportCommand extends Command {
$this->suppliers = Supplier::All(['name']); $this->suppliers = Supplier::All(['name']);
$this->accessories = Accessory::All(['name']); $this->accessories = Accessory::All(['name']);
$this->consumables = Consumable::All(['name']); $this->consumables = Consumable::All(['name']);
$this->status_labels = Statuslabel::All(['name']);
// Loop through the records // Loop through the records
DB::transaction(function() use (&$newarray){ DB::transaction(function() use (&$newarray){
$item_type = strtolower($this->option('item-type')); $item_type = strtolower($this->option('item-type'));
@ -104,6 +106,8 @@ class ObjectImportCommand extends Command {
$item_company_name = $this->array_smart_fetch($row, "company"); $item_company_name = $this->array_smart_fetch($row, "company");
$item_location = $this->array_smart_fetch($row, "location"); $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"); $item["item_name"] = $this->array_smart_fetch($row, "item name");
$item["purchase_date"] = date("Y-m-d 00:00:01", strtotime($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")));
$item["purchase_cost"] = $this->array_smart_fetch($row, "purchase cost"); $item["purchase_cost"] = $this->array_smart_fetch($row, "purchase cost");
@ -113,6 +117,7 @@ class ObjectImportCommand extends Command {
$item["requestable"] = $this->array_smart_fetch($row, "requestable"); $item["requestable"] = $this->array_smart_fetch($row, "requestable");
$this->current_assetId = $item["item_name"]; $this->current_assetId = $item["item_name"];
$this->log('Category Name: ' . $item_category); $this->log('Category Name: ' . $item_category);
$this->log('Location: ' . $item_location); $this->log('Location: ' . $item_location);
@ -127,6 +132,8 @@ class ObjectImportCommand extends Command {
$item["manufacturer"] = $this->createOrFetchManufacturer($row); $item["manufacturer"] = $this->createOrFetchManufacturer($row);
$item["company"] = $this->createOrFetchCompany($item_company_name); $item["company"] = $this->createOrFetchCompany($item_company_name);
$item["status_label"] = $this->createOrFetchStatusLabel($item_status_name);
switch ($item_type) { switch ($item_type) {
case "asset": case "asset":
$this->createAssetIfNotExists($row, $item); $this->createAssetIfNotExists($row, $item);
@ -329,7 +336,38 @@ class ObjectImportCommand extends Command {
return $company; return $company;
} }
} }
private $status_labels;
/**
* @param string $asset_statuslabel_name
* @return Company
*/
public function createOrFetchStatusLabel($asset_statuslabel_name)
{
if(empty($asset_statuslabel_name))
return;
foreach ($this->status_labels as $tempstatus) {
if ($tempstatus->name === $asset_statuslabel_name) {
$this->comment('A matching Status ' . $asset_statuslabel_name . ' already exists');
return $tempstatus;
}
}
$status = new Statuslabel();
$status->name = $asset_statuslabel_name;
$this->status_labels->add($status);
if(!$this->option('testrun')) {
if ($status->save()) {
$this->comment('Status ' . $asset_statuslabel_name . ' was created');
return $status;
} else {
$this->comment('Something went wrong! Status ' . $asset_statuslabel_name . ' was NOT created');
return $status;
}
} else {
return $status;
}
}
private $manufacturers; private $manufacturers;
@ -557,7 +595,7 @@ class ObjectImportCommand extends Command {
*/ */
public function createAssetIfNotExists(array $row, array $item ) public function createAssetIfNotExists(array $row, array $item )
{ {
$status_id = 1;
$asset_serial = $this->array_smart_fetch($row, "serial number"); $asset_serial = $this->array_smart_fetch($row, "serial number");
$asset_tag = $this->array_smart_fetch($row, "asset tag"); $asset_tag = $this->array_smart_fetch($row, "asset tag");
$asset_image = $this->array_smart_fetch($row, "image"); $asset_image = $this->array_smart_fetch($row, "image");
@ -579,6 +617,13 @@ class ObjectImportCommand extends Command {
} }
} }
if(empty($item["status_label"])) {
$this->comment("No status field found, defaulting to id 1.");
$status_id = 1;
} else {
$status_id = $item["status_label"]->id;
}
$asset = new Asset(); $asset = new Asset();
$asset->name = $item["item_name"]; $asset->name = $item["item_name"];
if ($item["purchase_date"] != '') { if ($item["purchase_date"] != '') {