2018-03-23 06:17:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\ImportExport;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\ImportExport;
|
2018-03-23 06:17:37 +08:00
|
|
|
use App\Models\Aircraft;
|
|
|
|
use App\Models\Airport;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Models\Subfleet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import expenses
|
|
|
|
*/
|
|
|
|
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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param Expense $expense
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-04-23 21:46:28 +08:00
|
|
|
public function export($expense): array
|
2018-03-23 06:17:37 +08:00
|
|
|
{
|
|
|
|
$ret = [];
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach (self::$columns as $col) {
|
2018-03-23 06:17:37 +08:00
|
|
|
$ret[$col] = $expense->{$col};
|
|
|
|
}
|
|
|
|
|
2018-03-24 05:38:56 +08:00
|
|
|
// Special fields
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($ret['airline']) {
|
2018-03-23 06:17:37 +08:00
|
|
|
$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-08-27 00:40:04 +08:00
|
|
|
if (!$obj) { // bail out
|
2018-03-23 06:17:37 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|