Skip to content

Commit 2055286

Browse files
committed
Logged time by activity
1 parent ef9c636 commit 2055286

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

app/Filament/Pages/TimesheetDashboard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\Pages;
44

5+
use App\Filament\Widgets\Timesheet\ActivitiesReport;
56
use App\Filament\Widgets\Timesheet\MonthlyReport;
67
use Filament\Pages\Page;
78

@@ -33,7 +34,8 @@ protected static function getNavigationGroup(): ?string
3334
protected function getWidgets(): array
3435
{
3536
return [
36-
MonthlyReport::class
37+
MonthlyReport::class,
38+
ActivitiesReport::class
3739
];
3840
}
3941
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Filament\Widgets\Timesheet;
6+
7+
use App\Models\TicketHour;
8+
use App\Models\User;
9+
use Carbon\Carbon;
10+
use Filament\Widgets\BarChartWidget;
11+
use Illuminate\Database\Eloquent\Collection;
12+
use Illuminate\Support\Facades\DB;
13+
14+
class ActivitiesReport extends BarChartWidget
15+
{
16+
protected int|string|array $columnSpan = [
17+
'sm' => 1,
18+
'md' => 6,
19+
'lg' => 3
20+
];
21+
22+
protected function getHeading(): string
23+
{
24+
return __('Logged time by activity');
25+
}
26+
27+
protected function getFilters(): ?array
28+
{
29+
return [
30+
2022 => 2022,
31+
2023 => 2023
32+
];
33+
}
34+
35+
protected function getData(): array
36+
{
37+
$collection = $this->filter(auth()->user(), [
38+
'year' => $this->filter
39+
]);
40+
41+
$datasets = $this->getDatasets($collection);
42+
43+
return [
44+
'datasets' => [
45+
[
46+
'label' => __('Total time logged'),
47+
'data' => $datasets['sets'],
48+
'backgroundColor' => [
49+
'rgba(54, 162, 235, .6)'
50+
],
51+
'borderColor' => [
52+
'rgba(54, 162, 235, .8)'
53+
],
54+
],
55+
],
56+
'labels' => $datasets['labels'],
57+
];
58+
}
59+
60+
protected function getDatasets(Collection $collection): array
61+
{
62+
$datasets = [
63+
'sets' => [],
64+
'labels' => []
65+
];
66+
67+
foreach ($collection as $item) {
68+
$datasets['sets'][] = $item->value;
69+
$datasets['labels'][] = $item->activity->name;
70+
}
71+
72+
return $datasets;
73+
}
74+
75+
protected function filter(User $user, array $params): Collection
76+
{
77+
return TicketHour::with('activity')
78+
->select([
79+
'activity_id',
80+
DB::raw('SUM(value) as value'),
81+
])
82+
->whereRaw(
83+
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year']))
84+
)
85+
->where('user_id', $user->id)
86+
->groupBy('activity_id')
87+
->get();
88+
}
89+
}

0 commit comments

Comments
 (0)