issue solved in hourly chart patrol

This commit is contained in:
dhanabalan
2025-06-27 08:43:47 +05:30
parent 723d424b88
commit fea6573a7a
2 changed files with 675 additions and 331 deletions

View File

@@ -68,6 +68,7 @@ class GuardPatrolHourlyCount extends Page
$set('time_range', null);
session()->forget('selected_time');
session()->forget('valid_sessions');
// $this->dispatch('patrolEntryChart');
}),
@@ -84,6 +85,7 @@ class GuardPatrolHourlyCount extends Page
session()->forget('selected_name');
$set('time_range', null);
session()->forget('selected_time');
session()->forget('valid_sessions');
// $this->dispatch('patrolEntryChart');
}),
@@ -91,14 +93,16 @@ class GuardPatrolHourlyCount extends Page
->label('Guard Name')
->options(function (callable $get) {
$plantId = $get('plant');
if (!$plantId) {
$Date = $get('date');
if (!$plantId || !$Date) {
return [];
}
return GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId)
->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id')
->select('guard_names.id', 'guard_names.name')
->groupBy('guard_names.id', 'guard_names.name')
->pluck('guard_names.name', 'guard_names.id')
return GuardPatrolEntry::with('guardNames')->where('plant_id', $plantId)
->whereDate('patrol_time', $Date)->orderBy('patrol_time','asc')
->get()
->unique('guard_name_id')
->pluck('guardNames.name', 'guard_name_id')
->toArray();
})
@@ -118,176 +122,131 @@ class GuardPatrolHourlyCount extends Page
// Clear the time range field and its session
$set('time_range', null);
session()->forget('selected_time');
session()->forget('valid_sessions');
}),
// Select::make('time_range')
// ->label('Patrol Time Range')
// ->options(function (callable $get) {
// $plantId = $get('plant');
// $guardName = trim($get('guard_name'));
// $date = $get('date');
// Select::make('time_range')
// ->label('Patrol Time Range')
// ->options(function (callable $get) {
// $plantId = $get('plant');
// $guardId = $get('guard_name');
// $date = $get('date');
// if (!$plantId || !$guardName || !$date) {
// session(['valid_sessions' => []]);
// return [];
// }
// if (!$plantId || !$guardId || !$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']);
// $patrols = GuardPatrolEntry::where('plant_id', $plantId)
// ->where('guard_name_id', $guardId)
// ->whereDate('patrol_time', $date)
// ->orderBy('patrol_time', 'asc')
// ->get(['patrol_time']);
// if ($patrols->isEmpty()) {
// session(['valid_sessions' => []]);
// return [];
// }
// if ($patrols->isEmpty())
// {
// return [];
// }
// $chunkSize = CheckPointTime::where('plant_id', $plantId)->count()+ 1;;
// $chunks = $patrols->chunk($chunkSize);
// $firstCheckpoint = CheckPointName::where('plant_id', $plantId)
// ->orderBy('id')
// ->first(['id', 'name']);
// $lastCheckpoint = CheckPointName::where('plant_id', $plantId)
// ->orderByDesc('id')
// ->first(['id', 'name']);
// $options = [];
// foreach ($chunks as $index => $chunk) {
// $first = $chunk->first();
// $last = $chunk->last();
// $start = Carbon::parse($first->patrol_time)->format('H:i:s');
// $end = Carbon::parse($last->patrol_time)->format('H:i:s');
// $timeRange = "$start - $end";
// $options[$timeRange] = $timeRange;
// }
// $startCheckpointId = $firstCheckpoint->id;
// $endCheckpointId = $lastCheckpoint->id;
// 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');
$guardId = $get('guard_name');
$date = $get('date');
// $firstPatrolCheckpointId = $patrols[0]->check_point_name_id;
// $firstPatrolCheckpointName = CheckPointName::where('id', $firstPatrolCheckpointId)->value('name');
// $firstCheckpointName = $firstCheckpoint->name;
if (!$plantId || !$guardId || !$date) {
session(['valid_sessions' => []]);
return [];
}
// $allSessionsInvalid = ($firstPatrolCheckpointName != $firstCheckpointName);
// Get patrols with checkpoints
$patrols = GuardPatrolEntry::where('plant_id', $plantId)
->where('guard_name_id', $guardId)
->whereDate('patrol_time', $date)
->orderBy('patrol_time', 'asc')
->get(['patrol_time', 'check_point_name_id']);
// $sessions = [];
// $currentSession = [];
if ($patrols->isEmpty()) {
session(['valid_sessions' => []]);
return [];
}
// foreach ($patrols as $patrol) {
// $currentSession[] = $patrol;
// if ($patrol->check_point_name_id == $endCheckpointId) {
// $sessions[] = $currentSession;
// $currentSession = [];
// }
// }
// Get checkpoint sequence
$checkPointTimes = CheckPointTime::where('plant_id', $plantId)
->orderBy('sequence_number')
->get(['check_point1_id', 'check_point2_id']);
// if (!empty($currentSession)) {
// $sessions[] = $currentSession;
// }
// Build expected sequence
$expectedSequence = [];
foreach ($checkPointTimes as $row) {
$expectedSequence[] = $row->check_point1_id;
}
if ($checkPointTimes->isNotEmpty()) {
$expectedSequence[] = $checkPointTimes->last()->check_point2_id;
}
// $options = [];
// $validSessions = [];
$chunkSize = $checkPointTimes->count() + 1;
$chunks = $patrols->chunk($chunkSize);
// 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;
$options = [];
$validSessions = [];
// // Check if session is valid
// $isValid = !$allSessionsInvalid &&
// ($session[0]->check_point_name_id == $startCheckpointId) &&
// (end($session)->check_point_name_id == $endCheckpointId);
// $validSessions[$timeRange] = $isValid;
// }
foreach ($chunks as $index => $chunk) {
$first = $chunk->first();
$last = $chunk->last();
$start = Carbon::parse($first->patrol_time)->format('H:i:s');
$end = Carbon::parse($last->patrol_time)->format('H:i:s');
$timeRange = "$start - $end";
// // Store validSessions in session
// session(['valid_sessions' => $validSessions]);
// Validate sequence for this chunk
$currentSeqIndex = 0;
$isValid = true;
$anyMatch = false;
// 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) {
session(['valid_sessions' => []]);
return [];
foreach ($chunk as $patrol) {
if ($currentSeqIndex < count($expectedSequence) &&
$patrol->check_point_name_id == $expectedSequence[$currentSeqIndex]) {
$currentSeqIndex++;
$anyMatch = true;
} else {
$isValid = false;
}
}
// 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']);
// Consider valid only if at least one checkpoint matches
$isValid = $isValid && $anyMatch;
$validSessions[$timeRange] = $isValid;
$options[$timeRange] = $timeRange;
}
if ($patrols->isEmpty()) {
session(['valid_sessions' => []]);
return [];
}
// Get the first and last sequence for the plant
$firstSequence = CheckPointTime::where('plant_id', $plantId)
->orderBy('sequence_number')
->first(['check_point1_id', 'check_point2_id']);
$lastSequence = CheckPointTime::where('plant_id', $plantId)
->orderByDesc('sequence_number')
->first(['check_point1_id', 'check_point2_id']);
$startCheckpointId = $firstSequence ? $firstSequence->check_point1_id : null;
$endCheckpointId = $lastSequence ? $lastSequence->check_point2_id : null;
$firstCheckpointName = $startCheckpointId ? CheckPointName::where('id', $startCheckpointId)->value('name') : null;
$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]);
}),
session(['valid_sessions' => $validSessions]);
return $options;
})
->reactive()
->required()
->afterStateUpdated(function ($state) {
session(['selected_time' => $state]);
})
])
->columns(4);