diff --git a/app/AccountClass.php b/app/AccountClass.php index 1aca5a3..18edca4 100644 --- a/app/AccountClass.php +++ b/app/AccountClass.php @@ -6,5 +6,13 @@ use Illuminate\Database\Eloquent\Model; class AccountClass extends Model { - // + /* + * Get the IO-type of the account class. + * + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function ioType() + { + return $this->belongsTo('App\IOType'); + } } diff --git a/app/AccountType.php b/app/AccountType.php index 40238ca..cb3f05e 100644 --- a/app/AccountType.php +++ b/app/AccountType.php @@ -6,5 +6,23 @@ use Illuminate\Database\Eloquent\Model; class AccountType extends Model { - // + /* + * Get the account class of the account type. + * + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function accountClass() + { + return $this->belongsTo('App\AccountClass'); + } + + /* + * Get the IO-type of the account type. + * + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function ioType() + { + return $this->accountClass->ioType(); + } } diff --git a/app/Booking.php b/app/Booking.php index 663e86c..04013cd 100644 --- a/app/Booking.php +++ b/app/Booking.php @@ -6,5 +6,13 @@ use Illuminate\Database\Eloquent\Model; class Booking extends Model { - // + public function amount() + { + return $this->hasMany('App\SubBooking')->amount; + } + + public function signedAmount() + { + return $this->amount; + } } diff --git a/app/PersonTarget.php b/app/PersonTarget.php index aa47ae4..1ae83b8 100644 --- a/app/PersonTarget.php +++ b/app/PersonTarget.php @@ -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(); } } diff --git a/app/SubBooking.php b/app/SubBooking.php index 041f69f..ee87c88 100644 --- a/app/SubBooking.php +++ b/app/SubBooking.php @@ -6,13 +6,78 @@ use Illuminate\Database\Eloquent\Model; class SubBooking extends Model { - /* - * Get the booking date for a sub-booking. - * - * @return string - */ - public function date() - { - return $this->belongsTo('\App\Booking', 'booking_id')->first()->date; - } + /************************************************************************** + * BOOKINGS * + **************************************************************************/ + + /* + * Get the booking which contains the sub-booking. + * + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function booking() { + return $this->belongsTo('\App\Booking', 'booking_id'); + } + + /* + * Get the booking date for a sub-booking. + * + * @return string + */ + public function date() + { + return $this->booking->date; + } + + /************************************************************************** + * ACCOUNT TYPES * + **************************************************************************/ + + /* + * Get the account type related to the sub-booking. + * + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function accountType() + { + return $this->belongsTo('App\AccountType', 'account_type_id'); + } + + /* + * Get the IO-type of the sub-booking. + * + * @return + */ + public function ioType() + { + return $this->accountType->ioType(); + } + + /************************************************************************** + * PERSON TARGETS * + **************************************************************************/ + + /* + * Get the person-target related to the sub-booking. + * + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function personTarget() + { + return $this->belongsTo('App\PersonTarget', 'person_target_id'); + } + + /************************************************************************** + * PROJECTS * + **************************************************************************/ + + /* + * Get the project related to the sub-booking. + * + * @return Illuminate\Database\Eloquent\Relations\belongsTo + */ + public function project() + { + return $this->belongsTo('App\Project', 'project_id'); + } }