Add fare import/exporter #194
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user