Optimized (sub-)booking methods and index view for eager loading.
This commit is contained in:
+74
-32
@@ -6,50 +6,92 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
|
|
||||||
class Booking extends Model
|
class Booking extends Model
|
||||||
{
|
{
|
||||||
public function amount()
|
/**************************************************************************
|
||||||
{
|
* DIRECT RELATIONS *
|
||||||
return $this->subBookings()->sum('amount');
|
**************************************************************************/
|
||||||
}
|
|
||||||
|
|
||||||
public function signedAmount()
|
|
||||||
{
|
|
||||||
return $this->subBookings()->withSignedAmount()->get()
|
|
||||||
->sum('signedAmount');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all sub-bookings which belong to the booking.
|
||||||
|
*
|
||||||
|
* @return Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
public function subBookings()
|
public function subBookings()
|
||||||
{
|
{
|
||||||
return $this->hasMany('App\SubBooking');
|
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 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
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function isSimple()
|
public function isSimple()
|
||||||
{
|
{
|
||||||
return $this->sub_bookings_count === 1;
|
return $this->subBookings->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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,10 @@ class BookingsController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index($year)
|
public function index($year)
|
||||||
{
|
{
|
||||||
$bookings = Booking::whereYear('date', '=', $year)
|
$bookings = Booking::with('subBookings.accountType.accountClass.ioType',
|
||||||
|
'subBookings.project')
|
||||||
|
->whereYear('date', '=', $year)
|
||||||
->orderBy('date')
|
->orderBy('date')
|
||||||
->withCount('subBookings')
|
|
||||||
->get()
|
->get()
|
||||||
->groupBy(function($item) {
|
->groupBy(function($item) {
|
||||||
return Carbon::parse($item->date)->format('n');
|
return Carbon::parse($item->date)->format('n');
|
||||||
|
|||||||
+69
-87
@@ -8,9 +8,77 @@ use Carbon\Carbon;
|
|||||||
|
|
||||||
class SubBooking extends Model
|
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()
|
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');
|
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
|
* Join the tables 'i_o_types', 'account_classes' and 'account_types' into
|
||||||
* the query.
|
* the query.
|
||||||
@@ -195,79 +252,4 @@ class SubBooking extends Model
|
|||||||
->join('account_classes', 'account_classes.id', '=', 'account_types.account_class_id')
|
->join('account_classes', 'account_classes.id', '=', 'account_types.account_class_id')
|
||||||
->join('i_o_types', 'i_o_types.id', '=', 'account_classes.io_type_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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,17 +60,18 @@
|
|||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
@if ($booking->isSimple())
|
@if ($booking->isSimple())
|
||||||
{{ $booking->accountType->name }}
|
{{ $booking->accountType()->number . ' - ' .
|
||||||
|
$booking->accountType()->name }}
|
||||||
@endif
|
@endif
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
@if ($booking->isSimple())
|
@if ($booking->isSimple())
|
||||||
{{ $booking->project->name }}
|
{{ $booking->project()->name }}
|
||||||
@endif
|
@endif
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@if (!$booking->isSimple())
|
@if (!$booking->isSimple())
|
||||||
@foreach ($booking->subBookings()->withSignedAmount()->get() as $subBooking)
|
@foreach ($booking->subBookings() as $subBooking)
|
||||||
<tr>
|
<tr>
|
||||||
<td>+</td>
|
<td>+</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
@@ -78,17 +79,18 @@
|
|||||||
<td>{{ $subBooking->reference }}</td>
|
<td>{{ $subBooking->reference }}</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td align='right'>
|
<td align='right'>
|
||||||
@if ($subBooking->signedAmount < 0)
|
@if ($subBooking->signedAmount() < 0)
|
||||||
{{ formatAmount($subBooking->signedAmount) }}
|
{{ formatAmount($subBooking->signedAmount) }}
|
||||||
@endif
|
@endif
|
||||||
</td>
|
</td>
|
||||||
<td align='right'>
|
<td align='right'>
|
||||||
@if ($subBooking->signedAmount >= 0)
|
@if ($subBooking->signedAmount() >= 0)
|
||||||
{{ formatAmount($subBooking->signedAmount) }}
|
{{ formatAmount($subBooking->signedAmount) }}
|
||||||
@endif
|
@endif
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ $subBooking->accountType->name }}
|
{{ $subBooking->accountType->number . ' - ' .
|
||||||
|
$subBooking->accountType->name }}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ $subBooking->project->name }}
|
{{ $subBooking->project->name }}
|
||||||
@@ -120,10 +122,10 @@
|
|||||||
</td>
|
</td>
|
||||||
<td>Saldo</td>
|
<td>Saldo</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td></td>
|
|
||||||
<td>{{ formatAmount($sums[$month]['balance']) }}</td>
|
<td>{{ formatAmount($sums[$month]['balance']) }}</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td></td>
|
<td></td>
|
||||||
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@for ($i = 0; $i < 9; $i++)
|
@for ($i = 0; $i < 9; $i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user