Skip to content

Commit 78d9ac1

Browse files
committed
Timesheet fixes on reports
1 parent 5e2a06e commit 78d9ac1

4 files changed

Lines changed: 47 additions & 19 deletions

File tree

app/Filament/Pages/TimesheetDashboard.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
use App\Filament\Widgets\Timesheet\ActivitiesReport;
66
use App\Filament\Widgets\Timesheet\MonthlyReport;
77
use App\Filament\Widgets\Timesheet\WeeklyReport;
8-
use Filament\Pages\Page;
8+
use Filament\Pages\Dashboard;
99

10-
class TimesheetDashboard extends Page
10+
class TimesheetDashboard extends Dashboard
1111
{
1212
protected static ?string $slug = 'timesheet-dashboard';
1313

1414
protected static ?int $navigationSort = 2;
1515

16-
protected string $view = 'filament::pages.dashboard';
17-
1816
public function getColumns(): int | array
1917
{
2018
return 6;

app/Filament/Widgets/Timesheet/ActivitiesReport.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ class ActivitiesReport extends ChartWidget
1919
'lg' => 3
2020
];
2121

22-
public ?string $filter = '2023';
22+
public ?string $filter = null;
23+
24+
public function mount(): void
25+
{
26+
$this->filter = (string) Carbon::now()->year;
27+
parent::mount();
28+
}
2329

2430
public function getHeading(): string
2531
{
@@ -33,16 +39,23 @@ public function getType(): string
3339

3440
public function getFilters(): ?array
3541
{
36-
return [
37-
2022 => 2022,
38-
2023 => 2023
39-
];
42+
$currentYear = (int) Carbon::now()->year;
43+
$firstYear = (int) (TicketHour::min('created_at')
44+
? Carbon::parse(TicketHour::min('created_at'))->year
45+
: $currentYear);
46+
47+
$years = [];
48+
for ($year = $firstYear; $year <= $currentYear; $year++) {
49+
$years[(string) $year] = (string) $year;
50+
}
51+
52+
return $years;
4053
}
4154

4255
public function getData(): array
4356
{
4457
$collection = $this->filter(auth()->user(), [
45-
'year' => $this->filter
58+
'year' => $this->filter ?: Carbon::now()->year
4659
]);
4760

4861
$datasets = $this->getDatasets($collection);

app/Filament/Widgets/Timesheet/MonthlyReport.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public function getHeading(): string
1818
return __('Logged time monthly');
1919
}
2020

21-
public ?string $filter = '2023';
21+
public ?string $filter = null;
22+
23+
public function mount(): void
24+
{
25+
$this->filter = (string) Carbon::now()->year;
26+
parent::mount();
27+
}
2228

2329
public function getType(): string
2430
{
@@ -52,10 +58,17 @@ public function getData(): array
5258

5359
public function getFilters(): ?array
5460
{
55-
return [
56-
2022 => 2022,
57-
2023 => 2023
58-
];
61+
$currentYear = (int) Carbon::now()->year;
62+
$firstYear = (int) (TicketHour::min('created_at')
63+
? Carbon::parse(TicketHour::min('created_at'))->year
64+
: $currentYear);
65+
66+
$years = [];
67+
for ($year = $firstYear; $year <= $currentYear; $year++) {
68+
$years[(string) $year] = (string) $year;
69+
}
70+
71+
return $years;
5972
}
6073

6174
protected ?array $options = [

app/Filament/Widgets/Timesheet/WeeklyReport.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ class WeeklyReport extends ChartWidget
2020
'lg' => 3
2121
];
2222

23-
public function __construct($id = null)
23+
public function mount(): void
2424
{
25-
$weekDaysData = $this->getWeekStartAndFinishDays();
25+
parent::mount();
2626

27+
$weekDaysData = $this->getWeekStartAndFinishDays();
2728
$this->filter = $weekDaysData['weekStartDate'] . ' - ' . $weekDaysData['weekEndDate'];
28-
29-
parent::__construct($id);
3029
}
3130

3231
public function getHeading(): string
@@ -41,6 +40,11 @@ public function getType(): string
4140

4241
public function getData(): array
4342
{
43+
if (empty($this->filter) || !str_contains($this->filter, ' - ')) {
44+
$weekDefaults = $this->getWeekStartAndFinishDays();
45+
$this->filter = $weekDefaults['weekStartDate'] . ' - ' . $weekDefaults['weekEndDate'];
46+
}
47+
4448
$weekDaysData = explode(' - ', $this->filter);
4549

4650
$collection = $this->filter(auth()->user(), [

0 commit comments

Comments
 (0)