diff --git a/app/Events/Fares.php b/app/Events/Fares.php new file mode 100644 index 00000000..25546241 --- /dev/null +++ b/app/Events/Fares.php @@ -0,0 +1,39 @@ + '', # displayed as the memo + * 'type' => [INTEGER], # from FareType enum class + * 'price' => [DECIMAL], + * 'cost' => [DECIMAL], # optional + * 'notes' => '', # used as Transaction Group + * ]); + * + * The event caller will check the 'type' to make sure that it + * will filter out fares that only apply to the current process + * + * The event will have a copy of the PIREP model, if it's applicable + */ +class Fares extends Event +{ + public $pirep; + + /** + * @param Pirep|null $pirep + */ + public function __construct(Pirep $pirep = null) + { + $this->pirep = $pirep; + } +} diff --git a/app/Listeners/ExpenseListener.php b/app/Listeners/ExpenseListener.php index 3de6c596..c1deba9b 100644 --- a/app/Listeners/ExpenseListener.php +++ b/app/Listeners/ExpenseListener.php @@ -4,6 +4,8 @@ namespace App\Listeners; use App\Contracts\Listener; use App\Events\Expenses; +use App\Models\Enums\ExpenseType; +use App\Models\Expense; class ExpenseListener extends Listener { @@ -23,7 +25,7 @@ class ExpenseListener extends Listener // The transaction group is how it will show as a line item /*$expenses[] = new Expense([ 'type' => ExpenseType::FLIGHT, - 'amount' => 15000, # $150 + 'amount' => 150, # $150 'transaction_group' => '', 'charge_to_user' => true|false ]);*/ diff --git a/app/Listeners/FareListener.php b/app/Listeners/FareListener.php new file mode 100644 index 00000000..d77a2c56 --- /dev/null +++ b/app/Listeners/FareListener.php @@ -0,0 +1,39 @@ +pirep and any associated data + // Cost may be skipped at all if not needed + // Notes will be used as transaction group and it is how it will show as a line item + /* + $fares[] = new Fare([ + 'name' => 'Duty Free Sales', + 'type' => FareType::PASSENGER, + 'price' => 985, + 'cost' => 126, + 'notes' => 'InFlight Sales', + ]); + */ + + return $fares; + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index c3da1f89..023a15db 100755 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,11 +3,13 @@ namespace App\Providers; use App\Events\Expenses; +use App\Events\Fares; use App\Events\PirepFiled; use App\Events\UserStatsChanged; use App\Listeners\AwardHandler; use App\Listeners\BidEventHandler; use App\Listeners\ExpenseListener; +use App\Listeners\FareListener; use App\Listeners\FinanceEventHandler; use App\Listeners\PirepEventsHandler; use App\Listeners\UserStateListener; @@ -25,6 +27,10 @@ class EventServiceProvider extends ServiceProvider ExpenseListener::class, ], + Fares::class => [ + FareListener::class, + ], + PirepFiled::class => [ UserStateListener::class, ], diff --git a/app/Services/Finance/PirepFinanceService.php b/app/Services/Finance/PirepFinanceService.php index f5150950..8203294c 100644 --- a/app/Services/Finance/PirepFinanceService.php +++ b/app/Services/Finance/PirepFinanceService.php @@ -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 *