2017-06-11 07:27:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Service;
|
2017-06-11 07:27:19 +08:00
|
|
|
use App\Models\Fare;
|
2018-01-07 23:19:46 +08:00
|
|
|
use App\Models\Flight;
|
2018-03-02 06:20:13 +08:00
|
|
|
use App\Models\Pirep;
|
|
|
|
use App\Models\PirepFare;
|
2018-01-07 23:19:46 +08:00
|
|
|
use App\Models\Subfleet;
|
2018-02-24 05:12:09 +08:00
|
|
|
use App\Support\Math;
|
2020-10-11 03:24:10 +08:00
|
|
|
use function count;
|
2020-10-28 06:46:15 +08:00
|
|
|
use Illuminate\Database\Eloquent\Relations\Pivot;
|
2018-02-25 05:38:25 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2020-10-14 06:12:52 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-10-28 06:46:15 +08:00
|
|
|
use InvalidArgumentException;
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class FareService extends Service
|
2018-01-07 23:19:46 +08:00
|
|
|
{
|
2018-02-25 05:38:25 +08:00
|
|
|
/**
|
2020-10-28 06:46:15 +08:00
|
|
|
* @param Collection[Fare] $subfleet_fares The fare for a subfleet
|
|
|
|
* @param Collection[Fare] $flight_fares The fares on a flight
|
2018-02-25 05:38:25 +08:00
|
|
|
*
|
2020-10-28 06:46:15 +08:00
|
|
|
* @return Collection[Fare] Collection of Fare
|
|
|
|
*/
|
|
|
|
public function getFareWithOverrides($subfleet_fares, $flight_fares): Collection
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Make sure we've got something in terms of fares on the subfleet or the flight
|
|
|
|
*/
|
|
|
|
if (empty($subfleet_fares) && empty($flight_fares)) {
|
|
|
|
return collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if there are any subfleet fares. This might only have fares on the
|
|
|
|
* flight, no matter how rare that might be
|
|
|
|
*/
|
|
|
|
if ($subfleet_fares === null || count($subfleet_fares) === 0) {
|
|
|
|
return $flight_fares->map(function ($fare, $_) {
|
|
|
|
return $this->getFareWithPivot($fare, $fare->pivot);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subfleet_fares->map(function ($sf_fare, $_) use ($flight_fares) {
|
|
|
|
/**
|
|
|
|
* Get the fare, using the subfleet's pivot values. This will return
|
|
|
|
* the fares with all the costs, etc, that are overridden for the given subfleet
|
|
|
|
*/
|
|
|
|
$fare = $this->getFareWithPivot($sf_fare, $sf_fare->pivot);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now, using the fares that have already been used from the subfleet
|
|
|
|
* now pass those fares in for the flight to override.
|
|
|
|
*
|
|
|
|
* First look to see that there actually is an override for that fare that's on
|
|
|
|
* the flight
|
|
|
|
*/
|
|
|
|
$flight_fare = $flight_fares->whereStrict('id', $fare->id)->first();
|
|
|
|
if ($flight_fare === null) {
|
|
|
|
return $fare;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Found an override on the flight for the given fare. Check to see if we
|
|
|
|
* have values there that can be used to override or act as a pivot
|
|
|
|
*/
|
|
|
|
$fare = $this->getFareWithPivot($fare, $flight_fare->pivot);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finally return the fare that we have, it should have gone through the
|
|
|
|
* multiple levels of reconciliation that were required
|
|
|
|
*/
|
|
|
|
return $fare;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will return the flight but all of the subfleets will have the corrected fares with the
|
|
|
|
* right amounts based on the pivots, and with the correct "inheritence" for the flights
|
|
|
|
*
|
|
|
|
* @param Flight $flight
|
|
|
|
*
|
|
|
|
* @return \App\Models\Flight
|
|
|
|
*/
|
|
|
|
public function getReconciledFaresForFlight(Flight $flight): Flight
|
|
|
|
{
|
|
|
|
$subfleets = $flight->subfleets;
|
|
|
|
$flight_fares = $flight->fares;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int $key
|
|
|
|
* @var Subfleet $subfleet
|
|
|
|
*/
|
|
|
|
foreach ($subfleets as $key => $subfleet) {
|
|
|
|
$subfleet->fares = $this->getFareWithOverrides($subfleet->fares, $flight_fares);
|
|
|
|
}
|
|
|
|
|
|
|
|
$flight->subfleets = $subfleets;
|
|
|
|
return $flight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the fares for a particular flight, with the subfleet that is in use being passed in
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param Flight|null $flight
|
2018-02-25 05:38:25 +08:00
|
|
|
* @param Subfleet|null $subfleet
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAllFares($flight, $subfleet)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!$flight) {
|
2018-02-25 05:38:25 +08:00
|
|
|
$flight_fares = collect();
|
|
|
|
} else {
|
2020-10-28 06:46:15 +08:00
|
|
|
$flight_fares = $flight->fares;
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
if (empty($subfleet)) {
|
2020-10-28 06:46:15 +08:00
|
|
|
throw new InvalidArgumentException('Subfleet argument missing');
|
2020-02-09 02:29:34 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:46:15 +08:00
|
|
|
return $this->getFareWithOverrides($subfleet->fares, $flight_fares);
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
/**
|
2020-10-25 03:11:08 +08:00
|
|
|
* Get a fare with the proper prices/costs populated in the pivot
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-24 05:12:09 +08:00
|
|
|
* @param $fare
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-24 05:12:09 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-10-25 03:11:08 +08:00
|
|
|
public function getFares($fare)
|
2018-02-24 05:12:09 +08:00
|
|
|
{
|
2020-10-28 06:46:15 +08:00
|
|
|
return $this->getFareWithPivot($fare, $fare->pivot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the correct price of something supplied with the correct pivot
|
|
|
|
*
|
|
|
|
* @param Fare $fare
|
|
|
|
* @param Pivot $pivot
|
|
|
|
*
|
|
|
|
* @return \App\Models\Fare
|
|
|
|
*/
|
|
|
|
public function getFareWithPivot(Fare $fare, Pivot $pivot)
|
|
|
|
{
|
2020-10-11 03:24:10 +08:00
|
|
|
if (filled($pivot->price)) {
|
2020-10-28 06:46:15 +08:00
|
|
|
if (strpos($pivot->price, '%', -1) !== false) {
|
2020-11-27 05:44:57 +08:00
|
|
|
$fare->price = Math::getPercent($fare->price, $pivot->price);
|
2018-02-24 05:12:09 +08:00
|
|
|
} else {
|
2020-10-28 06:46:15 +08:00
|
|
|
$fare->price = $pivot->price;
|
2018-02-24 05:12:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 03:24:10 +08:00
|
|
|
if (filled($pivot->cost)) {
|
|
|
|
if (strpos($pivot->cost, '%', -1) !== false) {
|
2020-11-27 05:44:57 +08:00
|
|
|
$fare->cost = Math::getPercent($fare->cost, $pivot->cost);
|
2018-02-24 05:12:09 +08:00
|
|
|
} else {
|
2020-10-28 06:46:15 +08:00
|
|
|
$fare->cost = $pivot->cost;
|
2018-02-24 05:12:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 03:24:10 +08:00
|
|
|
if (filled($pivot->capacity)) {
|
|
|
|
if (strpos($pivot->capacity, '%', -1) !== false) {
|
2020-11-27 05:44:57 +08:00
|
|
|
$fare->capacity = floor(Math::getPercent($fare->capacity, $pivot->capacity));
|
2018-02-24 05:12:09 +08:00
|
|
|
} else {
|
2020-10-25 04:04:09 +08:00
|
|
|
$fare->capacity = floor($pivot->capacity);
|
2018-02-24 05:12:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fare;
|
|
|
|
}
|
|
|
|
|
2021-03-03 01:29:04 +08:00
|
|
|
/**
|
|
|
|
* Return all the fares for an aircraft. check the pivot
|
|
|
|
* table to see if the price/cost/capacity has been overridden
|
|
|
|
* and return the correct amounts.
|
|
|
|
*
|
|
|
|
* @param Subfleet $subfleet
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getForSubfleet(Subfleet $subfleet)
|
|
|
|
{
|
|
|
|
$fares = $subfleet->fares->map(function ($fare) {
|
|
|
|
return $this->getFares($fare);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $fares;
|
|
|
|
}
|
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
/**
|
|
|
|
* Attach a fare to an flight
|
|
|
|
*
|
|
|
|
* @param Flight $flight
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param Fare $fare
|
2018-01-07 23:19:46 +08:00
|
|
|
* @param array set the price/cost/capacity
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @return Flight
|
|
|
|
*/
|
|
|
|
public function setForFlight(Flight $flight, Fare $fare, array $override = []): Flight
|
|
|
|
{
|
2020-10-14 06:10:31 +08:00
|
|
|
Log::info('Setting fare "'.$fare->name.'" to flight "'.$flight->ident.'"');
|
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
$flight->fares()->syncWithoutDetaching([$fare->id]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// modify any pivot values?
|
2020-10-11 03:24:10 +08:00
|
|
|
if (count($override) > 0) {
|
2018-01-07 23:19:46 +08:00
|
|
|
$flight->fares()->updateExistingPivot($fare->id, $override);
|
|
|
|
}
|
|
|
|
|
|
|
|
$flight->save();
|
|
|
|
$flight->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
return $flight;
|
|
|
|
}
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
/**
|
|
|
|
* @param Flight $flight
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param Fare $fare
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @return Flight
|
|
|
|
*/
|
|
|
|
public function delFareFromFlight(Flight $flight, Fare $fare)
|
|
|
|
{
|
2020-10-14 06:10:31 +08:00
|
|
|
Log::info('Removing fare "'.$fare->name.'" to flight "'.$flight->ident.'"');
|
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
$flight->fares()->detach($fare->id);
|
|
|
|
$flight->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
return $flight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a fare to a subfleet
|
2017-06-11 07:27:19 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @param Subfleet $subfleet
|
2017-06-11 07:27:19 +08:00
|
|
|
* @param Fare $fare
|
|
|
|
* @param array set the price/cost/capacity
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @return Subfleet
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function setForSubfleet(Subfleet $subfleet, Fare $fare, array $override = []): Subfleet
|
2017-12-14 01:29:14 +08:00
|
|
|
{
|
2020-10-14 06:10:31 +08:00
|
|
|
Log::info('Setting fare "'.$fare->name.'" to subfleet "'.$subfleet->name.'"');
|
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
$subfleet->fares()->syncWithoutDetaching([$fare->id]);
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// modify any pivot values?
|
2020-10-11 03:24:10 +08:00
|
|
|
if (count($override) > 0) {
|
2017-06-25 00:09:27 +08:00
|
|
|
$subfleet->fares()->updateExistingPivot($fare->id, $override);
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
$subfleet->save();
|
2018-01-07 23:19:46 +08:00
|
|
|
$subfleet->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
return $subfleet;
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
/**
|
|
|
|
* Delete the fare from a subfleet
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @param Subfleet $subfleet
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param Fare $fare
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @return Subfleet|null|static
|
|
|
|
*/
|
|
|
|
public function delFareFromSubfleet(Subfleet &$subfleet, Fare &$fare)
|
2017-06-11 07:27:19 +08:00
|
|
|
{
|
2020-10-14 06:10:31 +08:00
|
|
|
Log::info('Removing fare "'.$fare->name.'" from subfleet "'.$subfleet->name.'"');
|
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
$subfleet->fares()->detach($fare->id);
|
2018-01-07 23:19:46 +08:00
|
|
|
$subfleet->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
return $subfleet;
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
2018-03-02 06:20:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the fares for a PIREP, this just returns the PirepFare
|
|
|
|
* model which includes the counts for that particular fare
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-02 06:20:13 +08:00
|
|
|
* @param Pirep $pirep
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-02 06:20:13 +08:00
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getForPirep(Pirep $pirep)
|
|
|
|
{
|
2021-02-18 07:54:18 +08:00
|
|
|
return PirepFare::where('pirep_id', $pirep->id)->get();
|
2018-03-02 06:20:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the list of fares
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2021-06-04 22:51:59 +08:00
|
|
|
* @param Pirep $pirep
|
|
|
|
* @param PirepFare[] $fares
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-02 06:20:13 +08:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function saveForPirep(Pirep $pirep, array $fares)
|
|
|
|
{
|
2021-06-04 22:51:59 +08:00
|
|
|
if (!$fares || empty($fares)) {
|
2018-03-02 06:20:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Remove all the previous fares
|
2018-03-02 06:20:13 +08:00
|
|
|
PirepFare::where('pirep_id', $pirep->id)->delete();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add them in
|
2018-03-02 06:20:13 +08:00
|
|
|
foreach ($fares as $fare) {
|
2021-06-04 22:51:59 +08:00
|
|
|
$fare->pirep_id = $pirep->id;
|
2021-02-18 07:54:18 +08:00
|
|
|
Log::info('Saving fare pirep='.$pirep->id.', fare='.$fare['count']);
|
2021-06-04 22:51:59 +08:00
|
|
|
$fare->save();
|
2018-03-02 06:20:13 +08:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|