Files

131 lines
3.1 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;
}
/**
* Get the person target of the booking. If the booking consists of more than
* one sub-booking, null is returned.
*
* @return App\AccountType
*/
public function personTarget()
{
if ($this->isSimple()) {
return $this->subBookings->first()->personTarget;
}
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;
}
/**
* Get the date of the very first booking.
*
* @return string
*/
public static function firstDate() {
return Booking::min('date');
}
/**
* Get the date of the last booking.
*
* @return string
*/
public static function lastDate() {
return Booking::max('date');
}
}