phpvms/app/Services/ImportExport/ExpenseExporter.php

77 lines
1.9 KiB
PHP
Raw Normal View History

2018-03-23 06:17:37 +08:00
<?php
namespace App\Services\ImportExport;
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()
{
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};
}
// 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;
}
2020-10-20 03:22:06 +08:00
if ($ret['flight_type']) {
$ret['flight_type'] = $ret['flight_type'][0];
}
2018-03-23 06:17:37 +08:00
// 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 {
$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
2020-10-20 03:22:06 +08:00
return array_values($ret);
2018-03-23 06:17:37 +08:00
}
}