Skip to content

Commit

Permalink
Set expenses on specific flight types #348 (#549)
Browse files Browse the repository at this point in the history
* Set expenses on specific flight types #348

* Formatting

* Use strict check for in_array
  • Loading branch information
nabeelio authored Feb 12, 2020
1 parent f16af4d commit b9993b9
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class ExpensesAddFlightType extends Migration
{
/**
* Add a `flight_type` column to the expenses table
*/
public function up()
{
Schema::table('expenses', function (Blueprint $table) {
$table->string('flight_type', 50)
->nullable()
->after('type');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('expenses', function (Blueprint $table) {
$table->dropColumn('flight_type');
});
}
}
5 changes: 3 additions & 2 deletions app/Http/Controllers/Admin/ExpenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Controller;
use App\Http\Controllers\Admin\Traits\Importable;
use App\Models\Enums\ExpenseType;
use App\Models\Enums\FlightType;
use App\Models\Enums\ImportExportType;
use App\Models\Expense;
use App\Repositories\AirlineRepository;
Expand Down Expand Up @@ -69,6 +70,7 @@ public function create()
return view('admin.expenses.create', [
'airlines_list' => $this->airlineRepo->selectBoxList(true),
'expense_types' => ExpenseType::select(),
'flight_types' => FlightType::select(),
]);
}

Expand Down Expand Up @@ -135,6 +137,7 @@ public function edit($id)
'expense' => $expense,
'airlines_list' => $this->airlineRepo->selectBoxList(true),
'expense_types' => ExpenseType::select(),
'flight_types' => FlightType::select(),
]);
}

Expand All @@ -154,14 +157,12 @@ public function update($id, Request $request)

if (empty($expenses)) {
Flash::error('Expense not found');

return redirect(route('admin.expenses.index'));
}

$this->expenseRepo->update($request->all(), $id);

Flash::success('Expense updated successfully.');

return redirect(route('admin.expenses.index'));
}

Expand Down
35 changes: 33 additions & 2 deletions app/Models/Expense.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
use App\Models\Traits\ReferenceTrait;

/**
* Class Expense
*
* @property int airline_id
* @property float amount
* @property string name
* @property string type
* @property string flight_type
* @property string ref_model
* @property string ref_model_id
*
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class Expense extends Model
{
Expand All @@ -25,6 +27,7 @@ class Expense extends Model
'name',
'amount',
'type',
'flight_type',
'multiplier',
'charge_to_user',
'ref_model',
Expand All @@ -40,6 +43,34 @@ class Expense extends Model
'charge_to_user' => 'bool',
];

/**
* flight_type is stored a comma delimited list in table. Retrieve it as an array
*
* @return array
*/
public function getFlightTypeAttribute()
{
if (empty(trim($this->attributes['flight_type']))) {
return [];
}

return explode(',', $this->attributes['flight_type']);
}

/**
* Make sure the flight type is stored a comma-delimited list in the table
*
* @param string $value
*/
public function setFlightTypeAttribute($value)
{
if (is_array($value)) {
$this->attributes['flight_type'] = implode(',', $value);
} else {
$this->attributes['flight_type'] = trim($value);
}
}

/**
* Foreign Keys
*/
Expand Down
1 change: 1 addition & 0 deletions app/Models/Pirep.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @property string flight_number
* @property string route_code
* @property string route_leg
* @property string flight_type
* @property int airline_id
* @property int user_id
* @property int aircraft_id
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Traits/ReferenceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function referencesObject($object)
/**
* Return an instance of the object or null
*
* @return \App\Contracts\Model|null
* @return \App\Contracts\Model|$this|null
*/
public function getReferencedObject()
{
Expand Down
25 changes: 17 additions & 8 deletions app/Services/Finance/PirepFinanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use App\Contracts\Service;
use App\Events\Expenses as ExpensesEvent;
use App\Models\Aircraft;
use App\Models\Airport;
use App\Models\Enums\ExpenseType;
use App\Models\Enums\PirepSource;
use App\Models\Expense;
use App\Models\Pirep;
use App\Models\Subfleet;
use App\Repositories\ExpenseRepository;
use App\Repositories\JournalRepository;
use App\Services\FareService;
Expand Down Expand Up @@ -222,13 +225,19 @@ public function payExpensesForPirep(Pirep $pirep): void
/*
* Go through the expenses and apply a mulitplier if present
*/
$expenses->map(function ($expense, $i) use ($pirep) {
/*if ($expense->multiplier) {
# TODO: Modify the amount
}*/

$expenses->map(function (/** @var \App\Models\Expense */ $expense, $i) use ($pirep) {
Log::info('Finance: PIREP: '.$pirep->id.', expense:', $expense->toArray());

// Check to see if there is a certain fleet or flight type set on this expense
// if there is and it doesn't match up the flight type for the PIREP, skip it
if ($expense->ref_model === Expense::class) {
if (is_array($expense->flight_type) && count($expense->flight_type) > 0) {
if (!in_array($pirep->flight_type, $expense->flight_type, true)) {
return;
}
}
}

// Get the transaction group name from the ref_model name
// This way it can be more dynamic and don't have to add special
// tables or specific expense calls to accomodate all of these
Expand All @@ -239,13 +248,13 @@ public function payExpensesForPirep(Pirep $pirep): void
}

// Form the memo, with some specific ones depending on the group
if ($klass === 'Airport') {
if ($expense->ref_model === Airport::class) {
$memo = "Airport Expense: {$expense->name} ({$expense->ref_model_id})";
$transaction_group = "Airport: {$expense->ref_model_id}";
} elseif ($klass === 'Subfleet') {
} elseif ($expense->ref_model === Subfleet::class) {
$memo = "Subfleet Expense: {$expense->name} ({$pirep->aircraft->subfleet->name})";
$transaction_group = "Subfleet: {$expense->name} ({$pirep->aircraft->subfleet->name})";
} elseif ($klass === 'Aircraft') {
} elseif ($expense->ref_model === Aircraft::class) {
$memo = "Aircraft Expense: {$expense->name} ({$pirep->aircraft->name})";
$transaction_group = "Aircraft: {$expense->name} "
."({$pirep->aircraft->name}-{$pirep->aircraft->registration})";
Expand Down
14 changes: 11 additions & 3 deletions resources/views/admin/expenses/fields.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row">
<!-- Code Field -->
<div class="form-group col-sm-6">
<div class="form-group col-sm-4">
{{ Form::label('airline_id', 'Airline:') }}

{{ Form::select('airline_id', $airlines_list, null , ['class' => 'form-control select2']) }}
Expand All @@ -11,12 +11,20 @@
@endcomponent
</div>

<!-- Name Field -->
<div class="form-group col-sm-6">
<div class="form-group col-sm-4">
{{ Form::label('type', 'Expense Type:') }}&nbsp;<span class="required">*</span>
{{ Form::select('type', $expense_types, null , ['class' => 'form-control select2']) }}
<p class="text-danger">{{ $errors->first('type') }}</p>
</div>

<div class="form-group col-sm-4">
{{ Form::label('flight_type', 'Flight Types:') }}&nbsp;
{{ Form::select('flight_type[]', $flight_types, null , ['class' => 'form-control select2', 'multiple']) }}
<p class="text-danger">{{ $errors->first('type') }}</p>
@component('admin.components.info')
If selected and the expense type is "flight", this expense will only apply to the specified flight types
@endcomponent
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
Expand Down
105 changes: 101 additions & 4 deletions tests/FinanceTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use App\Models\Enums\ExpenseType;
use App\Models\Enums\FlightType;
use App\Models\Expense;
use App\Models\Subfleet;
use App\Repositories\ExpenseRepository;
use App\Repositories\JournalRepository;
use App\Services\FareService;
Expand Down Expand Up @@ -79,6 +82,7 @@ public function createFullPirep()

$pirep = factory(App\Models\Pirep::class)->create([
'flight_number' => $flight->flight_number,
'flight_type' => FlightType::SCHED_PAX,
'route_code' => $flight->route_code,
'route_leg' => $flight->route_leg,
'dpt_airport_id' => $dpt_apt->id,
Expand Down Expand Up @@ -114,7 +118,7 @@ public function createFullPirep()

// Add a subfleet expense
factory(App\Models\Expense::class)->create([
'ref_model' => \App\Models\Subfleet::class,
'ref_model' => Subfleet::class,
'ref_model_id' => $subfleet['subfleet']->id,
'amount' => 200,
]);
Expand Down Expand Up @@ -594,7 +598,7 @@ public function testPirepExpenses()
$expenses = $this->expenseRepo->getAllForType(
ExpenseType::FLIGHT,
$airline->id,
\App\Models\Expense::class
Expense::class
);

$this->assertCount(2, $expenses);
Expand All @@ -615,7 +619,7 @@ public function testPirepExpenses()
$subfleet = factory(App\Models\Subfleet::class)->create();
factory(App\Models\Expense::class)->create([
'airline_id' => null,
'ref_model' => \App\Models\Subfleet::class,
'ref_model' => Subfleet::class,
'ref_model_id' => $subfleet->id,
]);

Expand All @@ -628,7 +632,7 @@ public function testPirepExpenses()
$this->assertCount(1, $expenses);

$expense = $expenses->random();
$this->assertEquals(\App\Models\Subfleet::class, $expense->ref_model);
$this->assertEquals(Subfleet::class, $expense->ref_model);
$obj = $expense->getReferencedObject();
$this->assertEquals($obj->id, $expense->ref_model_id);
}
Expand Down Expand Up @@ -680,4 +684,97 @@ public function testPirepFinances()
$this->assertEquals($count, $find->count());
}
}

/**
* @throws Exception
*/
public function testPirepFinancesSpecificExpense()
{
$journalRepo = app(JournalRepository::class);

// Add an expense that's only for a cargo flight
factory(App\Models\Expense::class)->create([
'airline_id' => null,
'amount' => 100,
'flight_type' => FlightType::SCHED_CARGO,
]);

[$user, $pirep, $fares] = $this->createFullPirep();
$user->airline->initJournal(config('phpvms.currency'));

// Override the fares
$fare_counts = [];
foreach ($fares as $fare) {
$fare_counts[] = [
'fare_id' => $fare->id,
'price' => $fare->price,
'count' => 100,
];
}

$this->fareSvc->saveForPirep($pirep, $fare_counts);

// This should process all of the
$pirep = $this->pirepSvc->accept($pirep);

$transactions = $journalRepo->getAllForObject($pirep);

// $this->assertCount(9, $transactions['transactions']);
$this->assertEquals(3020, $transactions['credits']->getValue());
$this->assertEquals(1960, $transactions['debits']->getValue());

// Check that all the different transaction types are there
// test by the different groups that exist
$transaction_tags = [
'fuel' => 1,
'expense' => 1,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];

foreach ($transaction_tags as $type => $count) {
$find = $transactions['transactions']->where('tags', $type);
$this->assertEquals($count, $find->count());
}

// Add a new PIREP;
$pirep2 = factory(App\Models\Pirep::class)->create([
'flight_number' => 100,
'flight_type' => FlightType::SCHED_CARGO,
'dpt_airport_id' => $pirep->dpt_airport_id,
'arr_airport_id' => $pirep->arr_airport_id,
'user_id' => $user->id,
'airline_id' => $user->airline_id,
'aircraft_id' => $pirep->aircraft_id,
'source' => PirepSource::ACARS,
'flight_time' => 120,
'block_fuel' => 10,
'fuel_used' => 9,
]);

$this->fareSvc->saveForPirep($pirep2, $fare_counts);
$pirep2 = $this->pirepSvc->accept($pirep2);

$transactions = $journalRepo->getAllForObject($pirep2);
$this->assertEquals(3020, $transactions['credits']->getValue());
$this->assertEquals(2060, $transactions['debits']->getValue());

// Check that all the different transaction types are there
// test by the different groups that exist
$transaction_tags = [
'fuel' => 1,
'expense' => 2,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];

foreach ($transaction_tags as $type => $count) {
$find = $transactions['transactions']->where('tags', $type);
$this->assertEquals($count, $find->count());
}
}
}

0 comments on commit b9993b9

Please sign in to comment.