2018-03-23 06:17:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\ImportExport;
|
|
|
|
|
|
|
|
use App\Interfaces\ImportExport;
|
|
|
|
use App\Models\Aircraft;
|
|
|
|
use App\Models\Airport;
|
|
|
|
use App\Models\Enums\ExpenseType;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Models\Subfleet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import expenses
|
|
|
|
* @package App\Services\Import
|
|
|
|
*/
|
|
|
|
class ExpenseExporter extends ImportExport
|
|
|
|
{
|
|
|
|
public $assetType = 'expense';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current columns and other setup
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-03-31 06:27:29 +08:00
|
|
|
self::$columns = array_keys(ExpenseImporter::$columns);
|
2018-03-23 06:17:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a flight, parse out the different rows
|
|
|
|
* @param Expense $expense
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-04-23 21:46:28 +08:00
|
|
|
public function export($expense): array
|
2018-03-23 06:17:37 +08:00
|
|
|
{
|
|
|
|
$ret = [];
|
|
|
|
|
|
|
|
foreach(self::$columns as $col) {
|
|
|
|
$ret[$col] = $expense->{$col};
|
|
|
|
}
|
|
|
|
|
2018-03-24 05:38:56 +08:00
|
|
|
// Special fields
|
|
|
|
|
2018-03-23 06:17:37 +08:00
|
|
|
if($ret['airline']) {
|
|
|
|
$ret['airline'] = $expense->airline->icao;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the different expense types, instead of exporting
|
|
|
|
// the ID, export a specific column
|
2018-04-02 03:32:01 +08:00
|
|
|
if ($expense->ref_model === Expense::class) {
|
|
|
|
$ret['ref_model'] = '';
|
|
|
|
$ret['ref_model_id'] = '';
|
2018-03-23 06:17:37 +08:00
|
|
|
} else {
|
2018-04-02 11:26:20 +08:00
|
|
|
$obj = $expense->getReferencedObject();
|
2018-03-23 06:17:37 +08:00
|
|
|
if(!$obj) { // bail out
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:32:01 +08:00
|
|
|
if ($expense->ref_model === Aircraft::class) {
|
|
|
|
$ret['ref_model_id'] = $obj->registration;
|
|
|
|
} elseif ($expense->ref_model === Airport::class) {
|
|
|
|
$ret['ref_model_id'] = $obj->icao;
|
|
|
|
} elseif ($expense->ref_model === Subfleet::class) {
|
|
|
|
$ret['ref_model_id'] = $obj->type;
|
2018-03-23 06:17:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:32:01 +08:00
|
|
|
// And convert the ref_model into the shorter name
|
|
|
|
$ret['ref_model'] = str_replace('App\Models\\', '', $ret['ref_model']);
|
2018-03-23 06:17:37 +08:00
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|