98 lines
2.4 KiB
PHP
98 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Booking extends Model
|
|
{
|
|
/**************************************************************************
|
|
* DIRECT RELATIONS *
|
|
**************************************************************************/
|
|
|
|
/**
|
|
* Return all sub-bookings which belong to the booking.
|
|
*
|
|
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function subBookings()
|
|
{
|
|
return $this->hasMany('App\SubBooking');
|
|
}
|
|
|
|
/**************************************************************************
|
|
* INDIRECT RELATIONS *
|
|
**************************************************************************/
|
|
|
|
/**
|
|
* Get the project to which the booking belongs. If the booking consists of
|
|
* more than one sub-booking, null is returned.
|
|
*
|
|
* @return App\Project
|
|
*/
|
|
public function project()
|
|
{
|
|
if ($this->isSimple()) {
|
|
return $this->subBookings->first()->project;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get the account type of the booking. If the booking consists of more than
|
|
* one sub-booking, null is returned.
|
|
*
|
|
* @return App\AccountType
|
|
*/
|
|
public function accountType()
|
|
{
|
|
if ($this->isSimple()) {
|
|
return $this->subBookings->first()->accountType;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**************************************************************************
|
|
* INDIRECT PROPERTIES *
|
|
**************************************************************************/
|
|
|
|
/**
|
|
* Get the (unsigned) amount of all sub-bookings.
|
|
*
|
|
* @return float
|
|
*/
|
|
public function amount()
|
|
{
|
|
return $this->subBookings->sum('amount');
|
|
}
|
|
|
|
/**
|
|
* Return the sum of all signed amounts of all sub-bookings related to the
|
|
* booking.
|
|
*
|
|
* @return float
|
|
*/
|
|
public function signedAmount()
|
|
{
|
|
$sum = 0;
|
|
|
|
foreach ($this->subBookings as $subBooking) {
|
|
$sum += $subBooking->signedAmount();
|
|
}
|
|
|
|
return $sum;
|
|
}
|
|
|
|
/**
|
|
* Return true, if the booking consists only of one sub-booking.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isSimple()
|
|
{
|
|
return $this->subBookings->count() === 1;
|
|
}
|
|
}
|