Skip to content

Commit c11c9db

Browse files
committed
Weekly report + minor fixes
1 parent 7ebd46d commit c11c9db

3 files changed

Lines changed: 105 additions & 2 deletions

File tree

app/Filament/Pages/TimesheetDashboard.php

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

55
use App\Filament\Widgets\Timesheet\ActivitiesReport;
66
use App\Filament\Widgets\Timesheet\MonthlyReport;
7+
use App\Filament\Widgets\Timesheet\WeeklyReport;
78
use Filament\Pages\Page;
89

910
class TimesheetDashboard extends Page
@@ -35,7 +36,8 @@ protected function getWidgets(): array
3536
{
3637
return [
3738
MonthlyReport::class,
38-
ActivitiesReport::class
39+
ActivitiesReport::class,
40+
WeeklyReport::class
3941
];
4042
}
4143
}

app/Filament/Widgets/Timesheet/MonthlyReport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function filter(User $user, array $params)
7777
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year']))
7878
)
7979
->where('user_id', $user->id)
80-
->groupBy(DB::raw("DATE_FORMAT (created_at, '%m')"))
80+
->groupBy(DB::raw("DATE_FORMAT(created_at,'%m')"))
8181
->get();
8282
}
8383

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 Carbon\CarbonPeriod;
11+
use Filament\Widgets\BarChartWidget;
12+
use Illuminate\Database\Eloquent\Collection;
13+
use Illuminate\Support\Facades\DB;
14+
15+
class WeeklyReport extends BarChartWidget
16+
{
17+
protected int|string|array $columnSpan = [
18+
'sm' => 1,
19+
'md' => 6,
20+
'lg' => 3
21+
];
22+
23+
protected function getData(): array
24+
{
25+
$now = Carbon::now();
26+
27+
$weekStartDate = $now->startOfWeek()->format('Y-m-d');
28+
$weekEndDate = $now->endOfWeek()->format('Y-m-d');
29+
30+
$collection = $this->filter(auth()->user(), [
31+
'year' => null,
32+
'weekStartDate' => $weekStartDate,
33+
'weekEndDate' => $weekEndDate
34+
]);
35+
36+
$dates = $this->buildDatesRange($weekStartDate, $weekEndDate);
37+
38+
$datasets = $this->buildRapport($collection, $dates);
39+
40+
return [
41+
'datasets' => [
42+
[
43+
'label' => __('Weekly time logged'),
44+
'data' => $datasets,
45+
'backgroundColor' => [
46+
'rgba(54, 162, 235, .6)'
47+
],
48+
'borderColor' => [
49+
'rgba(54, 162, 235, .8)'
50+
],
51+
],
52+
],
53+
'labels' => $dates,
54+
];
55+
}
56+
57+
protected function buildRapport(Collection $collection, array $dates): array
58+
{
59+
$template = $this->createReportTemplate($dates);
60+
foreach ($collection as $item) {
61+
$template[$item->day]['value'] = $item->value;
62+
}
63+
return collect($template)->pluck('value')->toArray();
64+
}
65+
66+
protected function filter(User $user, array $params)
67+
{
68+
return TicketHour::select([
69+
DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d') as day"),
70+
DB::raw('SUM(value) as value'),
71+
])
72+
->whereBetween('created_at', [$params['weekStartDate'], $params['weekEndDate']])
73+
->whereRaw(
74+
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year']))
75+
)
76+
->where('user_id', $user->id)
77+
->groupBy(DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d')"))
78+
->get();
79+
}
80+
81+
protected function buildDatesRange($weekStartDate, $weekEndDate): array
82+
{
83+
$period = CarbonPeriod::create($weekStartDate, $weekEndDate);
84+
85+
$dates = [];
86+
foreach ($period as $item) {
87+
$dates[] = $item->format('Y-m-d');
88+
}
89+
90+
return $dates;
91+
}
92+
93+
protected function createReportTemplate(array $dates): array
94+
{
95+
$template = [];
96+
foreach ($dates as $date) {
97+
$template[$date]['value'] = 0;
98+
}
99+
return $template;
100+
}
101+
}

0 commit comments

Comments
 (0)