Add fare import/exporter #194
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Interfaces\Service;
|
||||
use App\Services\ImportExport\AircraftExporter;
|
||||
use App\Services\ImportExport\AirportExporter;
|
||||
use App\Services\ImportExport\ExpenseExporter;
|
||||
use App\Services\ImportExport\FareExporter;
|
||||
use App\Services\ImportExport\FlightExporter;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Csv\CharsetConverter;
|
||||
@@ -97,6 +98,18 @@ class ExportService extends Service
|
||||
return $this->runExport($expenses, $exporter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all of the fares
|
||||
* @param Collection $fares
|
||||
* @return mixed
|
||||
* @throws \League\Csv\CannotInsertRecord
|
||||
*/
|
||||
public function exportFares($fares)
|
||||
{
|
||||
$exporter = new FareExporter();
|
||||
return $this->runExport($fares, $exporter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all of the flights
|
||||
* @param Collection $flights
|
||||
|
||||
39
app/Services/ImportExport/FareExporter.php
Normal file
39
app/Services/ImportExport/FareExporter.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ImportExport;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Fare;
|
||||
|
||||
/**
|
||||
* The flight importer can be imported or export. Operates on rows
|
||||
*
|
||||
* @package App\Services\Import
|
||||
*/
|
||||
class FareExporter extends ImportExport
|
||||
{
|
||||
public $assetType = 'fare';
|
||||
|
||||
/**
|
||||
* Set the current columns and other setup
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$columns = FareImporter::$columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a flight, parse out the different rows
|
||||
* @param Fare $fare
|
||||
* @return array
|
||||
*/
|
||||
public function export(Fare $fare): array
|
||||
{
|
||||
$ret = [];
|
||||
foreach(self::$columns as $column) {
|
||||
$ret[$column] = $fare->{$column};
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
53
app/Services/ImportExport/FareImporter.php
Normal file
53
app/Services/ImportExport/FareImporter.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ImportExport;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Fare;
|
||||
|
||||
/**
|
||||
* Import aircraft
|
||||
* @package App\Services\Import
|
||||
*/
|
||||
class FareImporter extends ImportExport
|
||||
{
|
||||
public $assetType = 'fare';
|
||||
|
||||
/**
|
||||
* All of the columns that are in the CSV import
|
||||
* Should match the database fields, for the most part
|
||||
*/
|
||||
public static $columns = [
|
||||
'code',
|
||||
'name',
|
||||
'price',
|
||||
'cost',
|
||||
'capacity',
|
||||
'notes',
|
||||
'active',
|
||||
];
|
||||
|
||||
/**
|
||||
* Import a flight, parse out the different rows
|
||||
* @param array $row
|
||||
* @param int $index
|
||||
* @return bool
|
||||
*/
|
||||
public function import(array $row, $index): bool
|
||||
{
|
||||
# Try to add or update
|
||||
$fare = Fare::firstOrNew([
|
||||
'code' => $row['code'],
|
||||
], $row);
|
||||
|
||||
try {
|
||||
$fare->save();
|
||||
} catch(\Exception $e) {
|
||||
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->log('Imported '.$row['code'].' '.$row['name']);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@
|
||||
namespace App\Services\ImportExport;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Fare;
|
||||
use App\Models\Subfleet;
|
||||
use App\Services\FareService;
|
||||
|
||||
/**
|
||||
* Import subfleets
|
||||
@@ -21,8 +23,19 @@ class SubfleetImporter extends ImportExport
|
||||
'airline',
|
||||
'type',
|
||||
'name',
|
||||
'fares',
|
||||
];
|
||||
|
||||
private $fareSvc;
|
||||
|
||||
/**
|
||||
* FlightImportExporter constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->fareSvc = app(FareService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a flight, parse out the different rows
|
||||
* @param array $row
|
||||
@@ -45,7 +58,28 @@ class SubfleetImporter extends ImportExport
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->processFares($subfleet, $row['fares']);
|
||||
|
||||
$this->log('Imported '.$row['type']);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse all of the fares in the multi-format
|
||||
* @param Subfleet $subfleet
|
||||
* @param $col
|
||||
*/
|
||||
protected function processFares(Subfleet &$subfleet, $col): void
|
||||
{
|
||||
$fares = $this->parseMultiColumnValues($col);
|
||||
foreach ($fares as $fare_code => $fare_attributes) {
|
||||
if (\is_int($fare_code)) {
|
||||
$fare_code = $fare_attributes;
|
||||
$fare_attributes = [];
|
||||
}
|
||||
|
||||
$fare = Fare::firstOrCreate(['code' => $fare_code], ['name' => $fare_code]);
|
||||
$this->fareSvc->setForSubfleet($subfleet, $fare, $fare_attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use App\Repositories\FlightRepository;
|
||||
use App\Services\ImportExport\AircraftImporter;
|
||||
use App\Services\ImportExport\AirportImporter;
|
||||
use App\Services\ImportExport\ExpenseImporter;
|
||||
use App\Services\ImportExport\FareImporter;
|
||||
use App\Services\ImportExport\FlightImporter;
|
||||
use App\Services\ImportExport\SubfleetImporter;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -112,7 +113,6 @@ class ImportService extends Service
|
||||
* @param string $csv_file
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importAircraft($csv_file, bool $delete_previous = true)
|
||||
@@ -135,7 +135,7 @@ class ImportService extends Service
|
||||
* @param string $csv_file
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importAirports($csv_file, bool $delete_previous = true)
|
||||
{
|
||||
@@ -157,7 +157,7 @@ class ImportService extends Service
|
||||
* @param string $csv_file
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importExpenses($csv_file, bool $delete_previous = true)
|
||||
{
|
||||
@@ -174,12 +174,35 @@ class ImportService extends Service
|
||||
return $this->runImport($reader, $importer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import fares
|
||||
* @param string $csv_file
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importFares($csv_file, bool $delete_previous = true)
|
||||
{
|
||||
if ($delete_previous) {
|
||||
# TODO: Delete all from: fares
|
||||
}
|
||||
|
||||
$reader = $this->openCsv($csv_file);
|
||||
if (!$reader) {
|
||||
# TODO: Throw an error
|
||||
return false;
|
||||
}
|
||||
|
||||
$importer = new FareImporter();
|
||||
return $this->runImport($reader, $importer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import flights
|
||||
* @param string $csv_file
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importFlights($csv_file, bool $delete_previous = true)
|
||||
{
|
||||
@@ -202,7 +225,7 @@ class ImportService extends Service
|
||||
* @param string $csv_file
|
||||
* @param bool $delete_previous
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importSubfleets($csv_file, bool $delete_previous = true)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user