From d2272e32a627523a89812014720f655a1cd1a01a Mon Sep 17 00:00:00 2001 From: "B.Fatih KOZ" <74361521+FatihKoz@users.noreply.github.com> Date: Thu, 6 May 2021 15:16:33 +0300 Subject: [PATCH] 100LL and MOGAS Fuel Cost (#1172) * Add defaults for 100LL and MOGAS prices Add missing defaults for 100LL and MOGAS Fuel prices. * Calculate Fuel Cost according to Fuel Type Calculate pirep fuel costs according to aircraft (subfleet) fuel type, use JetA as fail safe default. * Fix for Failing AwardTest At least attempting to fix * Fix fuel types logic * Style * Invert the logic Co-authored-by: Nabeel Shahzad --- app/Database/seeds/settings.yml | 14 ++++++++ app/Models/Subfleet.php | 1 + app/Services/Finance/PirepFinanceService.php | 37 +++++++++++++++----- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app/Database/seeds/settings.yml b/app/Database/seeds/settings.yml index ed04d7ca..e353a517 100644 --- a/app/Database/seeds/settings.yml +++ b/app/Database/seeds/settings.yml @@ -144,6 +144,20 @@ options: type: text description: If an airport's Jet A Fuel Cost isn't added, set this value by default +- key: airports.default_100ll_fuel_cost + name: 'Default 100LL Fuel Cost' + group: airports + value: 0.9 + options: + type: text + description: If an airport's 100LL Fuel Cost isn't added, set this value by default +- key: airports.default_mogas_fuel_cost + name: 'Default MOGAS Fuel Cost' + group: airports + value: 0.8 + options: + type: text + description: If an airport's MOGAS Fuel Cost isn't added, set this value by default - key: bids.disable_flight_on_bid name: 'Disable flight on bid' group: bids diff --git a/app/Models/Subfleet.php b/app/Models/Subfleet.php index 0f10ad7f..fad32c9a 100644 --- a/app/Models/Subfleet.php +++ b/app/Models/Subfleet.php @@ -20,6 +20,7 @@ use App\Models\Traits\FilesTrait; * @property float cost_delay_minute * @property Airline airline * @property Airport hub + * @property int fuel_type */ class Subfleet extends Model { diff --git a/app/Services/Finance/PirepFinanceService.php b/app/Services/Finance/PirepFinanceService.php index c0435235..41ea80ed 100644 --- a/app/Services/Finance/PirepFinanceService.php +++ b/app/Services/Finance/PirepFinanceService.php @@ -8,7 +8,10 @@ use App\Models\Aircraft; use App\Models\Airport; use App\Models\Enums\ExpenseType; use App\Models\Enums\FareType; +use App\Models\Enums\FuelType; use App\Models\Enums\PirepSource; +use App\Models\Enums\PirepState; +use App\Models\Enums\PirepStatus; use App\Models\Expense; use App\Models\Pirep; use App\Models\Subfleet; @@ -151,18 +154,37 @@ class PirepFinanceService extends Service */ public function payFuelCosts(Pirep $pirep): void { + // Get Airport Fuel Prices or Use Defaults $ap = $pirep->dpt_airport; - // Get Airport Fuel Cost or revert back to settings - if (empty($ap->fuel_jeta_cost)) { - $fuel_cost = setting('airports.default_jet_a_fuel_cost'); + + // Get Aircraft Fuel Type from Subfleet + // And set $fuel_cost according to type (Failsafe is Jet A) + $sf = $pirep->aircraft->subfleet; + if ($sf) { + $fuel_type = $sf->fuel_type; } else { - $fuel_cost = $ap->fuel_jeta_cost; + $fuel_type = FuelType::JET_A; + } + + if ($fuel_type === FuelType::LOW_LEAD) { + $fuel_cost = !empty($ap->fuel_100ll_cost) ? $ap->fuel_100ll_cost : setting('airports.default_100ll_fuel_cost'); + } elseif ($fuel_type === FuelType::MOGAS) { + $fuel_cost = !empty($ap->fuel_mogas_cost) ? $ap->fuel_mogas_cost : setting('airports.default_mogas_fuel_cost'); + } else { // Default to JetA + $fuel_cost = !empty($ap->fuel_jeta_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 - $prev_flight = Pirep::where('aircraft_id', $ac->id)->where('state', 2)->where('status', 'ONB')->orderby('submitted_at', 'desc')->skip(1)->first(); + $prev_flight = Pirep::where([ + 'aircraft_id' => $pirep->aircraft->id, + 'state' => PirepState::ACCEPTED, + 'status' => PirepStatus::ARRIVED, + ]) + ->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 @@ -181,8 +203,7 @@ class PirepFinanceService extends Service } $debit = Money::createFromAmount($fuel_amount * $fuel_cost); - Log::info('Finance: Fuel cost, (fuel='.$fuel_amount.', cost='.$fuel_cost.') D=' - .$debit->getAmount()); + Log::info('Finance: Fuel cost, (fuel='.$fuel_amount.', cost='.$fuel_cost.') D='.$debit->getAmount()); $this->financeSvc->debitFromJournal( $pirep->airline->journal,