Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Fuel and Ground Handling Costs #1050

Merged
merged 14 commits into from
Mar 18, 2021
44 changes: 28 additions & 16 deletions app/Services/Finance/PirepFinanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public function payFaresForPirep($pirep): void
public function payFuelCosts(Pirep $pirep): void
{
$ap = $pirep->dpt_airport;
// Get Airport Fuel Cost or revert back to settings
$fuel_cost = $ap->fuel_jeta_cost ?? setting('airports.default_jet_a_fuel_cost');
if (setting('pireps.advanced_fuel', false)) {
$ac = $pirep->aircraft;
// Reading second row by skip(1) to reach the previous accepted pirep. Current pirep is at the first row
Expand All @@ -173,15 +175,15 @@ public function payFuelCosts(Pirep $pirep): void
$fuel_amount = $pirep->fuel_used;
}

$debit = Money::createFromAmount($fuel_amount * $ap->fuel_jeta_cost);
Log::info('Finance: Fuel cost, (fuel='.$fuel_amount.', cost='.$ap->fuel_jeta_cost.') D='
$debit = Money::createFromAmount($fuel_amount * $fuel_cost);
Log::info('Finance: Fuel cost, (fuel='.$fuel_amount.', cost='.$fuel_cost.') D='
.$debit->getAmount());

$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
$debit,
$pirep,
'Fuel Cost ('.$ap->fuel_jeta_cost.'/'.config('phpvms.internal_units.fuel').')',
'Fuel Cost ('.$fuel_cost.'/'.config('phpvms.internal_units.fuel').')',
'Fuel',
'fuel'
);
Expand Down Expand Up @@ -415,14 +417,26 @@ public function payExpensesEventsForPirep(Pirep $pirep): void
*/
public function payGroundHandlingForPirep(Pirep $pirep): void
{
$ground_handling_cost = $this->getGroundHandlingCost($pirep);
Log::info('Finance: PIREP: '.$pirep->id.'; ground handling: '.$ground_handling_cost);
$ground_handling_cost = $this->getGroundHandlingCost($pirep, $pirep->dpt_airport);
Log::info('Finance: PIREP: '.$pirep->id.'; dpt ground handling: '.$ground_handling_cost);

$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
Money::createFromAmount($ground_handling_cost),
$pirep,
'Ground Handling (Departure)',
'Ground Handling',
'ground_handling'
);

$ground_handling_cost = $this->getGroundHandlingCost($pirep, $pirep->arr_airport);
Log::info('Finance: PIREP: '.$pirep->id.'; arrival ground handling: '.$ground_handling_cost);

$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
Money::createFromAmount($ground_handling_cost),
$pirep,
'Ground Handling (Departure)',
'Ground Handling',
'ground_handling'
);
Expand Down Expand Up @@ -525,23 +539,21 @@ public function getReconciledFaresForPirep($pirep)
* Return the costs for the ground handling, with the multiplier
* being applied from the subfleet
*
* @param Pirep $pirep
* @param Pirep $pirep
* @param Airport $airport
*
* @return float|null
*/
public function getGroundHandlingCost(Pirep $pirep)
public function getGroundHandlingCost(Pirep $pirep, Airport $airport): ?float
{
if (filled($pirep->aircraft->subfleet->ground_handling_multiplier)) {
// force into percent mode
$multiplier = $pirep->aircraft->subfleet->ground_handling_multiplier.'%';

return Math::applyAmountOrPercent(
$pirep->arr_airport->ground_handling_cost,
$multiplier
);
$gh_cost = $airport->ground_handling_cost ?? setting('airports.default_ground_handling_cost');
if (!filled($pirep->aircraft->subfleet->ground_handling_multiplier)) {
return $gh_cost;
}

return $pirep->arr_airport->ground_handling_cost;
// force into percent mode
$multiplier = $pirep->aircraft->subfleet->ground_handling_multiplier.'%';
return Math::applyAmountOrPercent($gh_cost, $multiplier);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/FinanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ public function testPirepFinances()

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

// Check that all the different transaction types are there
// test by the different groups that exist
Expand All @@ -910,7 +910,7 @@ public function testPirepFinances()
'expense' => 1,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'ground_handling' => 2,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];

Expand Down Expand Up @@ -956,7 +956,7 @@ public function testPirepFinancesSpecificExpense()

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

// Check that all the different transaction types are there
// test by the different groups that exist
Expand All @@ -966,7 +966,7 @@ public function testPirepFinancesSpecificExpense()
'expense' => 1,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'ground_handling' => 2,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];

Expand Down Expand Up @@ -995,7 +995,7 @@ public function testPirepFinancesSpecificExpense()

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

// Check that all the different transaction types are there
// test by the different groups that exist
Expand All @@ -1005,7 +1005,7 @@ public function testPirepFinancesSpecificExpense()
'expense' => 2,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'ground_handling' => 2,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];

Expand Down