2016-05-17 19:25:17 -07:00
< ? 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\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\Location ;
use App\Models\Manufacturer ;
2016-05-24 15:14:01 -07:00
use App\Models\Statuslabel ;
2016-05-17 19:25:17 -07:00
use App\Models\Supplier ;
use App\Models\User ;
2016-06-13 21:41:09 -07:00
use App\Models\CustomField ;
2016-05-17 19:25:17 -07:00
use DB ;
2016-06-13 21:41:09 -07:00
use App\Models\Setting ;
use Illuminate\Database\Eloquent\Model ;
ini_set ( 'max_execution_time' , 600 ); //600 seconds = 10 minutes
ini_set ( 'memory_limit' , '500M' );
2016-05-17 19:25:17 -07:00
/**
* Class ObjectImportCommand
*/
class ObjectImportCommand extends Command {
/**
* The console command name .
*
* @ var string
*/
2016-05-18 20:38:19 -07:00
protected $name = 'snipeit:import' ;
2016-05-17 19:25:17 -07:00
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Import Items from CSV' ;
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function fire ()
{
$filename = $this -> argument ( 'filename' );
2016-05-26 19:29:29 -07:00
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 . ' =========' );
}
2016-05-17 19:25:17 -07:00
}
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 ;
2016-06-13 21:45:52 -07:00
foreach ( $results as $index => $arraytoNormalize )
{
2016-05-17 19:25:17 -07:00
$internalnewarray = array_change_key_case ( $arraytoNormalize );
$newarray [ $index ] = $internalnewarray ;
}
2016-06-13 21:45:52 -07:00
2016-05-17 19:25:17 -07:00
$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' ]);
2016-05-26 19:29:29 -07:00
$this -> status_labels = Statuslabel :: All ([ 'name' , 'id' ]);
$this -> suppliers = Supplier :: All ([ 'name' , 'id' ]);
2016-05-17 19:25:17 -07:00
$this -> assets = Asset :: all ([ 'asset_tag' ]);
$this -> accessories = Accessory :: All ([ 'name' ]);
$this -> consumables = Consumable :: All ([ 'name' ]);
2016-06-13 21:45:52 -07:00
$this -> customfields = CustomField :: All ([ 'name' ]);
2016-06-19 20:56:10 -07:00
$bar = NULL ;
if ( ! $this -> option ( 'web-importer' )) {
$bar = $this -> output -> createProgressBar ( count ( $newarray ));
}
2016-05-17 19:25:17 -07:00
// Loop through the records
2016-06-13 21:45:52 -07:00
DB :: transaction ( function () use ( & $newarray , $bar ){
Model :: unguard ();
2016-05-25 18:19:12 -07:00
$item_type = strtolower ( $this -> option ( 'item-type' ));
2016-06-13 21:45:52 -07:00
2016-05-17 19:25:17 -07:00
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 " );
2016-05-24 15:14:01 -07:00
$item_status_name = $this -> array_smart_fetch ( $row , " status " );
2016-05-17 19:25:17 -07:00
$item [ " item_name " ] = $this -> array_smart_fetch ( $row , " item name " );
2016-06-10 05:00:49 -07:00
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 ;
}
2016-05-17 19:25:17 -07:00
$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 " );
2016-05-24 15:14:01 -07:00
2016-05-17 19:25:17 -07:00
2016-05-25 17:34:54 -07:00
$this -> current_assetId = $item [ " item_name " ];
2016-06-13 21:45:52 -07:00
$this -> log ( 'Category: ' . $item_category );
2016-05-25 17:34:54 -07:00
$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 );
2016-05-26 19:29:29 -07:00
$this -> log ( 'Status: ' . $item_status_name );
2016-05-17 19:25:17 -07:00
$item [ " user " ] = $this -> createOrFetchUser ( $row );
$item [ " location " ] = $this -> createOrFetchLocation ( $item_location );
2016-05-25 18:19:12 -07:00
$item [ " category " ] = $this -> createOrFetchCategory ( $item_category , $item_type );
2016-05-17 19:25:17 -07:00
$item [ " manufacturer " ] = $this -> createOrFetchManufacturer ( $row );
$item [ " company " ] = $this -> createOrFetchCompany ( $item_company_name );
2016-05-24 15:14:01 -07:00
$item [ " status_label " ] = $this -> createOrFetchStatusLabel ( $item_status_name );
2016-05-25 18:19:12 -07:00
switch ( $item_type ) {
2016-05-17 19:25:17 -07:00
case " asset " :
2016-06-13 21:45:52 -07:00
// -----------------------------
// 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 ));
}
}
2016-05-17 19:25:17 -07:00
$this -> createAssetIfNotExists ( $row , $item );
break ;
case " accessory " :
$this -> createAccessoryIfNotExists ( $item );
break ;
case 'consumable' :
$this -> createConsumableIfNotExists ( $item );
break ;
}
2016-06-13 21:45:52 -07:00
2016-06-19 20:56:10 -07:00
if ( ! $this -> option ( 'web-importer' )) {
$bar -> advance ();
}
2016-05-25 17:34:54 -07:00
$this -> log ( '------------- Action Summary ----------------' );
2016-05-17 19:25:17 -07:00
}
});
2016-06-19 20:56:10 -07:00
if ( ! $this -> option ( 'web-importer' )) {
$bar -> finish ();
}
2016-06-13 21:45:52 -07:00
2016-05-26 19:29:29 -07:00
2016-05-25 17:34:54 -07:00
$this -> log ( '=====================================' );
2016-05-26 19:29:29 -07:00
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 ;
2016-05-25 17:34:54 -07:00
}
}
$this -> comment ( " " );
2016-05-17 19:25:17 -07:00
2016-05-26 19:29:29 -07:00
return 2 ;
2016-05-17 19:25:17 -07:00
}
2016-05-25 17:34:54 -07:00
// Tracks the current item for error messages
private $current_assetId ;
// An array of errors encountered while parsing
private $errors ;
2016-05-26 19:29:29 -07:00
public function jsonError ( $field , $errorString )
2016-05-25 17:34:54 -07:00
{
2016-05-26 19:29:29 -07:00
$this -> errors [ $this -> current_assetId ] = array ( $field => $errorString );
2016-05-25 17:34:54 -07:00
if ( $this -> option ( 'verbose' ))
2016-05-26 19:29:29 -07:00
parent :: error ( $errorString );
2016-05-25 17:34:54 -07:00
}
/**
2016-06-13 21:45:52 -07:00
* Log a message to file , configurable by the -- log - file parameter .
* If a warning message is passed , we ' ll spit it to the console as well .
*
* @ author Daniel Melzter
* @ since 3.0
* @ param string $string
* @ param string $level
2016-05-25 17:34:54 -07:00
*/
private function log ( $string , $level = 'info' )
{
2016-05-26 19:29:29 -07:00
if ( $this -> option ( 'web-importer' ))
return ;
2016-05-25 17:34:54 -07:00
if ( $level === 'warning' )
{
\Log :: warning ( $string );
$this -> comment ( $string );
}
else {
\Log :: Info ( $string );
if ( $this -> option ( 'verbose' )) {
$this -> comment ( $string );
}
}
}
2016-05-17 19:25:17 -07:00
/**
* Check to see if the given key exists in the array , and trim excess white space before returning it
2016-06-13 21:45:52 -07:00
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ param $array array
* @ param $key string
* @ param $default string
* @ return string
*/
2016-06-13 21:45:52 -07:00
public function array_smart_fetch ( Array $array , $key , $default = '' ) {
2016-05-17 19:47:38 -07:00
return array_key_exists ( $key , $array ) ? e ( trim ( $array [ $key ])) : $default ;
2016-05-17 19:25:17 -07:00
}
2016-06-13 21:45:52 -07:00
/**
* Figure out the fieldname of the custom field
*
* @ author A . Gianotto < snipe @ snipe . net >
* @ since 3.0
* @ param $array array
* @ return string
*/
public function array_smart_custom_field_fetch ( Array $array , $key ) {
$index_name = strtolower ( $key -> name );
return array_key_exists ( $index_name , $array ) ? e ( trim ( $array [ $index_name ])) : '' ;
}
2016-05-17 19:47:38 -07:00
private $asset_models ;
/**
2016-06-13 21:45:52 -07:00
* Select the asset model if it exists , otherwise create it .
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:47:38 -07:00
* @ param array
* @ param $category Category
* @ param $manufacturer Manufacturer
* @ return Model
* @ internal param $asset_modelno string
2016-05-17 19:25:17 -07:00
*/
2016-05-17 19:47:38 -07:00
public function createOrFetchAssetModel ( array $row , $category , $manufacturer )
2016-05-17 19:25:17 -07:00
{
2016-05-17 19:47:38 -07:00
$asset_model_name = $this -> array_smart_fetch ( $row , " model name " );
$asset_modelno = $this -> array_smart_fetch ( $row , " model number " );
if ( empty ( $asset_model_name ))
$asset_model_name = 'Unknown' ;
if ( empty ( $asset_modelno ))
2016-06-13 21:45:52 -07:00
$asset_modelno = '' ;
2016-05-25 17:34:54 -07:00
$this -> log ( 'Model Name: ' . $asset_model_name );
$this -> log ( 'Model No: ' . $asset_modelno );
2016-05-17 19:25:17 -07:00
2016-05-17 19:47:38 -07:00
foreach ( $this -> asset_models as $tempmodel ) {
2016-06-01 17:31:24 -07:00
if (( strcasecmp ( $tempmodel -> name , $asset_model_name ) == 0 )
2016-05-17 19:47:38 -07:00
&& $tempmodel -> modelno == $asset_modelno
&& $tempmodel -> category_id == $category -> id
&& $tempmodel -> manufacturer_id == $manufacturer -> id )
{
2016-05-25 17:34:54 -07:00
$this -> log ( 'A matching model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' already exists' );
2016-05-17 19:47:38 -07:00
return $tempmodel ;
2016-05-17 19:25:17 -07:00
}
}
2016-05-17 19:47:38 -07:00
$asset_model = new AssetModel ();
$asset_model -> name = $asset_model_name ;
$asset_model -> manufacturer_id = $manufacturer -> id ;
2016-05-18 20:33:55 -07:00
$asset_model -> modelno = $asset_modelno ;
2016-05-17 19:47:38 -07:00
$asset_model -> category_id = $category -> id ;
2016-06-19 20:06:54 -07:00
$asset_model -> user_id = $this -> option ( 'user_id' );
2016-05-25 17:34:54 -07:00
2016-05-17 19:25:17 -07:00
2016-05-17 19:47:38 -07:00
if ( ! $this -> option ( 'testrun' )) {
if ( $asset_model -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> asset_models -> add ( $asset_model );
$this -> log ( 'Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelno . ' was created' );
2016-05-17 19:47:38 -07:00
return $asset_model ;
2016-05-17 19:25:17 -07:00
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'Asset Model' , $asset_model -> getErrors ());
2016-05-17 19:47:38 -07:00
return $asset_model ;
2016-05-17 19:25:17 -07:00
}
} else {
2016-05-25 17:34:54 -07:00
$this -> asset_models -> add ( $asset_model );
2016-05-17 19:47:38 -07:00
return $asset_model ;
2016-05-17 19:25:17 -07:00
}
2016-05-17 19:47:38 -07:00
2016-05-17 19:25:17 -07:00
}
private $categories ;
/**
* Finds a category with the same name and item type in the database , otherwise creates it
2016-06-13 21:45:52 -07:00
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ param $asset_category string
2016-05-31 17:00:11 -07:00
* @ param $item_type string
2016-05-17 19:25:17 -07:00
* @ return Category
*/
public function createOrFetchCategory ( $asset_category , $item_type )
{
2016-05-17 19:47:38 -07:00
if ( empty ( $asset_category ))
$asset_category = 'Unnamed Category' ;
2016-05-17 19:25:17 -07:00
foreach ( $this -> categories as $tempcategory ) {
2016-06-01 17:31:24 -07:00
if ( ( strcasecmp ( $tempcategory -> name , $asset_category ) == 0 ) && $tempcategory -> category_type === $item_type ) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'Category ' . $asset_category . ' already exists' );
2016-05-17 19:25:17 -07:00
return $tempcategory ;
}
}
$category = new Category ();
2016-05-17 19:47:38 -07:00
$category -> name = $asset_category ;
2016-05-17 19:25:17 -07:00
$category -> category_type = $item_type ;
2016-06-19 20:06:54 -07:00
$category -> user_id = $this -> option ( 'user_id' );
2016-05-25 17:34:54 -07:00
2016-05-17 19:25:17 -07:00
if ( ! $this -> option ( 'testrun' )) {
if ( $category -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> categories -> add ( $category );
$this -> log ( 'Category ' . $asset_category . ' was created' );
2016-05-17 19:25:17 -07:00
return $category ;
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'Category' , $category -> getErrors ());
2016-05-25 17:34:54 -07:00
return $category ;
2016-05-17 19:25:17 -07:00
}
} else {
2016-05-25 17:34:54 -07:00
$this -> categories -> add ( $category );
2016-05-17 19:25:17 -07:00
return $category ;
}
}
2016-05-17 19:47:38 -07:00
private $companies ;
/**
2016-06-13 21:45:52 -07:00
* Fetch an existing company , or create new if it doesn ' t exist
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:47:38 -07:00
* @ param $asset_company_name string
* @ return Company
*/
public function createOrFetchCompany ( $asset_company_name )
{
foreach ( $this -> companies as $tempcompany ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $tempcompany -> name , $asset_company_name ) == 0 ) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'A matching Company ' . $asset_company_name . ' already exists' );
2016-05-17 19:47:38 -07:00
return $tempcompany ;
}
}
$company = new Company ();
$company -> name = $asset_company_name ;
if ( ! $this -> option ( 'testrun' )) {
if ( $company -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> companies -> add ( $company );
$this -> log ( 'Company ' . $asset_company_name . ' was created' );
2016-05-17 19:47:38 -07:00
return $company ;
} else {
2016-05-31 16:04:01 -07:00
$this -> log ( 'Company' , $company -> getErrors ());
2016-05-17 19:47:38 -07:00
}
} else {
2016-05-25 17:34:54 -07:00
$this -> companies -> add ( $company );
2016-05-17 19:47:38 -07:00
return $company ;
}
}
2016-05-24 15:14:01 -07:00
private $status_labels ;
/**
2016-06-13 21:45:52 -07:00
* Fetch the existing status label or create new if it doesn ' t exist .
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-24 15:14:01 -07:00
* @ 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 ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $tempstatus -> name , $asset_statuslabel_name ) == 0 ) {
2016-05-25 19:13:26 -07:00
$this -> log ( 'A matching Status ' . $asset_statuslabel_name . ' already exists' );
2016-05-24 15:14:01 -07:00
return $tempstatus ;
}
}
$status = new Statuslabel ();
$status -> name = $asset_statuslabel_name ;
2016-05-25 19:13:26 -07:00
2016-05-24 15:14:01 -07:00
if ( ! $this -> option ( 'testrun' )) {
if ( $status -> save ()) {
2016-05-25 19:13:26 -07:00
$this -> status_labels -> add ( $status );
$this -> log ( 'Status ' . $asset_statuslabel_name . ' was created' );
2016-05-24 15:14:01 -07:00
return $status ;
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'Status' , $status -> getErrors ());
2016-05-24 15:14:01 -07:00
return $status ;
}
} else {
2016-05-25 19:13:26 -07:00
$this -> status_labels -> add ( $status );
2016-05-24 15:14:01 -07:00
return $status ;
}
}
2016-05-17 19:25:17 -07:00
private $manufacturers ;
/**
* Finds a manufacturer with matching name , otherwise create it .
2016-06-13 21:45:52 -07:00
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ param $row array
* @ return Manufacturer
* @ internal param $asset_mfgr string
*/
2016-05-17 19:47:38 -07:00
2016-05-17 19:25:17 -07:00
public function createOrFetchManufacturer ( array $row )
{
$asset_mfgr = $this -> array_smart_fetch ( $row , " manufacturer " );
if ( empty ( $asset_mfgr )) {
$asset_mfgr = 'Unknown' ;
}
2016-05-25 17:34:54 -07:00
$this -> log ( 'Manufacturer ID: ' . $asset_mfgr );
2016-05-17 19:25:17 -07:00
foreach ( $this -> manufacturers as $tempmanufacturer ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $tempmanufacturer -> name , $asset_mfgr ) == 0 ) {
$this -> log ( 'Manufacturer ' . $asset_mfgr . ' already exists' ) ;
2016-05-17 19:25:17 -07:00
return $tempmanufacturer ;
}
}
//Otherwise create a manufacturer.
$manufacturer = new Manufacturer ();
2016-05-17 19:47:38 -07:00
$manufacturer -> name = $asset_mfgr ;
2016-06-19 20:06:54 -07:00
$manufacturer -> user_id = $this -> option ( 'user_id' );
2016-05-17 19:25:17 -07:00
if ( ! $this -> option ( 'testrun' )) {
if ( $manufacturer -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> manufacturers -> add ( $manufacturer );
$this -> log ( 'Manufacturer ' . $manufacturer -> name . ' was created' );
2016-05-17 19:25:17 -07:00
return $manufacturer ;
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'Manufacturer' , $manufacturer -> getErrors ());
2016-05-17 19:25:17 -07:00
return $manufacturer ;
}
} else {
2016-05-25 17:34:54 -07:00
$this -> manufacturers -> add ( $manufacturer );
2016-05-17 19:25:17 -07:00
return $manufacturer ;
}
}
2016-06-13 21:45:52 -07:00
/**
2016-05-17 19:47:38 -07:00
* @ var
2016-05-17 19:25:17 -07:00
*/
2016-05-17 19:47:38 -07:00
private $locations ;
2016-05-17 19:25:17 -07:00
/**
2016-05-17 19:47:38 -07:00
* Checks the DB to see if a location with the same name exists , otherwise create it
2016-06-13 21:45:52 -07:00
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:47:38 -07:00
* @ param $asset_location string
* @ return Location
2016-05-17 19:25:17 -07:00
*/
2016-05-17 19:47:38 -07:00
public function createOrFetchLocation ( $asset_location )
2016-05-17 19:25:17 -07:00
{
2016-05-17 19:47:38 -07:00
foreach ( $this -> locations as $templocation ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $templocation -> name , $asset_location ) == 0 ) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'Location ' . $asset_location . ' already exists' );
2016-05-17 19:47:38 -07:00
return $templocation ;
2016-05-17 19:25:17 -07:00
}
}
2016-05-17 19:47:38 -07:00
// No matching locations in the collection, create a new one.
$location = new Location ();
2016-05-17 19:25:17 -07:00
2016-05-17 19:47:38 -07:00
if ( ! empty ( $asset_location )) {
$location -> name = $asset_location ;
$location -> address = '' ;
$location -> city = '' ;
$location -> state = '' ;
$location -> country = '' ;
2016-06-19 20:06:54 -07:00
$location -> user_id = $this -> option ( 'user_id' );
2016-05-17 19:25:17 -07:00
2016-05-17 19:47:38 -07:00
if ( ! $this -> option ( 'testrun' )) {
if ( $location -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> locations -> add ( $location );
$this -> log ( 'Location ' . $asset_location . ' was created' );
2016-05-17 19:47:38 -07:00
return $location ;
} else {
2016-05-31 16:04:01 -07:00
$this -> log ( 'Location' , $location -> getErrors ()) ;
2016-05-17 19:47:38 -07:00
return $location ;
}
2016-05-17 19:25:17 -07:00
} else {
2016-05-25 17:34:54 -07:00
$this -> locations -> add ( $location );
2016-05-17 19:47:38 -07:00
return $location ;
2016-05-17 19:25:17 -07:00
}
} else {
2016-05-25 17:34:54 -07:00
$this -> log ( 'No location given, so none created.' );
2016-05-17 19:47:38 -07:00
return $location ;
2016-05-17 19:25:17 -07:00
}
2016-05-17 19:47:38 -07:00
2016-05-17 19:25:17 -07:00
}
private $suppliers ;
/**
2016-06-13 21:45:52 -07:00
* Fetch an existing supplier or create new if it doesn ' t exist
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ param $row array
* @ return Supplier
*/
public function createOrFetchSupplier ( array $row )
{
$supplier_name = $this -> array_smart_fetch ( $row , " supplier " );
if ( empty ( $supplier_name ))
$supplier_name = 'Unknown' ;
foreach ( $this -> suppliers as $tempsupplier ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $tempsupplier -> name , $supplier_name ) == 0 ) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'A matching Company ' . $supplier_name . ' already exists' );
2016-05-17 19:25:17 -07:00
return $tempsupplier ;
}
}
$supplier = new Supplier ();
2016-05-17 19:47:38 -07:00
$supplier -> name = $supplier_name ;
2016-06-19 20:06:54 -07:00
$supplier -> user_id = $this -> option ( 'user_id' );
2016-05-25 18:19:12 -07:00
2016-05-17 19:25:17 -07:00
if ( ! $this -> option ( 'testrun' )) {
if ( $supplier -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> suppliers -> add ( $supplier );
$this -> log ( 'Supplier ' . $supplier_name . ' was created' );
2016-05-17 19:25:17 -07:00
return $supplier ;
} else {
2016-05-31 16:04:01 -07:00
$this -> log ( 'Supplier' , $supplier -> getErrors ());
2016-05-25 17:34:54 -07:00
return $supplier ;
2016-05-17 19:25:17 -07:00
}
} else {
2016-05-25 17:34:54 -07:00
$this -> suppliers -> add ( $supplier );
2016-05-17 19:25:17 -07:00
return $supplier ;
}
}
2016-05-17 19:47:38 -07:00
2016-06-13 21:45:52 -07:00
/**
2016-05-17 19:47:38 -07:00
* Finds the user matching given data , or creates a new one if there is no match
2016-06-13 21:45:52 -07:00
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:47:38 -07:00
* @ param $row array
* @ return User Model w / matching name
* @ internal param string $user_username Username extracted from CSV
* @ internal param string $user_email Email extracted from CSV
* @ internal param string $first_name
* @ internal param string $last_name
*/
public function createOrFetchUser ( $row )
{
$user_name = $this -> array_smart_fetch ( $row , " name " );
$user_email = $this -> array_smart_fetch ( $row , " email " );
$user_username = $this -> array_smart_fetch ( $row , " username " );
// A number was given instead of a name
if ( is_numeric ( $user_name )) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'User ' . $user_name . ' is not a name - assume this user already exists' );
2016-05-17 19:47:38 -07:00
$user_username = '' ;
2016-05-18 21:33:18 -07:00
$first_name = '' ;
$last_name = '' ;
2016-05-17 19:47:38 -07:00
2016-05-18 21:33:18 -07:00
// No name was given
2016-05-17 19:47:38 -07:00
} elseif ( empty ( $user_name )) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'No user data provided - skipping user creation, just adding asset' );
2016-05-17 19:47:38 -07:00
$first_name = '' ;
$last_name = '' ;
2016-05-18 21:33:18 -07:00
//$user_username = '';
2016-05-17 19:47:38 -07:00
} else {
2016-06-13 21:45:52 -07:00
$user_email_array = User :: generateFormattedNameFromFullName ( Setting :: getSettings () -> email_format , $user_name );
2016-05-17 19:47:38 -07:00
$first_name = $user_email_array [ 'first_name' ];
$last_name = $user_email_array [ 'last_name' ];
2016-05-18 21:33:18 -07:00
if ( $user_email == '' ) {
2016-06-13 21:45:52 -07:00
$user_email = $user_email_array [ 'username' ] . '@' . Setting :: getSettings () -> email_domain ;
2016-05-17 19:47:38 -07:00
}
2016-05-18 21:33:18 -07:00
if ( $user_username == '' ) {
if ( $this -> option ( 'username_format' ) == 'email' ) {
2016-05-17 19:47:38 -07:00
$user_username = $user_email ;
} else {
2016-06-13 21:45:52 -07:00
$user_name_array = User :: generateFormattedNameFromFullName ( Setting :: getSettings () -> username_format , $user_name );
2016-05-17 19:47:38 -07:00
$user_username = $user_name_array [ 'username' ];
}
}
}
2016-05-25 17:34:54 -07:00
$this -> log ( " --- User Data --- " );
$this -> log ( 'Full Name: ' . $user_name );
$this -> log ( 'First Name: ' . $first_name );
$this -> log ( 'Last Name: ' . $last_name );
$this -> log ( 'Username: ' . $user_username );
$this -> log ( 'Email: ' . $user_email );
$this -> log ( '--- End User Data ---' );
2016-05-17 19:47:38 -07:00
if ( $this -> option ( 'testrun' ))
return new User ;
2016-05-18 21:33:18 -07:00
2016-05-17 19:47:38 -07:00
if ( ! empty ( $user_username )) {
if ( $user = User :: MatchEmailOrUsername ( $user_username , $user_email )
2016-05-18 21:33:18 -07:00
-> whereNotNull ( 'username' ) -> first ()) {
2016-05-24 19:40:52 -07:00
2016-05-25 17:34:54 -07:00
$this -> log ( 'User ' . $user_username . ' already exists' );
2016-05-24 19:40:52 -07:00
} else if (( $first_name != '' ) && ( $last_name != '' ) && ( $user_username != '' )) {
2016-05-18 21:33:18 -07:00
$user = new \App\Models\User ;
$password = substr ( str_shuffle ( " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " ), 0 , 20 );
$user -> first_name = $first_name ;
$user -> last_name = $last_name ;
$user -> username = $user_username ;
$user -> email = $user_email ;
$user -> password = bcrypt ( $password );
$user -> activated = 1 ;
if ( $user -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'User ' . $first_name . ' created' );
2016-05-18 21:33:18 -07:00
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'User' , $user -> getErrors ());
2016-05-18 21:33:18 -07:00
}
2016-05-24 19:40:52 -07:00
} else {
$user = new User ;
2016-05-17 19:47:38 -07:00
}
} else {
$user = new User ;
}
2016-05-18 21:33:18 -07:00
return $user ;
2016-05-17 19:47:38 -07:00
}
2016-05-17 19:25:17 -07:00
private $assets ;
/**
2016-06-13 21:45:52 -07:00
* Create the asset if it doesn ' t exist .
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ param array $row
* @ param array $item
*/
public function createAssetIfNotExists ( array $row , array $item )
{
$asset_serial = $this -> array_smart_fetch ( $row , " serial number " );
$asset_tag = $this -> array_smart_fetch ( $row , " asset tag " );
2016-05-18 20:33:55 -07:00
$asset_image = $this -> array_smart_fetch ( $row , " image " );
2016-05-25 19:13:26 -07:00
$asset_warranty_months = intval ( $this -> array_smart_fetch ( $row , " warranty months " ));
if ( empty ( $asset_warranty_months )) {
$asset_warranty_months = NULL ;
}
2016-05-17 19:25:17 -07:00
// Check for the asset model match and create it if it doesn't exist
$asset_model = $this -> createOrFetchAssetModel ( $row , $item [ " category " ], $item [ " manufacturer " ]);
$supplier = $this -> createOrFetchSupplier ( $row );
2016-05-25 17:34:54 -07:00
$this -> current_assetId = $asset_tag ;
$this -> log ( 'Serial No: ' . $asset_serial );
$this -> log ( 'Asset Tag: ' . $asset_tag );
$this -> log ( 'Notes: ' . $item [ " notes " ]);
2016-06-19 20:06:54 -07:00
$this -> log ( 'Warranty Months: ' . $asset_warranty_months );
2016-05-17 19:25:17 -07:00
foreach ( $this -> assets as $tempasset ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $tempasset -> asset_tag , $asset_tag ) == 0 ) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'A matching Asset ' . $asset_tag . ' already exists' );
2016-05-26 19:29:29 -07:00
// $this->comment('A matching Asset ' . $asset_tag . ' already exists');
2016-05-17 19:25:17 -07:00
return ;
}
}
2016-05-26 19:29:29 -07:00
if ( $item [ " status_label " ]) {
$status_id = $item [ " status_label " ] -> id ;
} else {
2016-06-13 21:45:52 -07:00
// FIXME: We're already grabbing the list of statuses, we should probably not hardcode here
2016-05-25 19:13:26 -07:00
$this -> log ( " No status field found, defaulting to id 1. " );
2016-05-24 15:14:01 -07:00
$status_id = 1 ;
}
2016-05-17 19:25:17 -07:00
$asset = new Asset ();
2016-05-18 20:33:55 -07:00
$asset -> name = $item [ " item_name " ];
2016-05-17 19:25:17 -07:00
if ( $item [ " purchase_date " ] != '' ) {
$asset -> purchase_date = $item [ " purchase_date " ];
} else {
$asset -> purchase_date = NULL ;
}
2016-06-19 20:55:17 -07:00
if ( array_key_exists ( 'custom_fields' , $item )) {
foreach ( $item [ 'custom_fields' ] as $custom_field => $val ) {
$asset -> { $custom_field } = $val ;
}
}
2016-06-13 21:45:52 -07:00
2016-05-26 19:29:29 -07:00
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 );
2016-05-25 17:34:54 -07:00
$this -> log ( " Asset cost parsed: " . $asset -> purchase_cost );
2016-05-17 19:25:17 -07:00
} else {
$asset -> purchase_cost = 0.00 ;
}
2016-05-18 20:33:55 -07:00
$asset -> serial = $asset_serial ;
$asset -> asset_tag = $asset_tag ;
2016-06-19 20:06:54 -07:00
$asset -> warranty_months = $asset_warranty_months ;
2016-05-25 17:34:54 -07:00
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 ;
2016-06-19 20:06:54 -07:00
$asset -> user_id = $this -> option ( 'user_id' );
2016-05-26 19:29:29 -07:00
$this -> log ( " status_id: " . $status_id );
2016-05-17 19:25:17 -07:00
$asset -> status_id = $status_id ;
2016-05-25 17:34:54 -07:00
if ( $item [ " company " ])
$asset -> company_id = $item [ " company " ] -> id ;
2016-05-17 19:25:17 -07:00
$asset -> order_number = $item [ " order_number " ];
2016-05-25 17:34:54 -07:00
if ( $supplier )
$asset -> supplier_id = $supplier -> id ;
2016-05-18 20:33:55 -07:00
$asset -> notes = $item [ " notes " ];
$asset -> image = $asset_image ;
2016-05-18 20:37:59 -07:00
$this -> assets -> add ( $asset );
2016-05-17 19:25:17 -07:00
if ( ! $this -> option ( 'testrun' )) {
if ( $asset -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'Asset ' . $item [ " item_name " ] . ' with serial number ' . $asset_serial . ' was created' );
2016-05-17 19:25:17 -07:00
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'Asset' , $asset -> getErrors ());
2016-05-17 19:25:17 -07:00
}
} else {
return ;
}
}
private $accessories ;
/**
* Create an accessory if a duplicate does not exist
2016-06-13 21:45:52 -07:00
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ param $item array
*/
public function createAccessoryIfNotExists ( array $item )
{
2016-05-25 17:34:54 -07:00
$this -> log ( " Creating Accessory " );
2016-05-17 19:25:17 -07:00
foreach ( $this -> accessories as $tempaccessory ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $tempaccessory -> name , $item [ " item_name " ] ) == 0 ) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'A matching Accessory ' . $item [ " item_name " ] . ' already exists. ' );
2016-05-17 19:25:17 -07:00
// FUTURE: Adjust quantity on import maybe?
return ;
}
}
$accessory = new Accessory ();
2016-05-18 20:33:55 -07:00
$accessory -> name = $item [ " item_name " ];
2016-05-25 17:34:54 -07:00
2016-05-17 19:25:17 -07:00
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 ;
}
2016-05-25 18:19:12 -07:00
if ( $item [ " location " ])
$accessory -> location_id = $item [ " location " ] -> id ;
2016-06-19 20:06:54 -07:00
$accessory -> user_id = $this -> option ( 'user_id' );
2016-05-25 18:19:12 -07:00
if ( $item [ " company " ])
$accessory -> company_id = $item [ " company " ] -> id ;
2016-05-17 19:25:17 -07:00
$accessory -> order_number = $item [ " order_number " ];
2016-05-25 18:19:12 -07:00
if ( $item [ " category " ])
$accessory -> category_id = $item [ " category " ] -> id ;
2016-05-17 19:25:17 -07:00
//TODO: Implement
// $accessory->notes = e($item_notes);
$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 ( ! $this -> option ( 'testrun' )) {
if ( $accessory -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> log ( 'Accessory ' . $item [ " item_name " ] . ' was created' );
2016-05-26 19:29:29 -07:00
// $this->comment('Accessory ' . $item["item_name"] . ' was created');
2016-05-25 18:19:12 -07:00
2016-05-17 19:25:17 -07:00
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'Accessory' , $accessory -> getErrors ()) ;
2016-05-17 19:25:17 -07:00
}
} else {
2016-05-25 17:34:54 -07:00
$this -> log ( 'TEST RUN - Accessory ' . $item [ " item_name " ] . ' not created' );
2016-05-17 19:25:17 -07:00
}
}
private $consumables ;
/**
* Create a consumable if a duplicate does not exist
2016-06-13 21:45:52 -07:00
*
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ param $item array
*/
public function createConsumableIfNotExists ( array $item )
{
2016-05-25 17:34:54 -07:00
$this -> log ( " Creating Consumable " );
2016-05-17 19:25:17 -07:00
foreach ( $this -> consumables as $tempconsumable ) {
2016-06-01 17:31:24 -07:00
if ( strcasecmp ( $tempconsumable -> name , $item [ " item_name " ]) == 0 ) {
2016-05-25 18:19:12 -07:00
$this -> log ( " A matching consumable " . $item [ " item_name " ] . " already exists " );
2016-05-17 19:25:17 -07:00
//TODO: Adjust quantity if different maybe?
return ;
}
}
$consumable = new Consumable ();
2016-05-18 20:33:55 -07:00
$consumable -> name = $item [ " item_name " ];
2016-05-17 19:25:17 -07:00
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 -> location_id = $item [ " location " ] -> id ;
2016-06-19 20:06:54 -07:00
$consumable -> user_id = $this -> option ( 'user_id' );
2016-05-17 19:25:17 -07:00
$consumable -> company_id = $item [ " company " ] -> id ;
$consumable -> order_number = $item [ " order_number " ];
$consumable -> category_id = $item [ " category " ] -> id ;
// TODO:Implement
//$consumable->notes= e($item_notes);
$consumable -> requestable = filter_var ( $item [ " requestable " ], FILTER_VALIDATE_BOOLEAN );
if ( $item [ " quantity " ] > - 1 ) {
$consumable -> qty = $item [ " quantity " ];
} else {
$consumable -> qty = 1 ;
}
if ( ! $this -> option ( " testrun " )) {
if ( $consumable -> save ()) {
2016-05-25 17:34:54 -07:00
$this -> log ( " Consumable " . $item [ " item_name " ] . ' was created' );
2016-05-26 19:29:29 -07:00
// $this->comment("Consumable " . $item["item_name"] . ' was created');
2016-05-25 18:19:12 -07:00
2016-05-17 19:25:17 -07:00
} else {
2016-05-26 19:29:29 -07:00
$this -> jsonError ( 'Consumable' , $consumable -> getErrors ());
2016-05-17 19:25:17 -07:00
}
} else {
2016-05-25 17:34:54 -07:00
$this -> log ( 'TEST RUN - Consumable ' . $item [ 'item_name' ] . ' not created' );
2016-05-17 19:25:17 -07:00
}
}
/**
* Get the console command arguments .
*
2016-06-13 21:45:52 -07:00
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ return array
*/
protected function getArguments ()
{
return array (
array ( 'filename' , InputArgument :: REQUIRED , 'File for the CSV import.' ),
);
}
/**
* Get the console command options .
*
2016-06-13 21:45:52 -07:00
* @ author Daniel Melzter
* @ since 3.0
2016-05-17 19:25:17 -07:00
* @ return array
*/
protected function getOptions ()
{
return array (
array ( 'email_format' , null , InputOption :: VALUE_REQUIRED , 'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname' , null ),
array ( 'username_format' , null , InputOption :: VALUE_REQUIRED , 'The format of the username that should be generated. Options are firstname.lastname, firstname, filastname, email' , null ),
array ( 'testrun' , null , InputOption :: VALUE_NONE , 'If set, will parse and output data without adding to database' , null ),
2016-05-25 18:19:12 -07:00
array ( 'logfile' , null , InputOption :: VALUE_REQUIRED , 'The path to log output to. storage/logs/importer.log by default' , storage_path ( 'logs/importer.log' ) ),
2016-05-26 19:29:29 -07:00
array ( 'item-type' , null , InputOption :: VALUE_REQUIRED , 'Item Type To import. Valid Options are Asset, Consumable, Or Accessory' , 'Asset' ),
2016-06-19 20:06:54 -07:00
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 )
2016-05-17 19:25:17 -07:00
);
}
2016-05-31 17:00:11 -07:00
}