66
77use App \Filament \Resources \Users \UserResource ;
88use App \Filament \Widgets \Components \Stat ;
9- use App \Models \Appointment ;
10- use App \Models \Beneficiary ;
11- use App \Models \Intervention ;
12- use App \Models \User ;
13- use App \Services \StatCount ;
149use Filament \Support \Icons \Heroicon ;
15- use Filament \Widgets \StatsOverviewWidget ;
1610
1711class CoordinatorStatsWidget extends StatsOverviewWidget
1812{
19- protected static ?int $ sort = 0 ;
20-
21- protected ?string $ pollingInterval = null ;
22-
2313 public static function canView (): bool
2414 {
2515 return auth ()->user ()->isCoordinator ();
2616 }
2717
28- public function getHeading (): string
29- {
30- return __ ('dashboard.stats.heading ' );
31- }
32-
3318 protected function getColumns (): int |array
3419 {
3520 return [
@@ -40,71 +25,37 @@ protected function getColumns(): int|array
4025
4126 protected function getStats (): array
4227 {
43- return [
44- $ this ->getAppointmentsStat (),
45- $ this ->getAllNursesStat (),
46- $ this ->getAllBeneficiariesStat (),
47- $ this ->getActiveBeneficiariesStat (),
48- $ this ->getRealizedServicesStat (),
49- ];
50- }
51-
52- private function getAppointmentsStat (): Stat
53- {
54- $ value = Appointment::select (StatCount::comparedBy ('date ' ))
55- ->toBase ()
56- ->first ();
57-
58- return Stat::make (__ ('dashboard.stats.appointments ' ))
59- ->icon (Heroicon::Calendar)
60- ->trend ($ value );
61- }
62-
63- private function getAllNursesStat (): Stat
64- {
65- $ value = User::query ()
66- ->onlyNurses ()
67- ->activatesInCounty (auth ()->user ()->county_id )
68- ->count ();
28+ $ countyId = auth ()->user ()->county_id ;
6929
70- $ url = UserResource::getUrl ('index ' );
30+ $ stats = static ::cache ("coordinator-stats-widget:county: $ countyId " , fn (): array => [
31+ 'appointments ' => static ::appointmentsTrend (),
32+ 'nurses_total ' => static ::allNursesCount ($ countyId ),
33+ 'beneficiaries_total ' => static ::allBeneficiariesCount (),
34+ 'beneficiaries_active ' => static ::activeBeneficiariesCount (),
35+ 'services ' => static ::realizedServicesTrend (),
36+ ]);
7137
72- return Stat::make (__ ('dashboard.stats.nurses_total ' ))
73- ->icon (Heroicon::UserGroup)
74- ->value ($ value )
75- ->url ($ url );
76- }
77-
78- private function getAllBeneficiariesStat (): Stat
79- {
80- $ value = Beneficiary::count ();
81-
82- return Stat::make (__ ('dashboard.stats.beneficiaries_total ' ))
83- ->icon (Heroicon::UserGroup)
84- ->value ($ value );
85- }
86-
87- private function getActiveBeneficiariesStat (): Stat
88- {
89- $ value = Beneficiary::query ()
90- ->onlyActive ()
91- ->count ();
92-
93- return Stat::make (__ ('dashboard.stats.beneficiaries_active ' ))
94- ->icon (Heroicon::Users)
95- ->value ($ value );
96- }
97-
98- private function getRealizedServicesStat (): Stat
99- {
100- $ value = Intervention::select (StatCount::comparedBy ('closed_at ' ))
101- ->onlyIndividualServices ()
102- ->onlyRealized ()
103- ->toBase ()
104- ->first ();
105-
106- return Stat::make (__ ('dashboard.stats.services ' ))
107- ->icon (Heroicon::Bolt)
108- ->trend ($ value );
38+ return [
39+ Stat::make (__ ('dashboard.stats.appointments ' ))
40+ ->icon (Heroicon::Calendar)
41+ ->trend ($ stats ['appointments ' ]),
42+
43+ Stat::make (__ ('dashboard.stats.nurses_total ' ))
44+ ->icon (Heroicon::UserGroup)
45+ ->value ($ stats ['nurses_total ' ])
46+ ->url (UserResource::getUrl ('index ' )),
47+
48+ Stat::make (__ ('dashboard.stats.beneficiaries_total ' ))
49+ ->icon (Heroicon::UserGroup)
50+ ->value ($ stats ['beneficiaries_total ' ]),
51+
52+ Stat::make (__ ('dashboard.stats.beneficiaries_active ' ))
53+ ->icon (Heroicon::Users)
54+ ->value ($ stats ['beneficiaries_active ' ]),
55+
56+ Stat::make (__ ('dashboard.stats.services ' ))
57+ ->icon (Heroicon::Bolt)
58+ ->trend ($ stats ['services ' ]),
59+ ];
10960 }
11061}
0 commit comments