snipe-it/app/Importer/ConsumableImporter.php
Daniel Meltzer 6ce20c32b1 Component Importer and various Importer Fixes (#3132)
* Importer fix: we were trimming the wrong part of the classname when creating a category.  This led to categories not being recognized.

* Add a component importer.  Uses same fields as consumable importer.  Only trick: If an asset_tag is present, we checkout a component to that asset on import

Enable component importer.  Also calculate the importer classname in a cleaner fashion.

* Fix comparisons.  find can return an index of 0, which is falsy.
2017-01-05 15:45:12 -08:00

105 lines
3.5 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: parallelgrapefruit
* Date: 12/24/16
* Time: 1:03 PM
*/
namespace App\Importer;
use App\Helpers\Helper;
use App\Models\Consumable;
class ConsumableImporter extends ItemImporter
{
protected $consumables;
function __construct($filename, $logCallback, $progressCallback, $errorCallback, $testRun = false, $user_id = -1, $updating = false, $usernameFormat = null)
{
parent::__construct($filename, $logCallback, $progressCallback, $errorCallback, $testRun, $user_id, $updating, $usernameFormat);
$this->consumables = Consumable::all();
}
protected function handle($row)
{
parent::handle($row); // TODO: Change the autogenerated stub
$this->createConsumableIfNotExists();
}
/**
* Create a consumable if a duplicate does not exist
*
* @author Daniel Melzter
* @since 3.0
*/
public function createConsumableIfNotExists()
{
$consumable = null;
$editingConsumable = false;
$this->log("Creating Consumable");
$consumable = $this->consumables->search(function ($key) {
return strcasecmp($key->name, $this->item['item_name']) == 0;
});
if ($consumable !== false) {
$editingConsumable = true;
if (!$this->updating) {
$this->log('A matching Consumable ' . $this->item["item_name"] . ' already exists. ');
return;
}
} else {
$this->log("No matching consumable, creating one");
$consumable = new Consumable();
}
if (!$editingConsumable) {
$consumable->name = $this->item["item_name"];
}
if (!empty($this->item["purchase_date"])) {
$consumable->purchase_date = $this->item["purchase_date"];
} else {
$consumable->purchase_date = null;
}
if (!empty($this->item["purchase_cost"])) {
$consumable->purchase_cost = Helper::ParseFloat($this->item["purchase_cost"]);
}
if (isset($this->item["location"])) {
$consumable->location_id = $this->item["location"]->id;
}
$consumable->user_id = $this->user_id;
if (isset($this->item["company"])) {
$consumable->company_id = $this->item["company"]->id;
}
if (!empty($this->item["order_number"])) {
$consumable->order_number = $this->item["order_number"];
}
if (isset($this->item["category"])) {
$consumable->category_id = $this->item["category"]->id;
}
// TODO:Implement
//$consumable->notes= e($this->item_notes);
if (!empty($this->item["requestable"])) {
$consumable->requestable = filter_var($this->item["requestable"], FILTER_VALIDATE_BOOLEAN);
}
if (!empty($this->item["quantity"])) {
if ($this->item["quantity"] > -1) {
$consumable->qty = $this->item["quantity"];
} else {
$consumable->qty = 1;
}
}
if (!$this->testRun) {
if ($consumable->save()) {
$consumable->logCreate('Imported using CSV Importer');
$this->log("Consumable " . $this->item["item_name"] . ' was created');
} else {
$this->jsonError($consumable, 'Consumable', $consumable->getErrors());
}
} else {
$this->log('TEST RUN - Consumable ' . $this->item['item_name'] . ' not created');
}
}
}