Skip to content

Commit e556943

Browse files
authored
Merge pull request #44 from mihaisolomon/feature/timesheet-dashboard
Timesheet dashboard
2 parents 23a0714 + 647390f commit e556943

3 files changed

Lines changed: 165 additions & 35 deletions

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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
public ?string $filter = '2023';
23+
24+
protected function getHeading(): string
25+
{
26+
return __('Logged time by activity');
27+
}
28+
29+
protected function getFilters(): ?array
30+
{
31+
return [
32+
2022 => 2022,
33+
2023 => 2023
34+
];
35+
}
36+
37+
protected function getData(): array
38+
{
39+
$collection = $this->filter(auth()->user(), [
40+
'year' => $this->filter
41+
]);
42+
43+
$datasets = $this->getDatasets($collection);
44+
45+
return [
46+
'datasets' => [
47+
[
48+
'label' => __('Total time logged'),
49+
'data' => $datasets['sets'],
50+
'backgroundColor' => [
51+
'rgba(54, 162, 235, .6)'
52+
],
53+
'borderColor' => [
54+
'rgba(54, 162, 235, .8)'
55+
],
56+
],
57+
],
58+
'labels' => $datasets['labels'],
59+
];
60+
}
61+
62+
protected function getDatasets(Collection $collection): array
63+
{
64+
$datasets = [
65+
'sets' => [],
66+
'labels' => []
67+
];
68+
69+
foreach ($collection as $item) {
70+
$datasets['sets'][] = $item->value;
71+
$datasets['labels'][] = $item->activity->name;
72+
}
73+
74+
return $datasets;
75+
}
76+
77+
protected function filter(User $user, array $params): Collection
78+
{
79+
return TicketHour::with('activity')
80+
->select([
81+
'activity_id',
82+
DB::raw('SUM(value) as value'),
83+
])
84+
->whereRaw(
85+
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year']))
86+
)
87+
->where('user_id', $user->id)
88+
->groupBy('activity_id')
89+
->get();
90+
}
91+
}

app/Filament/Widgets/Timesheet/MonthlyReport.php

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Models\User;
99
use Carbon\Carbon;
1010
use Filament\Widgets\BarChartWidget;
11+
use Illuminate\Database\Eloquent\Collection;
1112
use Illuminate\Support\Facades\DB;
1213

1314
class MonthlyReport extends BarChartWidget
@@ -17,44 +18,21 @@ protected function getHeading(): string
1718
return __('Logged time monthly');
1819
}
1920

21+
public ?string $filter = '2023';
22+
2023
protected function getData(): array
2124
{
22-
$months = [
23-
1 => ['January', 0],
24-
2 => ['February', 0],
25-
3 => ['March', 0],
26-
4 => ['April', 0],
27-
5 => ['May', 0],
28-
6 => ['June', 0],
29-
7 => ['July', 0],
30-
8 => ['August', 0],
31-
9 => ['September', 0],
32-
10 => ['October', 0],
33-
11 => ['November', 0],
34-
12 => ['December', 0]
35-
];
36-
25+
$collection = $this->filter(auth()->user(), [
26+
'year' => $this->filter
27+
]);
3728

38-
$data = $this->filter(auth()->user());
39-
40-
foreach ($data as $value) {
41-
if (isset($months[(int)$value->month])) {
42-
$months[(int)$value->month][1] = (float)$value->value;
43-
}
44-
}
45-
46-
$datasets = [];
47-
$labels = [];
48-
foreach ($months as $month) {
49-
$datasets[] = $month[1];
50-
$labels[] = $month[0];
51-
}
29+
$datasets = $this->getDatasets($this->buildRapport($collection));
5230

5331
return [
5432
'datasets' => [
5533
[
5634
'label' => __('Total time logged'),
57-
'data' => $datasets,
35+
'data' => $datasets['sets'],
5836
'backgroundColor' => [
5937
'rgba(54, 162, 235, .6)'
6038
],
@@ -63,25 +41,84 @@ protected function getData(): array
6341
],
6442
],
6543
],
66-
'labels' => $labels,
44+
'labels' => $datasets['labels'],
6745
];
6846
}
6947

48+
protected function getFilters(): ?array
49+
{
50+
return [
51+
2022 => 2022,
52+
2023 => 2023
53+
];
54+
}
55+
56+
protected static ?array $options = [
57+
'plugins' => [
58+
'legend' => [
59+
'display' => true,
60+
],
61+
],
62+
];
63+
7064
protected int|string|array $columnSpan = [
7165
'sm' => 1,
7266
'md' => 6,
7367
'lg' => 3
7468
];
7569

76-
protected function filter(User $user)
70+
protected function filter(User $user, array $params)
7771
{
7872
return TicketHour::select([
79-
DB::raw("DATE_FORMAT (created_at, '%m') as month"),
73+
DB::raw("DATE_FORMAT(created_at,'%m') as month"),
8074
DB::raw('SUM(value) as value'),
8175
])
76+
->whereRaw(
77+
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year']))
78+
)
8279
->where('user_id', $user->id)
83-
->whereRaw(DB::raw("YEAR(created_at)=") . Carbon::now()->format('Y'))
8480
->groupBy(DB::raw("DATE_FORMAT (created_at, '%m')"))
8581
->get();
8682
}
83+
84+
protected function getDatasets(array $rapportData): array
85+
{
86+
$datasets = [
87+
'sets' => [],
88+
'labels' => []
89+
];
90+
91+
foreach ($rapportData as $data) {
92+
$datasets['sets'][] = $data[1];
93+
$datasets['labels'][] = $data[0];
94+
}
95+
96+
return $datasets;
97+
}
98+
99+
protected function buildRapport(Collection $collection): array
100+
{
101+
$months = [
102+
1 => ['January', 0],
103+
2 => ['February', 0],
104+
3 => ['March', 0],
105+
4 => ['April', 0],
106+
5 => ['May', 0],
107+
6 => ['June', 0],
108+
7 => ['July', 0],
109+
8 => ['August', 0],
110+
9 => ['September', 0],
111+
10 => ['October', 0],
112+
11 => ['November', 0],
113+
12 => ['December', 0]
114+
];
115+
116+
foreach ($collection as $value) {
117+
if (isset($months[(int)$value->month])) {
118+
$months[(int)$value->month][1] = (float)$value->value;
119+
}
120+
}
121+
122+
return $months;
123+
}
87124
}

0 commit comments

Comments
 (0)