90d1708aab
* Update SubfleetImporter.php Correction to the import to include Simbrief Code * Update SubfleetImporter.php Added in the missing fields HUB-ID and SIMBrief as these are input on the main screen * Update AircraftImporter.php Part of the missing data fields in csv import export * Update FlightImporter.php Part of the missing fields in csv import and export * Update AircraftImporter.php * Update FlightImporter.php * Update aircraft.csv Test data amended as part of the missing csv fields * Update subfleets.csv Part of the fix for missing fields in csv files used for import/export * Update flights.csv * Update FlightImporter.php * Update subfleets.csv Removed unused fields * Update FlightImporter.php * Update FlightImporter.php * Update FlightImporter.php * amended for new csv file layouts
92 lines
2.4 KiB
PHP
92 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ImportExport;
|
|
|
|
use App\Contracts\ImportExport;
|
|
use App\Models\Fare;
|
|
use App\Models\Subfleet;
|
|
use App\Services\FareService;
|
|
|
|
/**
|
|
* Import subfleets
|
|
*/
|
|
class SubfleetImporter extends ImportExport
|
|
{
|
|
public $assetType = 'subfleet';
|
|
|
|
/**
|
|
* All of the columns that are in the CSV import
|
|
* Should match the database fields, for the most part
|
|
*/
|
|
public static $columns = [
|
|
'airline' => 'required',
|
|
'hub_id' => 'required',
|
|
'type' => 'required',
|
|
'simbrief_type' => 'nullable',
|
|
'name' => 'required',
|
|
'fuel_type' => 'nullable',
|
|
'cost_block_hour' => 'nullable',
|
|
'cost_delay_minute' => 'nullable',
|
|
'ground_handling_multiplier' => 'nullable',
|
|
'fares' => 'nullable',
|
|
];
|
|
|
|
private $fareSvc;
|
|
|
|
/**
|
|
* FlightImportExporter constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->fareSvc = app(FareService::class);
|
|
}
|
|
|
|
/**
|
|
* Import a flight, parse out the different rows
|
|
*
|
|
* @param array $row
|
|
* @param int $index
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function import(array $row, $index): bool
|
|
{
|
|
$airline = $this->getAirline($row['airline']);
|
|
$row['airline_id'] = $airline->id;
|
|
|
|
try {
|
|
$subfleet = Subfleet::updateOrCreate([
|
|
'type' => $row['type'],
|
|
], $row);
|
|
} catch (\Exception $e) {
|
|
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
|
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::updateOrCreate(['code' => $fare_code], ['name' => $fare_code]);
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, $fare_attributes);
|
|
}
|
|
}
|
|
}
|