Optimized (sub-)booking methods and index view for eager loading.
This commit is contained in:
+69
-87
@@ -8,9 +8,77 @@ use Carbon\Carbon;
|
||||
|
||||
class SubBooking extends Model
|
||||
{
|
||||
/**************************************************************************
|
||||
* DIRECT RELATIONS *
|
||||
**************************************************************************/
|
||||
|
||||
/*
|
||||
* 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 person-target related to the sub-booking.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function personTarget()
|
||||
{
|
||||
return $this->belongsTo('App\PersonTarget', 'person_target_id');
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the project related to the sub-booking.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function project()
|
||||
{
|
||||
return $this->belongsTo('App\Project', 'project_id');
|
||||
}
|
||||
|
||||
/*
|
||||
* 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');
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* INDIRECT PROPERTIES *
|
||||
**************************************************************************/
|
||||
|
||||
/*
|
||||
* Get the booking date for a sub-booking.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function date()
|
||||
{
|
||||
return $this->booking->date;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the IO-type of the sub-booking.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public function ioType()
|
||||
{
|
||||
return $this->accountType->accountClass->ioType;
|
||||
}
|
||||
|
||||
|
||||
public function signedAmount()
|
||||
{
|
||||
return $this->ioType->factor * $this->amount;
|
||||
return $this->ioType()->factor * $this->amount;
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
@@ -172,17 +240,6 @@ class SubBooking extends Model
|
||||
return $query->join('bookings', 'bookings.id', '=', 'sub_bookings.booking_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a signed amount property to the table.
|
||||
*
|
||||
* @return Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeWithSignedAmount($query)
|
||||
{
|
||||
return $query->joinAccountBranch()
|
||||
->addSelect(DB::raw('sub_bookings.*, (i_o_types.factor * sub_bookings.amount) as signedAmount'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Join the tables 'i_o_types', 'account_classes' and 'account_types' into
|
||||
* the query.
|
||||
@@ -195,79 +252,4 @@ class SubBooking extends Model
|
||||
->join('account_classes', 'account_classes.id', '=', 'account_types.account_class_id')
|
||||
->join('i_o_types', 'i_o_types.id', '=', 'account_classes.io_type_id');
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* 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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user