Fix for Fuel and Ground Handling Costs (#1050)

* Fix for Fuel and Ground Handling Costs

PR aims to fix issue #1048  and implements the feature request #1049 

If no fuel cost is defined for departure airport, settings / airport fuel price will be used.
If no ground handling cost is defined for airports, settings / airport ground handling cost will be used.

Ground handling prices of both departure and arrival airport will be used for calculations.

* Resolve conflict with latest dev

PR will still fail checks due to double ground handling fares and will work on it to have two records at transactions for dep/arr

* Remove Double GH Costs / Fix The Bug Only

Removed double GH costs for now, pr aims only fixing the current issue (general settings not being read for fuel and ground handling costs)

* Add departure and arrival airports to ground handling

* Style fix

* Fix tests

Co-authored-by: Nabeel S <nabeelio@users.noreply.github.com>
Co-authored-by: Nabeel Shahzad <nabeel@nabeel.sh>
This commit is contained in:
B.Fatih KOZ 2021-03-18 18:32:40 +03:00 committed by GitHub
parent 63eef59060
commit 1d83b85d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 22 deletions

View File

@ -152,6 +152,8 @@ class PirepFinanceService extends Service
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
@ -173,15 +175,15 @@ class PirepFinanceService extends Service
$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'
);
@ -415,14 +417,26 @@ class PirepFinanceService extends Service
*/
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'
);
@ -526,22 +540,20 @@ class PirepFinanceService extends Service
* being applied from the subfleet
*
* @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);
}
/**

View File

@ -900,7 +900,7 @@ class FinanceTest extends TestCase
// $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
@ -910,7 +910,7 @@ class FinanceTest extends TestCase
'expense' => 1,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'ground_handling' => 2,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];
@ -956,7 +956,7 @@ class FinanceTest extends TestCase
// $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
@ -966,7 +966,7 @@ class FinanceTest extends TestCase
'expense' => 1,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'ground_handling' => 2,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];
@ -995,7 +995,7 @@ class FinanceTest extends TestCase
$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
@ -1005,7 +1005,7 @@ class FinanceTest extends TestCase
'expense' => 2,
'subfleet' => 2,
'fare' => 3,
'ground_handling' => 1,
'ground_handling' => 2,
'pilot_pay' => 2, // debit on the airline, credit to the pilot
];