diff --git a/app/Http/Controllers/ModuleFilterDataController.php b/app/Http/Controllers/ModuleFilterDataController.php index 6bef6bd..f46c20f 100644 --- a/app/Http/Controllers/ModuleFilterDataController.php +++ b/app/Http/Controllers/ModuleFilterDataController.php @@ -227,40 +227,109 @@ class ModuleFilterDataController extends Controller ->whereBetween('created_at', [$startDate, $endDate]) ->count(); - // return response()->json([ - // 'status_code' => 'SUCCESS', - // 'status_description' => $rowCount - // ], 200); $chartName = $request->header('chart-name'); if ($chartName == 'Production Hourly Count') { - $hourlyCounts = []; - // Force start at 8:00 AM today - $hourStart = now()->startOfDay()->addHours(8); // today 08:00 AM - $hourEndFinal = $hourStart->copy()->addDay(); // next day 08:00 AM + if ($filterHeader == 'Today' || $filterHeader == 'Yesterday') { + $hourlyCounts = []; - while ($hourStart < $hourEndFinal) { - $hourEnd = $hourStart->copy()->addHour(); + $hourStart = now()->startOfDay()->addHours(8); // Today 8:00 AM + if ($filterHeader == 'Yesterday') { + $hourStart->subDay(); // Yesterday 8:00 AM + } - $label = $hourStart->format('g:i A') . ' to ' . $hourEnd->format('g:i A'); + $hourEndFinal = $hourStart->copy()->addDay(); // +1 day = 24 hourly slots - $count = ProductionQuantity::where('plant_id', $plant->id) - ->where('line_id', $line->id) - ->whereBetween('created_at', [$hourStart, $hourEnd]) - ->count(); + while ($hourStart < $hourEndFinal) { + $hourEnd = $hourStart->copy()->addHour(); - $hourlyCounts[$label] = $count; + $label = $hourStart->format('g:i A') . ' to ' . $hourEnd->format('g:i A'); - $hourStart = $hourEnd; + $count = ProductionQuantity::where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereBetween('created_at', [$hourStart, $hourEnd]) + ->count(); + + $hourlyCounts[$label] = $count; + + $hourStart = $hourEnd; + } + + return response()->json([ + 'status_code' => 'SUCCESS', + 'hourly_data' => $hourlyCounts + ], 200); } - return response()->json([ - 'status_code' => 'SUCCESS', - 'status_description' => $hourlyCounts - ], 200); + // THIS WEEK: group by weekdays (Mon to Sun) + if ($filterHeader == 'This Week') { + $dowCounts = ProductionQuantity::selectRaw('EXTRACT(DOW FROM created_at) as dow, COUNT(*) as total') + ->where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereBetween('created_at', [$startDate, $endDate]) + ->groupBy('dow') + ->pluck('total', 'dow'); + + $labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; + $data = array_fill(0, 7, 0); + + foreach ($dowCounts as $dow => $count) { + $data[$dow] = $count; + } + + // Reorder from Monday (1) to Sunday (0) + $orderedData = [ + $data[1] ?? 0, + $data[2] ?? 0, + $data[3] ?? 0, + $data[4] ?? 0, + $data[5] ?? 0, + $data[6] ?? 0, + $data[0] ?? 0 + ]; + + return response()->json([ + 'status_code' => 'SUCCESS', + 'weekly_labels' => $labels, + 'weekly_counts' => $orderedData + ], 200); + } + + //THIS MONTH: group by weeks + if ($filterHeader == 'This Month') { + // Count records grouped by week number + $weekCounts = ProductionQuantity::selectRaw("FLOOR((EXTRACT(DAY FROM created_at) - 1) / 7) + 1 as week_number, COUNT(*) as total") + ->where('plant_id', $plant->id) + ->where('line_id', $line->id) + ->whereBetween('created_at', [$startDate, $endDate]) + ->groupBy('week_number') + ->pluck('total', 'week_number'); + + $weeksCount = ceil($endDate->day / 7); // Total number of weeks in month + $allWeeks = array_fill(1, $weeksCount, 0); + $data = array_replace($allWeeks, $weekCounts->toArray()); + + // Prepare labels like Week 1 (01 Jul - 07 Jul) + $labels = []; + for ($i = 1; $i <= $weeksCount; $i++) { + $weekStart = $startDate->copy()->addDays(($i - 1) * 7); + $weekEnd = $startDate->copy()->addDays($i * 7 - 1)->min($endDate); + + $labels[] = "Week $i ({$weekStart->format('d M')} - {$weekEnd->format('d M')})"; + } + + $orderedData = array_values($data); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'monthly_labels' => $labels, + 'monthly_counts' => $orderedData + ], 200); + } } + if ($chartName == 'Production Line Count') { return response()->json([