2017-06-11 07:27:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\Aircraft;
|
|
|
|
use App\Models\Fare;
|
|
|
|
|
|
|
|
class FareService extends BaseService {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a fare to an aircraft
|
|
|
|
*
|
|
|
|
* @param Aircraft $aircraft
|
|
|
|
* @param Fare $fare
|
|
|
|
* @param array set the price/cost/capacity
|
|
|
|
*
|
|
|
|
* @return Aircraft
|
|
|
|
*/
|
2017-06-11 07:41:35 +08:00
|
|
|
public function setForAircraft(
|
2017-06-11 07:27:19 +08:00
|
|
|
Aircraft &$aircraft,
|
|
|
|
Fare &$fare,
|
|
|
|
array $override=[]
|
|
|
|
) {
|
|
|
|
$aircraft->fares()->syncWithoutDetaching([$fare->id]);
|
|
|
|
|
|
|
|
# modify any pivot values?
|
|
|
|
if(count($override) > 0) {
|
|
|
|
$aircraft->fares()->updateExistingPivot($fare->id, $override);
|
|
|
|
}
|
|
|
|
|
|
|
|
$aircraft->save();
|
|
|
|
$aircraft = $aircraft->fresh();
|
|
|
|
return $aircraft;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 Aircraft $aircraft
|
|
|
|
* @return Fare[]
|
|
|
|
*/
|
2017-06-11 07:41:35 +08:00
|
|
|
public function getForAircraft(Aircraft &$aircraft)
|
2017-06-11 07:27:19 +08:00
|
|
|
{
|
|
|
|
$fares = [];
|
|
|
|
foreach($aircraft->fares as $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;
|
|
|
|
}
|
|
|
|
array_push($fares, $fare);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fares;
|
|
|
|
}
|
|
|
|
|
2017-06-11 07:41:35 +08:00
|
|
|
public function delFromAircraft(Aircraft &$aircraft, Fare &$fare)
|
2017-06-11 07:27:19 +08:00
|
|
|
{
|
|
|
|
$aircraft->fares()->detach($fare->id);
|
|
|
|
$aircraft = $aircraft->fresh();
|
|
|
|
return $aircraft;
|
|
|
|
}
|
|
|
|
}
|