Files
pollux/app/Person.php
T

135 lines
3.8 KiB
PHP

<?php
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 isMember()
{
$latest = $this->getMemberStates()->last();
if (is_null($latest)) {
return false;
} else {
return is_null($latest['leaving_date']);
}
}
/*
* 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.
*
* @return Illuminate\Database\Eloquent\Collection
*/
public static function active()
{
return Person::join('member_states', 'people.id', '=', 'member_states.person_id')
->where('member_states.leaving_date', null)
->select('person_id as id', 'title', 'name_or_company',
'surname', 'date_of_birth', 'city', 'street',
'postal_code', 'email', 'mobile', 'phone',
'joining_date', 'leaving_date')
->get();
}
/*
* Return the last date since when the person is an active member.
*
* @return Carbon\Carbon
*/
public function activeSince()
{
$latest = $this->hasMany('\App\MemberState')
->orderBy('joining_date', 'desc')
->first();
return Carbon::parse($latest['joining_date']);
}
/*
* Returns the active time of the person until now in years.
*
* @return float
*/
public function activeTime()
{
$activeTimes = $this->hasMany('\App\MemberState')->get();
$diff = 0;
foreach ($activeTimes as $period) {
$start = Carbon::parse($period['joining_date']);
$end = is_null($period['leaving_date']) ?
Carbon::now() :
Carbon::parse($period['leaving_date']);
$diffYears = Carbon::parse($start)->diffInYears($end);
$diffDays = Carbon::parse($start)->diffInDays($end);
$diff += $diffYears + $diffDays / 365 - $diffYears;
}
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();
}
}