88use App \Models \User ;
99use Carbon \Carbon ;
1010use Filament \Widgets \BarChartWidget ;
11+ use Illuminate \Database \Eloquent \Collection ;
1112use Illuminate \Support \Facades \DB ;
1213
1314class 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