Add Custom Fares (#1323)

* Update EventServiceProvider.php

Add FareListener

* Create Fares.php

* Create FareListener.php

* Update ExpenseListener.php

* Update PirepFinanceService.php

Add listener for the fares, process them like the custom expenses and apply if there are any returned
This commit is contained in:
B.Fatih KOZ
2021-09-30 18:10:05 +03:00
committed by GitHub
parent 7a29630f57
commit 9a28cf22ff
5 changed files with 137 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Services\Finance;
use App\Contracts\Service;
use App\Events\Expenses as ExpensesEvent;
use App\Events\Fares as FaresEvent;
use App\Models\Aircraft;
use App\Models\Airport;
use App\Models\Enums\ExpenseType;
@@ -13,6 +14,7 @@ use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
use App\Models\Expense;
use App\Models\Fare;
use App\Models\Pirep;
use App\Models\Subfleet;
use App\Repositories\ExpenseRepository;
@@ -81,6 +83,7 @@ class PirepFinanceService extends Service
// Now start and pay from scratch
$this->payFuelCosts($pirep);
$this->payFaresForPirep($pirep);
$this->payFaresEventsForPirep($pirep);
$this->payExpensesForSubfleet($pirep);
$this->payExpensesForPirep($pirep);
$this->payAirportExpensesForPirep($pirep);
@@ -145,6 +148,53 @@ class PirepFinanceService extends Service
}
}
/**
* Collect all of the fares from listeners and apply those to the journal
*
* @param Pirep $pirep
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function payFaresEventsForPirep(Pirep $pirep): void
{
// Throw an event and collect any fares returned from it
$gathered_fares = event(new FaresEvent($pirep));
if (!\is_array($gathered_fares)) {
return;
}
foreach ($gathered_fares as $event_fare) {
if (!\is_array($event_fare)) {
continue;
}
foreach ($event_fare as $fare) {
// Make sure it's of type Fare Model
if (!($fare instanceof Fare)) {
Log::info('Finance: Event Fare is not an instance of Fare Model, aborting process!');
continue;
}
$credit = Money::createFromAmount($fare->price);
$debit = Money::createFromAmount($fare->cost);
Log::info('Finance: Income From Listener N='.$fare->name.', C='.$credit.', D='.$debit);
$this->journalRepo->post(
$pirep->airline->journal,
$credit,
$debit,
$pirep,
$fare->name,
null,
$fare->notes,
'additional-sales'
);
}
}
}
/**
* Calculate the fuel used by the PIREP and add those costs in
*