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\Models\CheckPointName;
|
||||
use App\Models\CheckPointTime;
|
||||
use App\Models\GuardPatrolEntry;
|
||||
use App\Models\Plant;
|
||||
use Carbon\Carbon;
|
||||
@@ -50,8 +51,24 @@ class GuardPatrolHourlyCount extends Page
|
||||
->label('Select Plant')
|
||||
->reactive()
|
||||
->required()
|
||||
->afterStateUpdated(function ($state) {
|
||||
session(['selected_plant' => $state]);
|
||||
->afterStateUpdated(function ($state,callable $set) {
|
||||
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');
|
||||
}),
|
||||
DatePicker::make('date')
|
||||
@@ -61,8 +78,13 @@ class GuardPatrolHourlyCount extends Page
|
||||
->required()
|
||||
->beforeOrEqual(now())
|
||||
->default(now()->format('Y-m-d'))
|
||||
->afterStateUpdated(function ($state) {
|
||||
->afterStateUpdated(function ($state,callable $set) {
|
||||
session(['selected_date' => $state]);
|
||||
$set('guard_name', null);
|
||||
session()->forget('selected_name');
|
||||
$set('time_range', null);
|
||||
session()->forget('selected_time');
|
||||
|
||||
// $this->dispatch('patrolEntryChart');
|
||||
}),
|
||||
Select::make('guard_name')
|
||||
@@ -99,61 +121,65 @@ class GuardPatrolHourlyCount extends Page
|
||||
}),
|
||||
|
||||
// Select::make('time_range')
|
||||
// ->label('Patrol Time Range')
|
||||
// ->options(function (callable $get) {
|
||||
// $plantId = $get('plant');
|
||||
// $guardName = trim($get('guard_name'));
|
||||
// $date = $get('date');
|
||||
// ->label('Patrol Time Range')
|
||||
// ->options(function (callable $get) {
|
||||
// $plantId = $get('plant');
|
||||
// $guardName = trim($get('guard_name'));
|
||||
// $date = $get('date');
|
||||
|
||||
// if (!$plantId || !$guardName || !$date) {
|
||||
// return [];
|
||||
// }
|
||||
// if (!$plantId || !$guardName || !$date) {
|
||||
// session(['valid_sessions' => []]);
|
||||
// return [];
|
||||
// }
|
||||
|
||||
// $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']);
|
||||
// // 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()) {
|
||||
// return [];
|
||||
// }
|
||||
// if ($patrols->isEmpty()) {
|
||||
// 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)
|
||||
// ->orderByDesc('id')
|
||||
// ->first(['id', 'name']);
|
||||
|
||||
// $startCheckpointId = $firstCheckpoint->id;
|
||||
// $endCheckpointId = $lastCheckpoint->id;
|
||||
|
||||
// $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 = [];
|
||||
// $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;
|
||||
|
||||
// $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) {
|
||||
// $start = Carbon::parse($session[0]->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;
|
||||
|
||||
// // 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);
|
||||
// $validSessions[$timeRange] = $isValid;
|
||||
// }
|
||||
|
||||
// // Store validSessions in session or somewhere accessible
|
||||
// // Store validSessions in session
|
||||
// session(['valid_sessions' => $validSessions]);
|
||||
|
||||
// return $options;
|
||||
|
||||
// // $options = [];
|
||||
// // 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;
|
||||
// // }
|
||||
// // 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]);
|
||||
// }),
|
||||
|
||||
// })
|
||||
// ->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');
|
||||
->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 [];
|
||||
}
|
||||
|
||||
// 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 = [];
|
||||
if (!$plantId || !$guardName || !$date) {
|
||||
session(['valid_sessions' => []]);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
// Add any remaining patrols as a session (optional)
|
||||
if (!empty($currentSession)) {
|
||||
$sessions[] = $currentSession;
|
||||
}
|
||||
|
||||
$options = [];
|
||||
$validSessions = [];
|
||||
// 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']);
|
||||
|
||||
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;
|
||||
if ($patrols->isEmpty()) {
|
||||
session(['valid_sessions' => []]);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Check if session is valid
|
||||
$isValid = !$allSessionsInvalid && // All sessions are invalid if first patrol is not first checkpoint
|
||||
($session[0]->check_point_name_id == $startCheckpointId) &&
|
||||
(end($session)->check_point_name_id == $endCheckpointId);
|
||||
$validSessions[$timeRange] = $isValid;
|
||||
}
|
||||
// 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']);
|
||||
|
||||
// Store validSessions in session
|
||||
session(['valid_sessions' => $validSessions]);
|
||||
|
||||
return $options;
|
||||
})
|
||||
->reactive()
|
||||
->required()
|
||||
->afterStateUpdated(function ($state) {
|
||||
session(['selected_time' => $state]);
|
||||
}),
|
||||
$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]);
|
||||
}),
|
||||
|
||||
])
|
||||
->columns(4);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user