snipe-it/app/Importer/ConsumableImporter.php
Martin Meredith e3e0d57f56 Minor code cleanup bits and bobs (#6805)
* Add IDE Helper files

* Cleanup imports

- Alphabetises imports
- Removes unused imports

* Add Platform requirements

* Move filling asset into block where asset exists

* Remove duplicate array keys
2019-03-13 20:12:03 -07:00

56 lines
1.8 KiB
PHP

<?php
namespace App\Importer;
use App\Models\Consumable;
class ConsumableImporter extends ItemImporter
{
public function __construct($filename)
{
parent::__construct($filename);
}
protected function handle($row)
{
parent::handle($row);
$this->createConsumableIfNotExists($row);
}
/**
* Create a consumable if a duplicate does not exist
*
* @author Daniel Melzter
* @param array $row CSV Row Being parsed.
* @since 3.0
*/
public function createConsumableIfNotExists($row)
{
$consumable = Consumable::where('name', $this->item['name'])->first();
if ($consumable) {
if (!$this->updating) {
$this->log('A matching Consumable ' . $this->item["name"] . ' already exists. ');
return;
}
$this->log('Updating Consumable');
$consumable->update($this->sanitizeItemForUpdating($consumable));
$consumable->save();
return;
}
$this->log("No matching consumable, creating one");
$consumable = new Consumable();
$this->item['model_number'] = $this->findCsvMatch($row, "model_number");;
$this->item['item_no'] = $this->findCsvMatch($row, "item_number");
$consumable->fill($this->sanitizeItemForStoring($consumable));
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
$consumable->unsetEventDispatcher();
if ($consumable->save()) {
$consumable->logCreate('Imported using CSV Importer');
$this->log("Consumable " . $this->item["name"] . ' was created');
return;
}
$this->logError($consumable, 'Consumable');
return;
}
}