2018-03-23 06:48:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\ImportExport;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\ImportExport;
|
2018-03-23 06:48:57 +08:00
|
|
|
use App\Models\Fare;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import aircraft
|
|
|
|
*/
|
|
|
|
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 = [
|
2018-03-31 06:27:29 +08:00
|
|
|
'code' => 'required',
|
|
|
|
'name' => 'required',
|
|
|
|
'price' => 'nullable|numeric',
|
|
|
|
'cost' => 'nullable|numeric',
|
|
|
|
'capacity' => 'required|integer',
|
|
|
|
'notes' => 'nullable',
|
|
|
|
'active' => 'nullable|boolean',
|
2018-03-23 06:48:57 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a flight, parse out the different rows
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:48:57 +08:00
|
|
|
* @param array $row
|
|
|
|
* @param int $index
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:48:57 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function import(array $row, $index): bool
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Try to add or update
|
2018-03-23 06:48:57 +08:00
|
|
|
$fare = Fare::firstOrNew([
|
|
|
|
'code' => $row['code'],
|
|
|
|
], $row);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$fare->save();
|
2018-08-27 00:40:04 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-03-23 06:48:57 +08:00
|
|
|
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->log('Imported '.$row['code'].' '.$row['name']);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|