From eb93a8602fd76d93c68b4cf28ec0c12061424ca0 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 27 Jun 2025 16:28:43 +0530 Subject: [PATCH] solved total count issue in cumulative chart --- app/Filament/Widgets/CumulativeChart.php | 57 ++++++++++++++++++------ 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/app/Filament/Widgets/CumulativeChart.php b/app/Filament/Widgets/CumulativeChart.php index 92e17a3f1..85f516a1d 100644 --- a/app/Filament/Widgets/CumulativeChart.php +++ b/app/Filament/Widgets/CumulativeChart.php @@ -12,7 +12,7 @@ class CumulativeChart extends ChartWidget { protected static ?string $heading = 'Production Line Count'; - protected $listeners = ['cumulativeChart']; + //protected $listeners = ['cumulativeChart']; protected static ?string $maxHeight = '400px'; @@ -85,25 +85,50 @@ class CumulativeChart extends ChartWidget { $selectedPlant = session('selected_plant'); $activeFilter = $this->filter; - // Define date range - switch ($activeFilter) - { + // switch ($activeFilter) + // { + // case 'yesterday': + // $startDate = now()->subDay()->startOfDay(); + // $endDate = now()->subDay()->endOfDay(); + // break; + // case 'this_week': + // $startDate = now()->startOfWeek(); + // $endDate = now()->endOfWeek(); + // break; + // case 'this_month': + // $startDate = now()->startOfMonth(); + // $endDate = now()->endOfMonth(); + // break; + // default: // today + // $startDate = now()->startOfDay(); + // $endDate = now()->endOfDay(); + // break; + // } + // Define date range with 8 AM shift boundaries + switch ($activeFilter) { case 'yesterday': - $startDate = now()->subDay()->startOfDay(); - $endDate = now()->subDay()->endOfDay(); + $startDate = now()->subDay()->setTime(8, 0, 0); + $endDate = now()->setTime(8, 0, 0); + $groupBy = 'EXTRACT(HOUR FROM created_at)'; break; + case 'this_week': - $startDate = now()->startOfWeek(); - $endDate = now()->endOfWeek(); + $startDate = now()->startOfWeek()->setTime(8, 0, 0); + $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + $groupBy = 'EXTRACT(DOW FROM created_at)'; break; + case 'this_month': - $startDate = now()->startOfMonth(); - $endDate = now()->endOfMonth(); + $startDate = now()->startOfMonth()->setTime(8, 0, 0); + $endDate = now()->endOfMonth()->addDay()->setTime(8, 0, 0); + $groupBy = "FLOOR((EXTRACT(DAY FROM created_at) - 1) / 7) + 1"; break; + default: // today - $startDate = now()->startOfDay(); - $endDate = now()->endOfDay(); + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + $groupBy = 'EXTRACT(HOUR FROM created_at)'; break; } @@ -130,12 +155,17 @@ class CumulativeChart extends ChartWidget //FG production from quality_validations $fgProduction = QualityValidation::select('line_id', \DB::raw('COUNT(*) as total_quantity')) - ->whereBetween('created_at', [$startDate, $endDate]) + // ->whereBetween('created_at', [$startDate, $endDate]) + ->where('created_at', '>=', $startDate) + ->where('created_at', '<', $endDate) ->whereIn('line_id', $fgLineIds) ->groupBy('line_id') + ->groupByRaw($groupBy) + ->orderByRaw($groupBy) ->pluck('total_quantity', 'line_id') ->toArray(); + //Non-FG production from production_quantities $nonFgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity')) ->whereBetween('created_at', [$startDate, $endDate]) @@ -248,7 +278,6 @@ class CumulativeChart extends ChartWidget ], ]; } - // public static function canView(): bool // { // return request()->routeIs([