-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
app/Database/migrations_data/2023_12_19_174436_correct_expenses.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |