From 50acdbd71e350b22408dedc196e5cf6b9a8d8f53 Mon Sep 17 00:00:00 2001 From: Lukas Halbritter Date: Sun, 9 Sep 2018 09:58:52 +0200 Subject: [PATCH] Added functions to get targets an actuals of a person. --- app/Person.php | 60 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/app/Person.php b/app/Person.php index 9cc22e5..39bfa8a 100644 --- a/app/Person.php +++ b/app/Person.php @@ -4,20 +4,24 @@ namespace App; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; + use App\MemberState; +use App\PersonTarget; class Person extends Model { + /************************************************************************** + * MEMBER STATE FUNCTIONS * + **************************************************************************/ + /* * Return true, if the person is active member, return false otherwise. * * @return boolean */ - public function isActiveMember() + public function isMember() { - $latest = $this->hasMany('\App\MemberState') - ->orderBy('joining_date', 'desc') - ->first(); + $latest = $this->getMemberStates()->last(); if (is_null($latest)) { return false; @@ -26,6 +30,18 @@ class Person extends Model } } + /* + * Returns true, if the person was active member at some time in the past. + * Note, that this function also returns true, if the person is still an + * active member. + * + * @return boolean + */ + public function wasMember() + { + return !($this->getMemberStates()->isEmpty()); + } + /* * Return a collection of all persons which are active members. * @@ -38,7 +54,7 @@ class Person extends Model ->select('person_id as id', 'title', 'name_or_company', 'surname', 'date_of_birth', 'city', 'street', 'postal_code', 'email', 'mobile', 'phone', - 'people.created_at', 'people.updated_at') + 'joining_date', 'leaving_date') ->get(); } @@ -81,4 +97,38 @@ class Person extends Model return $diff; } + + /* + * Get all member state records associated with a person. + * + * @return Illuminate\Database\Eloquent\Collection + */ + public function getMemberStates() + { + return $this->hasMany('\App\MemberState')->orderBy('joining_date')->get(); + } + + /************************************************************************** + * PERSON TARGET FUNCTIONS * + **************************************************************************/ + + /* + * Get all targets associated with a person. + * + * @return Illuminate\Database\Eloquent\Collection + */ + public function getTargets() + { + return $this->hasMany('\App\PersonTarget')->orderBy('date')->get(); + } + + /* + * Get all actuals associated with a person. + * + * @return Illuminate\Database\Eloquent\Collection + */ + public function getActuals() + { + return $this->hasManyThrough('\App\SubBooking', '\App\PersonTarget')->get(); + } }