Added some basic methods to the models to get roperties from related models or
the related models themselfes.
This commit is contained in:
+65
-2
@@ -6,13 +6,76 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PersonTarget extends Model
|
||||
{
|
||||
/*
|
||||
* Get the signed amount of the target.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function signedAmount()
|
||||
{
|
||||
return $this->amount * $this->ioType->factor;
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* BOOKINGS *
|
||||
**************************************************************************/
|
||||
|
||||
/*
|
||||
* Get all bookings associated with a target.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getBookings()
|
||||
public function bookings()
|
||||
{
|
||||
return $this->hasMany('\App\SubBooking')->get();
|
||||
return $this->hasMany('\App\SubBooking');
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* IOTYPES *
|
||||
**************************************************************************/
|
||||
|
||||
/*
|
||||
* Get the IOType of the target.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function ioType()
|
||||
{
|
||||
return $this->belongsTo('App\IOType');
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* ACTUALS *
|
||||
**************************************************************************/
|
||||
|
||||
/*
|
||||
* 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->actualsSum() + $this->signedAmount();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user