phpvms/app/Services/FareService.php

136 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Models\Fare;
use App\Models\Flight;
use App\Models\Subfleet;
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;
}
/**
* 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
*
* @param Subfleet $subfleet
* @param Fare $fare
* @param array set the price/cost/capacity
* @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]);
# modify any pivot values?
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.
* @param Subfleet $subfleet
* @return Fare[]
*/
public function getForSubfleet(Subfleet $subfleet)
{
2017-06-25 00:09:27 +08:00
$fares = $subfleet->fares->map(function($fare) {
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;
});
return $fares;
}
/**
* 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-25 00:09:27 +08:00
$subfleet->fares()->detach($fare->id);
$subfleet->refresh();
2017-06-25 00:09:27 +08:00
return $subfleet;
}
}