Skip to content

Commit

Permalink
Importer: Implement item update and interface improvments (#2507)
Browse files Browse the repository at this point in the history
* Add support for updating assets to the importer.

If an asset with a matching asset tag is found, and the --update flag is
passed to the importer, we edit the matching asset with any
asset-specific values, and persist to the database.  Any missing/blank
values are skipped.

TODO: Add to web interface, add support in consumables/accessories

* Allow deleting of files on the import page.

* Extend web interface to allow updating of imported items.

This adds a modal dialog to the import process.  Currently the dialog
allows the choice of update vs ignore, and choosing the item type to
import (Accessory, Asset, Consumable).

Also use Helper::ParseFloat() for purchase_cost processing.  It exists,
and fixes issues on my end at least.

* Implement editing of consumables and accessories.

* Rename getProcessImportFile to postProcessImportFile to reflect how it's now used

* Fix copy-pasta error.
  • Loading branch information
dmeltzer authored and snipe committed Aug 29, 2016
1 parent 0d088f3 commit 276e0a7
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 83 deletions.
204 changes: 142 additions & 62 deletions app/Console/Commands/ObjectImportCommand.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use League\Csv\Reader;
use App\Helpers\Helper;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\CustomField;
use App\Models\Location;
use App\Models\Manufacturer;
use App\Models\Setting;
use App\Models\Statuslabel;
use App\Models\Supplier;
use App\Models\User;
use App\Models\CustomField;
use DB;
use App\Models\Setting;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use League\Csv\Reader;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

ini_set('max_execution_time', 600); //600 seconds = 10 minutes
ini_set('memory_limit', '500M');
Expand Down Expand Up @@ -102,9 +103,17 @@ public function fire()
$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']);
switch (strtolower($this->option('item-type'))) {
case "asset":
$this->assets = Asset::all();
break;
case "accessory":
$this->accessories = Accessory::All();
break;
case "consumable":
$this->consumables = Consumable::All();
break;
}
$this->customfields = CustomField::All(['name']);
$bar = null;

Expand Down Expand Up @@ -732,6 +741,24 @@ public function createOrFetchUser($row, $password = null)
*/
public function createAssetIfNotExists(array $row, array $item)
{
$asset = null;
$editingAsset = false;
foreach ($this->assets as $tempasset) {
if (strcasecmp($tempasset->asset_tag, $item['asset_tag']) == 0) {
$this->log('A matching Asset ' . $item['asset_tag'] . ' already exists');
if (!$this->option('update')) {
$this->log("Skipping item.");
return;
}
$this->log('Updating matching asset with new values');
$editingAsset = true;
$asset = $tempasset;
}
}
if (is_null($asset)) {
$this->log("No Matching Asset, Creating a new one");
$asset = new Asset;
}
$asset_serial = $this->array_smart_fetch($row, "serial number");
$asset_image = $this->array_smart_fetch($row, "image");
$asset_warranty_months = intval($this->array_smart_fetch($row, "warranty months"));
Expand All @@ -747,12 +774,7 @@ public function createAssetIfNotExists(array $row, array $item)
$this->log('Notes: '.$item["notes"]);
$this->log('Warranty Months: ' . $asset_warranty_months);

foreach ($this->assets as $tempasset) {
if (strcasecmp($tempasset->asset_tag, $item['asset_tag']) == 0) {
$this->log('A matching Asset ' . $item['asset_tag'] . ' already exists');
return;
}
}


if ($item["status_label"]) {
$status_id = $item["status_label"]->id;
Expand All @@ -763,12 +785,14 @@ public function createAssetIfNotExists(array $row, array $item)
$status_id = 1;
}

$asset = new Asset();
$asset->name = $item["item_name"];
if ($item["purchase_date"] != '') {
if (!$editingAsset) {
$asset->asset_tag = $item['asset_tag']; // This doesn't need to be guarded for empty because it's the key we use to identify the asset.
}
if (!empty($item['item_name'])) {
$asset->name = $item["item_name"];
}
if (!empty($item["purchase_date"])) {
$asset->purchase_date = $item["purchase_date"];
} else {
$asset->purchase_date = null;
}

if (array_key_exists('custom_fields', $item)) {
Expand All @@ -780,37 +804,53 @@ public function createAssetIfNotExists(array $row, array $item)
if (!empty($item["purchase_cost"])) {
//TODO How to generalize this for not USD?
$purchase_cost = substr($item["purchase_cost"], 0, 1) === '$' ? substr($item["purchase_cost"], 1) : $item["purchase_cost"];
$asset->purchase_cost = number_format($purchase_cost, 2, '.', '');
// $asset->purchase_cost = number_format($purchase_cost, 2, '.', '');
$asset->purchase_cost = Helper::ParseFloat($purchase_cost);
$this->log("Asset cost parsed: " . $asset->purchase_cost);
} else {
$asset->purchase_cost = 0.00;
}
$asset->serial = $asset_serial;
$asset->asset_tag = $item['asset_tag'];
$asset->warranty_months = $asset_warranty_months;
if (!empty($asset_serial)) {
$asset->serial = $asset_serial;
}
if (!empty($asset_warranty_months)) {
$asset->warranty_months = $asset_warranty_months;
}

if ($asset_model) {
$asset->model_id = $asset_model->id;
}

if ($item["user"]) {
$asset->assigned_to = $item["user"]->id;
}

if ($item["location"]) {
$asset->rtd_location_id = $item["location"]->id;
}

$asset->user_id = $this->option('user_id');
$this->log("status_id: " . $status_id);
$asset->status_id = $status_id;
if (!empty($status_id)) {
$asset->status_id = $status_id;
}
if ($item["company"]) {
$asset->company_id = $item["company"]->id;
}
$asset->order_number = $item["order_number"];
if ($item["order_number"]) {
$asset->order_number = $item["order_number"];
}
if ($supplier) {
$asset->supplier_id = $supplier->id;
}
$asset->notes = $item["notes"];
$asset->image = $asset_image;
$this->assets->add($asset);
if ($item["notes"]) {
$asset->notes = $item["notes"];
}
if (!empty($asset_image)) {
$asset->image = $asset_image;
}
if (!$editingAsset) {
$this->assets->add($asset);
}
if (!$this->option('testrun')) {

if ($asset->save()) {
Expand All @@ -835,49 +875,66 @@ public function createAssetIfNotExists(array $row, array $item)
*/
public function createAccessoryIfNotExists(array $item)
{
$accessory = null;
$editingAccessory = false;
$this->log("Creating Accessory");
foreach ($this->accessories as $tempaccessory) {
if (strcasecmp($tempaccessory->name, $item["item_name"]) == 0) {
$this->log('A matching Accessory ' . $item["item_name"] . ' already exists. ');
// FUTURE: Adjust quantity on import maybe?
return;
if (!$this->option('update')) {
$this->log("Skipping accessory.");
return;
}
$this->log('Updating matching accessory with new values');
$editingAccessory = true;
$accessory = $tempaccessory;
}
}
if (is_null($accessory)) {
$this->log("No Matching Accessory, Creating a new one");
$accessory = new Accessory();
}

$accessory = new Accessory();
$accessory->name = $item["item_name"];
if (!$editingAccessory) {
$accessory->name = $item["item_name"];
}

if (!empty($item["purchase_date"])) {
$accessory->purchase_date = $item["purchase_date"];
} else {
$accessory->purchase_date = null;
}
if (!empty($item["purchase_cost"])) {
$accessory->purchase_cost = number_format(e($item["purchase_cost"]), 2);
} else {
$accessory->purchase_cost = 0.00;
$accessory->purchase_cost = Helper::ParseFloat($item["purchase_cost"]);
}

if ($item["location"]) {
$accessory->location_id = $item["location"]->id;
}
$accessory->user_id = $this->option('user_id');
if ($item["company"]) {
$accessory->company_id = $item["company"]->id;
}
$accessory->order_number = $item["order_number"];
if (!empty($item["order_number"])) {
$accessory->order_number = $item["order_number"];
}
if ($item["category"]) {
$accessory->category_id = $item["category"]->id;
}

//TODO: Implement
// $accessory->notes = e($item_notes);
$accessory->requestable = filter_var($item["requestable"], FILTER_VALIDATE_BOOLEAN);
if (!empty($item["requestable"])) {
$accessory->requestable = filter_var($item["requestable"], FILTER_VALIDATE_BOOLEAN);
}

//Must have at least zero of the item if we import it.
if ($item["quantity"] > -1) {
$accessory->qty = $item["quantity"];
} else {
$accessory->qty = 1;
if (!empty($item["quantity"])) {
if ($item["quantity"] > -1) {
$accessory->qty = $item["quantity"];
} else {
$accessory->qty = 1;
}
}

if (!$this->option('testrun')) {
Expand All @@ -904,45 +961,67 @@ public function createAccessoryIfNotExists(array $item)
*/
public function createConsumableIfNotExists(array $item)
{
$consumable = null;
$editingConsumable = false;
$this->log("Creating Consumable");
foreach ($this->consumables as $tempconsumable) {
if (strcasecmp($tempconsumable->name, $item["item_name"]) == 0) {
$this->log("A matching consumable " . $item["item_name"] . " already exists");
//TODO: Adjust quantity if different maybe?
return;
if (!$this->option('update')) {
$this->log("Skipping consumable.");
return;
}
$this->log('Updating matching consumable with new values');
$editingConsumable = true;
$consumable = $tempconsumable;
}
}

$consumable = new Consumable();
$consumable->name = $item["item_name"];

if (is_null($consumable)) {
$this->log("No matching consumable, creating one");
$consumable = new Consumable();
}
if (!$editingConsumable) {
$consumable->name = $item["item_name"];
}
if (!empty($item["purchase_date"])) {
$consumable->purchase_date = $item["purchase_date"];
} else {
$consumable->purchase_date = null;
}

if (!empty($item["purchase_cost"])) {
$consumable->purchase_cost = number_format(e($item["purchase_cost"]), 2);
} else {
$consumable->purchase_cost = 0.00;
$consumable->purchase_cost = Helper::ParseFloat($item["purchase_cost"]);
}
if ($item["location"]) {
$consumable->location_id = $item["location"]->id;
}
$consumable->location_id = $item["location"]->id;
$consumable->user_id = $this->option('user_id');
$consumable->company_id = $item["company"]->id;
$consumable->order_number = $item["order_number"];
$consumable->category_id = $item["category"]->id;
if ($item["company"]) {
$consumable->company_id = $item["company"]->id;
}
if (!empty($item["order_number"])) {
$consumable->order_number = $item["order_number"];
}
if ($item["category"]) {
$consumable->category_id = $item["category"]->id;
}
// TODO:Implement
//$consumable->notes= e($item_notes);
$consumable->requestable = filter_var($item["requestable"], FILTER_VALIDATE_BOOLEAN);
if (!empty($item["requestable"])) {
$consumable->requestable = filter_var($item["requestable"], FILTER_VALIDATE_BOOLEAN);
}

if ($item["quantity"] > -1) {
$consumable->qty = $item["quantity"];
} else {
$consumable->qty = 1;
if (!empty($item["quantity"])) {
if ($item["quantity"] > -1) {
$consumable->qty = $item["quantity"];
} else {
$consumable->qty = 1;
}
}

if (!$this->option("testrun")) {
// dd($consumable);
if ($consumable->save()) {
$this->log("Consumable " . $item["item_name"] . ' was created');
// $this->comment("Consumable " . $item["item_name"] . ' was created');
Expand Down Expand Up @@ -985,8 +1064,9 @@ protected function getOptions()
array('testrun', null, InputOption::VALUE_NONE, 'If set, will parse and output data without adding to database', null),
array('logfile', null, InputOption::VALUE_REQUIRED, 'The path to log output to. storage/logs/importer.log by default', storage_path('logs/importer.log') ),
array('item-type', null, InputOption::VALUE_REQUIRED, 'Item Type To import. Valid Options are Asset, Consumable, Or Accessory', 'Asset'),
array('web-importer', null, InputOption::VALUE_NONE, 'Internal: packages output for use with the web importer'),
array('user_id', null, InputOption::VALUE_REQUIRED, 'ID of user creating items', 1)
array('web-importer', null, InputOption::VALUE_NONE, 'Internal: packages output for use with the web importer'),
array('user_id', null, InputOption::VALUE_REQUIRED, 'ID of user creating items', 1),
array('update', null, InputOption::VALUE_NONE, 'If a matching item is found, update item information'),
);

}
Expand Down
Loading

0 comments on commit 276e0a7

Please sign in to comment.