Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Added totals to report. Related to #9
Browse files Browse the repository at this point in the history
  • Loading branch information
straube committed Jul 30, 2020
1 parent 4239ad0 commit 07510a7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/Http/Controllers/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Activity;
use App\Project;
use App\Report;
use App\Support\Formatter;
use App\Time;
use App\User;
use Carbon\Carbon;
Expand Down Expand Up @@ -41,12 +42,21 @@ public function index(Request $request): Renderable
{
$reports = Report::select('id', 'name')->orderBy('name')->pluck('name', 'id');
$report = $this->getReportFromRequest($request);
$times = Time::fromReport($report)->with('project', 'activity', 'user')->paginate(50);
$query = Time::fromReport($report);
$times = (clone $query)->with('project', 'activity', 'user')->paginate(50);
$grandTotal = 0;
$totals = (clone $query)->selectActivityTotals()->pluck('total', 'name')->map(function ($total) use (&$grandTotal) {
$grandTotal += $total;
return Formatter::intervalFromSeconds($total);
});
$grandTotal = Formatter::intervalFromSeconds($grandTotal);

$data = [
'reports' => $reports,
'report' => $report,
'times' => $times,
'totals' => $totals,
'grandTotal' => $grandTotal,
];
return view('reports.index', $data);
}
Expand Down
7 changes: 7 additions & 0 deletions app/Support/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class Formatter
{

// TODO: Rename to just `interval`
public static function timeInterval(CarbonInterval $interval, bool $seconds = false)
{
$format = '%I';
Expand All @@ -24,11 +25,17 @@ public static function timeInterval(CarbonInterval $interval, bool $seconds = fa
return str_pad((int) $interval->totalHours, 2, '0', STR_PAD_LEFT) . ':' . $interval->format($format);
}

// TODO: Rename to `intervalFromDates`
public static function timeDiff(Carbon $from, Carbon $to = null, bool $seconds = false)
{
if ($to === null) {
$to = Carbon::now();
}
return self::timeInterval($to->diffAsCarbonInterval($from), $seconds);
}

public static function intervalFromSeconds(int $interval, bool $seconds = false)
{
return self::timeInterval(CarbonInterval::seconds($interval)->cascade(), $seconds);
}
}
12 changes: 12 additions & 0 deletions app/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Support\Formatter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
* Time model.
Expand Down Expand Up @@ -100,4 +101,15 @@ public function scopeFromReport(Builder $query, Report $report): Builder

return $query;
}

public function scopeSelectActivityTotals(Builder $query): Builder
{
return $query->select([
'activities.id',
'activities.name',
DB::raw('SUM(TIMESTAMPDIFF(SECOND, times.started, IFNULL(times.finished, CURRENT_TIMESTAMP))) AS total'),
])
->join('activities', 'times.activity_id', '=', 'activities.id')
->groupBy('activities.id', 'activities.name');
}
}
59 changes: 57 additions & 2 deletions resources/views/reports/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@
</div>
</div>

<h2 class="pt-4 mb-4">{{ $report->name }}</h2>
<div class="row align-items-center">
<div class="col">
<h2 class="pt-4 mb-4">{{ $report->name }}</h2>
</div>
@if (count($totals) > 0)
<div class="col-auto ml-auto">
<button class="btn btn-link" data-toggle="modal" data-target="#report-totals">View totals</button>
</div>
@endif
</div>

@if (count($times) === 0)
<div class="card">
Expand All @@ -56,12 +65,13 @@
@php
$hasMyTime = $times->where('user_id', auth()->user()->id)->count() > 0;
@endphp

<table class="table">
<thead class="sr-only">
<tr>
<th class="align-top">{{ __('Task') }}</th>
<th class="align-top">{{ __('User') }}</th>
<th class="align-top">{{ __('Tracked time') }}</th>
<th class="align-top text-right">{{ __('Tracked time') }}</th>
@if ($hasMyTime)
<th></th>
@endif
Expand Down Expand Up @@ -109,6 +119,51 @@ class="btn btn-link">Edit</a>
@endsection

@push('modals')
@if (count($totals) > 0)
<div class="modal fade" ref="modal" id="report-totals" tabindex="-1" role="dialog" aria-labelledby="report-total-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="report-totals-label">Totals for {{ $report->name }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body pt-0 pb-0">
<table class="table mb-0">
<thead class="sr-only">
<tr>
<th class="align-top">{{ __('Activity') }}</th>
<th class="align-top text-right">{{ __('Tracked time') }}</th>
</tr>
</thead>
<tbody>
@foreach ($totals as $name => $total)
<tr>
<td class="align-middle @if ($loop->first) border-top-0 @endif">{{ $name }}</td>
<td class="align-middle text-right @if ($loop->first) border-top-0 @endif">
<samp>{{ $total }}</samp>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th class="align-middle">{{ __('Total') }}</th>
<th class="align-middle text-right">
<samp>{{ $grandTotal }}</samp>
</th>
</tr>
</tfoot>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
@endif
<create-report></create-report>
<edit-time></edit-time>
@endpush

0 comments on commit 07510a7

Please sign in to comment.