Quality Validation - Dashboard - Production Quantity Validation and Plan Quantity Updation
This commit is contained in:
@@ -12,72 +12,169 @@ class ItemOverview extends ChartWidget
|
||||
|
||||
//protected $listeners = ['filtersUpdated' => '$refresh']; // Listen for filter updates
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$activeFilter = $this->filter;
|
||||
// Get filter values from session
|
||||
$selectedPlant = session('selected_plant');
|
||||
$selectedLine = session('selected_line');
|
||||
// protected function getData(): array
|
||||
// {
|
||||
// $activeFilter = $this->filter;
|
||||
// // Get filter values from session
|
||||
// $selectedPlant = session('selected_plant');
|
||||
// $selectedLine = session('selected_line');
|
||||
|
||||
$query = \DB::table('production_quantities')
|
||||
->join('plants', 'production_quantities.plant_id', '=', 'plants.id') // Join plants table
|
||||
->join('lines', 'production_quantities.line_id', '=', 'lines.id') // Join lines table
|
||||
->selectRaw('EXTRACT(HOUR FROM production_quantities.created_at) AS hour, count(*) AS total_quantity')
|
||||
->whereBetween('production_quantities.created_at', [now()->startOfDay(), now()->endOfDay()])
|
||||
// $query = \DB::table('production_quantities')
|
||||
// ->join('plants', 'production_quantities.plant_id', '=', 'plants.id') // Join plants table
|
||||
// ->join('lines', 'production_quantities.line_id', '=', 'lines.id') // Join lines table
|
||||
// ->selectRaw('EXTRACT(HOUR FROM production_quantities.created_at) AS hour, count(*) AS total_quantity')
|
||||
// ->whereBetween('production_quantities.created_at', [now()->startOfDay(), now()->endOfDay()])
|
||||
// ->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('EXTRACT(HOUR FROM production_quantities.created_at)')
|
||||
// ->orderByRaw('EXTRACT(HOUR FROM production_quantities.created_at)')
|
||||
// ->pluck('total_quantity', 'hour')
|
||||
// ->toArray();
|
||||
|
||||
// $allHours = array_fill(0, 24, 0);
|
||||
// $data = array_replace($allHours, $query);
|
||||
|
||||
// $shiftedKeys = range(8, 23); // 8 AM to 11 PM
|
||||
// $shiftedKeys = array_merge($shiftedKeys, range(0, 8));
|
||||
|
||||
// $orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys);
|
||||
|
||||
// return [
|
||||
// 'datasets' => [
|
||||
// [
|
||||
// 'label' => 'Hourly Production',
|
||||
// 'data' => array_values($orderedData),
|
||||
// 'borderColor' => 'rgba(75, 192, 192, 1)',
|
||||
// 'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
||||
// 'fill' => false,
|
||||
// 'tension' => 0.3,
|
||||
// ],
|
||||
// ],
|
||||
// // Correct label sequence from 8 AM to 7 AM
|
||||
// 'labels' => array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys),
|
||||
// ];
|
||||
|
||||
// }
|
||||
protected function getData(): array
|
||||
{
|
||||
$activeFilter = $this->filter;
|
||||
$selectedPlant = session('selected_plant');
|
||||
$selectedLine = session('selected_line');
|
||||
|
||||
if ($activeFilter === 'yesterday') {
|
||||
$startDate = now()->subDay()->startOfDay();
|
||||
$endDate = now()->subDay()->endOfDay();
|
||||
$groupBy = 'EXTRACT(HOUR FROM production_quantities.created_at)';
|
||||
}
|
||||
else if ($activeFilter === 'this_week') {
|
||||
$startDate = now()->startOfWeek(); // Monday 12:00 AM
|
||||
$endDate = now()->endOfWeek(); // Sunday 11:59 PM
|
||||
$groupBy = 'EXTRACT(DOW FROM production_quantities.created_at)'; // Group by day of week
|
||||
}
|
||||
else if ($activeFilter === 'this_month') {
|
||||
$startDate = now()->startOfMonth();
|
||||
$endDate = now()->endOfMonth();
|
||||
|
||||
// Group records into weeks
|
||||
$groupBy = "FLOOR((EXTRACT(DAY FROM production_quantities.created_at) - 1) / 7) + 1";
|
||||
}
|
||||
else
|
||||
{
|
||||
$startDate = now()->startOfDay();
|
||||
$endDate = now()->endOfDay();
|
||||
$groupBy = 'EXTRACT(HOUR FROM production_quantities.created_at)';
|
||||
}
|
||||
|
||||
$query = \DB::table('production_quantities')
|
||||
->join('plants', 'production_quantities.plant_id', '=', 'plants.id')
|
||||
->join('lines', 'production_quantities.line_id', '=', 'lines.id')
|
||||
->selectRaw("$groupBy AS time_unit, count(*) AS total_quantity")
|
||||
->whereBetween('production_quantities.created_at', [$startDate, $endDate])
|
||||
->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('EXTRACT(HOUR FROM production_quantities.created_at)')
|
||||
->orderByRaw('EXTRACT(HOUR FROM production_quantities.created_at)')
|
||||
->pluck('total_quantity', 'hour')
|
||||
|
||||
->groupByRaw($groupBy)
|
||||
->orderByRaw($groupBy)
|
||||
->pluck('total_quantity', 'time_unit')
|
||||
->toArray();
|
||||
if ($activeFilter === 'this_month') {
|
||||
$weeksCount = ceil($endDate->day / 7); // Calculate total weeks dynamically
|
||||
$allWeeks = array_fill(1, $weeksCount, 0); // Initialize all weeks with 0
|
||||
$data = array_replace($allWeeks, $query); // Fill missing weeks with 0
|
||||
|
||||
// // Ensure all 24 hours are covered, filling missing ones with zero
|
||||
// $data = array_replace(array_fill(8, 24, 0), $query);
|
||||
// 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)";
|
||||
}
|
||||
|
||||
// return [
|
||||
// 'datasets' => [
|
||||
// [
|
||||
// 'label' => 'Hourly Production',
|
||||
// 'data' => array_values($data),
|
||||
// 'borderColor' => 'rgba(75, 192, 192, 1)',
|
||||
// 'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
||||
// 'fill' => false,
|
||||
// 'tension' => 0.3,
|
||||
// ],
|
||||
// ],
|
||||
// // 'labels' => array_map(fn ($hour) => ($hour == 0 ? '12 AM' : ($hour == 12 ? '12 PM' : ($hour < 12 ? "{$hour} AM" : ($hour - 12) . " PM"))), array_keys($data)),
|
||||
// // Labels in 24-hour format
|
||||
// 'labels' => array_map(fn ($hour) => date("g A", strtotime("$hour:00")), array_keys($data)), // Improved hour formatting
|
||||
// ];
|
||||
$orderedData = array_values($data);
|
||||
}
|
||||
else if ($activeFilter === 'this_week') {
|
||||
// Correct week labels: ['Mon', 'Tue', ..., 'Sun']
|
||||
$labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||
|
||||
$allHours = array_fill(0, 24, 0);
|
||||
$data = array_replace($allHours, $query);
|
||||
// Initialize default data for all 7 days
|
||||
$data = array_fill(0, 7, 0);
|
||||
|
||||
$shiftedKeys = range(8, 23); // 8 AM to 11 PM
|
||||
$shiftedKeys = array_merge($shiftedKeys, range(0, 8));
|
||||
// Fill in data from query results
|
||||
foreach ($query as $dow => $count) {
|
||||
$data[$dow] = $count;
|
||||
}
|
||||
|
||||
$orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys);
|
||||
// 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 {
|
||||
// Hourly data (same as before)
|
||||
$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, 8));
|
||||
$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);
|
||||
}
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Hourly Production',
|
||||
'data' => array_values($orderedData),
|
||||
'label' => match ($activeFilter) {
|
||||
'this_week' => "Daily Production This Week",
|
||||
'this_month' => "Weekly Production This Month", // Updated Label
|
||||
'yesterday' => "Yesterday's Hourly Production",
|
||||
default => "Today's Hourly Production",
|
||||
},
|
||||
'data' => $orderedData,
|
||||
'borderColor' => 'rgba(75, 192, 192, 1)',
|
||||
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
||||
'fill' => false,
|
||||
'tension' => 0.3,
|
||||
],
|
||||
],
|
||||
// Correct label sequence from 8 AM to 7 AM
|
||||
'labels' => array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys),
|
||||
'labels' => $labels,
|
||||
];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
@@ -102,6 +199,9 @@ class ItemOverview extends ChartWidget
|
||||
{
|
||||
return [
|
||||
'today' => 'Today',
|
||||
'yesterday' => 'Yesterday',
|
||||
'this_week'=> 'This Week',
|
||||
'this_month'=> 'This Month',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user