Implemented eager loading for the people show view.

This commit is contained in:
Lukas Halbritter
2018-10-01 09:58:04 +02:00
parent 8f865e9bff
commit e9077193af
4 changed files with 164 additions and 167 deletions
+76 -82
View File
@@ -7,6 +7,82 @@ use Illuminate\Support\Facades\DB;
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 *
@@ -130,86 +206,4 @@ class PersonTarget extends Model
// Create a collection of PersonTargets from the raw query result.
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;
}
}