2018-03-21 08:17:11 +08:00
|
|
|
<?php
|
|
|
|
|
2018-03-23 01:43:58 +08:00
|
|
|
namespace App\Services\ImportExport;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\ImportExport;
|
2018-03-23 06:48:57 +08:00
|
|
|
use App\Models\Fare;
|
2018-03-21 08:17:11 +08:00
|
|
|
use App\Models\Subfleet;
|
2018-03-23 06:48:57 +08:00
|
|
|
use App\Services\FareService;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Import subfleets
|
|
|
|
*/
|
|
|
|
class SubfleetImporter extends ImportExport
|
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
public $assetType = 'subfleet';
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* All of the columns that are in the CSV import
|
|
|
|
* Should match the database fields, for the most part
|
|
|
|
*/
|
|
|
|
public static $columns = [
|
2020-11-27 02:34:04 +08:00
|
|
|
'airline' => 'required',
|
2021-09-08 21:50:34 +08:00
|
|
|
'hub_id' => 'required',
|
2020-11-27 02:34:04 +08:00
|
|
|
'type' => 'required',
|
2021-09-08 21:50:34 +08:00
|
|
|
'simbrief_type' => 'nullable',
|
2020-11-27 02:34:04 +08:00
|
|
|
'name' => 'required',
|
|
|
|
'fuel_type' => 'nullable',
|
|
|
|
'cost_block_hour' => 'nullable',
|
|
|
|
'cost_delay_minute' => 'nullable',
|
|
|
|
'ground_handling_multiplier' => 'nullable',
|
|
|
|
'fares' => 'nullable',
|
2018-03-21 08:17:11 +08:00
|
|
|
];
|
|
|
|
|
2018-03-23 06:48:57 +08:00
|
|
|
private $fareSvc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FlightImportExporter constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->fareSvc = app(FareService::class);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* Import a flight, parse out the different rows
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param array $row
|
|
|
|
* @param int $index
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-03-23 06:17:37 +08:00
|
|
|
public function import(array $row, $index): bool
|
2018-03-21 08:17:11 +08:00
|
|
|
{
|
|
|
|
$airline = $this->getAirline($row['airline']);
|
|
|
|
$row['airline_id'] = $airline->id;
|
|
|
|
|
|
|
|
try {
|
2020-02-09 02:29:34 +08:00
|
|
|
$subfleet = Subfleet::updateOrCreate([
|
|
|
|
'type' => $row['type'],
|
|
|
|
], $row);
|
2018-08-27 00:40:04 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-03-23 06:17:37 +08:00
|
|
|
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
2018-03-21 08:17:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-23 06:48:57 +08:00
|
|
|
$this->processFares($subfleet, $row['fares']);
|
|
|
|
|
2018-03-23 06:17:37 +08:00
|
|
|
$this->log('Imported '.$row['type']);
|
2018-03-21 08:17:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-23 06:48:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse all of the fares in the multi-format
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:48:57 +08:00
|
|
|
* @param Subfleet $subfleet
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param $col
|
2018-03-23 06:48:57 +08:00
|
|
|
*/
|
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
$fare = Fare::updateOrCreate(['code' => $fare_code], ['name' => $fare_code]);
|
2018-03-23 06:48:57 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, $fare_attributes);
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 08:17:11 +08:00
|
|
|
}
|