Export flights to CSV in admin #194
This commit is contained in:
72
app/Services/ExporterService.php
Normal file
72
app/Services/ExporterService.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Interfaces\Service;
|
||||
use App\Repositories\FlightRepository;
|
||||
use App\Services\Import\FlightExporter;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Csv\CharsetConverter;
|
||||
use League\Csv\Writer;
|
||||
|
||||
/**
|
||||
* Class ImporterService
|
||||
* @package App\Services
|
||||
*/
|
||||
class ExporterService extends Service
|
||||
{
|
||||
protected $flightRepo;
|
||||
|
||||
/**
|
||||
* ImporterService constructor.
|
||||
* @param FlightRepository $flightRepo
|
||||
*/
|
||||
public function __construct(FlightRepository $flightRepo) {
|
||||
$this->flightRepo = $flightRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $csv_file
|
||||
* @return Writer
|
||||
*/
|
||||
public function openCsv($csv_file): Writer
|
||||
{
|
||||
$writer = Writer::createFromPath($csv_file, 'w+');
|
||||
CharsetConverter::addTo($writer, 'utf-8', 'iso-8859-15');
|
||||
return $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actual importer
|
||||
* @param Collection $collection
|
||||
* @param Writer $writer
|
||||
* @param ImportExport $exporter
|
||||
* @return bool
|
||||
* @throws \League\Csv\CannotInsertRecord
|
||||
*/
|
||||
protected function runExport(Collection $collection, Writer $writer, ImportExport $exporter): bool
|
||||
{
|
||||
$writer->insertOne($exporter->getColumns());
|
||||
foreach ($collection as $row) {
|
||||
$writer->insertOne($exporter->export($row));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all of the flights
|
||||
* @param Collection $flights
|
||||
* @param string $csv_file
|
||||
* @return mixed
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
public function exportFlights($flights, $csv_file)
|
||||
{
|
||||
$writer = $this->openCsv($csv_file);
|
||||
|
||||
$exporter = new FlightExporter();
|
||||
return $this->runExport($flights, $writer, $exporter);
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,12 @@ class FareService extends Service
|
||||
{
|
||||
$flight->fares()->syncWithoutDetaching([$fare->id]);
|
||||
|
||||
foreach($override as $key => $item) {
|
||||
if(!$item) {
|
||||
unset($override[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
# modify any pivot values?
|
||||
if (\count($override) > 0) {
|
||||
$flight->fares()->updateExistingPivot($fare->id, $override);
|
||||
|
||||
107
app/Services/Import/FlightExporter.php
Normal file
107
app/Services/Import/FlightExporter.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Import;
|
||||
|
||||
use App\Interfaces\ImportExport;
|
||||
use App\Models\Enums\FlightType;
|
||||
use App\Models\Fare;
|
||||
use App\Models\Flight;
|
||||
use App\Models\Subfleet;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* The flight importer can be imported or export. Operates on rows
|
||||
*
|
||||
* @package App\Services\Import
|
||||
*/
|
||||
class FlightExporter extends ImportExport
|
||||
{
|
||||
/**
|
||||
* Set the current columns and other setup
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$columns = FlightImporter::$columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a flight, parse out the different rows
|
||||
* @param Flight $flight
|
||||
* @return array
|
||||
*/
|
||||
public function export(Flight $flight): array
|
||||
{
|
||||
$ret = [];
|
||||
foreach(self::$columns as $column) {
|
||||
$ret[$column] = $flight->{$column};
|
||||
}
|
||||
|
||||
# Modify special fields
|
||||
$ret['airline'] = $ret['airline']->icao;
|
||||
$ret['distance'] = $ret['distance']->toNumber();
|
||||
|
||||
$ret['fares'] = $this->getFares($flight);
|
||||
$ret['fields'] = $this->getFields($flight);
|
||||
$ret['subfleets'] = $this->getSubfleets($flight);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return any custom fares that have been made to this flight
|
||||
* @param Flight $flight
|
||||
* @return string
|
||||
*/
|
||||
protected function getFares(Flight &$flight): string
|
||||
{
|
||||
$fares = [];
|
||||
foreach($flight->fares as $fare) {
|
||||
$fare_export = [];
|
||||
if($fare->pivot->price) {
|
||||
$fare_export['price'] = $fare->pivot->price;
|
||||
}
|
||||
|
||||
if ($fare->pivot->cost) {
|
||||
$fare_export['cost'] = $fare->pivot->cost;
|
||||
}
|
||||
|
||||
if ($fare->pivot->capacity) {
|
||||
$fare_export['capacity'] = $fare->pivot->capacity;
|
||||
}
|
||||
|
||||
$fares[$fare->code] = $fare_export;
|
||||
}
|
||||
|
||||
return $this->objectToMultiString($fares);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse all of the subfields
|
||||
* @param Flight $flight
|
||||
* @return string
|
||||
*/
|
||||
protected function getFields(Flight &$flight): string
|
||||
{
|
||||
$ret = [];
|
||||
foreach ($flight->field_values as $field) {
|
||||
$ret[$field->name] = $field->value;
|
||||
}
|
||||
|
||||
return $this->objectToMultiString($ret);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the list of subfleets that are associated here
|
||||
* @param Flight $flight
|
||||
* @return string
|
||||
*/
|
||||
protected function getSubfleets(Flight &$flight): string
|
||||
{
|
||||
$subfleets = [];
|
||||
foreach($flight->subfleets as $subfleet) {
|
||||
$subfleets[] = $subfleet->type;
|
||||
}
|
||||
|
||||
return $this->objectToMultiString($subfleets);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user