From 3a796857b03796937f1129fa9ea4d4ba1ae6aabc Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 25 Jun 2025 10:34:11 +0530 Subject: [PATCH] Added guard patrol day count chart --- app/Filament/Pages/GuardPatrolDayCount.php | 69 +++++++ app/Filament/Widgets/GuardPatrolDayChart.php | 169 ++++++++++++++++++ database/seeders/PermissionSeeder.php | 1 + .../pages/guard-patrol-day-count.blade.php | 11 ++ 4 files changed, 250 insertions(+) create mode 100644 app/Filament/Pages/GuardPatrolDayCount.php create mode 100644 app/Filament/Widgets/GuardPatrolDayChart.php create mode 100644 resources/views/filament/pages/guard-patrol-day-count.blade.php diff --git a/app/Filament/Pages/GuardPatrolDayCount.php b/app/Filament/Pages/GuardPatrolDayCount.php new file mode 100644 index 0000000..e63464d --- /dev/null +++ b/app/Filament/Pages/GuardPatrolDayCount.php @@ -0,0 +1,69 @@ +forget(['selected_plant']); + $this->filtersForm->fill([ + 'plant' => null + ]); + } + + public function filtersForm(Form $form): Form + { + return $form + ->statePath('filters') + ->schema([ + Select::make('plant') + ->options(Plant::pluck('name', 'id')) + ->label('Select Plant') + ->reactive() + ->afterStateUpdated(function ($state) { + session(['selected_plant' => $state]); + $this->dispatch('patrolEntryChart'); + }), + ]); + + } + + + public static function getNavigationLabel(): string + { + return 'Guard Patrol Day Count'; + } + + public function getHeading(): string + { + return 'Guard Patrol Day Chart'; + } + + public function getWidgets(): array + { + return [ + GuardPatrolDayChart::class, + ]; + } + + public static function canAccess(): bool + { + return Auth::check() && Auth::user()->can('view guard patrol day count dashboard'); + } + +} diff --git a/app/Filament/Widgets/GuardPatrolDayChart.php b/app/Filament/Widgets/GuardPatrolDayChart.php new file mode 100644 index 0000000..26b9e02 --- /dev/null +++ b/app/Filament/Widgets/GuardPatrolDayChart.php @@ -0,0 +1,169 @@ +filter; + + switch ($activeFilter) + { + case 'yesterday': + $startDate = now()->subDay()->startOfDay(); + $endDate = now()->subDay()->endOfDay(); + break; + case 'this_week': + $startDate = now()->startOfWeek(); + $endDate = now()->endOfWeek(); + break; + case 'this_month': + $startDate = now()->startOfMonth(); + $endDate = now()->endOfMonth(); + break; + default: // today + $startDate = now()->startOfDay(); + $endDate = now()->endOfDay(); + break; + } + + + $uniqueGuardNames = GuardPatrolEntry::join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id') + ->where('guard_patrol_entries.plant_id', $selectedPlant) + ->select('guard_names.id', 'guard_names.name') + ->groupBy('guard_names.id', 'guard_names.name') + ->orderBy('guard_names.name') + ->pluck('name') + ->toArray(); + + //dd($uniqueGuardNames); + + $guardPatrols = GuardPatrolEntry::join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id') + ->where('guard_patrol_entries.plant_id', $selectedPlant) + ->whereBetween('guard_patrol_entries.patrol_time', [$startDate, $endDate]) + ->select('guard_names.id', 'guard_names.name', 'guard_patrol_entries.patrol_time') + ->orderBy('guard_names.name') + ->orderBy('guard_patrol_entries.patrol_time') + ->get() + ->groupBy('name'); + + $guardTimeSums = []; + foreach ($guardPatrols as $guardName => $patrols) { + $totalSeconds = 0; + $prevTime = null; + foreach ($patrols as $patrol) + { + $currentTime = Carbon::parse($patrol->patrol_time); + if ($prevTime) + { + $totalSeconds += abs($currentTime->diffInSeconds($prevTime)); + } + $prevTime = $currentTime; + } + //$guardTimeSums[$guardName] = $totalSeconds / 60; + //$guardTimeSums[$guardName] = round($totalSeconds / 60); + $guardTimeSums[$guardName] = [ + 'minutes' => round($totalSeconds / 60), + 'hours' => round($totalSeconds / 3600, 1), + ]; + + } + + $chartData = []; + foreach ($uniqueGuardNames as $guardName) + { + if ($activeFilter === 'today') { + $chartData[] = $guardTimeSums[$guardName]['minutes'] ?? 0; + } + elseif ($activeFilter === 'yesterday') { + $chartData[] = $guardTimeSums[$guardName]['minutes'] ?? 0; + } + else + { + $chartData[] = $guardTimeSums[$guardName]['hours'] ?? 0; + } + } + + return [ + 'labels' => array_values($uniqueGuardNames), + 'datasets' => [ + [ + 'label' => match ($activeFilter) + { + 'yesterday' => "Patrols by Guard (Yesterday) Minutes", + 'this_week' => "Patrols by Guard (This Week) Hours", + 'this_month' => "Patrols by Guard (This Month) Hours", + default => "Patrols by Guard (Today) Minutes", + }, + 'data' => $chartData, + ], + ], + ]; + + } + + protected function getType(): string + { + return 'bar'; + } + + protected function getFilters(): ?array + { + return [ + 'today' => 'Today', + 'yesterday' => 'Yesterday', + 'this_week'=> 'This Week', + 'this_month'=> 'This Month', + ]; + } + + protected function getOptions(): array + { + return [ + 'scales' => [ + 'y' => [ + 'beginAtZero' => true, //Start Y-axis from 0 + 'ticks' => [ + 'stepSize' => 5, + ], + ], + ], + ]; + } + // protected function getOptions(): array + // { + // return [ + // 'scales' => [ + // 'y' => [ + // 'beginAtZero' => true, // Start Y-axis from 0 + // 'ticks' => [ + // 'stepSize' => 5, + // ], + // ], + // ], + // 'plugins' => [ + // 'tooltip' => [ + // 'callbacks' => [ + // 'label' => "function(context) { return context.parsed.y + ' (min)'; }", + // ], + // ], + // ], + // ]; + // } + +} diff --git a/database/seeders/PermissionSeeder.php b/database/seeders/PermissionSeeder.php index ccac8c4..46d178c 100644 --- a/database/seeders/PermissionSeeder.php +++ b/database/seeders/PermissionSeeder.php @@ -130,6 +130,7 @@ class PermissionSeeder extends Seeder Permission::updateOrCreate(['name' => 'view production order count dashboard']); Permission::updateOrCreate(['name' => 'view production line stop count dashboard']); Permission::updateOrCreate(['name' => 'view guard patrol entry dashboard']); + Permission::updateOrCreate(['name' => 'view guard patrol day count dashboard']); } } diff --git a/resources/views/filament/pages/guard-patrol-day-count.blade.php b/resources/views/filament/pages/guard-patrol-day-count.blade.php new file mode 100644 index 0000000..d89e5f5 --- /dev/null +++ b/resources/views/filament/pages/guard-patrol-day-count.blade.php @@ -0,0 +1,11 @@ + +
+ {{-- Filters form --}} + {{ $this->filtersForm($this->form) }} + + {{-- Chart widget --}} + + +
+ +