From 6f9306d84a584d54aef599fff90a0e4f4ddab0fb Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 18 Nov 2026 11:45:30 +0530 Subject: [PATCH] Changed process order chart logic in widget --- app/Filament/Widgets/ProcessOrderChart.php | 220 ++++++++++++++------- 1 file changed, 145 insertions(+), 75 deletions(-) diff --git a/app/Filament/Widgets/ProcessOrderChart.php b/app/Filament/Widgets/ProcessOrderChart.php index 810f1e3b..90d2accb 100644 --- a/app/Filament/Widgets/ProcessOrderChart.php +++ b/app/Filament/Widgets/ProcessOrderChart.php @@ -15,89 +15,89 @@ class ProcessOrderChart extends ChartWidget protected $listeners = ['processOrderChart']; - protected function getData(): array - { - $activeFilter = $this->filter; + // protected function getData(): array + // { + // $activeFilter = $this->filter; - $selectedPlant = session('selected_plant'); - $selectedLine = session('selected_line'); + // $selectedPlant = session('selected_plant'); + // $selectedLine = session('selected_line'); - // dd($selectedPlant, $selectedLine); + // // dd($selectedPlant, $selectedLine); - if (!$selectedPlant || !$selectedLine) { - return [ - 'datasets' => [], - 'labels' => [], - ]; - } + // if (!$selectedPlant || !$selectedLine) { + // return [ + // 'datasets' => [], + // 'labels' => [], + // ]; + // } - if ($activeFilter == 'yesterday') - { - $startDate = now()->subDay()->setTime(8, 0, 0); - $endDate = now()->setTime(8, 0, 0); - $groupBy = "EXTRACT(HOUR FROM created_at)"; - } - else if ($activeFilter == 'this_week') - { - $startDate = now()->startOfWeek()->setTime(8, 0, 0); - $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); - $groupBy = "EXTRACT(DOW FROM created_at)"; - } - else if ($activeFilter == 'this_month') - { - $startDate = now()->startOfMonth(); - $endDate = now()->endOfMonth(); - $groupBy = "FLOOR((EXTRACT(DAY FROM created_at) - 1) / 7) + 1"; - } - else - { - $startDate = now()->setTime(8, 0, 0); - $endDate = now()->copy()->addDay()->setTime(8, 0, 0); - $groupBy = "EXTRACT(HOUR FROM created_at)"; - } + // if ($activeFilter == 'yesterday') + // { + // $startDate = now()->subDay()->setTime(8, 0, 0); + // $endDate = now()->setTime(8, 0, 0); + // $groupBy = "EXTRACT(HOUR FROM created_at)"; + // } + // else if ($activeFilter == 'this_week') + // { + // $startDate = now()->startOfWeek()->setTime(8, 0, 0); + // $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + // $groupBy = "EXTRACT(DOW FROM created_at)"; + // } + // else if ($activeFilter == 'this_month') + // { + // $startDate = now()->startOfMonth(); + // $endDate = now()->endOfMonth(); + // $groupBy = "FLOOR((EXTRACT(DAY FROM created_at) - 1) / 7) + 1"; + // } + // else + // { + // $startDate = now()->setTime(8, 0, 0); + // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + // $groupBy = "EXTRACT(HOUR FROM created_at)"; + // } - // Get process orders for selected plant and line - $processOrders = DB::table('process_orders') - ->select('process_order') - ->where('plant_id', $selectedPlant) - ->where('line_id', $selectedLine) - ->distinct(); + // // Get process orders for selected plant and line + // $processOrders = DB::table('process_orders') + // ->select('process_order') + // ->where('plant_id', $selectedPlant) + // ->where('line_id', $selectedLine) + // ->distinct(); - // Get OK and Not OK counts against each process order - $records = CharacteristicValue::query() - ->where('plant_id', $selectedPlant) - ->whereIn('process_order', $processOrders) - ->where('created_at', '>=', $startDate) - ->where('created_at', '<', $endDate) - ->selectRaw(" - process_order, - SUM(CASE WHEN status = 'Ok' THEN 1 ELSE 0 END) as ok_count, - SUM(CASE WHEN status = 'NotOk' THEN 1 ELSE 0 END) as not_ok_count - ") - ->groupBy('process_order') - ->orderBy('process_order') - ->get(); + // // Get OK and Not OK counts against each process order + // $records = CharacteristicValue::query() + // ->where('plant_id', $selectedPlant) + // ->whereIn('process_order', $processOrders) + // ->where('created_at', '>=', $startDate) + // ->where('created_at', '<', $endDate) + // ->selectRaw(" + // process_order, + // SUM(CASE WHEN status = 'Ok' THEN 1 ELSE 0 END) as ok_count, + // SUM(CASE WHEN status = 'NotOk' THEN 1 ELSE 0 END) as not_ok_count + // ") + // ->groupBy('process_order') + // ->orderBy('process_order') + // ->get(); - return [ - 'datasets' => [ - [ - 'label' => 'OK', - 'data' => $records->pluck('ok_count') - ->map(fn ($v) => (int) $v) - ->toArray(), - 'backgroundColor' => '#16A34A', - ], - [ - 'label' => 'Not OK', - 'data' => $records->pluck('not_ok_count') - ->map(fn ($v) => (int) $v) - ->toArray(), - 'backgroundColor' => '#DC2626', - ], - ], + // return [ + // 'datasets' => [ + // [ + // 'label' => 'OK', + // 'data' => $records->pluck('ok_count') + // ->map(fn ($v) => (int) $v) + // ->toArray(), + // 'backgroundColor' => '#16A34A', + // ], + // [ + // 'label' => 'Not OK', + // 'data' => $records->pluck('not_ok_count') + // ->map(fn ($v) => (int) $v) + // ->toArray(), + // 'backgroundColor' => '#DC2626', + // ], + // ], - 'labels' => $records->pluck('process_order')->toArray(), - ]; + // 'labels' => $records->pluck('process_order')->toArray(), + // ]; // $records = CharacteristicValue::query() // ->selectRaw(" @@ -125,6 +125,76 @@ class ProcessOrderChart extends ChartWidget // 'labels' => $records->pluck('process_order')->toArray(), // ]; + // } + + protected function getData(): array + { + $activeFilter = $this->filter; + + $selectedPlant = session('selected_plant'); + $selectedLine = session('selected_line'); + + if (!$selectedPlant || !$selectedLine) { + return [ + 'datasets' => [], + 'labels' => [], + ]; + } + + if ($activeFilter == 'yesterday') { + + $startDate = now()->subDay()->setTime(8, 0, 0); + $endDate = now()->setTime(8, 0, 0); + + } else if ($activeFilter == 'this_week') { + + $startDate = now()->startOfWeek()->setTime(8, 0, 0); + $endDate = now()->startOfWeek()->addWeek()->setTime(8, 0, 0); + + } else if ($activeFilter == 'this_month') { + + $startDate = now()->startOfMonth()->setTime(8, 0, 0); + $endDate = now()->addMonthNoOverflow()->startOfMonth()->setTime(8, 0, 0); + + } else { + + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + } + + $records = CharacteristicValue::query() + ->where('plant_id', $selectedPlant) + ->whereIn('process_order', function ($query) use ($selectedPlant, $selectedLine) { + $query->select('process_order') + ->from('process_orders') + ->where('plant_id', $selectedPlant) + ->where('line_id', $selectedLine) + ->distinct(); + }) + ->where('created_at', '>=', $startDate) + ->where('created_at', '<', $endDate) + ->selectRaw(" + SUM(CASE WHEN status = 'Ok' THEN 1 ELSE 0 END) as ok_count, + SUM(CASE WHEN status = 'NotOk' THEN 1 ELSE 0 END) as not_ok_count + ") + ->first(); + + return [ + 'datasets' => [ + [ + 'label' => 'Quantity', + 'data' => [ + (int) ($records->ok_count ?? 0), + (int) ($records->not_ok_count ?? 0), + ], + 'backgroundColor' => [ + '#16A34A', + '#DC2626', + ], + ], + ], + 'labels' => ['OK', 'Not OK'], + ]; } protected function getType(): string