2017-12-13 06:58:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2018-02-23 00:44:15 +08:00
|
|
|
use App\Exceptions\BidExists;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Service;
|
|
|
|
use App\Models\Bid;
|
2017-12-13 06:58:27 +08:00
|
|
|
use App\Models\Flight;
|
2018-03-21 02:06:06 +08:00
|
|
|
use App\Models\FlightFieldValue;
|
2017-12-13 06:58:27 +08:00
|
|
|
use App\Models\User;
|
2018-02-21 02:06:52 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
|
|
|
use App\Repositories\NavdataRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Log;
|
2017-12-13 06:58:27 +08:00
|
|
|
|
2018-02-21 02:06:52 +08:00
|
|
|
/**
|
|
|
|
* Class FlightService
|
|
|
|
* @package App\Services
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class FlightService extends Service
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
2018-02-25 05:38:25 +08:00
|
|
|
private $fareSvc,
|
2018-03-20 09:50:40 +08:00
|
|
|
$flightRepo,
|
|
|
|
$navDataRepo,
|
|
|
|
$userSvc;
|
2018-02-10 04:36:36 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* FlightService constructor.
|
|
|
|
* @param FareService $fareSvc
|
|
|
|
* @param FlightRepository $flightRepo
|
|
|
|
* @param NavdataRepository $navdataRepo
|
|
|
|
* @param UserService $userSvc
|
|
|
|
*/
|
2018-02-21 02:06:52 +08:00
|
|
|
public function __construct(
|
2018-02-25 05:38:25 +08:00
|
|
|
FareService $fareSvc,
|
2018-02-21 02:06:52 +08:00
|
|
|
FlightRepository $flightRepo,
|
|
|
|
NavdataRepository $navdataRepo,
|
|
|
|
UserService $userSvc
|
2018-03-20 09:50:40 +08:00
|
|
|
) {
|
2018-02-25 05:38:25 +08:00
|
|
|
$this->fareSvc = $fareSvc;
|
2018-02-10 05:36:13 +08:00
|
|
|
$this->flightRepo = $flightRepo;
|
2018-02-21 02:06:52 +08:00
|
|
|
$this->navDataRepo = $navdataRepo;
|
2018-02-10 04:36:36 +08:00
|
|
|
$this->userSvc = $userSvc;
|
|
|
|
}
|
|
|
|
|
2018-02-10 05:36:13 +08:00
|
|
|
/**
|
|
|
|
* Filter out any flights according to different settings
|
|
|
|
* @param $user
|
|
|
|
* @return FlightRepository
|
|
|
|
*/
|
|
|
|
public function filterFlights($user)
|
|
|
|
{
|
|
|
|
$where = [];
|
|
|
|
if (setting('pilots.only_flights_from_current', false)) {
|
|
|
|
$where['dpt_airport_id'] = $user->curr_airport_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->flightRepo
|
2018-02-25 05:38:25 +08:00
|
|
|
->whereOrder($where, 'flight_number', 'asc');
|
2018-02-10 05:36:13 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 04:36:36 +08:00
|
|
|
/**
|
|
|
|
* Filter out subfleets to only include aircraft that a user has access to
|
|
|
|
* @param $user
|
|
|
|
* @param $flight
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function filterSubfleets($user, $flight)
|
|
|
|
{
|
2018-02-10 05:07:34 +08:00
|
|
|
$subfleets = $flight->subfleets;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only allow aircraft that the user has access to in their rank
|
|
|
|
*/
|
2018-02-10 04:36:36 +08:00
|
|
|
if (setting('pireps.restrict_aircraft_to_rank', false)) {
|
|
|
|
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
|
2018-02-10 05:07:34 +08:00
|
|
|
$subfleets = $subfleets->filter(function ($subfleet, $i) use ($allowed_subfleets) {
|
|
|
|
if ($allowed_subfleets->contains($subfleet->id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only allow aircraft that are at the current departure airport
|
|
|
|
*/
|
2018-03-31 00:08:53 +08:00
|
|
|
if (setting('pireps.only_aircraft_at_dpt_airport', false)) {
|
2018-02-25 05:38:25 +08:00
|
|
|
foreach ($subfleets as $subfleet) {
|
2018-02-10 05:07:34 +08:00
|
|
|
$subfleet->aircraft = $subfleet->aircraft->filter(
|
|
|
|
function ($aircraft, $i) use ($flight) {
|
|
|
|
if ($aircraft->airport_id === $flight->dpt_airport_id) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-10 04:36:36 +08:00
|
|
|
}
|
2018-02-10 05:07:34 +08:00
|
|
|
);
|
|
|
|
}
|
2018-02-10 04:36:36 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 05:07:34 +08:00
|
|
|
$flight->subfleets = $subfleets;
|
|
|
|
|
2018-02-10 04:36:36 +08:00
|
|
|
return $flight;
|
|
|
|
}
|
|
|
|
|
2018-04-26 00:53:32 +08:00
|
|
|
/**
|
|
|
|
* Check if this flight has a duplicate already
|
|
|
|
* @param Flight $flight
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isFlightDuplicate(Flight $flight)
|
|
|
|
{
|
|
|
|
$where = [
|
|
|
|
['id', '<>', $flight->id],
|
|
|
|
'airline_id' => $flight->airline_id,
|
|
|
|
'flight_number' => $flight->flight_number,
|
|
|
|
];
|
|
|
|
|
|
|
|
$found_flights = $this->flightRepo->findWhere($where);
|
|
|
|
if($found_flights->count() === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find within all the flights with the same flight number
|
|
|
|
// Return any flights that have the same route code and leg
|
|
|
|
// If this list is > 0, then this has a duplicate
|
|
|
|
$found_flights = $found_flights->filter(function($value, $key) use ($flight) {
|
|
|
|
if($flight->route_code === $value->route_code
|
|
|
|
&& $flight->route_leg === $value->route_leg) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if($found_flights->count() === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
/**
|
|
|
|
* Delete a flight, and all the user bids, etc associated with it
|
|
|
|
* @param Flight $flight
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2018-03-21 02:06:06 +08:00
|
|
|
public function deleteFlight(Flight $flight): void
|
2018-01-01 23:32:04 +08:00
|
|
|
{
|
|
|
|
$where = ['flight_id' => $flight->id];
|
2018-02-28 03:25:32 +08:00
|
|
|
Bid::where($where)->delete();
|
2018-01-01 23:32:04 +08:00
|
|
|
$flight->delete();
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:06:06 +08:00
|
|
|
/**
|
|
|
|
* Update any custom PIREP fields
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param Flight $flight
|
|
|
|
* @param array $field_values
|
2018-03-21 02:06:06 +08:00
|
|
|
*/
|
2018-03-21 08:17:11 +08:00
|
|
|
public function updateCustomFields(Flight $flight, array $field_values): void
|
2018-03-21 02:06:06 +08:00
|
|
|
{
|
|
|
|
foreach ($field_values as $fv) {
|
|
|
|
FlightFieldValue::updateOrCreate(
|
|
|
|
[
|
2018-03-21 08:17:11 +08:00
|
|
|
'flight_id' => $flight->id,
|
2018-03-21 02:06:06 +08:00
|
|
|
'name' => $fv['name'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'value' => $fv['value']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:06:52 +08:00
|
|
|
/**
|
|
|
|
* Return all of the navaid points as a collection
|
|
|
|
* @param Flight $flight
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public function getRoute(Flight $flight)
|
|
|
|
{
|
2018-02-25 05:38:25 +08:00
|
|
|
if (!$flight->route) {
|
2018-02-21 02:06:52 +08:00
|
|
|
return collect();
|
|
|
|
}
|
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
$route_points = array_map(function ($point) {
|
2018-02-21 02:06:52 +08:00
|
|
|
return strtoupper($point);
|
|
|
|
}, explode(' ', $flight->route));
|
|
|
|
|
|
|
|
$route = $this->navDataRepo->findWhereIn('id', $route_points);
|
|
|
|
|
|
|
|
// Put it back into the original order the route is in
|
|
|
|
$return_points = [];
|
2018-02-25 05:38:25 +08:00
|
|
|
foreach ($route_points as $rp) {
|
2018-02-21 02:06:52 +08:00
|
|
|
$return_points[] = $route->where('id', $rp)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return collect($return_points);
|
|
|
|
}
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
/**
|
|
|
|
* Allow a user to bid on a flight. Check settings and all that good stuff
|
|
|
|
* @param Flight $flight
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param User $user
|
2018-02-28 03:25:32 +08:00
|
|
|
* @return Bid|null
|
2018-02-23 00:44:15 +08:00
|
|
|
* @throws \App\Exceptions\BidExists
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
2018-01-08 01:16:44 +08:00
|
|
|
public function addBid(Flight $flight, User $user)
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
|
|
|
# If it's already been bid on, then it can't be bid on again
|
2018-02-25 05:38:25 +08:00
|
|
|
if ($flight->has_bid && setting('bids.disable_flight_on_bid')) {
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info($flight->id.' already has a bid, skipping');
|
2018-02-23 00:44:15 +08:00
|
|
|
throw new BidExists();
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# See if we're allowed to have multiple bids or not
|
2018-02-25 05:38:25 +08:00
|
|
|
if (!setting('bids.allow_multiple_bids')) {
|
2018-02-28 03:25:32 +08:00
|
|
|
$user_bids = Bid::where(['user_id' => $user->id])->first();
|
2018-02-25 05:38:25 +08:00
|
|
|
if ($user_bids) {
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info('User "'.$user->id.'" already has bids, skipping');
|
2018-02-23 05:15:00 +08:00
|
|
|
throw new BidExists();
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# See if this user has this flight bid on already
|
|
|
|
$bid_data = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user_id' => $user->id,
|
2017-12-13 06:58:27 +08:00
|
|
|
'flight_id' => $flight->id
|
|
|
|
];
|
|
|
|
|
2018-02-28 03:25:32 +08:00
|
|
|
$user_bid = Bid::where($bid_data)->first();
|
2018-02-25 05:38:25 +08:00
|
|
|
if ($user_bid) {
|
2017-12-13 06:58:27 +08:00
|
|
|
return $user_bid;
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:25:32 +08:00
|
|
|
$user_bid = Bid::create($bid_data);
|
2017-12-13 06:58:27 +08:00
|
|
|
|
|
|
|
$flight->has_bid = true;
|
|
|
|
$flight->save();
|
|
|
|
|
|
|
|
return $user_bid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a bid from a given flight
|
|
|
|
* @param Flight $flight
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param User $user
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
|
|
|
public function removeBid(Flight $flight, User $user)
|
|
|
|
{
|
2018-02-28 03:25:32 +08:00
|
|
|
$user_bid = Bid::where([
|
2017-12-13 06:58:27 +08:00
|
|
|
'flight_id' => $flight->id, 'user_id' => $user->id
|
|
|
|
])->first();
|
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
if ($user_bid) {
|
2017-12-13 06:58:27 +08:00
|
|
|
$user_bid->forceDelete();
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:26:27 +08:00
|
|
|
# Only flip the flag if there are no bids left for this flight
|
2018-02-28 03:25:32 +08:00
|
|
|
if (!Bid::where('flight_id', $flight->id)->exists()) {
|
2017-12-13 07:26:27 +08:00
|
|
|
$flight->has_bid = false;
|
|
|
|
$flight->save();
|
|
|
|
}
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
}
|