From 524d9a196033cbe6421efd271ed61de23454a649 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 15 May 2025 14:14:53 +0530 Subject: [PATCH] Updated this_month logic in invoice chart --- app/Filament/Widgets/InvoiceChart.php | 123 ++++++++++++++++++++------ 1 file changed, 95 insertions(+), 28 deletions(-) diff --git a/app/Filament/Widgets/InvoiceChart.php b/app/Filament/Widgets/InvoiceChart.php index 18fdd4d..71f17b2 100644 --- a/app/Filament/Widgets/InvoiceChart.php +++ b/app/Filament/Widgets/InvoiceChart.php @@ -418,7 +418,8 @@ class InvoiceChart extends ChartWidget ]]; } - elseif ($activeFilter == 'this_week') { + elseif ($activeFilter == 'this_week') + { $daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; $importedInvoicesPerDay = array_fill(0, 7, 0); $completedInvoicesPerDay = array_fill(0, 7, 0); @@ -488,52 +489,57 @@ class InvoiceChart extends ChartWidget ], ]; } - - elseif ($activeFilter == 'this_month') - { - $startOfMonth = now()->startOfMonth(); - $endOfMonth = now()->endOfMonth(); + 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 $monthName = $startOfMonth->format('M'); - // Prepare weeks for the month $labels = []; - $startDay = 1; - $totalDays = $endOfMonth->day; + $importedInvoicesPerWeek = []; + $completedInvoicesPerWeek = []; - while ($startDay <= $totalDays) { - $endDay = min($startDay + 6, $totalDays); + $weekIndex = 0; + $weekStart = $startOfMonth; + + while ($weekStart < $endOfMonth) { + $weekEnd = $weekStart->copy()->addDays(7); + if ($weekEnd > $endOfMonth) { + $weekEnd = $endOfMonth; + } + + $startDay = $weekStart->day; + $endDay = $weekEnd->copy()->subDay()->day; $labels[] = "{$monthName}({$startDay}-{$endDay})"; - $startDay += 7; - } - $importedInvoicesPerWeek = array_fill(0, count($labels), 0); - $completedInvoicesPerWeek = array_fill(0, count($labels), 0); + $queryImported = DB::table('invoice_validations') + ->where('plant_id', $selectedPlant) + ->whereBetween('created_at', [$weekStart, $weekEnd]); - foreach ($labels as $index => $label) { - $weekStart = now()->startOfMonth()->addDays(($index * 7))->setTime(8, 0, 0); - $weekEnd = $weekStart->copy()->addDays(6)->setTime(8, 0, 0); - - $queryImported = DB::table('invoice_validations')->where('plant_id', $selectedPlant)->whereBetween('created_at', [$weekStart, $weekEnd]); - $queryCompleted = DB::table('invoice_validations')->where('plant_id', $selectedPlant)->whereBetween('updated_at', [$weekStart, $weekEnd]); + $queryCompleted = DB::table('invoice_validations') + ->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','!=', ''); + ->whereNotNull('serial_number') + ->where('serial_number', '!=', ''); } elseif ($selectedInvoice == 'serial_invoice') { $queryImported->whereNull('quantity'); $queryCompleted->whereNull('quantity') - ->where('scanned_status', 'Scanned'); + ->where('scanned_status', 'Scanned'); } elseif ($selectedInvoice == 'bundle_material') { $queryImported->where('quantity', '>', 1); $queryCompleted->where('quantity', '>', 1) - ->whereNotNull('serial_number') - ->where('serial_number','!=', ''); + ->whereNotNull('serial_number') + ->where('serial_number', '!=', ''); } - $importedInvoicesPerWeek[$index] = $queryImported->distinct('invoice_number')->count('invoice_number'); - $completedInvoicesPerWeek[$index] = $queryCompleted->distinct('invoice_number')->count('invoice_number'); + $importedInvoicesPerWeek[$weekIndex] = $queryImported->distinct('invoice_number')->count('invoice_number'); + $completedInvoicesPerWeek[$weekIndex] = $queryCompleted->distinct('invoice_number')->count('invoice_number'); + + $weekStart = $weekEnd; + $weekIndex++; } $datasets = [ @@ -549,6 +555,67 @@ class InvoiceChart extends ChartWidget ], ]; } + + // elseif ($activeFilter == 'this_month') + // { + // $startOfMonth = now()->startOfMonth(); + // $endOfMonth = now()->endOfMonth(); + // $monthName = $startOfMonth->format('M'); + + // // Prepare weeks for the month + // $labels = []; + // $startDay = 1; + // $totalDays = $endOfMonth->day; + + // while ($startDay <= $totalDays) { + // $endDay = min($startDay + 6, $totalDays); + // $labels[] = "{$monthName}({$startDay}-{$endDay})"; + // $startDay += 7; + // } + + // $importedInvoicesPerWeek = array_fill(0, count($labels), 0); + // $completedInvoicesPerWeek = array_fill(0, count($labels), 0); + + // foreach ($labels as $index => $label) { + // $weekStart = now()->startOfMonth()->addDays(($index * 7))->setTime(8, 0, 0); + // $weekEnd = $weekStart->copy()->addDays(6)->setTime(8, 0, 0); + + // $queryImported = DB::table('invoice_validations')->where('plant_id', $selectedPlant)->whereBetween('created_at', [$weekStart, $weekEnd]); + // $queryCompleted = DB::table('invoice_validations')->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','!=', ''); + // } elseif ($selectedInvoice == 'serial_invoice') { + // $queryImported->whereNull('quantity'); + // $queryCompleted->whereNull('quantity') + // ->where('scanned_status', 'Scanned'); + // } elseif ($selectedInvoice == 'bundle_material') { + // $queryImported->where('quantity', '>', 1); + // $queryCompleted->where('quantity', '>', 1) + // ->whereNotNull('serial_number') + // ->where('serial_number','!=', ''); + // } + + // $importedInvoicesPerWeek[$index] = $queryImported->distinct('invoice_number')->count('invoice_number'); + // $completedInvoicesPerWeek[$index] = $queryCompleted->distinct('invoice_number')->count('invoice_number'); + // } + + // $datasets = [ + // [ + // 'label' => 'Imported Invoices', + // 'data' => $importedInvoicesPerWeek, + // 'backgroundColor' => 'rgba(75, 192, 192, 1)', + // ], + // [ + // 'label' => 'Completed Invoices', + // 'data' => $completedInvoicesPerWeek, + // 'backgroundColor' => 'rgba(23, 211, 80, 1)', + // ], + // ]; + // } else { $labels = ['Imported Invoice', 'Completed Invoice']; $datasets = [[