Skip to content

Commit 0d7dbfa

Browse files
committed
Updates on weekly time filtering
1 parent 6d74f50 commit 0d7dbfa

1 file changed

Lines changed: 49 additions & 7 deletions

File tree

app/Filament/Widgets/Timesheet/WeeklyReport.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,31 @@ class WeeklyReport extends BarChartWidget
2020
'lg' => 3
2121
];
2222

23-
protected function getData(): array
23+
public function __construct($id = null)
2424
{
25-
$now = Carbon::now();
25+
$weekDaysData = $this->getWeekStartAndFinishDays();
26+
27+
$this->filter = $weekDaysData['weekStartDate'] . ' - ' . $weekDaysData['weekEndDate'];
28+
29+
parent::__construct($id);
30+
}
31+
32+
protected function getHeading(): string
33+
{
34+
return __('Weekly logged time');
35+
}
2636

27-
$weekStartDate = $now->startOfWeek()->format('Y-m-d');
28-
$weekEndDate = $now->endOfWeek()->format('Y-m-d');
37+
protected function getData(): array
38+
{
39+
$weekDaysData = explode(' - ', $this->filter);
2940

3041
$collection = $this->filter(auth()->user(), [
3142
'year' => null,
32-
'weekStartDate' => $weekStartDate,
33-
'weekEndDate' => $weekEndDate
43+
'weekStartDate' => $weekDaysData[0],
44+
'weekEndDate' => $weekDaysData[1]
3445
]);
3546

36-
$dates = $this->buildDatesRange($weekStartDate, $weekEndDate);
47+
$dates = $this->buildDatesRange($weekDaysData[0], $weekDaysData[1]);
3748

3849
$datasets = $this->buildRapport($collection, $dates);
3950

@@ -54,6 +65,11 @@ protected function getData(): array
5465
];
5566
}
5667

68+
protected function getFilters(): ?array
69+
{
70+
return $this->yearWeeks();
71+
}
72+
5773
protected function buildRapport(Collection $collection, array $dates): array
5874
{
5975
$template = $this->createReportTemplate($dates);
@@ -98,4 +114,30 @@ protected function createReportTemplate(array $dates): array
98114
}
99115
return $template;
100116
}
117+
118+
protected function yearWeeks(): array
119+
{
120+
$year = date_create('today')->format('Y');
121+
122+
$dtStart = date_create('2 jan ' . $year)->modify('last Monday');
123+
$dtEnd = date_create('last monday of Dec ' . $year);
124+
125+
for ($weeks = []; $dtStart <= $dtEnd; $dtStart->modify('+1 week')) {
126+
$from = $dtStart->format('Y-m-d');
127+
$to = (clone $dtStart)->modify('+6 Days')->format('Y-m-d');
128+
$weeks[$from . ' - ' . $to] = $from . ' - ' . $to;
129+
}
130+
131+
return $weeks;
132+
}
133+
134+
protected function getWeekStartAndFinishDays(): array
135+
{
136+
$now = Carbon::now();
137+
138+
return [
139+
'weekStartDate' => $now->startOfWeek()->format('Y-m-d'),
140+
'weekEndDate' => $now->endOfWeek()->format('Y-m-d')
141+
];
142+
}
101143
}

0 commit comments

Comments
 (0)