specify fares, js to dynamically change fare form; get applicable fares for the flight/pirep #125
This commit is contained in:
@@ -6,9 +6,50 @@ use App\Models\Fare;
|
||||
use App\Models\Flight;
|
||||
use App\Models\Subfleet;
|
||||
use App\Support\Math;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class FareService
|
||||
* @package App\Services
|
||||
*/
|
||||
class FareService extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the fares for a particular flight, with an optional subfleet
|
||||
* This will go through if there are any fares assigned to the flight,
|
||||
* and then check the fares assigned on the subfleet, and give the
|
||||
* final "authoritative" list of the fares for a flight.
|
||||
*
|
||||
* If a subfleet is passed in,
|
||||
* @param Flight|null $flight
|
||||
* @param Subfleet|null $subfleet
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAllFares($flight, $subfleet)
|
||||
{
|
||||
if(!$flight) {
|
||||
$flight_fares = collect();
|
||||
} else {
|
||||
$flight_fares = $this->getForFlight($flight);
|
||||
}
|
||||
|
||||
$subfleet_fares = $this->getForSubfleet($subfleet);
|
||||
|
||||
# Go through all of the fares assigned by the subfleet
|
||||
# See if any of the same fares are assigned to the flight
|
||||
$fares = $subfleet_fares->map(function($fare, $idx) use ($flight_fares)
|
||||
{
|
||||
$flight_fare = $flight_fares->whereStrict('id', $fare->id)->first();
|
||||
if(!$flight_fare) {
|
||||
return $fare;
|
||||
}
|
||||
|
||||
return $flight_fare;
|
||||
});
|
||||
|
||||
return $fares;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fares
|
||||
* @param $fare
|
||||
@@ -70,7 +111,7 @@ class FareService extends BaseService
|
||||
* table to see if the price/cost/capacity has been overridden
|
||||
* and return the correct amounts.
|
||||
* @param Flight $flight
|
||||
* @return Fare[]
|
||||
* @return Collection
|
||||
*/
|
||||
public function getForFlight(Flight $flight)
|
||||
{
|
||||
@@ -120,7 +161,7 @@ class FareService extends BaseService
|
||||
* table to see if the price/cost/capacity has been overridden
|
||||
* and return the correct amounts.
|
||||
* @param Subfleet $subfleet
|
||||
* @return Fare[]
|
||||
* @return Collection
|
||||
*/
|
||||
public function getForSubfleet(Subfleet $subfleet)
|
||||
{
|
||||
|
||||
25
app/Services/FinanceService.php
Normal file
25
app/Services/FinanceService.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class FinanceService extends BaseService
|
||||
{
|
||||
private $fareSvc,
|
||||
$flightSvc;
|
||||
|
||||
/**
|
||||
* FinanceService constructor.
|
||||
* @param FareService $fareSvc
|
||||
* @param FlightService $flightSvc
|
||||
*/
|
||||
public function __construct(
|
||||
FareService $fareSvc,
|
||||
FlightService $flightSvc
|
||||
) {
|
||||
$this->fareSvc = $fareSvc;
|
||||
$this->flightSvc = $flightSvc;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App\Exceptions\BidExists;
|
||||
use App\Models\Flight;
|
||||
use App\Models\Subfleet;
|
||||
use App\Models\User;
|
||||
use App\Models\UserBid;
|
||||
use App\Repositories\FlightRepository;
|
||||
@@ -16,13 +17,19 @@ use Log;
|
||||
*/
|
||||
class FlightService extends BaseService
|
||||
{
|
||||
protected $flightRepo, $navDataRepo, $userSvc;
|
||||
private $fareSvc,
|
||||
$flightRepo,
|
||||
$navDataRepo,
|
||||
$userSvc;
|
||||
|
||||
public function __construct(
|
||||
FareService $fareSvc,
|
||||
FlightRepository $flightRepo,
|
||||
NavdataRepository $navdataRepo,
|
||||
UserService $userSvc
|
||||
) {
|
||||
)
|
||||
{
|
||||
$this->fareSvc = $fareSvc;
|
||||
$this->flightRepo = $flightRepo;
|
||||
$this->navDataRepo = $navdataRepo;
|
||||
$this->userSvc = $userSvc;
|
||||
@@ -41,7 +48,7 @@ class FlightService extends BaseService
|
||||
}
|
||||
|
||||
return $this->flightRepo
|
||||
->whereOrder($where, 'flight_number', 'asc');
|
||||
->whereOrder($where, 'flight_number', 'asc');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,8 +77,8 @@ class FlightService extends BaseService
|
||||
/**
|
||||
* Only allow aircraft that are at the current departure airport
|
||||
*/
|
||||
if(setting('pireps.only_aircraft_at_dep_airport', false)) {
|
||||
foreach($subfleets as $subfleet) {
|
||||
if (setting('pireps.only_aircraft_at_dep_airport', false)) {
|
||||
foreach ($subfleets as $subfleet) {
|
||||
$subfleet->aircraft = $subfleet->aircraft->filter(
|
||||
function ($aircraft, $i) use ($flight) {
|
||||
if ($aircraft->airport_id === $flight->dpt_airport_id) {
|
||||
@@ -106,11 +113,11 @@ class FlightService extends BaseService
|
||||
*/
|
||||
public function getRoute(Flight $flight)
|
||||
{
|
||||
if(!$flight->route) {
|
||||
if (!$flight->route) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$route_points = array_map(function($point) {
|
||||
$route_points = array_map(function ($point) {
|
||||
return strtoupper($point);
|
||||
}, explode(' ', $flight->route));
|
||||
|
||||
@@ -118,7 +125,7 @@ class FlightService extends BaseService
|
||||
|
||||
// Put it back into the original order the route is in
|
||||
$return_points = [];
|
||||
foreach($route_points as $rp) {
|
||||
foreach ($route_points as $rp) {
|
||||
$return_points[] = $route->where('id', $rp)->first();
|
||||
}
|
||||
|
||||
@@ -135,15 +142,15 @@ class FlightService extends BaseService
|
||||
public function addBid(Flight $flight, User $user)
|
||||
{
|
||||
# If it's already been bid on, then it can't be bid on again
|
||||
if($flight->has_bid && setting('bids.disable_flight_on_bid')) {
|
||||
if ($flight->has_bid && setting('bids.disable_flight_on_bid')) {
|
||||
Log::info($flight->id . ' already has a bid, skipping');
|
||||
throw new BidExists();
|
||||
}
|
||||
|
||||
# See if we're allowed to have multiple bids or not
|
||||
if(!setting('bids.allow_multiple_bids')) {
|
||||
if (!setting('bids.allow_multiple_bids')) {
|
||||
$user_bids = UserBid::where(['user_id' => $user->id])->first();
|
||||
if($user_bids) {
|
||||
if ($user_bids) {
|
||||
Log::info('User "' . $user->id . '" already has bids, skipping');
|
||||
throw new BidExists();
|
||||
}
|
||||
@@ -156,7 +163,7 @@ class FlightService extends BaseService
|
||||
];
|
||||
|
||||
$user_bid = UserBid::where($bid_data)->first();
|
||||
if($user_bid) {
|
||||
if ($user_bid) {
|
||||
return $user_bid;
|
||||
}
|
||||
|
||||
@@ -179,12 +186,12 @@ class FlightService extends BaseService
|
||||
'flight_id' => $flight->id, 'user_id' => $user->id
|
||||
])->first();
|
||||
|
||||
if($user_bid) {
|
||||
if ($user_bid) {
|
||||
$user_bid->forceDelete();
|
||||
}
|
||||
|
||||
# Only flip the flag if there are no bids left for this flight
|
||||
if(!UserBid::where('flight_id', $flight->id)->exists()) {
|
||||
if (!UserBid::where('flight_id', $flight->id)->exists()) {
|
||||
$flight->has_bid = false;
|
||||
$flight->save();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use App\Models\Enums\PirepSource;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Navdata;
|
||||
use App\Models\Pirep;
|
||||
use App\Models\PirepFare;
|
||||
use App\Models\PirepFieldValues;
|
||||
use App\Models\User;
|
||||
use App\Repositories\AcarsRepository;
|
||||
@@ -210,6 +211,29 @@ class PIREPService extends BaseService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the list of fares
|
||||
* @param $pirep_id
|
||||
* @param array $fares ['field_id', 'count']
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function saveFares($pirep_id, array $fares)
|
||||
{
|
||||
if(!$fares) { return; }
|
||||
|
||||
# Remove all the previous fares
|
||||
PirepFare::where('pirep_id', $pirep_id)->delete();
|
||||
|
||||
# Add them in
|
||||
foreach($fares as $fare) {
|
||||
$fare['pirep_id'] = $pirep_id;
|
||||
# other fields: ['fare_id', 'count']
|
||||
|
||||
$field = new PirepFare($fare);
|
||||
$field->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Pirep $pirep
|
||||
* @param int $new_state
|
||||
|
||||
Reference in New Issue
Block a user