Import/export expenses #194
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Interfaces\ImportExport;
|
||||
use App\Interfaces\Service;
|
||||
use App\Services\ImportExport\AircraftExporter;
|
||||
use App\Services\ImportExport\AirportExporter;
|
||||
use App\Services\ImportExport\ExpenseExporter;
|
||||
use App\Services\ImportExport\FlightExporter;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Csv\CharsetConverter;
|
||||
@@ -84,6 +85,18 @@ class ExportService extends Service
|
||||
return $this->runExport($airports, $exporter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all of the airports
|
||||
* @param Collection $expenses
|
||||
* @return mixed
|
||||
* @throws \League\Csv\CannotInsertRecord
|
||||
*/
|
||||
public function exportExpenses($expenses)
|
||||
{
|
||||
$exporter = new ExpenseExporter();
|
||||
return $this->runExport($expenses, $exporter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all of the flights
|
||||
* @param Collection $flights
|
||||
|
||||
@@ -52,7 +52,6 @@ class AircraftImporter extends ImportExport
|
||||
public function import(array $row, $index): bool
|
||||
{
|
||||
$subfleet = $this->getSubfleet($row['subfleet']);
|
||||
|
||||
$row['subfleet_id'] = $subfleet->id;
|
||||
|
||||
# Generate a hex code
|
||||
@@ -76,11 +75,11 @@ class AircraftImporter extends ImportExport
|
||||
try {
|
||||
$aircraft->save();
|
||||
} catch(\Exception $e) {
|
||||
$this->status = 'Error in row '.$index.': '.$e->getMessage();
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->status = 'Imported '.$row['registration'].' '.$row['name'];
|
||||
$this->log('Imported '.$row['registration'].' '.$row['name']);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,11 +47,11 @@ class AirportImporter extends ImportExport
|
||||
try {
|
||||
$airport->save();
|
||||
} catch(\Exception $e) {
|
||||
$this->status = 'Error in row '.$index.': '.$e->getMessage();
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->status = 'Imported ' . $row['icao'];
|
||||
$this->log('Imported '.$row['icao']);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
72
app/Services/ImportExport/ExpenseExporter.php
Normal file
72
app/Services/ImportExport/ExpenseExporter.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ImportExport;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Airport;
|
||||
use App\Models\Enums\ExpenseType;
|
||||
use App\Models\Expense;
|
||||
use App\Models\Subfleet;
|
||||
|
||||
/**
|
||||
* Import expenses
|
||||
* @package App\Services\Import
|
||||
*/
|
||||
class ExpenseExporter extends ImportExport
|
||||
{
|
||||
public $assetType = 'expense';
|
||||
|
||||
/**
|
||||
* Set the current columns and other setup
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$columns = ExpenseImporter::$columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a flight, parse out the different rows
|
||||
* @param Expense $expense
|
||||
* @return array
|
||||
*/
|
||||
public function export(Expense $expense): array
|
||||
{
|
||||
$ret = [];
|
||||
|
||||
foreach(self::$columns as $col) {
|
||||
$ret[$col] = $expense->{$col};
|
||||
}
|
||||
|
||||
if($ret['airline']) {
|
||||
$ret['airline'] = $expense->airline->icao;
|
||||
}
|
||||
|
||||
$ret['type'] = ExpenseType::convertToCode($ret['type']);
|
||||
|
||||
// For the different expense types, instead of exporting
|
||||
// the ID, export a specific column
|
||||
if ($expense->ref_class === Expense::class) {
|
||||
$ret['ref_class'] = '';
|
||||
$ret['ref_class_id'] = '';
|
||||
} else {
|
||||
$obj = $expense->getReference();
|
||||
if(!$obj) { // bail out
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if ($expense->ref_class === Aircraft::class) {
|
||||
$ret['ref_class_id'] = $obj->registration;
|
||||
} elseif ($expense->ref_class === Airport::class) {
|
||||
$ret['ref_class_id'] = $obj->icao;
|
||||
} elseif ($expense->ref_class === Subfleet::class) {
|
||||
$ret['ref_class_id'] = $obj->type;
|
||||
}
|
||||
}
|
||||
|
||||
// And convert the ref_class into the shorter name
|
||||
$ret['ref_class'] = str_replace('App\Models\\', '', $ret['ref_class']);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
113
app/Services/ImportExport/ExpenseImporter.php
Normal file
113
app/Services/ImportExport/ExpenseImporter.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ImportExport;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Airport;
|
||||
use App\Models\Enums\ExpenseType;
|
||||
use App\Models\Expense;
|
||||
use App\Models\Subfleet;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Import expenses
|
||||
* @package App\Services\Import
|
||||
*/
|
||||
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',
|
||||
'name',
|
||||
'amount',
|
||||
'type',
|
||||
'charge_to_user',
|
||||
'multiplier',
|
||||
'active',
|
||||
'ref_class',
|
||||
'ref_class_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Import a flight, parse out the different rows
|
||||
* @param array $row
|
||||
* @param int $index
|
||||
* @return bool
|
||||
*/
|
||||
public function import(array $row, $index): bool
|
||||
{
|
||||
if($row['airline']) {
|
||||
$row['airline_id'] = $this->getAirline($row['airline'])->id;
|
||||
}
|
||||
|
||||
# Figure out what this is referring to
|
||||
$row = $this->getRefClassInfo($row);
|
||||
|
||||
$row['type'] = ExpenseType::getFromCode($row['type']);
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* See if this expense refers to a ref_class
|
||||
* @param array $row
|
||||
* @return array
|
||||
*/
|
||||
protected function getRefClassInfo(array $row)
|
||||
{
|
||||
$row['ref_class'] = trim($row['ref_class']);
|
||||
|
||||
// class from import is being saved as the name of the model only
|
||||
// prepend the full class path so we can search it out
|
||||
if (\strlen($row['ref_class']) > 0) {
|
||||
if (substr_count($row['ref_class'], 'App\Models\\') === 0) {
|
||||
$row['ref_class'] = 'App\Models\\'.$row['ref_class'];
|
||||
}
|
||||
} else {
|
||||
$row['ref_class'] = Expense::class;
|
||||
return $row;
|
||||
}
|
||||
|
||||
$class = $row['ref_class'];
|
||||
$id = $row['ref_class_id'];
|
||||
$obj = null;
|
||||
|
||||
if ($class === Aircraft::class) {
|
||||
Log::info('Trying to import expense on aircraft, registration: ' . $id);
|
||||
$obj = Aircraft::where('registration', $id)->first();
|
||||
} elseif ($class === Airport::class) {
|
||||
Log::info('Trying to import expense on airport, icao: ' . $id);
|
||||
$obj = Airport::where('icao', $id)->first();
|
||||
} elseif ($class === Subfleet::class) {
|
||||
Log::info('Trying to import expense on subfleet, type: ' . $id);
|
||||
$obj = Subfleet::where('type', $id)->first();
|
||||
} else {
|
||||
$this->errorLog('Unknown/unsupported Expense class: '.$class);
|
||||
}
|
||||
|
||||
if(!$obj) {
|
||||
return $row;
|
||||
}
|
||||
|
||||
$row['ref_class_id'] = $obj->id;
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Services\ImportExport;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Enums\FlightType;
|
||||
use App\Models\Flight;
|
||||
|
||||
/**
|
||||
@@ -38,6 +39,8 @@ class FlightExporter extends ImportExport
|
||||
$ret['airline'] = $ret['airline']->icao;
|
||||
$ret['distance'] = $ret['distance']->toNumber();
|
||||
|
||||
$ret['flight_type'] = FlightType::convertToCode($ret['flight_type']);
|
||||
|
||||
$ret['fares'] = $this->getFares($flight);
|
||||
$ret['fields'] = $this->getFields($flight);
|
||||
$ret['subfleets'] = $this->getSubfleets($flight);
|
||||
|
||||
@@ -68,7 +68,7 @@ class FlightImporter extends ImportExport
|
||||
* @param int $index
|
||||
* @return bool
|
||||
*/
|
||||
public function import(array $row, $index)
|
||||
public function import(array $row, $index): bool
|
||||
{
|
||||
// Get the airline ID from the ICAO code
|
||||
$airline = $this->getAirline($row['airline']);
|
||||
@@ -89,7 +89,7 @@ class FlightImporter extends ImportExport
|
||||
try {
|
||||
$flight->save();
|
||||
} catch (\Exception $e) {
|
||||
$this->status = 'Error in row '.$index.': '.$e->getMessage();
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ class FlightImporter extends ImportExport
|
||||
$this->processFares($flight, $row['fares']);
|
||||
$this->processFields($flight, $row['fields']);
|
||||
|
||||
$this->status = 'Imported row '.$index;
|
||||
$this->log('Imported row '.$index);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,14 +29,9 @@ class SubfleetImporter extends ImportExport
|
||||
* @param int $index
|
||||
* @return bool
|
||||
*/
|
||||
public function import(array $row, $index)
|
||||
public function import(array $row, $index): bool
|
||||
{
|
||||
$airline = $this->getAirline($row['airline']);
|
||||
if(!$airline) {
|
||||
$this->status = 'Airline '.$row['airline'].' not found, row: '.$index;
|
||||
return false;
|
||||
}
|
||||
|
||||
$row['airline_id'] = $airline->id;
|
||||
|
||||
$subfleet = Subfleet::firstOrNew([
|
||||
@@ -46,11 +41,11 @@ class SubfleetImporter extends ImportExport
|
||||
try {
|
||||
$subfleet->save();
|
||||
} catch(\Exception $e) {
|
||||
$this->status = 'Error in row '.$index.': '.$e->getMessage();
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->status = 'Imported ' . $row['type'];
|
||||
$this->log('Imported '.$row['type']);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,18 @@ namespace App\Services;
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Interfaces\Service;
|
||||
use App\Models\Airport;
|
||||
use App\Models\Expense;
|
||||
use App\Repositories\FlightRepository;
|
||||
use App\Services\ImportExport\AircraftImporter;
|
||||
use App\Services\ImportExport\AirportImporter;
|
||||
use App\Services\ImportExport\ExpenseImporter;
|
||||
use App\Services\ImportExport\FlightImporter;
|
||||
use App\Services\ImportExport\SubfleetImporter;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use League\Csv\Exception;
|
||||
use League\Csv\Reader;
|
||||
use Log;
|
||||
use Validator;
|
||||
|
||||
/**
|
||||
* Class ImportService
|
||||
@@ -29,51 +35,76 @@ class ImportService extends Service
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $csv_file
|
||||
* @return Reader
|
||||
* @throws \League\Csv\Exception
|
||||
* @param $error
|
||||
* @param $e
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function openCsv($csv_file)
|
||||
protected function throwError($error, \Exception $e= null): void
|
||||
{
|
||||
$reader = Reader::createFromPath($csv_file);
|
||||
$reader->setDelimiter(',');
|
||||
$reader->setEnclosure('"');
|
||||
Log::error($error);
|
||||
if($e) {
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
|
||||
return $reader;
|
||||
$validator = Validator::make([], []);
|
||||
$validator->errors()->add('csv_file', $error);
|
||||
throw new ValidationException($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actual importer
|
||||
* @param $csv_file
|
||||
* @return Reader
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function openCsv($csv_file)
|
||||
{
|
||||
try {
|
||||
$reader = Reader::createFromPath($csv_file);
|
||||
$reader->setDelimiter(',');
|
||||
$reader->setEnclosure('"');
|
||||
return $reader;
|
||||
} catch (Exception $e) {
|
||||
$this->throwError('Error opening CSV: '.$e->getMessage(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actual importer, pass in one of the Import classes which implements
|
||||
* the ImportExport interface
|
||||
* @param Reader $reader
|
||||
* @param ImportExport $importer
|
||||
* @return array
|
||||
* @throws ValidationException
|
||||
*/
|
||||
protected function runImport(Reader $reader, ImportExport $importer): array
|
||||
{
|
||||
$import_report = [
|
||||
'success' => [],
|
||||
'failed' => [],
|
||||
];
|
||||
|
||||
$cols = $importer->getColumns();
|
||||
$first_header = $cols[0];
|
||||
|
||||
$first = true;
|
||||
$records = $reader->getRecords($cols);
|
||||
foreach ($records as $offset => $row) {
|
||||
// check if the first row being read is the header
|
||||
if ($row[$first_header] === $first_header) {
|
||||
if ($first) {
|
||||
$first = false;
|
||||
|
||||
if($row[$first_header] !== $first_header) {
|
||||
$this->throwError('CSV file doesn\'t seem to match import type');
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$success = $importer->import($row, $offset);
|
||||
if ($success) {
|
||||
$import_report['success'][] = $importer->status;
|
||||
} else {
|
||||
$import_report['failed'][] = $importer->status;
|
||||
// Do a sanity check on the number of columns first
|
||||
if (!$importer->checkColumns($row)) {
|
||||
$importer->errorLog('Number of columns in row doesn\'t match');
|
||||
continue;
|
||||
}
|
||||
|
||||
$importer->import($row, $offset);
|
||||
}
|
||||
|
||||
return $import_report;
|
||||
return $importer->status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,6 +113,7 @@ class ImportService extends Service
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importAircraft($csv_file, bool $delete_previous = true)
|
||||
{
|
||||
@@ -120,6 +152,28 @@ class ImportService extends Service
|
||||
return $this->runImport($reader, $importer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import expenses
|
||||
* @param string $csv_file
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
public function importExpenses($csv_file, bool $delete_previous = true)
|
||||
{
|
||||
if ($delete_previous) {
|
||||
Expense::truncate();
|
||||
}
|
||||
|
||||
$reader = $this->openCsv($csv_file);
|
||||
if (!$reader) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$importer = new ExpenseImporter();
|
||||
return $this->runImport($reader, $importer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import flights
|
||||
* @param string $csv_file
|
||||
|
||||
Reference in New Issue
Block a user