solved logic in hourly guard patrol
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
|
|||||||
|
|
||||||
use App\Filament\Widgets\GuardPatrolHourlyChart;
|
use App\Filament\Widgets\GuardPatrolHourlyChart;
|
||||||
use App\Models\CheckPointName;
|
use App\Models\CheckPointName;
|
||||||
|
use App\Models\CheckPointTime;
|
||||||
use App\Models\GuardPatrolEntry;
|
use App\Models\GuardPatrolEntry;
|
||||||
use App\Models\Plant;
|
use App\Models\Plant;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
@@ -50,8 +51,24 @@ class GuardPatrolHourlyCount extends Page
|
|||||||
->label('Select Plant')
|
->label('Select Plant')
|
||||||
->reactive()
|
->reactive()
|
||||||
->required()
|
->required()
|
||||||
->afterStateUpdated(function ($state) {
|
->afterStateUpdated(function ($state,callable $set) {
|
||||||
session(['selected_plant' => $state]);
|
if (is_null($state)) {
|
||||||
|
session()->forget('selected_plant');
|
||||||
|
} else {
|
||||||
|
session(['selected_plant' => $state]);
|
||||||
|
}
|
||||||
|
|
||||||
|
session(['selected_plant' => $state]);
|
||||||
|
|
||||||
|
$set('date', null);
|
||||||
|
session()->forget('selected_date');
|
||||||
|
|
||||||
|
$set('guard_name', null);
|
||||||
|
session()->forget('selected_name');
|
||||||
|
|
||||||
|
$set('time_range', null);
|
||||||
|
session()->forget('selected_time');
|
||||||
|
|
||||||
// $this->dispatch('patrolEntryChart');
|
// $this->dispatch('patrolEntryChart');
|
||||||
}),
|
}),
|
||||||
DatePicker::make('date')
|
DatePicker::make('date')
|
||||||
@@ -61,8 +78,13 @@ class GuardPatrolHourlyCount extends Page
|
|||||||
->required()
|
->required()
|
||||||
->beforeOrEqual(now())
|
->beforeOrEqual(now())
|
||||||
->default(now()->format('Y-m-d'))
|
->default(now()->format('Y-m-d'))
|
||||||
->afterStateUpdated(function ($state) {
|
->afterStateUpdated(function ($state,callable $set) {
|
||||||
session(['selected_date' => $state]);
|
session(['selected_date' => $state]);
|
||||||
|
$set('guard_name', null);
|
||||||
|
session()->forget('selected_name');
|
||||||
|
$set('time_range', null);
|
||||||
|
session()->forget('selected_time');
|
||||||
|
|
||||||
// $this->dispatch('patrolEntryChart');
|
// $this->dispatch('patrolEntryChart');
|
||||||
}),
|
}),
|
||||||
Select::make('guard_name')
|
Select::make('guard_name')
|
||||||
@@ -99,61 +121,65 @@ class GuardPatrolHourlyCount extends Page
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
// Select::make('time_range')
|
// Select::make('time_range')
|
||||||
// ->label('Patrol Time Range')
|
// ->label('Patrol Time Range')
|
||||||
// ->options(function (callable $get) {
|
// ->options(function (callable $get) {
|
||||||
// $plantId = $get('plant');
|
// $plantId = $get('plant');
|
||||||
// $guardName = trim($get('guard_name'));
|
// $guardName = trim($get('guard_name'));
|
||||||
// $date = $get('date');
|
// $date = $get('date');
|
||||||
|
|
||||||
// if (!$plantId || !$guardName || !$date) {
|
// if (!$plantId || !$guardName || !$date) {
|
||||||
// return [];
|
// session(['valid_sessions' => []]);
|
||||||
// }
|
// return [];
|
||||||
|
// }
|
||||||
|
|
||||||
// $patrols = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId)
|
// // Get all patrols for the guard, plant, and date
|
||||||
// ->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id')
|
// $patrols = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId)
|
||||||
// ->where('guard_patrol_entries.guard_name_id', $guardName)
|
// ->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id')
|
||||||
// ->whereDate('guard_patrol_entries.patrol_time', $date)
|
// ->where('guard_patrol_entries.guard_name_id', $guardName)
|
||||||
// ->orderBy('guard_patrol_entries.patrol_time')
|
// ->whereDate('guard_patrol_entries.patrol_time', $date)
|
||||||
// ->get(['guard_patrol_entries.patrol_time', 'guard_patrol_entries.check_point_name_id']);
|
// ->orderBy('guard_patrol_entries.patrol_time')
|
||||||
|
// ->get(['guard_patrol_entries.patrol_time', 'guard_patrol_entries.check_point_name_id']);
|
||||||
|
|
||||||
// if ($patrols->isEmpty()) {
|
// if ($patrols->isEmpty()) {
|
||||||
// return [];
|
// session(['valid_sessions' => []]);
|
||||||
// }
|
// return [];
|
||||||
|
// }
|
||||||
|
|
||||||
// // $startCheckpointId = 1; // "STP BACKSIDE"
|
|
||||||
// // $endCheckpointId = 10; // "D 72 END"
|
|
||||||
// $firstCheckpoint = CheckPointName::where('plant_id', $plantId)
|
|
||||||
// ->orderBy('id')
|
|
||||||
// ->first(['id', 'name']);
|
|
||||||
|
|
||||||
// $lastCheckpoint = CheckPointName::where('plant_id', $plantId)
|
// $firstCheckpoint = CheckPointName::where('plant_id', $plantId)
|
||||||
// ->orderByDesc('id')
|
// ->orderBy('id')
|
||||||
// ->first(['id', 'name']);
|
// ->first(['id', 'name']);
|
||||||
|
// $lastCheckpoint = CheckPointName::where('plant_id', $plantId)
|
||||||
// $startCheckpointId = $firstCheckpoint->id;
|
// ->orderByDesc('id')
|
||||||
// $endCheckpointId = $lastCheckpoint->id;
|
// ->first(['id', 'name']);
|
||||||
|
|
||||||
// $sessions = [];
|
|
||||||
// $currentSession = [];
|
|
||||||
|
|
||||||
// foreach ($patrols as $patrol)
|
|
||||||
// {
|
|
||||||
// $currentSession[] = $patrol;
|
|
||||||
// if ($patrol->check_point_name_id == $endCheckpointId)
|
|
||||||
// {
|
|
||||||
// if (!empty($currentSession) && $currentSession[0]->check_point_name_id == $startCheckpointId) {
|
|
||||||
// $sessions[] = $currentSession;
|
|
||||||
// }
|
|
||||||
// $currentSession = [];
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $options = [];
|
|
||||||
// $validSessions = [];
|
|
||||||
|
|
||||||
// $startCheckpointId = $firstCheckpoint->id;
|
// $startCheckpointId = $firstCheckpoint->id;
|
||||||
// $endCheckpointId = $lastCheckpoint->id;
|
// $endCheckpointId = $lastCheckpoint->id;
|
||||||
|
|
||||||
|
// $firstPatrolCheckpointId = $patrols[0]->check_point_name_id;
|
||||||
|
// $firstPatrolCheckpointName = CheckPointName::where('id', $firstPatrolCheckpointId)->value('name');
|
||||||
|
// $firstCheckpointName = $firstCheckpoint->name;
|
||||||
|
|
||||||
|
// $allSessionsInvalid = ($firstPatrolCheckpointName != $firstCheckpointName);
|
||||||
|
|
||||||
|
// $sessions = [];
|
||||||
|
// $currentSession = [];
|
||||||
|
|
||||||
|
// foreach ($patrols as $patrol) {
|
||||||
|
// $currentSession[] = $patrol;
|
||||||
|
// if ($patrol->check_point_name_id == $endCheckpointId) {
|
||||||
|
// $sessions[] = $currentSession;
|
||||||
|
// $currentSession = [];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (!empty($currentSession)) {
|
||||||
|
// $sessions[] = $currentSession;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $options = [];
|
||||||
|
// $validSessions = [];
|
||||||
|
|
||||||
// foreach ($sessions as $session) {
|
// foreach ($sessions as $session) {
|
||||||
// $start = Carbon::parse($session[0]->patrol_time)->format('H:i:s');
|
// $start = Carbon::parse($session[0]->patrol_time)->format('H:i:s');
|
||||||
// $end = Carbon::parse(end($session)->patrol_time)->format('H:i:s');
|
// $end = Carbon::parse(end($session)->patrol_time)->format('H:i:s');
|
||||||
@@ -161,205 +187,108 @@ class GuardPatrolHourlyCount extends Page
|
|||||||
// $options[$timeRange] = $timeRange;
|
// $options[$timeRange] = $timeRange;
|
||||||
|
|
||||||
// // Check if session is valid
|
// // Check if session is valid
|
||||||
// $isValid = ($session[0]->check_point_name_id == $startCheckpointId) &&
|
// $isValid = !$allSessionsInvalid &&
|
||||||
|
// ($session[0]->check_point_name_id == $startCheckpointId) &&
|
||||||
// (end($session)->check_point_name_id == $endCheckpointId);
|
// (end($session)->check_point_name_id == $endCheckpointId);
|
||||||
// $validSessions[$timeRange] = $isValid;
|
// $validSessions[$timeRange] = $isValid;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// // Store validSessions in session or somewhere accessible
|
// // Store validSessions in session
|
||||||
// session(['valid_sessions' => $validSessions]);
|
// session(['valid_sessions' => $validSessions]);
|
||||||
|
|
||||||
// return $options;
|
// return $options;
|
||||||
|
// })
|
||||||
// // $options = [];
|
// ->reactive()
|
||||||
// // foreach ($sessions as $session)
|
// ->required()
|
||||||
// // {
|
// ->afterStateUpdated(function ($state) {
|
||||||
// // $start = Carbon::parse($session[0]->patrol_time)->format('H:i:s');
|
// session(['selected_time' => $state]);
|
||||||
// // $end = Carbon::parse(end($session)->patrol_time)->format('H:i:s');
|
// }),
|
||||||
// // $timeRange = "$start - $end";
|
|
||||||
// // $options[$timeRange] = $timeRange;
|
|
||||||
// // }
|
|
||||||
// // return $options;
|
|
||||||
// })
|
|
||||||
// ->reactive()
|
|
||||||
// ->required()
|
|
||||||
// ->afterStateUpdated(function ($state) {
|
|
||||||
// session(['selected_time' => $state]);
|
|
||||||
// }),
|
|
||||||
|
|
||||||
// Select::make('time_range')
|
|
||||||
// ->label('Patrol Time Range')
|
|
||||||
// ->options(function (callable $get) {
|
|
||||||
// $plantId = $get('plant');
|
|
||||||
// $guardName = trim($get('guard_name'));
|
|
||||||
// $date = $get('date');
|
|
||||||
|
|
||||||
// if (!$plantId || !$guardName || !$date) {
|
|
||||||
// return [];
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Get all patrols for the guard, plant, and date
|
|
||||||
// $patrols = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId)
|
|
||||||
// ->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id')
|
|
||||||
// ->where('guard_patrol_entries.guard_name_id', $guardName)
|
|
||||||
// ->whereDate('guard_patrol_entries.patrol_time', $date)
|
|
||||||
// ->orderBy('guard_patrol_entries.patrol_time')
|
|
||||||
// ->get(['guard_patrol_entries.patrol_time', 'guard_patrol_entries.check_point_name_id']);
|
|
||||||
|
|
||||||
// if ($patrols->isEmpty()) {
|
|
||||||
// session(['valid_sessions' => []]);
|
|
||||||
// return [];
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Get the name of the first checkpoint in the patrol
|
|
||||||
// $firstPatrolCheckpointId = $patrols[0]->check_point_name_id;
|
|
||||||
// $firstPatrolCheckpointName = CheckPointName::where('id', $firstPatrolCheckpointId)
|
|
||||||
// ->value('name');
|
|
||||||
|
|
||||||
// // Get the name of the first checkpoint in the system (for this plant)
|
|
||||||
// $firstCheckpointName = CheckPointName::where('plant_id', $plantId)
|
|
||||||
// ->orderBy('id')
|
|
||||||
// ->value('name');
|
|
||||||
|
|
||||||
// // If the first patrol checkpoint is not the system's first checkpoint, invalid
|
|
||||||
// if ($firstPatrolCheckpointName != $firstCheckpointName) {
|
|
||||||
// session(['valid_sessions' => []]);
|
|
||||||
// return [];
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Get the last checkpoint in the system (for this plant)
|
|
||||||
// $lastCheckpointName = CheckPointName::where('plant_id', $plantId)
|
|
||||||
// ->orderByDesc('id')
|
|
||||||
// ->value('name');
|
|
||||||
// $lastCheckpointId = CheckPointName::where('plant_id', $plantId)
|
|
||||||
// ->orderByDesc('id')
|
|
||||||
// ->value('id');
|
|
||||||
|
|
||||||
// $sessions = [];
|
|
||||||
// $currentSession = [];
|
|
||||||
|
|
||||||
// foreach ($patrols as $patrol) {
|
|
||||||
// $currentSession[] = $patrol;
|
|
||||||
// if ($patrol->check_point_name_id == $lastCheckpointId) {
|
|
||||||
// if (!empty($currentSession) && $currentSession[0]->check_point_name_id == $firstPatrolCheckpointId) {
|
|
||||||
// $sessions[] = $currentSession;
|
|
||||||
// }
|
|
||||||
// $currentSession = [];
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $options = [];
|
|
||||||
// $validSessions = [];
|
|
||||||
|
|
||||||
// foreach ($sessions as $session) {
|
|
||||||
// $start = Carbon::parse($session[0]->patrol_time)->format('H:i:s');
|
|
||||||
// $end = Carbon::parse(end($session)->patrol_time)->format('H:i:s');
|
|
||||||
// $timeRange = "$start - $end";
|
|
||||||
// $options[$timeRange] = $timeRange;
|
|
||||||
|
|
||||||
// // Check if session is valid (should always be true if the above checks pass)
|
|
||||||
// $isValid = ($session[0]->check_point_name_id == $firstPatrolCheckpointId) &&
|
|
||||||
// (end($session)->check_point_name_id == $lastCheckpointId);
|
|
||||||
// $validSessions[$timeRange] = $isValid;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// session(['valid_sessions' => $validSessions]);
|
|
||||||
// return $options;
|
|
||||||
// })
|
|
||||||
// ->reactive()
|
|
||||||
// ->required()
|
|
||||||
// ->afterStateUpdated(function ($state) {
|
|
||||||
// session(['selected_time' => $state]);
|
|
||||||
// }),
|
|
||||||
|
|
||||||
Select::make('time_range')
|
Select::make('time_range')
|
||||||
->label('Patrol Time Range')
|
->label('Patrol Time Range')
|
||||||
->options(function (callable $get) {
|
->options(function (callable $get) {
|
||||||
$plantId = $get('plant');
|
$plantId = $get('plant');
|
||||||
$guardName = trim($get('guard_name'));
|
$guardName = trim($get('guard_name'));
|
||||||
$date = $get('date');
|
$date = $get('date');
|
||||||
|
|
||||||
if (!$plantId || !$guardName || !$date) {
|
if (!$plantId || !$guardName || !$date) {
|
||||||
session(['valid_sessions' => []]);
|
session(['valid_sessions' => []]);
|
||||||
return [];
|
return [];
|
||||||
}
|
|
||||||
|
|
||||||
// Get all patrols for the guard, plant, and date
|
|
||||||
$patrols = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId)
|
|
||||||
->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id')
|
|
||||||
->where('guard_patrol_entries.guard_name_id', $guardName)
|
|
||||||
->whereDate('guard_patrol_entries.patrol_time', $date)
|
|
||||||
->orderBy('guard_patrol_entries.patrol_time')
|
|
||||||
->get(['guard_patrol_entries.patrol_time', 'guard_patrol_entries.check_point_name_id']);
|
|
||||||
|
|
||||||
if ($patrols->isEmpty()) {
|
|
||||||
session(['valid_sessions' => []]);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the first and last checkpoint for this plant
|
|
||||||
$firstCheckpoint = CheckPointName::where('plant_id', $plantId)
|
|
||||||
->orderBy('id')
|
|
||||||
->first(['id', 'name']);
|
|
||||||
$lastCheckpoint = CheckPointName::where('plant_id', $plantId)
|
|
||||||
->orderByDesc('id')
|
|
||||||
->first(['id', 'name']);
|
|
||||||
|
|
||||||
$startCheckpointId = $firstCheckpoint->id;
|
|
||||||
$endCheckpointId = $lastCheckpoint->id;
|
|
||||||
|
|
||||||
// Optionally: Check if the first patrol is the system's first checkpoint
|
|
||||||
// (If you want to mark all sessions as invalid if the first patrol is not the first checkpoint)
|
|
||||||
$firstPatrolCheckpointId = $patrols[0]->check_point_name_id;
|
|
||||||
$firstPatrolCheckpointName = CheckPointName::where('id', $firstPatrolCheckpointId)->value('name');
|
|
||||||
$firstCheckpointName = $firstCheckpoint->name;
|
|
||||||
|
|
||||||
$allSessionsInvalid = ($firstPatrolCheckpointName != $firstCheckpointName);
|
|
||||||
|
|
||||||
// Build sessions: group patrols between start and end checkpoints
|
|
||||||
// (Or just group all patrols into one session, or as you prefer)
|
|
||||||
// For this example, let's assume you want to group between start and end checkpoints
|
|
||||||
$sessions = [];
|
|
||||||
$currentSession = [];
|
|
||||||
|
|
||||||
foreach ($patrols as $patrol) {
|
|
||||||
$currentSession[] = $patrol;
|
|
||||||
if ($patrol->check_point_name_id == $endCheckpointId) {
|
|
||||||
$sessions[] = $currentSession;
|
|
||||||
$currentSession = [];
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Add any remaining patrols as a session (optional)
|
|
||||||
if (!empty($currentSession)) {
|
|
||||||
$sessions[] = $currentSession;
|
|
||||||
}
|
|
||||||
|
|
||||||
$options = [];
|
// Get all patrols for the guard, plant, and date
|
||||||
$validSessions = [];
|
$patrols = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId)
|
||||||
|
->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id')
|
||||||
|
->where('guard_patrol_entries.guard_name_id', $guardName)
|
||||||
|
->whereDate('guard_patrol_entries.patrol_time', $date)
|
||||||
|
->orderBy('guard_patrol_entries.patrol_time')
|
||||||
|
->get(['guard_patrol_entries.patrol_time', 'guard_patrol_entries.check_point_name_id']);
|
||||||
|
|
||||||
foreach ($sessions as $session) {
|
if ($patrols->isEmpty()) {
|
||||||
$start = Carbon::parse($session[0]->patrol_time)->format('H:i:s');
|
session(['valid_sessions' => []]);
|
||||||
$end = Carbon::parse(end($session)->patrol_time)->format('H:i:s');
|
return [];
|
||||||
$timeRange = "$start - $end";
|
}
|
||||||
$options[$timeRange] = $timeRange;
|
|
||||||
|
|
||||||
// Check if session is valid
|
// Get the first and last sequence for the plant
|
||||||
$isValid = !$allSessionsInvalid && // All sessions are invalid if first patrol is not first checkpoint
|
$firstSequence = CheckPointTime::where('plant_id', $plantId)
|
||||||
($session[0]->check_point_name_id == $startCheckpointId) &&
|
->orderBy('sequence_number')
|
||||||
(end($session)->check_point_name_id == $endCheckpointId);
|
->first(['check_point1_id', 'check_point2_id']);
|
||||||
$validSessions[$timeRange] = $isValid;
|
$lastSequence = CheckPointTime::where('plant_id', $plantId)
|
||||||
}
|
->orderByDesc('sequence_number')
|
||||||
|
->first(['check_point1_id', 'check_point2_id']);
|
||||||
|
|
||||||
// Store validSessions in session
|
|
||||||
session(['valid_sessions' => $validSessions]);
|
|
||||||
|
|
||||||
return $options;
|
$startCheckpointId = $firstSequence ? $firstSequence->check_point1_id : null;
|
||||||
})
|
|
||||||
->reactive()
|
$endCheckpointId = $lastSequence ? $lastSequence->check_point2_id : null;
|
||||||
->required()
|
|
||||||
->afterStateUpdated(function ($state) {
|
$firstCheckpointName = $startCheckpointId ? CheckPointName::where('id', $startCheckpointId)->value('name') : null;
|
||||||
session(['selected_time' => $state]);
|
$lastCheckpointName = $endCheckpointId ? CheckPointName::where('id', $endCheckpointId)->value('name') : null;
|
||||||
}),
|
|
||||||
|
$firstPatrolCheckpointId = $patrols[0]->check_point_name_id;
|
||||||
|
$firstPatrolCheckpointName = CheckPointName::where('id', $firstPatrolCheckpointId)->value('name');
|
||||||
|
|
||||||
|
$allSessionsInvalid = ($firstPatrolCheckpointName != $firstCheckpointName);
|
||||||
|
|
||||||
|
$sessions = [];
|
||||||
|
$currentSession = [];
|
||||||
|
|
||||||
|
foreach ($patrols as $patrol) {
|
||||||
|
$currentSession[] = $patrol;
|
||||||
|
if ($patrol->check_point_name_id == $endCheckpointId) {
|
||||||
|
$sessions[] = $currentSession;
|
||||||
|
$currentSession = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($currentSession)) {
|
||||||
|
$sessions[] = $currentSession;
|
||||||
|
}
|
||||||
|
|
||||||
|
$options = [];
|
||||||
|
$validSessions = [];
|
||||||
|
|
||||||
|
foreach ($sessions as $session) {
|
||||||
|
$start = Carbon::parse($session[0]->patrol_time)->format('H:i:s');
|
||||||
|
$end = Carbon::parse(end($session)->patrol_time)->format('H:i:s');
|
||||||
|
$timeRange = "$start - $end";
|
||||||
|
$options[$timeRange] = $timeRange;
|
||||||
|
|
||||||
|
$isValid = !$allSessionsInvalid &&
|
||||||
|
($session[0]->check_point_name_id == $startCheckpointId) &&
|
||||||
|
(end($session)->check_point_name_id == $endCheckpointId);
|
||||||
|
$validSessions[$timeRange] = $isValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
session(['valid_sessions' => $validSessions]);
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
})
|
||||||
|
->reactive()
|
||||||
|
->required()
|
||||||
|
->afterStateUpdated(function ($state) {
|
||||||
|
session(['selected_time' => $state]);
|
||||||
|
}),
|
||||||
|
|
||||||
])
|
])
|
||||||
->columns(4);
|
->columns(4);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use App\Models\GuardPatrolEntry;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use DB;
|
use DB;
|
||||||
use Filament\Widgets\ChartWidget;
|
use Filament\Widgets\ChartWidget;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class GuardPatrolHourlyChart extends ChartWidget
|
class GuardPatrolHourlyChart extends ChartWidget
|
||||||
{
|
{
|
||||||
@@ -17,6 +18,110 @@ class GuardPatrolHourlyChart extends ChartWidget
|
|||||||
|
|
||||||
protected int|string|array $columnSpan = 12;
|
protected int|string|array $columnSpan = 12;
|
||||||
|
|
||||||
|
// protected function getData(): array
|
||||||
|
// {
|
||||||
|
// $selectedPlant = session('selected_plant');
|
||||||
|
// $selectedDate = session('selected_date');
|
||||||
|
// $selectedGuardName = session('selected_name');
|
||||||
|
// $selectedTime = session('selected_time');
|
||||||
|
// $validSessions = session('valid_sessions', []);
|
||||||
|
|
||||||
|
// if (empty($selectedTime) || !isset($validSessions[$selectedTime]) || !$validSessions[$selectedTime]) {
|
||||||
|
// return [];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (!$selectedPlant || !$selectedDate || !$selectedGuardName || empty($selectedTime)) {
|
||||||
|
// return [];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $query = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $selectedPlant)
|
||||||
|
// ->where('guard_patrol_entries.guard_name_id', $selectedGuardName)
|
||||||
|
// ->whereDate('guard_patrol_entries.patrol_time', $selectedDate);
|
||||||
|
|
||||||
|
// if (!empty($selectedTime))
|
||||||
|
// {
|
||||||
|
// [$startTime, $endTime] = explode(' - ', $selectedTime);
|
||||||
|
// $startDateTime = Carbon::parse($selectedDate . ' ' . $startTime);
|
||||||
|
// $endDateTime = Carbon::parse($selectedDate . ' ' . $endTime);
|
||||||
|
// $query->whereBetween('guard_patrol_entries.patrol_time', [$startDateTime, $endDateTime]);
|
||||||
|
// }
|
||||||
|
// $patrols = $query->orderBy('guard_patrol_entries.patrol_time')->get();
|
||||||
|
|
||||||
|
// if ($patrols->count() < 2)
|
||||||
|
// {
|
||||||
|
// return [];
|
||||||
|
// }
|
||||||
|
// $checkpointNames = CheckPointName::where('plant_id', $selectedPlant)
|
||||||
|
// ->whereIn('id', $patrols->pluck('check_point_name_id')->unique())
|
||||||
|
// ->orderBy('id') // or another column
|
||||||
|
// ->pluck('name', 'id');
|
||||||
|
|
||||||
|
// $nameToSequence = [];
|
||||||
|
// $sequence = 1;
|
||||||
|
// foreach ($checkpointNames as $id => $name)
|
||||||
|
// {
|
||||||
|
// $nameToSequence[$name] = $sequence++;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $sequenceNumbers = array_values($nameToSequence);
|
||||||
|
|
||||||
|
// $cushioningData = CheckPointTime::where('plant_id', $selectedPlant)
|
||||||
|
// ->whereIn('sequence_number', $sequenceNumbers)
|
||||||
|
// ->get(['sequence_number', 'min_cushioning', 'max_cushioning'])
|
||||||
|
// ->keyBy('sequence_number');
|
||||||
|
|
||||||
|
// $numSequences = count($cushioningData);
|
||||||
|
|
||||||
|
|
||||||
|
// $intervals = [];
|
||||||
|
// $labels = [];
|
||||||
|
// $colors = [];
|
||||||
|
|
||||||
|
// for ($i = 0; $i < $patrols->count() - 1; $i++)
|
||||||
|
// {
|
||||||
|
// // Calculate interval
|
||||||
|
// $current = Carbon::parse($patrols[$i]->patrol_time);
|
||||||
|
// $next = Carbon::parse($patrols[$i+1]->patrol_time);
|
||||||
|
// $interval = $next->diffInMinutes($current, true);
|
||||||
|
// $intervals[] = $interval;
|
||||||
|
|
||||||
|
// $labels[] = "Seq " . ($i + 1);
|
||||||
|
// $sequenceNumber = ($i % $numSequences) + 1;
|
||||||
|
// $cushioning = $cushioningData[$sequenceNumber] ?? null;
|
||||||
|
|
||||||
|
// if (!$cushioning) {
|
||||||
|
// $colors[] = '#cccccc';
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $min = $cushioning->min_cushioning;
|
||||||
|
// $max = $cushioning->max_cushioning;
|
||||||
|
|
||||||
|
// if ($interval < $min)
|
||||||
|
// {
|
||||||
|
// $colors[] = '#ffd700';
|
||||||
|
// }
|
||||||
|
// elseif ($interval <= $max) {
|
||||||
|
// $colors[] = '#4caf50';
|
||||||
|
// } else {
|
||||||
|
// $colors[] = '#ff4c4c';
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return [
|
||||||
|
// 'labels' => $labels,
|
||||||
|
// 'datasets' => [
|
||||||
|
// [
|
||||||
|
// 'label' => 'Interval (minutes)',
|
||||||
|
// 'data' => $intervals,
|
||||||
|
// 'backgroundColor' => $colors,
|
||||||
|
// 'borderColor' => '#333',
|
||||||
|
// 'borderWidth' => 1,
|
||||||
|
// ],
|
||||||
|
// ],
|
||||||
|
// ];
|
||||||
|
// }
|
||||||
|
|
||||||
protected function getData(): array
|
protected function getData(): array
|
||||||
{
|
{
|
||||||
$selectedPlant = session('selected_plant');
|
$selectedPlant = session('selected_plant');
|
||||||
@@ -25,69 +130,96 @@ class GuardPatrolHourlyChart extends ChartWidget
|
|||||||
$selectedTime = session('selected_time');
|
$selectedTime = session('selected_time');
|
||||||
$validSessions = session('valid_sessions', []);
|
$validSessions = session('valid_sessions', []);
|
||||||
|
|
||||||
if (empty($selectedTime) || !isset($validSessions[$selectedTime]) || !$validSessions[$selectedTime]) {
|
// if (empty($selectedTime) || !isset($validSessions[$selectedTime]) || !$validSessions[$selectedTime]) {
|
||||||
|
// return [];
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (!$selectedPlant || !$selectedDate || !$selectedGuardName || empty($selectedTime)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$selectedPlant || !$selectedDate || !$selectedGuardName || empty($selectedTime)) {
|
if (empty($selectedTime) || !isset($validSessions[$selectedTime]) || !$validSessions[$selectedTime])
|
||||||
return [];
|
{
|
||||||
}
|
return $this->getInvalidSessionChartData();
|
||||||
|
}
|
||||||
|
|
||||||
$query = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $selectedPlant)
|
|
||||||
->where('guard_patrol_entries.guard_name_id', $selectedGuardName)
|
|
||||||
->whereDate('guard_patrol_entries.patrol_time', $selectedDate);
|
|
||||||
|
|
||||||
if (!empty($selectedTime))
|
$query = GuardPatrolEntry::where('plant_id', $selectedPlant)
|
||||||
{
|
->where('guard_name_id', $selectedGuardName)
|
||||||
[$startTime, $endTime] = explode(' - ', $selectedTime);
|
->whereDate('patrol_time', $selectedDate);
|
||||||
$startDateTime = Carbon::parse($selectedDate . ' ' . $startTime);
|
|
||||||
$endDateTime = Carbon::parse($selectedDate . ' ' . $endTime);
|
|
||||||
$query->whereBetween('guard_patrol_entries.patrol_time', [$startDateTime, $endDateTime]);
|
|
||||||
}
|
|
||||||
$patrols = $query->orderBy('guard_patrol_entries.patrol_time')->get();
|
|
||||||
|
|
||||||
if ($patrols->count() < 2)
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
$checkpointNames = CheckPointName::where('plant_id', $selectedPlant)
|
|
||||||
->whereIn('id', $patrols->pluck('check_point_name_id')->unique())
|
|
||||||
->orderBy('id') // or another column
|
|
||||||
->pluck('name', 'id');
|
|
||||||
|
|
||||||
$nameToSequence = [];
|
if (!empty($selectedTime)) {
|
||||||
$sequence = 1;
|
[$startTime, $endTime] = explode(' - ', $selectedTime);
|
||||||
foreach ($checkpointNames as $id => $name)
|
$startDateTime = Carbon::parse($selectedDate . ' ' . $startTime);
|
||||||
{
|
$endDateTime = Carbon::parse($selectedDate . ' ' . $endTime);
|
||||||
$nameToSequence[$name] = $sequence++;
|
$query->whereBetween('guard_patrol_entries.patrol_time', [$startDateTime, $endDateTime]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$sequenceNumbers = array_values($nameToSequence);
|
// $patrols = $query->orderBy('guard_patrol_entries.patrol_time')->get();
|
||||||
|
$patrols = $query->orderBy('patrol_time')->get();
|
||||||
|
|
||||||
$cushioningData = CheckPointTime::where('plant_id', $selectedPlant)
|
if ($patrols->count() < 2) {
|
||||||
->whereIn('sequence_number', $sequenceNumbers)
|
return [];
|
||||||
->get(['sequence_number', 'min_cushioning', 'max_cushioning'])
|
}
|
||||||
|
|
||||||
|
// Get all CheckPointTime records for the plant
|
||||||
|
$cushioningData = CheckPointTime::where('plant_id', $selectedPlant)
|
||||||
|
->orderBy('sequence_number')
|
||||||
|
->get(['sequence_number', 'min_cushioning', 'max_cushioning', 'check_point1_id', 'check_point2_id'])
|
||||||
->keyBy('sequence_number');
|
->keyBy('sequence_number');
|
||||||
|
|
||||||
$numSequences = count($cushioningData);
|
// Get all unique checkpoint IDs from CheckPointTime
|
||||||
|
$checkPointIds = collect($cushioningData)
|
||||||
|
->flatMap(function ($item) {
|
||||||
|
return [$item->check_point1_id, $item->check_point2_id];
|
||||||
|
})
|
||||||
|
->unique();
|
||||||
|
|
||||||
|
|
||||||
$intervals = [];
|
$checkPointNames = CheckPointName::whereIn('id', $checkPointIds)
|
||||||
$labels = [];
|
->pluck('name', 'id');
|
||||||
$colors = [];
|
|
||||||
|
$sequenceToName = [];
|
||||||
|
//$sequenceNumbers = [];
|
||||||
|
foreach ($cushioningData as $sequenceNumber => $data)
|
||||||
|
{
|
||||||
|
$lastNameId = $data->check_point2_id;
|
||||||
|
$name = $checkPointNames[$lastNameId] ?? null;
|
||||||
|
if ($name)
|
||||||
|
{
|
||||||
|
$sequenceToName[$sequenceNumber] = $name;
|
||||||
|
//$sequenceNumbers[] = $sequenceNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$numSequences = count($sequenceToName);
|
||||||
|
|
||||||
|
// Prepare chart data
|
||||||
|
$intervals = [];
|
||||||
|
$labels = [];
|
||||||
|
$colors = [];
|
||||||
|
|
||||||
for ($i = 0; $i < $patrols->count() - 1; $i++)
|
for ($i = 0; $i < $patrols->count() - 1; $i++)
|
||||||
{
|
{
|
||||||
// Calculate interval
|
|
||||||
$current = Carbon::parse($patrols[$i]->patrol_time);
|
$current = Carbon::parse($patrols[$i]->patrol_time);
|
||||||
$next = Carbon::parse($patrols[$i+1]->patrol_time);
|
$next = Carbon::parse($patrols[$i+1]->patrol_time);
|
||||||
$interval = $next->diffInMinutes($current, true);
|
$interval = $next->diffInMinutes($current, true);
|
||||||
$intervals[] = $interval;
|
$intervals[] = $interval;
|
||||||
|
|
||||||
$labels[] = "Seq " . ($i + 1);
|
// if ($next < $current)
|
||||||
$sequenceNumber = ($i % $numSequences) + 1;
|
// {
|
||||||
$cushioning = $cushioningData[$sequenceNumber] ?? null;
|
// $colors[] = '#ff4c4c'; // Red for out-of-order
|
||||||
|
// // $intervals[] = $interval;
|
||||||
|
// $intervals[] = "X";
|
||||||
|
// $labels[] = "Seq " . ($i + 1);
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
$sequenceNumber = ($i % $numSequences) + 1;
|
||||||
|
$labels[] = "Seq " . ($i + 1);
|
||||||
|
|
||||||
|
$cushioning = $cushioningData[$sequenceNumber] ?? null;
|
||||||
if (!$cushioning) {
|
if (!$cushioning) {
|
||||||
$colors[] = '#cccccc';
|
$colors[] = '#cccccc';
|
||||||
continue;
|
continue;
|
||||||
@@ -105,143 +237,42 @@ class GuardPatrolHourlyChart extends ChartWidget
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'labels' => $labels,
|
||||||
return [
|
'datasets' => [
|
||||||
'labels' => $labels,
|
[
|
||||||
'datasets' => [
|
'label' => 'Interval (minutes)',
|
||||||
[
|
'data' => $intervals,
|
||||||
'label' => 'Interval (minutes)',
|
'backgroundColor' => $colors,
|
||||||
'data' => $intervals,
|
'borderColor' => '#333',
|
||||||
'backgroundColor' => $colors,
|
'borderWidth' => 1,
|
||||||
'borderColor' => '#333',
|
],
|
||||||
'borderWidth' => 1,
|
|
||||||
],
|
],
|
||||||
],
|
];
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
// $colors = [];
|
|
||||||
// $intervals = [];
|
|
||||||
|
|
||||||
// for ($i = 0; $i < $patrols->count() - 1; $i++) {
|
|
||||||
// $current = Carbon::parse($patrols[$i]->patrol_time);
|
|
||||||
// $next = Carbon::parse($patrols[$i+1]->patrol_time);
|
|
||||||
// $interval = $next->diffInMinutes($current, true);
|
|
||||||
// $intervals[] = $interval;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $numSequences = count($sequenceNumbers);
|
|
||||||
|
|
||||||
// for ($i = 0; $i < $patrols->count() - 1; $i++)
|
|
||||||
// {
|
|
||||||
// $sequenceNumber = $sequenceNumbers[$i % $numSequences];
|
|
||||||
// $cushioning = $cushioningData[$sequenceNumber] ?? null;
|
|
||||||
|
|
||||||
// if (!$cushioning) {
|
|
||||||
// $colors[] = '#cccccc';
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $min = $cushioning->min_cushioning;
|
|
||||||
// $max = $cushioning->max_cushioning;
|
|
||||||
// $interval = $intervals[$i];
|
|
||||||
|
|
||||||
// if ($interval < $min) {
|
|
||||||
// $colors[] = '#ffd700';
|
|
||||||
// } elseif ($interval <= $max) {
|
|
||||||
// $colors[] = '#4caf50';
|
|
||||||
// } else {
|
|
||||||
// $colors[] = '#ff4c4c';
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $labels = [];
|
|
||||||
// for ($i = 0; $i < count($intervals); $i++) {
|
|
||||||
// $labels[] = "Seq " . ($i + 1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return [
|
|
||||||
// 'labels' => $labels,
|
|
||||||
// 'datasets' => [
|
|
||||||
// [
|
|
||||||
// 'label' => 'Interval (minutes)',
|
|
||||||
// 'data' => $intervals,
|
|
||||||
// 'backgroundColor' => $colors,
|
|
||||||
// 'borderColor' => '#333',
|
|
||||||
// 'borderWidth' => 1,
|
|
||||||
// ],
|
|
||||||
// ],
|
|
||||||
// ];
|
|
||||||
|
|
||||||
|
|
||||||
// $colors = [];
|
|
||||||
|
|
||||||
// $intervals = [];
|
|
||||||
// for ($i = 0; $i < $patrols->count() - 1; $i++) {
|
|
||||||
// $current = Carbon::parse($patrols[$i]->patrol_time);
|
|
||||||
// $next = Carbon::parse($patrols[$i+1]->patrol_time);
|
|
||||||
// $interval = $next->diffInMinutes($current, true);
|
|
||||||
// $intervals[] = $interval;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for ($i = 0; $i < $patrols->count() - 1; $i++)
|
|
||||||
// {
|
|
||||||
// $checkpointId = $patrols[$i+1]->check_point_name_id;
|
|
||||||
// $name = $checkpointNames[$checkpointId] ?? null;
|
|
||||||
// $sequenceNumber = $nameToSequence[$name] ?? null;
|
|
||||||
|
|
||||||
// if (!$sequenceNumber)
|
|
||||||
// {
|
|
||||||
// $colors[] = '#cccccc';
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $cushioning = $cushioningData[$sequenceNumber] ?? null;
|
|
||||||
|
|
||||||
// if (!$cushioning)
|
|
||||||
// {
|
|
||||||
// $colors[] = '#cccccc';
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $min = $cushioning->min_cushioning;
|
|
||||||
// $max = $cushioning->max_cushioning;
|
|
||||||
// $interval = $intervals[$i];
|
|
||||||
|
|
||||||
// if ($interval < $min)
|
|
||||||
// {
|
|
||||||
// $colors[] = '#ffd700';
|
|
||||||
// } elseif ($interval <= $max)
|
|
||||||
// {
|
|
||||||
// $colors[] = '#4caf50';
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// $colors[] = '#ff4c4c';
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $labels = [];
|
|
||||||
// for ($i = 0; $i < count($intervals); $i++)
|
|
||||||
// {
|
|
||||||
// $labels[] = "Seq " . ($i + 1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return [
|
|
||||||
// 'labels' => $labels,
|
|
||||||
// 'datasets' => [
|
|
||||||
// [
|
|
||||||
// 'label' => 'Interval (minutes)',
|
|
||||||
// 'data' => $intervals,
|
|
||||||
// 'backgroundColor' => $colors,
|
|
||||||
// 'borderColor' => '#333',
|
|
||||||
// 'borderWidth' => 1,
|
|
||||||
// ],
|
|
||||||
// ],
|
|
||||||
// ];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getInvalidSessionChartData(): array
|
||||||
|
{
|
||||||
|
$labels = [];
|
||||||
|
for ($i = 1; $i <= 9; $i++) {
|
||||||
|
$labels[] = "Seq " . $i;
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
'labels' => $labels,
|
||||||
|
'datasets' => [
|
||||||
|
[
|
||||||
|
'label' => 'Interval (X)',
|
||||||
|
'data' => array_fill(0, 9, 0), // <-- Changed to 100
|
||||||
|
'backgroundColor' => array_fill(0, 9, '#ff4c4c'),
|
||||||
|
'borderColor' => '#333',
|
||||||
|
'borderWidth' => 1,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected function getType(): string
|
protected function getType(): string
|
||||||
{
|
{
|
||||||
return 'bar';
|
return 'bar';
|
||||||
|
|||||||
Reference in New Issue
Block a user