From 3b09645b5b99b5accda4058c1a9d5bf3af4895e1 Mon Sep 17 00:00:00 2001 From: Lukas Halbritter Date: Fri, 28 Sep 2018 22:45:32 +0200 Subject: [PATCH] Added the functions to get the models connected with the booking. --- app/Booking.php | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/app/Booking.php b/app/Booking.php index 04013cd..c7730ca 100644 --- a/app/Booking.php +++ b/app/Booking.php @@ -8,11 +8,48 @@ class Booking extends Model { public function amount() { - return $this->hasMany('App\SubBooking')->amount; + return $this->subBookings()->sum('amount'); } public function signedAmount() { - return $this->amount; + return $this->subBookings()->withSignedAmount()->get() + ->sum('signedAmount'); + } + + public function subBookings() + { + return $this->hasMany('App\SubBooking'); + } + + /** + * Return true, if the booking consists only of one sub-booking. + * NOTICE: The book instance has to be loaded with the + * ->withCount('subBookings') method to be able to access the comments + * count. + * + * @return boolean + */ + public function isSimple() + { + return $this->sub_bookings_count === 1; + } + + public function project() + { + if ($this->isSimple()) { + return $this->subBookings()->first()->project(); + } + + return null; + } + + public function accountType() + { + if ($this->isSimple()) { + return $this->subBookings()->first()->accountType(); + } + + return null; } }