diff --git a/app/Filament/Widgets/InvoiceChart.php b/app/Filament/Widgets/InvoiceChart.php index 71f17b2..9f3cab4 100644 --- a/app/Filament/Widgets/InvoiceChart.php +++ b/app/Filament/Widgets/InvoiceChart.php @@ -491,53 +491,61 @@ class InvoiceChart extends ChartWidget } elseif ($activeFilter == 'this_month') { $startOfMonth = now()->startOfMonth()->setTime(8, 0, 0); - $endOfMonth = now()->endOfMonth()->addDay()->setTime(8, 0, 0); // include last day till 8 AM next day + $endOfMonth = now()->endOfMonth()->addDay()->setTime(8, 0, 0); // include full last day $monthName = $startOfMonth->format('M'); + // Prepare weekly labels like "May(1-7)", "May(8-14)", etc. $labels = []; + $weekStart = $startOfMonth->copy(); $importedInvoicesPerWeek = []; $completedInvoicesPerWeek = []; $weekIndex = 0; - $weekStart = $startOfMonth; - while ($weekStart < $endOfMonth) { $weekEnd = $weekStart->copy()->addDays(7); - if ($weekEnd > $endOfMonth) { - $weekEnd = $endOfMonth; - } + $startDay = $weekStart->format('j'); + $weekEndLimit = $weekEnd->copy()->subDay(); + $actualEnd = $weekEndLimit->greaterThan($endOfMonth) ? $endOfMonth : $weekEndLimit; + $endDay = $actualEnd->format('j'); - $startDay = $weekStart->day; - $endDay = $weekEnd->copy()->subDay()->day; $labels[] = "{$monthName}({$startDay}-{$endDay})"; - $queryImported = DB::table('invoice_validations') - ->where('plant_id', $selectedPlant) + $queryImported = InvoiceValidation::where('plant_id', $selectedPlant) ->whereBetween('created_at', [$weekStart, $weekEnd]); + if ($selectedInvoice == 'individual_material') { + $queryImported->where('quantity', 1); + } elseif ($selectedInvoice == 'serial_invoice') { + $queryImported->whereNull('quantity'); + } elseif ($selectedInvoice == 'bundle_material') { + $queryImported->where('quantity', '>', 1); + } + + $importedInvoicesPerWeek[$weekIndex] = $queryImported->distinct('invoice_number')->count('invoice_number'); + + // --- Completed --- $queryCompleted = DB::table('invoice_validations') + ->select('invoice_number') ->where('plant_id', $selectedPlant) ->whereBetween('updated_at', [$weekStart, $weekEnd]); if ($selectedInvoice == 'individual_material') { - $queryImported->where('quantity', 1); $queryCompleted->where('quantity', 1) - ->whereNotNull('serial_number') - ->where('serial_number', '!=', ''); + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); } elseif ($selectedInvoice == 'serial_invoice') { - $queryImported->whereNull('quantity'); $queryCompleted->whereNull('quantity') - ->where('scanned_status', 'Scanned'); + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"); } elseif ($selectedInvoice == 'bundle_material') { - $queryImported->where('quantity', '>', 1); $queryCompleted->where('quantity', '>', 1) - ->whereNotNull('serial_number') - ->where('serial_number', '!=', ''); + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); } - $importedInvoicesPerWeek[$weekIndex] = $queryImported->distinct('invoice_number')->count('invoice_number'); - $completedInvoicesPerWeek[$weekIndex] = $queryCompleted->distinct('invoice_number')->count('invoice_number'); + $completedInvoicesPerWeek[$weekIndex] = $queryCompleted->count(); + // Move to next week $weekStart = $weekEnd; $weekIndex++; } @@ -556,6 +564,7 @@ class InvoiceChart extends ChartWidget ]; } + // elseif ($activeFilter == 'this_month') // { // $startOfMonth = now()->startOfMonth();