Added this week nd this month and yesterday and today filter logic for hourly chart in production module
This commit is contained in:
@@ -227,19 +227,20 @@ class ModuleFilterDataController extends Controller
|
|||||||
->whereBetween('created_at', [$startDate, $endDate])
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
// return response()->json([
|
|
||||||
// 'status_code' => 'SUCCESS',
|
|
||||||
// 'status_description' => $rowCount
|
|
||||||
// ], 200);
|
|
||||||
$chartName = $request->header('chart-name');
|
$chartName = $request->header('chart-name');
|
||||||
|
|
||||||
if ($chartName == 'Production Hourly Count')
|
if ($chartName == 'Production Hourly Count')
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if ($filterHeader == 'Today' || $filterHeader == 'Yesterday') {
|
||||||
$hourlyCounts = [];
|
$hourlyCounts = [];
|
||||||
|
|
||||||
// Force start at 8:00 AM today
|
$hourStart = now()->startOfDay()->addHours(8); // Today 8:00 AM
|
||||||
$hourStart = now()->startOfDay()->addHours(8); // today 08:00 AM
|
if ($filterHeader == 'Yesterday') {
|
||||||
$hourEndFinal = $hourStart->copy()->addDay(); // next day 08:00 AM
|
$hourStart->subDay(); // Yesterday 8:00 AM
|
||||||
|
}
|
||||||
|
|
||||||
|
$hourEndFinal = $hourStart->copy()->addDay(); // +1 day = 24 hourly slots
|
||||||
|
|
||||||
while ($hourStart < $hourEndFinal) {
|
while ($hourStart < $hourEndFinal) {
|
||||||
$hourEnd = $hourStart->copy()->addHour();
|
$hourEnd = $hourStart->copy()->addHour();
|
||||||
@@ -258,9 +259,77 @@ class ModuleFilterDataController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'SUCCESS',
|
'status_code' => 'SUCCESS',
|
||||||
'status_description' => $hourlyCounts
|
'hourly_data' => $hourlyCounts
|
||||||
], 200);
|
], 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')
|
if ($chartName == 'Production Line Count')
|
||||||
{
|
{
|
||||||
return response()->json([
|
return response()->json([
|
||||||
|
|||||||
Reference in New Issue
Block a user