2017-06-11 07:27:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
use App\Models\Subfleet;
|
2017-06-11 07:27:19 +08:00
|
|
|
use App\Models\Fare;
|
|
|
|
|
|
|
|
class FareService extends BaseService {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a fare to an aircraft
|
|
|
|
*
|
2017-06-25 00:09:27 +08:00
|
|
|
* @param Aircraft $subfleet
|
2017-06-11 07:27:19 +08:00
|
|
|
* @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-25 00:09:27 +08:00
|
|
|
Subfleet &$subfleet,
|
2017-06-11 07:27:19 +08:00
|
|
|
Fare &$fare,
|
|
|
|
array $override=[]
|
|
|
|
) {
|
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();
|
|
|
|
$subfleet = $subfleet->fresh();
|
|
|
|
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.
|
2017-06-25 00:09:27 +08:00
|
|
|
* @param Aircraft $subfleet
|
2017-06-11 07:27:19 +08:00
|
|
|
* @return Fare[]
|
|
|
|
*/
|
2017-06-25 00:09:27 +08:00
|
|
|
public function getForAircraft(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;
|
|
|
|
}
|
|
|
|
|
2017-06-25 00:09:27 +08:00
|
|
|
public function delFromAircraft(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);
|
|
|
|
$subfleet = $subfleet->fresh();
|
|
|
|
return $subfleet;
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
}
|