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

39
app/Events/Fares.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace App\Events;
use App\Contracts\Event;
use App\Models\Pirep;
/**
* This event is dispatched when the fares for a flight report
* are collected. Your listeners should return a list of Fare
* models. Don't call save on the model!
*
* Example return:
*
* new Fare([
* 'name' => '', # 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;
}
}

View File

@@ -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
]);*/

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Listeners;
use App\Contracts\Listener;
use App\Events\Fares;
use App\Models\Enums\FareType;
use App\Models\Fare;
class FareListener extends Listener
{
/**
* Return a list of additional fares/income items
*
* @param Fares $event
*
* @return mixed
*/
public function handle(Fares $event)
{
$fares = [];
// This is an example of a fare to return
// You have the pirep on $event->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;
}
}

View File

@@ -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,
],

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
*