phpvms/app/Http/Controllers/Frontend/SimBriefController.php
B.Fatih KOZ e3b4a0ed2e
SimBrief integration enhancements (#1045)
* SimBrief Integration Update

* Added SimBrief Type field to subfleets, can be used to assign simbrief airframes to subfleets and fix non existing or wrong types. If not used simbrief form will use aircraft's icao type code

* Added Passenger and Baggage weights to settings

* Added setting for using PhpVms Pilot/User  Ident as simbrief atc callsign

* SimBrief form code cleaned up a bit and improved. Now form supports both cargo and passenger fares to be used at the same time.

Generated passenger baggage weight will be reduced from aircraft's cargo capacity and remaining amount will be used for random cargo generation.

Also multiple cargo fares or any mix is possible now (like only cargo, only passenger, multiple cargo and passenger fares)

* StyleFix (SimBrief Controller)

* Fix Callsign Setting Check

* Code Cleanup

Reduced loops and removed if's in loops, getting fares from aircraft instead of flight/subfleets.

No need to go through getReconciledFaresForFlight anymore. Aircraft provides all fare info we need.

Removed unnecessary html elements, added some comments.

* Update Simbrief Controller

Fixed setting checks.

Removed non used $subfleet and from main form and $aircraft from aircraft selection form blade.

Added/fixed comments.

* StyleFix for Controller
2021-02-24 09:32:29 -05:00

275 lines
8.4 KiB
PHP

<?php
namespace App\Http\Controllers\Frontend;
use App\Exceptions\AssetNotFound;
use App\Models\Aircraft;
use App\Models\Enums\FlightType;
use App\Models\SimBrief;
use App\Repositories\FlightRepository;
use App\Services\FareService;
use App\Services\SimBriefService;
use App\Services\UserService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class SimBriefController
{
private $fareSvc;
private $flightRepo;
private $simBriefSvc;
private $userSvc;
public function __construct(
FareService $fareSvc,
FlightRepository $flightRepo,
SimBriefService $simBriefSvc,
UserService $userSvc
) {
$this->fareSvc = $fareSvc;
$this->flightRepo = $flightRepo;
$this->simBriefSvc = $simBriefSvc;
$this->userSvc = $userSvc;
}
/**
* Show the main OFP form
*
* @param Request $request
*
* @throws \Exception
*
* @return mixed
*/
public function generate(Request $request)
{
/** @var \App\Models\User $user */
$user = Auth::user();
$flight_id = $request->input('flight_id');
$aircraft_id = $request->input('aircraft_id');
$flight = $this->flightRepo->with(['subfleets'])->find($flight_id);
// $flight = $this->fareSvc->getReconciledFaresForFlight($flight);
if (!$flight) {
flash()->error('Unknown flight');
return redirect(route('frontend.flights.index'));
}
$apiKey = setting('simbrief.api_key');
if (empty($apiKey)) {
flash()->error('Invalid SimBrief API key!');
return redirect(route('frontend.flights.index'));
}
// If no subfleets defined for flight get them from user
if ($flight->subfleets->count() > 0) {
$subfleets = $flight->subfleets;
} else {
$subfleets = $this->userSvc->getAllowableSubfleets($user);
}
// No aircraft selected, show selection form
if (!$aircraft_id) {
return view('flights.simbrief_aircraft', [
'flight' => $flight,
'subfleets' => $subfleets,
]);
}
// Check if a Simbrief profile already exists
$simbrief = SimBrief::select('id')->where([
'flight_id' => $flight_id,
'user_id' => $user->id,
])->first();
if ($simbrief) {
return redirect(route('frontend.simbrief.briefing', [$simbrief->id]));
}
// SimBrief profile does not exists and everything else is ok
// Select aircraft which will be used for calculations and details
$aircraft = Aircraft::where('id', $aircraft_id)->first();
// Get passenger and baggage weights with failsafe defaults
if ($flight->flight_type === FlightType::CHARTER_PAX_ONLY) {
$pax_weight = setting('simbrief.charter_pax_weight', 168);
$bag_weight = setting('simbrief.charter_baggage_weight', 28);
} else {
$pax_weight = setting('simbrief.noncharter_pax_weight', 185);
$bag_weight = setting('simbrief.noncharter_baggage_weight', 35);
}
// Get the load factors with failsafe for loadmax if nothing is defined
$lfactor = $flight->load_factor ?? setting('flights.default_load_factor');
$lfactorv = $flight->load_factor_variance ?? setting('flights.load_factor_variance');
$loadmin = $lfactor - $lfactorv;
$loadmin = $loadmin < 0 ? 0 : $loadmin;
$loadmax = $lfactor + $lfactorv;
$loadmax = $loadmax > 100 ? 100 : $loadmax;
if ($loadmax === 0) {
$loadmax = 100;
}
// Show the main simbrief form
return view('flights.simbrief_form', [
'flight' => $flight,
'aircraft' => $aircraft,
'pax_weight' => $pax_weight,
'bag_weight' => $bag_weight,
'loadmin' => $loadmin,
'loadmax' => $loadmax,
]);
}
/**
* Show the briefing
*
* @param string $id The OFP ID
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
public function briefing($id)
{
$simbrief = SimBrief::find($id);
if (!$simbrief) {
flash()->error('SimBrief briefing not found');
return redirect(route('frontend.flights.index'));
}
$str = $simbrief->xml->aircraft->equip;
$wc = stripos($str, '-');
$tr = stripos($str, '/');
$wakecat = substr($str, 0, $wc);
$equipment = substr($str, $wc + 1, $tr - 2);
$transponder = substr($str, $tr + 1);
return view('flights.simbrief_briefing', [
'simbrief' => $simbrief,
'wakecat' => $wakecat,
'equipment' => $equipment,
'transponder' => $transponder,
]);
}
/**
* Remove the flight_id from the SimBrief Briefing (to a create a new one)
* or if no pirep_id is attached to the briefing delete it completely
*
* @param \Illuminate\Http\Request $request
*
* @throws \Exception
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function generate_new(Request $request)
{
$simbrief = SimBrief::find($request->id);
// Invalid Simbrief ID/profile, go back to the main flight index
if (!$simbrief) {
return redirect(route('frontend.flights.index'));
}
// Cleanup the current Simbrief entry and redirect to the new generation form
// If there isn't a PIREP ID, then delete the entry, otherwise, remove the flight
$flight_id = $simbrief->flight_id;
if (!$simbrief->pirep_id) {
$simbrief->delete();
} else {
$simbrief->flight_id = null;
$simbrief->save();
}
return redirect(route('frontend.simbrief.generate').'?flight_id='.$flight_id);
}
/**
* Create a prefile of this PIREP with a given OFP. Then redirect the
* user to the newly prefiled PIREP
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function prefile(Request $request)
{
$sb = SimBrief::find($request->id);
if (!$sb) {
return redirect(route('frontend.flights.index'));
}
// Redirect to the prefile page, with the flight_id and a simbrief_id
$rd = route('frontend.pireps.create').'?sb_id='.$sb->id;
return redirect($rd);
}
/**
* Cancel the SimBrief request
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function cancel(Request $request)
{
$sb = SimBrief::find($request->id);
if (!$sb) {
$sb->delete();
}
return redirect(route('frontend.simbrief.prefile', ['id' => $request->id]));
}
/**
* Check whether the OFP was generated. Pass in two items, the flight_id and ofp_id
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function check_ofp(Request $request)
{
$ofp_id = $request->input('ofp_id');
$flight_id = $request->input('flight_id');
$simbrief = $this->simBriefSvc->downloadOfp(Auth::user()->id, $ofp_id, $flight_id);
if ($simbrief === null) {
$error = new AssetNotFound(new Exception('Simbrief OFP not found'));
return $error->getResponse();
}
return response()->json([
'id' => $simbrief->id,
]);
}
/**
* Generate the API code
*
* @param \Illuminate\Http\Request $request
*
* @throws \Exception
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function api_code(Request $request)
{
$apiKey = setting('simbrief.api_key', null);
if (empty($apiKey)) {
flash()->error('Invalid SimBrief API key!');
return redirect(route('frontend.flights.index'));
}
$api_code = md5($apiKey.$request->input('api_req'));
return response()->json([
'api_code' => $api_code,
]);
}
}