phpvms/app/Services/FareService.php

255 lines
6.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Interfaces\Service;
use App\Models\Fare;
use App\Models\Flight;
use App\Models\Pirep;
use App\Models\PirepFare;
use App\Models\Subfleet;
use App\Support\Math;
use Illuminate\Support\Collection;
/**
* Class FareService
*/
class FareService extends Service
{
/**
* 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
*
* @param Flight|null $flight
* @param Subfleet|null $subfleet
2018-08-27 00:40:04 +08:00
*
* @return Collection
*/
public function getAllFares($flight, $subfleet)
{
if (!$flight) {
$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
$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
2018-08-27 00:40:04 +08:00
*
* @param $fare
2018-08-27 00:40:04 +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;
}
/**
* Attach a fare to an flight
*
* @param Flight $flight
* @param Fare $fare
* @param array set the price/cost/capacity
2018-08-27 00:40:04 +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?
if (\count($override) > 0) {
$flight->fares()->updateExistingPivot($fare->id, $override);
}
$flight->save();
$flight->refresh();
return $flight;
}
/**
* 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
*
* @param Flight $flight
2018-08-27 00:40:04 +08:00
*
* @return Collection
*/
public function getForFlight(Flight $flight)
{
$fares = $flight->fares->map(function ($fare) {
return $this->getFares($fare);
});
return $fares;
}
/**
* @param Flight $flight
* @param Fare $fare
2018-08-27 00:40:04 +08:00
*
* @return Flight
*/
public function delFareFromFlight(Flight $flight, Fare $fare)
{
$flight->fares()->detach($fare->id);
$flight->refresh();
return $flight;
}
/**
* Attach a fare to a subfleet
*
* @param Subfleet $subfleet
* @param Fare $fare
* @param array set the price/cost/capacity
2018-08-27 00:40:04 +08:00
*
* @return Subfleet
*/
public function setForSubfleet(Subfleet $subfleet, Fare $fare, array $override = []): Subfleet
{
2017-06-25 00:09:27 +08:00
$subfleet->fares()->syncWithoutDetaching([$fare->id]);
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-25 00:09:27 +08:00
$subfleet->save();
$subfleet->refresh();
2017-06-25 00:09:27 +08:00
return $subfleet;
}
/**
* 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
*
* @param Subfleet $subfleet
2018-08-27 00:40:04 +08:00
*
* @return Collection
*/
public function getForSubfleet(Subfleet $subfleet)
{
$fares = $subfleet->fares->map(function ($fare) {
return $this->getFares($fare);
2017-06-14 08:53:02 +08:00
});
return $fares;
}
/**
* Delete the fare from a subfleet
2018-08-27 00:40:04 +08:00
*
* @param Subfleet $subfleet
* @param Fare $fare
2018-08-27 00:40:04 +08:00
*
* @return Subfleet|null|static
*/
public function delFareFromSubfleet(Subfleet &$subfleet, Fare &$fare)
{
2017-06-25 00:09:27 +08:00
$subfleet->fares()->detach($fare->id);
$subfleet->refresh();
2017-06-25 00:09:27 +08:00
return $subfleet;
}
/**
* 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
*
* @param Pirep $pirep
2018-08-27 00:40:04 +08:00
*
* @return Collection
*/
public function getForPirep(Pirep $pirep)
{
$fares = [];
$found_fares = PirepFare::where('pirep_id', $pirep->id)->get();
return $found_fares;
}
/**
* Save the list of fares
2018-08-27 00:40:04 +08:00
*
* @param Pirep $pirep
* @param array $fares ['fare_id', 'count']
2018-08-27 00:40:04 +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
PirepFare::where('pirep_id', $pirep->id)->delete();
2018-08-27 00:40:04 +08:00
// Add them in
foreach ($fares as $fare) {
$fare['pirep_id'] = $pirep->id;
2018-08-27 00:40:04 +08:00
// other fields: ['fare_id', 'count']
$field = new PirepFare($fare);
$field->save();
}
}
}