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;
|
2018-02-25 05:38:25 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
/**
|
|
|
|
* Class FareService
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* 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,
|
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 {
|
|
|
|
$flight_fares = $this->getForFlight($flight);
|
|
|
|
}
|
|
|
|
|
|
|
|
$subfleet_fares = $this->getForSubfleet($subfleet);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Go through all of the fares assigned by the subfleet
|
|
|
|
// See if any of the same fares are assigned to the flight
|
2018-03-20 09:50:40 +08:00
|
|
|
$fares = $subfleet_fares->map(function ($fare, $idx) use ($flight_fares) {
|
2018-02-25 05:38:25 +08:00
|
|
|
$flight_fare = $flight_fares->whereStrict('id', $fare->id)->first();
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!$flight_fare) {
|
2018-02-25 05:38:25 +08:00
|
|
|
return $fare;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $flight_fare;
|
|
|
|
});
|
|
|
|
|
|
|
|
return $fares;
|
|
|
|
}
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
/**
|
|
|
|
* Get fares
|
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
|
|
|
|
*/
|
|
|
|
protected function getFares($fare)
|
|
|
|
{
|
|
|
|
if (filled($fare->pivot->price)) {
|
|
|
|
if (substr_count($fare->pivot->price, '%', -1)) {
|
|
|
|
$fare->price = Math::addPercent($fare->price, $fare->pivot->price);
|
|
|
|
} else {
|
|
|
|
$fare->price = $fare->pivot->price;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filled($fare->pivot->cost)) {
|
|
|
|
if (substr_count($fare->pivot->cost, '%', -1)) {
|
|
|
|
$fare->cost = Math::addPercent($fare->cost, $fare->pivot->cost);
|
|
|
|
} else {
|
|
|
|
$fare->cost = $fare->pivot->cost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filled($fare->pivot->capacity)) {
|
|
|
|
if (substr_count($fare->pivot->capacity, '%', -1)) {
|
|
|
|
$fare->capacity = Math::addPercent($fare->capacity, $fare->pivot->capacity);
|
|
|
|
} else {
|
|
|
|
$fare->capacity = $fare->pivot->capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fare;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
$flight->fares()->syncWithoutDetaching([$fare->id]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($override as $key => $item) {
|
|
|
|
if (!$item) {
|
2018-03-22 06:07:30 +08:00
|
|
|
unset($override[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// modify any pivot values?
|
2018-01-07 23:19:46 +08:00
|
|
|
if (\count($override) > 0) {
|
|
|
|
$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
|
|
|
* return all the fares for a flight. check the pivot
|
|
|
|
* table to see if the price/cost/capacity has been overridden
|
|
|
|
* and return the correct amounts.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @param Flight $flight
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return Collection
|
2018-01-07 23:19:46 +08:00
|
|
|
*/
|
|
|
|
public function getForFlight(Flight $flight)
|
|
|
|
{
|
|
|
|
$fares = $flight->fares->map(function ($fare) {
|
2018-02-24 05:12:09 +08:00
|
|
|
return $this->getFares($fare);
|
2018-01-07 23:19:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return $fares;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
{
|
|
|
|
$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
|
|
|
{
|
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?
|
2018-08-27 02:50:08 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-07 23:19:46 +08:00
|
|
|
* @param Subfleet $subfleet
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return Collection
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
2018-01-07 23:19:46 +08:00
|
|
|
public function getForSubfleet(Subfleet $subfleet)
|
2017-06-11 07:27:19 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
$fares = $subfleet->fares->map(function ($fare) {
|
2018-02-24 05:12:09 +08:00
|
|
|
return $this->getFares($fare);
|
2017-06-14 08:53:02 +08:00
|
|
|
});
|
2017-06-11 07:27:19 +08:00
|
|
|
|
|
|
|
return $fares;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
$fares = [];
|
|
|
|
$found_fares = PirepFare::where('pirep_id', $pirep->id)->get();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
return $found_fares;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the list of fares
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-02 06:20:13 +08:00
|
|
|
* @param Pirep $pirep
|
|
|
|
* @param array $fares ['fare_id', 'count']
|
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)
|
|
|
|
{
|
|
|
|
if (!$fares) {
|
|
|
|
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) {
|
|
|
|
$fare['pirep_id'] = $pirep->id;
|
2018-08-27 00:40:04 +08:00
|
|
|
// other fields: ['fare_id', 'count']
|
2018-03-02 06:20:13 +08:00
|
|
|
|
|
|
|
$field = new PirepFare($fare);
|
|
|
|
$field->save();
|
|
|
|
}
|
|
|
|
}
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|