phpvms/app/Services/ImportExport/ExpenseImporter.php

118 lines
3.2 KiB
PHP
Raw Normal View History

2018-03-23 06:17:37 +08:00
<?php
namespace App\Services\ImportExport;
use App\Interfaces\ImportExport;
use App\Models\Aircraft;
use App\Models\Airport;
use App\Models\Expense;
use App\Models\Subfleet;
use Log;
/**
* Import expenses
*/
class ExpenseImporter extends ImportExport
{
public $assetType = 'expense';
/**
* All of the columns that are in the CSV import
* Should match the database fields, for the most part
*/
public static $columns = [
'airline' => 'nullable',
'name' => 'required',
'amount' => 'required|numeric',
'type' => 'required',
'charge_to_user' => 'nullable|boolean',
'multiplier' => 'nullable|numeric',
'active' => 'nullable|boolean',
2018-04-02 03:32:01 +08:00
'ref_model' => 'nullable',
'ref_model_id' => 'nullable',
2018-03-23 06:17:37 +08:00
];
/**
* Import a flight, parse out the different rows
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @param array $row
* @param int $index
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @return bool
*/
public function import(array $row, $index): bool
{
2018-08-27 00:40:04 +08:00
if ($row['airline']) {
2018-03-23 06:17:37 +08:00
$row['airline_id'] = $this->getAirline($row['airline'])->id;
}
2018-08-27 00:40:04 +08:00
// Figure out what this is referring to
2018-03-23 06:17:37 +08:00
$row = $this->getRefClassInfo($row);
2018-08-27 00:40:04 +08:00
if (!$row['active']) {
2018-03-23 08:59:35 +08:00
$row['active'] = true;
}
2018-03-23 06:17:37 +08:00
$expense = Expense::firstOrNew([
'name' => $row['name'],
], $row);
try {
$expense->save();
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
return false;
}
$this->log('Imported '.$row['name']);
return true;
}
/**
2018-04-02 03:32:01 +08:00
* See if this expense refers to a ref_model
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @param array $row
2018-08-27 00:40:04 +08:00
*
2018-03-23 06:17:37 +08:00
* @return array
*/
protected function getRefClassInfo(array $row)
{
2018-04-02 03:32:01 +08:00
$row['ref_model'] = trim($row['ref_model']);
2018-03-23 06:17:37 +08:00
// class from import is being saved as the name of the model only
// prepend the full class path so we can search it out
2018-04-02 03:32:01 +08:00
if (\strlen($row['ref_model']) > 0) {
if (substr_count($row['ref_model'], 'App\Models\\') === 0) {
$row['ref_model'] = 'App\Models\\'.$row['ref_model'];
2018-03-23 06:17:37 +08:00
}
} else {
2018-04-02 03:32:01 +08:00
$row['ref_model'] = Expense::class;
2018-03-23 06:17:37 +08:00
return $row;
}
2018-04-02 03:32:01 +08:00
$class = $row['ref_model'];
$id = $row['ref_model_id'];
2018-03-23 06:17:37 +08:00
$obj = null;
if ($class === Aircraft::class) {
2018-08-27 00:40:04 +08:00
Log::info('Trying to import expense on aircraft, registration: '.$id);
2018-03-23 06:17:37 +08:00
$obj = Aircraft::where('registration', $id)->first();
} elseif ($class === Airport::class) {
2018-08-27 00:40:04 +08:00
Log::info('Trying to import expense on airport, icao: '.$id);
2018-03-23 06:17:37 +08:00
$obj = Airport::where('icao', $id)->first();
} elseif ($class === Subfleet::class) {
2018-08-27 00:40:04 +08:00
Log::info('Trying to import expense on subfleet, type: '.$id);
2018-03-23 06:17:37 +08:00
$obj = Subfleet::where('type', $id)->first();
} else {
$this->errorLog('Unknown/unsupported Expense class: '.$class);
}
2018-08-27 00:40:04 +08:00
if (!$obj) {
2018-03-23 06:17:37 +08:00
return $row;
}
2018-04-02 03:32:01 +08:00
$row['ref_model_id'] = $obj->id;
2018-03-23 06:17:37 +08:00
return $row;
}
}