Added functions to get information about:
- active time of a member - state of a member - joining date of a member ... and a method to fetch all active people.
This commit is contained in:
+75
-1
@@ -3,8 +3,82 @@
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Carbon\Carbon;
|
||||
use App\MemberState;
|
||||
|
||||
class Person extends Model
|
||||
{
|
||||
//
|
||||
/*
|
||||
* Return true, if the person is active member, return false otherwise.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isActiveMember()
|
||||
{
|
||||
$latest = $this->hasMany('\App\MemberState')
|
||||
->orderBy('joining_date', 'desc')
|
||||
->first();
|
||||
|
||||
if (is_null($latest)) {
|
||||
return false;
|
||||
} else {
|
||||
return is_null($latest['leaving_date']);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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',
|
||||
'people.created_at', 'people.updated_at')
|
||||
->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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user