From 4fe323a7636b3647f4272a1dafe0853e4af1a979 Mon Sep 17 00:00:00 2001 From: "B.Fatih KOZ" <74361521+FatihKoz@users.noreply.github.com> Date: Wed, 24 Feb 2021 20:22:52 +0300 Subject: [PATCH] Advanced Fuel Calculations (#1044) * Advanced Fuel Calculations PR aims to have realistic fuel debit calculations as in the real ops. When enabled, remaining fuel amounts from previous flight will be reduced from block fuel and airline will only pay for uplifted fuel amount. If onboard fuel is enough for the flight or exceeds the required amount no fuel debit will be issued. If this is the first flight of the aircraft or the tanks are dry, full block fuel will be treated as uplifted. Disabled / uses current default (fuel_used will be debited) Aircraft table also will hold the fuel_onboard value (not needed for calculations, just saving for displaying purposes) * Style Fix * Another Style Fix Love StyleCi * Fix Settings Moved new setting under other pirep settings * Fix Setting Check * Fix EOF settings.yml * Fix Settings --- ..._02_23_150601_aircraft_add_fuelonboard.php | 21 ++++++++++++++++ app/Database/seeds/settings.yml | 8 ++++++ app/Services/Finance/PirepFinanceService.php | 25 ++++++++++++++++--- app/Services/PirepService.php | 1 + 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 app/Database/migrations/2021_02_23_150601_aircraft_add_fuelonboard.php diff --git a/app/Database/migrations/2021_02_23_150601_aircraft_add_fuelonboard.php b/app/Database/migrations/2021_02_23_150601_aircraft_add_fuelonboard.php new file mode 100644 index 00000000..ca18762f --- /dev/null +++ b/app/Database/migrations/2021_02_23_150601_aircraft_add_fuelonboard.php @@ -0,0 +1,21 @@ +unsignedDecimal('fuel_onboard') + ->nullable() + ->default(0.0) + ->after('zfw'); + }); + } +} diff --git a/app/Database/seeds/settings.yml b/app/Database/seeds/settings.yml index f7b6489a..080e88c3 100644 --- a/app/Database/seeds/settings.yml +++ b/app/Database/seeds/settings.yml @@ -263,6 +263,13 @@ options: '' type: boolean description: 'When a PIREP is accepted, remove the bid, if it exists' +- key: pireps.advanced_fuel + name: 'Advanced Fuel Calculations' + group: pireps + value: false + options: '' + type: boolean + description: 'Enables remaining fuel amounts to be considered for fuel expenses' - key: pilots.id_length name: 'Pilot ID Length' group: pilots @@ -340,3 +347,4 @@ options: '' type: 'text' description: 'Discord public channel ID for broadcasat notifications' + diff --git a/app/Services/Finance/PirepFinanceService.php b/app/Services/Finance/PirepFinanceService.php index 6a5c4518..623aeead 100644 --- a/app/Services/Finance/PirepFinanceService.php +++ b/app/Services/Finance/PirepFinanceService.php @@ -152,10 +152,29 @@ class PirepFinanceService extends Service public function payFuelCosts(Pirep $pirep): void { $ap = $pirep->dpt_airport; - $fuel_used = $pirep->fuel_used; + if (setting('pirep.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 + $prev_flight = Pirep::where('aircraft_id', $ac->id)->where('state', 2)->where('status', 'ONB')->orderby('submitted_at', 'desc')->skip(1)->first(); + if ($prev_flight) { + // If there is a pirep use its values to calculate the remaining fuel + // and calculate the uplifted fuel amount for this pirep + $fuel_amount = $pirep->block_fuel - ($prev_flight->block_fuel - $prev_flight->fuel_used); + // Aircraft has more than enough fuel in its tanks, no uplift necessary + if ($fuel_amount < 0) { + $fuel_amount = 0; + } + } else { + // No pirep found for aircraft, debit full block fuel + $fuel_amount = $pirep->block_fuel; + } + } else { + // Setting is false, switch back to basic calculation + $fuel_amount = $pirep->fuel_used; + } - $debit = Money::createFromAmount($fuel_used * $ap->fuel_jeta_cost); - Log::info('Finance: Fuel cost, (fuel='.$fuel_used.', cost='.$ap->fuel_jeta_cost.') D=' + $debit = Money::createFromAmount($fuel_amount * $ap->fuel_jeta_cost); + Log::info('Finance: Fuel cost, (fuel='.$fuel_amount.', cost='.$ap->fuel_jeta_cost.') D=' .$debit->getAmount()); $this->financeSvc->debitFromJournal( diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 3f7aac97..8ec68c21 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -539,6 +539,7 @@ class PirepService extends Service $pirep->aircraft->flight_time = $pirep->aircraft->flight_time + $pirep->flight_time; $pirep->aircraft->airport_id = $pirep->arr_airport_id; $pirep->aircraft->landing_time = $pirep->updated_at; + $pirep->aircraft->fuel_onboard = $pirep->block_fuel - $pirep->fuel_used; $pirep->aircraft->save(); $pirep->refresh();