2017-06-11 07:27:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\Fare;
|
2018-01-07 23:19:46 +08:00
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\Subfleet;
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
class FareService extends BaseService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Attach a fare to an flight
|
|
|
|
*
|
|
|
|
* @param Flight $flight
|
|
|
|
* @param Fare $fare
|
|
|
|
* @param array set the price/cost/capacity
|
|
|
|
* @return Flight
|
|
|
|
*/
|
|
|
|
public function setForFlight(Flight $flight, Fare $fare, array $override = []): Flight
|
|
|
|
{
|
|
|
|
$flight->fares()->syncWithoutDetaching([$fare->id]);
|
|
|
|
|
|
|
|
# modify any pivot values?
|
|
|
|
if (\count($override) > 0) {
|
|
|
|
$flight->fares()->updateExistingPivot($fare->id, $override);
|
|
|
|
}
|
|
|
|
|
|
|
|
$flight->save();
|
|
|
|
$flight->refresh();
|
|
|
|
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.
|
|
|
|
* @param Flight $flight
|
|
|
|
* @return Fare[]
|
|
|
|
*/
|
|
|
|
public function getForFlight(Flight $flight)
|
|
|
|
{
|
|
|
|
$fares = $flight->fares->map(function ($fare) {
|
|
|
|
if (null !== $fare->pivot->price) {
|
|
|
|
$fare->price = $fare->pivot->price;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $fare->pivot->cost) {
|
|
|
|
$fare->cost = $fare->pivot->cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $fare->pivot->capacity) {
|
|
|
|
$fare->capacity = $fare->pivot->capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fare;
|
|
|
|
});
|
|
|
|
|
|
|
|
return $fares;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Flight $flight
|
|
|
|
* @param Fare $fare
|
|
|
|
* @return Flight
|
|
|
|
*/
|
|
|
|
public function delFareFromFlight(Flight $flight, Fare $fare)
|
|
|
|
{
|
|
|
|
$flight->fares()->detach($fare->id);
|
|
|
|
$flight->refresh();
|
|
|
|
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-01-07 23:19:46 +08:00
|
|
|
* @return Subfleet
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
2018-01-07 23:19:46 +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
|
|
|
|
|
|
|
# modify any pivot values?
|
|
|
|
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();
|
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-01-07 23:19:46 +08:00
|
|
|
* @param Subfleet $subfleet
|
2017-06-11 07:27:19 +08:00
|
|
|
* @return Fare[]
|
|
|
|
*/
|
2018-01-07 23:19:46 +08:00
|
|
|
public function getForSubfleet(Subfleet $subfleet)
|
2017-06-11 07:27:19 +08:00
|
|
|
{
|
2017-06-25 00:09:27 +08:00
|
|
|
$fares = $subfleet->fares->map(function($fare) {
|
2017-06-11 07:27:19 +08:00
|
|
|
if(!is_null($fare->pivot->price)) {
|
|
|
|
$fare->price = $fare->pivot->price;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_null($fare->pivot->cost)) {
|
|
|
|
$fare->cost = $fare->pivot->cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_null($fare->pivot->capacity)) {
|
|
|
|
$fare->capacity = $fare->pivot->capacity;
|
|
|
|
}
|
2017-06-14 08:53:02 +08:00
|
|
|
|
|
|
|
return $fare;
|
|
|
|
});
|
2017-06-11 07:27:19 +08:00
|
|
|
|
|
|
|
return $fares;
|
|
|
|
}
|
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
/**
|
|
|
|
* Delete the fare from a subfleet
|
|
|
|
* @param Subfleet $subfleet
|
|
|
|
* @param Fare $fare
|
|
|
|
* @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();
|
2017-06-25 00:09:27 +08:00
|
|
|
return $subfleet;
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
}
|