281 lines
9.3 KiB
PHP
281 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Models\CheckPointName;
|
|
use App\Models\CheckPointTime;
|
|
use App\Models\GuardPatrolEntry;
|
|
use Carbon\Carbon;
|
|
use DB;
|
|
use Filament\Widgets\ChartWidget;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class GuardPatrolHourlyChart extends ChartWidget
|
|
{
|
|
protected static ?string $heading = 'Chart';
|
|
|
|
protected static ?string $maxHeight = '330px';
|
|
|
|
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
|
|
{
|
|
$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 [];
|
|
}
|
|
|
|
if (empty($selectedTime) || !isset($validSessions[$selectedTime]) || !$validSessions[$selectedTime])
|
|
{
|
|
return $this->getInvalidSessionChartData();
|
|
}
|
|
|
|
|
|
$query = GuardPatrolEntry::where('plant_id', $selectedPlant)
|
|
->where('guard_name_id', $selectedGuardName)
|
|
->whereDate('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();
|
|
$patrols = $query->orderBy('patrol_time')->get();
|
|
|
|
if ($patrols->count() < 2) {
|
|
return [];
|
|
}
|
|
|
|
// 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');
|
|
|
|
// Get all unique checkpoint IDs from CheckPointTime
|
|
$checkPointIds = collect($cushioningData)
|
|
->flatMap(function ($item) {
|
|
return [$item->check_point1_id, $item->check_point2_id];
|
|
})
|
|
->unique();
|
|
|
|
|
|
$checkPointNames = CheckPointName::whereIn('id', $checkPointIds)
|
|
->pluck('name', 'id');
|
|
|
|
$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++)
|
|
{
|
|
$current = Carbon::parse($patrols[$i]->patrol_time);
|
|
$next = Carbon::parse($patrols[$i+1]->patrol_time);
|
|
$interval = $next->diffInMinutes($current, true);
|
|
$intervals[] = $interval;
|
|
|
|
// if ($next < $current)
|
|
// {
|
|
// $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) {
|
|
$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 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
|
|
{
|
|
return 'bar';
|
|
}
|
|
}
|