hasMany('App\SubBooking'); } /************************************************************************** * INDIRECT RELATIONS * **************************************************************************/ /** * Get the project to which the booking belongs. If the booking consists of * more than one sub-booking, null is returned. * * @return App\Project */ public function project() { if ($this->isSimple()) { return $this->subBookings->first()->project; } return null; } /** * Get the account type of the booking. If the booking consists of more than * one sub-booking, null is returned. * * @return App\AccountType */ public function accountType() { if ($this->isSimple()) { return $this->subBookings->first()->accountType; } return null; } /************************************************************************** * INDIRECT PROPERTIES * **************************************************************************/ /** * Get the (unsigned) amount of all sub-bookings. * * @return float */ public function amount() { return $this->subBookings->sum('amount'); } /** * Return the sum of all signed amounts of all sub-bookings related to the * booking. * * @return float */ public function signedAmount() { $sum = 0; foreach ($this->subBookings as $subBooking) { $sum += $subBooking->signedAmount(); } return $sum; } /** * Return true, if the booking consists only of one sub-booking. * * @return boolean */ public function isSimple() { return $this->subBookings->count() === 1; } }