Implemented eager loading for the people show view.
This commit is contained in:
@@ -20,12 +20,11 @@ class PeopleController extends Controller
|
|||||||
|
|
||||||
public function show(Person $person)
|
public function show(Person $person)
|
||||||
{
|
{
|
||||||
$memberStates = $person->memberStates;
|
$person->load('targets.actuals.accountType.accountClass.ioType',
|
||||||
$targets = $person->targets;
|
'targets.actuals.booking',
|
||||||
$actuals = $person->actuals;
|
'targets.ioType');
|
||||||
|
|
||||||
return view('people.show', compact('person', 'memberStates', 'targets',
|
return view('people.show', compact('person'));
|
||||||
'actuals'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show the form for creating a new person
|
// Show the form for creating a new person
|
||||||
|
|||||||
+80
-76
@@ -11,10 +11,78 @@ use App\PersonTarget;
|
|||||||
class Person extends Model
|
class Person extends Model
|
||||||
{
|
{
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
* MEMBER STATE FUNCTIONS *
|
* 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 true, if the person is active member, return false otherwise.
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
@@ -29,7 +97,7 @@ class Person extends Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Returns true, if the person was active member at some time in the past.
|
* 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
|
* Note, that this function also returns true, if the person is still an
|
||||||
* active member.
|
* active member.
|
||||||
@@ -41,19 +109,7 @@ class Person extends Model
|
|||||||
return !($this->memberStates->isEmpty());
|
return !($this->memberStates->isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the last date since when the person is an active member.
|
* Return the last date since when the person is an active member.
|
||||||
*
|
*
|
||||||
* @return Carbon\Carbon
|
* @return Carbon\Carbon
|
||||||
@@ -67,7 +123,7 @@ class Person extends Model
|
|||||||
return Carbon::parse($latest['joining_date']);
|
return Carbon::parse($latest['joining_date']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Returns the active time of the person until now in years.
|
* Returns the active time of the person until now in years.
|
||||||
*
|
*
|
||||||
* @return float
|
* @return float
|
||||||
@@ -93,68 +149,16 @@ class Person extends Model
|
|||||||
return $diff;
|
return $diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Get all member state records associated with a person.
|
* Return a collection of all persons which are active members.
|
||||||
*
|
*
|
||||||
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
* @return Illuminate\Database\Eloquent\Collection
|
||||||
*/
|
*/
|
||||||
public function memberStates()
|
public static function active()
|
||||||
{
|
{
|
||||||
return $this->hasMany('\App\MemberState')->orderBy('joining_date');
|
return Person::whereHas('memberStates', function($memberState) {
|
||||||
}
|
$memberState->where('leaving_date', '=', null);
|
||||||
|
})->get();
|
||||||
/**************************************************************************
|
|
||||||
* PERSON TARGET FUNCTIONS *
|
|
||||||
**************************************************************************/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get all targets associated with a person.
|
|
||||||
*
|
|
||||||
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
|
||||||
*/
|
|
||||||
public function targets()
|
|
||||||
{
|
|
||||||
return $this->hasMany('\App\PersonTarget')->orderBy('date');
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get all actuals associated with a person.
|
|
||||||
*
|
|
||||||
* @return Illuminate\Database\Eloquent\Relations\HasManyThrough
|
|
||||||
*/
|
|
||||||
public function actuals()
|
|
||||||
{
|
|
||||||
return $this->hasManyThrough('\App\SubBooking', '\App\PersonTarget');
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make all attributes mass assignable.
|
// Make all attributes mass assignable.
|
||||||
|
|||||||
+76
-82
@@ -7,6 +7,82 @@ use Illuminate\Support\Facades\DB;
|
|||||||
|
|
||||||
class PersonTarget extends Model
|
class PersonTarget extends Model
|
||||||
{
|
{
|
||||||
|
/**************************************************************************
|
||||||
|
* DIRECT RELATIONS *
|
||||||
|
**************************************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the IOType of the target.
|
||||||
|
*
|
||||||
|
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
*/
|
||||||
|
public function ioType()
|
||||||
|
{
|
||||||
|
return $this->belongsTo('App\IOType');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the actuals connected with the target.
|
||||||
|
*
|
||||||
|
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
public function actuals()
|
||||||
|
{
|
||||||
|
return $this->hasMany('App\SubBooking');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* INDIRECT RELATIONS *
|
||||||
|
**************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* INDIRECT PROPERTIES *
|
||||||
|
**************************************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the signed amount of the target.
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function signedAmount()
|
||||||
|
{
|
||||||
|
return $this->amount * $this->ioType->factor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the sum of all actuals belonging to this target
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function actualsSum() {
|
||||||
|
$sum = 0;
|
||||||
|
|
||||||
|
foreach ($this->actuals as $actual) {
|
||||||
|
$sum += $actual->signedAmount();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the difference of target and actually payed amount so far. I. e.
|
||||||
|
* return the amount still to be payed.
|
||||||
|
*
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function difference() {
|
||||||
|
return $this->signedAmount() - $this->actualsSum();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true, if the target is paid and therefore closed.
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function isPaid() {
|
||||||
|
return $this->difference() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
* COLLECTIONS FILTERED BY PAYMENT STATES *
|
* COLLECTIONS FILTERED BY PAYMENT STATES *
|
||||||
@@ -130,86 +206,4 @@ class PersonTarget extends Model
|
|||||||
// Create a collection of PersonTargets from the raw query result.
|
// Create a collection of PersonTargets from the raw query result.
|
||||||
return PersonTarget::hydrate($selection);
|
return PersonTarget::hydrate($selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************
|
|
||||||
* BOOKINGS *
|
|
||||||
**************************************************************************/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get all bookings associated with a target.
|
|
||||||
*
|
|
||||||
* @return Illuminate\Database\Eloquent\Collection
|
|
||||||
*/
|
|
||||||
public function bookings()
|
|
||||||
{
|
|
||||||
return $this->hasMany('\App\SubBooking');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**************************************************************************
|
|
||||||
* IO TYPES *
|
|
||||||
**************************************************************************/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get the IOType of the target.
|
|
||||||
*
|
|
||||||
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
||||||
*/
|
|
||||||
public function ioType()
|
|
||||||
{
|
|
||||||
return $this->belongsTo('App\IOType');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**************************************************************************
|
|
||||||
* TARGET AND ACTUAL VALUES *
|
|
||||||
**************************************************************************/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get the signed amount of the target.
|
|
||||||
*
|
|
||||||
* @return float
|
|
||||||
*/
|
|
||||||
public function signedAmount()
|
|
||||||
{
|
|
||||||
return $this->amount * $this->ioType->factor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get the actuals connected with the target.
|
|
||||||
*
|
|
||||||
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
|
||||||
*/
|
|
||||||
public function actuals()
|
|
||||||
{
|
|
||||||
return $this->hasMany('App\SubBooking');
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the sum of all actuals belonging to this target
|
|
||||||
*
|
|
||||||
* @return float
|
|
||||||
*/
|
|
||||||
public function actualsSum() {
|
|
||||||
return $this->actuals->each(function ($item, $key) {
|
|
||||||
$item['signedAmount'] = $item->signedAmount();
|
|
||||||
})->sum('signedAmount');
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get the difference of target and actually payed amount so far. I. e.
|
|
||||||
* return the amount still to be payed.
|
|
||||||
*
|
|
||||||
* @return float
|
|
||||||
*/
|
|
||||||
public function difference() {
|
|
||||||
return $this->signedAmount() - $this->actualsSum();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return true, if the target is paid and therefore closed.
|
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function isPaid() {
|
|
||||||
return $this->difference() == 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,13 +105,13 @@
|
|||||||
<td align='right'>{{ formatAmount($target->actualsSum()) }}</td>
|
<td align='right'>{{ formatAmount($target->actualsSum()) }}</td>
|
||||||
<td align='right'>{{ formatAmount($target->difference()) }}</td>
|
<td align='right'>{{ formatAmount($target->difference()) }}</td>
|
||||||
<tr>
|
<tr>
|
||||||
@foreach ($target->bookings as $booking)
|
@foreach ($target->actuals as $actual)
|
||||||
<tr>
|
<tr>
|
||||||
<td></>
|
<td></>
|
||||||
<td>{{ $booking->date() }}</td>
|
<td>{{ $actual->date() }}</td>
|
||||||
<td>{{ $booking->reference }}</td>
|
<td>{{ $actual->reference }}</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td align='right'>{{ formatAmount($booking->signedAmount()) }}</td>
|
<td align='right'>{{ formatAmount($actual->signedAmount()) }}</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|||||||
Reference in New Issue
Block a user