Optimized (sub-)booking methods and index view for eager loading.

This commit is contained in:
Lukas Halbritter
2018-09-30 21:38:25 +02:00
parent 07ec0482a0
commit 3f6fe26ff0
4 changed files with 155 additions and 128 deletions
+69 -87
View File
@@ -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');
}
}