Skip to content

Commit

Permalink
Add data migration for the expenses
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed May 24, 2024
1 parent a936ffe commit f958b97
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use App\Contracts\Migration;
use App\Models\Expense;

/**
* Update the expenses to add the airline ID
*/
return new class() extends Migration {
public function up(): void
{
/** @var Expense[] $all_expenses */
$all_expenses = Expense::all();
foreach ($all_expenses as $expense) {
$this->getAirlineId($expense);
}
}

/**
* Figure out the airline ID
*
* @param Expense $expense
*
* @return void
*/
public function getAirlineId(Expense $expense): void
{
$klass = 'Expense';
if ($expense->ref_model) {
$ref = explode('\\', $expense->ref_model);
$klass = end($ref);
$obj = $expense->getReferencedObject();
}

if (empty($obj)) {
return;
}

if ($klass === 'Airport') {
// TODO: Get an airline ID?
} elseif ($klass === 'Subfleet') {
$expense->airline_id = $obj->airline_id;
} elseif ($klass === 'Aircraft') {
$expense->airline_id = $obj->airline->id;
}

$expense->save();
}
};

0 comments on commit f958b97

Please sign in to comment.