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
+74 -32
View File
@@ -6,50 +6,92 @@ use Illuminate\Database\Eloquent\Model;
class Booking extends Model
{
public function amount()
{
return $this->subBookings()->sum('amount');
}
public function signedAmount()
{
return $this->subBookings()->withSignedAmount()->get()
->sum('signedAmount');
}
/**************************************************************************
* 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.
* NOTICE: The book instance has to be loaded with the
* ->withCount('subBookings') method to be able to access the comments
* count.
*
* @return boolean
*/
public function isSimple()
{
return $this->sub_bookings_count === 1;
}
public function project()
{
if ($this->isSimple()) {
return $this->subBookings()->first()->project();
}
return null;
}
public function accountType()
{
if ($this->isSimple()) {
return $this->subBookings()->first()->accountType();
}
return null;
return $this->subBookings->count() === 1;
}
}
+3 -2
View File
@@ -16,9 +16,10 @@ class BookingsController extends Controller
*/
public function index($year)
{
$bookings = Booking::whereYear('date', '=', $year)
$bookings = Booking::with('subBookings.accountType.accountClass.ioType',
'subBookings.project')
->whereYear('date', '=', $year)
->orderBy('date')
->withCount('subBookings')
->get()
->groupBy(function($item) {
return Carbon::parse($item->date)->format('n');
+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');
}
}
+9 -7
View File
@@ -60,17 +60,18 @@
</td>
<td>
@if ($booking->isSimple())
{{ $booking->accountType->name }}
{{ $booking->accountType()->number . ' - ' .
$booking->accountType()->name }}
@endif
</td>
<td>
@if ($booking->isSimple())
{{ $booking->project->name }}
{{ $booking->project()->name }}
@endif
</td>
</tr>
@if (!$booking->isSimple())
@foreach ($booking->subBookings()->withSignedAmount()->get() as $subBooking)
@foreach ($booking->subBookings() as $subBooking)
<tr>
<td>+</td>
<td></td>
@@ -78,17 +79,18 @@
<td>{{ $subBooking->reference }}</td>
<td></td>
<td align='right'>
@if ($subBooking->signedAmount < 0)
@if ($subBooking->signedAmount() < 0)
{{ formatAmount($subBooking->signedAmount) }}
@endif
</td>
<td align='right'>
@if ($subBooking->signedAmount >= 0)
@if ($subBooking->signedAmount() >= 0)
{{ formatAmount($subBooking->signedAmount) }}
@endif
</td>
<td>
{{ $subBooking->accountType->name }}
{{ $subBooking->accountType->number . ' - ' .
$subBooking->accountType->name }}
</td>
<td>
{{ $subBooking->project->name }}
@@ -120,10 +122,10 @@
</td>
<td>Saldo</td>
<td></td>
<td></td>
<td>{{ formatAmount($sums[$month]['balance']) }}</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
@for ($i = 0; $i < 9; $i++)