1
0
forked from poc/pds

Added FG Line validations in Hourly Production Count Chart

This commit is contained in:
dhanabalan
2025-05-28 17:22:13 +05:30
parent f641602533
commit 3c70cd0260

View File

@@ -34,129 +34,96 @@ class ItemOverview extends ChartWidget
]; ];
} }
if ($activeFilter === 'yesterday') { // Determine if line is FG Line
$startDate = now()->subDay()->setTime(8, 0, 0); // Yesterday 8:00 AM $line = \App\Models\Line::find($selectedLine);
$endDate = now()->setTime(8, 0, 0); // Today 8:00 AM $isFgLine = $line?->type == 'FG Line';
$groupBy = 'EXTRACT(HOUR FROM production_quantities.created_at)';
}
else if ($activeFilter === 'this_week') { // Set date range and groupBy logic
// Monday 8:00 AM of the current week if ($activeFilter == 'yesterday')
{
$startDate = now()->subDay()->setTime(8, 0, 0);
$endDate = now()->setTime(8, 0, 0);
$groupBy = 'EXTRACT(HOUR FROM created_at)';
}
elseif ($activeFilter == 'this_week')
{
$startDate = now()->startOfWeek()->setTime(8, 0, 0); $startDate = now()->startOfWeek()->setTime(8, 0, 0);
$endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
$groupBy = 'EXTRACT(DOW FROM created_at)';
$groupBy = 'EXTRACT(DOW FROM production_quantities.created_at)'; // Group by day of week
} }
else if ($activeFilter === 'this_month') { elseif ($activeFilter == 'this_month')
{
$startDate = now()->startOfMonth(); $startDate = now()->startOfMonth();
$endDate = now()->endOfMonth(); $endDate = now()->endOfMonth();
$groupBy = "FLOOR((EXTRACT(DAY FROM production_quantities.created_at) - 1) / 7) + 1"; $groupBy = "FLOOR((EXTRACT(DAY FROM created_at) - 1) / 7) + 1";
} }
else else
{ {
$startDate = now()->setTime(8, 0, 0); // today at 8:00 AM $startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0); // tomorrow at 8:00 AM $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
$groupBy = 'EXTRACT(HOUR FROM production_quantities.created_at)'; $groupBy = 'EXTRACT(HOUR FROM created_at)';
} }
$query = \DB::table('production_quantities') $baseTable = $isFgLine ? 'quality_validations' : 'production_quantities';
->join('plants', 'production_quantities.plant_id', '=', 'plants.id') //inner join
->join('lines', 'production_quantities.line_id', '=', 'lines.id')
->selectRaw("$groupBy AS time_unit, count(*) AS total_quantity")
//->whereBetween('production_quantities.created_at', [$startDate, $endDate])
->where('production_quantities.created_at', '>=', $startDate) // $startDate should be >=
->where('production_quantities.created_at', '<', $endDate) // $endDate should be <
->when($selectedPlant, function ($q) use ($selectedPlant) {
return $q->where('plants.id', $selectedPlant);
})
->when($selectedLine, function ($q) use ($selectedLine) {
return $q->where('lines.id', $selectedLine);
})
->groupByRaw($groupBy) $query = \DB::table($baseTable)
->orderByRaw($groupBy) ->selectRaw("$groupBy AS time_unit, COUNT(*) AS total_quantity")
->pluck('total_quantity', 'time_unit') ->where('created_at', '>=', $startDate)
->toArray(); ->where('created_at', '<', $endDate)
->where('plant_id', $selectedPlant)
->where('line_id', $selectedLine)
->groupByRaw($groupBy)
->orderByRaw($groupBy)
->pluck('total_quantity', 'time_unit')
->toArray();
if ($activeFilter == 'this_month')
{
$weeksCount = ceil($endDate->day / 7);
$allWeeks = array_fill(1, $weeksCount, 0);
$data = array_replace($allWeeks, $query);
if ($activeFilter === 'this_month') { $labels = [];
$weeksCount = ceil($endDate->day / 7); // Calculate total weeks dynamically for ($i = 1; $i <= $weeksCount; $i++) {
$allWeeks = array_fill(1, $weeksCount, 0); // Initialize all weeks with 0 $weekStart = $startDate->copy()->addDays(($i - 1) * 7)->format('d M');
$data = array_replace($allWeeks, $query); // Fill missing weeks with 0 $weekEnd = $startDate->copy()->addDays($i * 7 - 1)->min($endDate)->format('d M');
$labels[] = "Week $i ($weekStart - $weekEnd)";
// Generate dynamic week labels
$labels = [];
for ($i = 1; $i <= $weeksCount; $i++) {
$weekStart = $startDate->copy()->addDays(($i - 1) * 7)->format('d M');
$weekEnd = $startDate->copy()->addDays($i * 7 - 1)->min($endDate)->format('d M');
$labels[] = "Week $i ($weekStart - $weekEnd)";
}
$orderedData = array_values($data);
} }
else if ($activeFilter === 'this_week') {
// Correct week labels: ['Mon', 'Tue', ..., 'Sun']
$labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
// Initialize default data for all 7 days $orderedData = array_values($data);
$data = array_fill(0, 7, 0); }
elseif ($activeFilter == 'this_week')
// Fill in data from query results {
foreach ($query as $dow => $count) { $labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
$data[$dow] = $count; $data = array_fill(0, 7, 0);
} foreach ($query as $dow => $count) {
$data[$dow] = $count;
// Ensure days are ordered from Monday to Sunday
$orderedData = [
$data[1] ?? 0, // Monday
$data[2] ?? 0, // Tuesday
$data[3] ?? 0, // Wednesday
$data[4] ?? 0, // Thursday
$data[5] ?? 0, // Friday
$data[6] ?? 0, // Saturday
$data[0] ?? 0, // Sunday (move to last)
];
} }
else if($activeFilter === 'yesterday') $orderedData = [
{ $data[1] ?? 0, $data[2] ?? 0, $data[3] ?? 0,
$data[4] ?? 0, $data[5] ?? 0, $data[6] ?? 0,
// Hourly data (same as before) $data[0] ?? 0,
$allHours = array_fill(0, 24, 0); ];
$data = array_replace($allHours, $query);
// Shift hours for proper display (8 AM to 7 AM)
$shiftedKeys = array_merge(range(8, 23), range(0, 7));
$orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys);
// Labels: ["8 AM", "9 AM", ..., "7 AM"]
$labels = array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys);
} }
else else
{ {
// Hourly data (same as before)
$allHours = array_fill(0, 24, 0); $allHours = array_fill(0, 24, 0);
$data = array_replace($allHours, $query); $data = array_replace($allHours, $query);
// Shift hours for proper display (8 AM to 7 AM)
$shiftedKeys = array_merge(range(8, 23), range(0, 7)); $shiftedKeys = array_merge(range(8, 23), range(0, 7));
$orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys); $orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys);
// Labels: ["8 AM", "9 AM", ..., "7 AM"]
$labels = array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys); $labels = array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys);
} }
return [ return [
'datasets' => [ 'datasets' => [
[ [
'label' => match ($activeFilter) { 'label' => match ($activeFilter) {
'this_week' => "Daily Production This Week", 'this_week' => $isFgLine ? 'Daily FG Count This Week' : 'Daily Production This Week',
'this_month' => "Weekly Production This Month", // Updated Label 'this_month' => $isFgLine ? 'Weekly FG Count This Month' : 'Weekly Production This Month',
'yesterday' => "Yesterday's Hourly Production", 'yesterday' => $isFgLine ? "Yesterday's FG Count" : "Yesterday's Hourly Production",
default => "Today's Hourly Production", default => $isFgLine ? "Today's FG Count" : "Today's Hourly Production",
}, },
'data' => $orderedData, 'data' => $orderedData,
'interaction' => [ 'interaction' => [
@@ -195,32 +162,6 @@ class ItemOverview extends ChartWidget
} }
// protected function getOptions(): array
// {
// return [
// 'plugins' => [
// 'datalabels' => [
// 'color' => '#3490dc',
// 'font' => [
// 'weight' => 'bold',
// 'size' => 14,
// ],
// 'backgroundColor' => 'rgba(255,255,255,0.8)',
// 'borderRadius' => 4,
// 'formatter' => \Illuminate\Support\Js::from(
// new Js('function(value, context) {
// if (Number(value) === 0) {
// return null;
// }
// return "Count: " + value;
// }')
// ),
// ]
// ]
// ];
// }
protected function getFilters(): ?array protected function getFilters(): ?array
{ {
return [ return [