Files
pollux/app/Person.php
T

167 lines
4.3 KiB
PHP

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use App\MemberState;
use App\PersonTarget;
class Person extends Model
{
/**************************************************************************
* DIRECT RELATIONS *
**************************************************************************/
/**
* Get all member state records associated with a person.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function memberStates()
{
return $this->hasMany('\App\MemberState')->orderBy('joining_date');
}
/**
* Get all targets associated with a person.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function targets()
{
return $this->hasMany('\App\PersonTarget')->orderBy('date');
}
/**************************************************************************
* INDIRECT RELATIONS *
**************************************************************************/
/**
* Get all actuals associated with a person.
*
* @return Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function actuals()
{
return $this->hasManyThrough('\App\SubBooking', '\App\PersonTarget');
}
/**************************************************************************
* INDIRECT PROPERTIES *
**************************************************************************/
/**
* Return, if the person is even.
*
* @return boolean
*/
public function isEven()
{
foreach ($this->targets as $target) {
if (!$target->isPaid()) {
return false;
}
}
return true;
}
/**
* Get the full name of the person.
*
* @return string
*/
public function fullName()
{
if ($this->title == 'Firma') {
return $this->name_or_company;
} else {
return $this->surname . ' ' . $this->name_or_company;
}
}
/**
* Return true, if the person is active member, return false otherwise.
*
* @return boolean
*/
public function isMember()
{
$latest = $this->memberStates->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->memberStates->isEmpty());
}
/**
* Return the last date since when the person is an active member.
*
* @return Carbon\Carbon
*/
public function activeSince()
{
$latest = $this->memberStates
->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->memberStates;
$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;
}
/**
* Return a collection of all persons which are active members.
*
* @return Illuminate\Database\Eloquent\Collection
*/
public static function active()
{
return Person::whereHas('memberStates', function($memberState) {
$memberState->where('leaving_date', '=', null);
})->get();
}
// Make all attributes mass assignable.
protected $guarded = [];
}