added invoice chart updated logic

This commit is contained in:
dhanabalan
2025-04-26 12:33:11 +05:30
parent c716a1fba9
commit b5ede7af73

View File

@@ -17,11 +17,292 @@ class InvoiceChart extends ChartWidget
protected $listeners = ['invoiceChart']; protected $listeners = ['invoiceChart'];
// protected function getData(): array
// {
// $selectedPlant = session('selec_plant');
// $selectedInvoice = session('select_invoice');
// $activeFilter = $this->filter;
// if (!$selectedPlant || !$selectedInvoice) {
// return [
// 'datasets' => [],
// 'labels' => [],
// ];
// }
// if ($activeFilter === 'yesterday') {
// $startDate = now()->subDay()->setTime(8, 0, 0);
// $endDate = now()->setTime(8, 0, 0);
// $groupBy = 'none'; // No grouping by hour
// } elseif ($activeFilter === 'this_week') {
// $startDate = now()->startOfWeek()->setTime(8, 0, 0);
// $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
// $groupBy = 'day_of_week';
// } elseif ($activeFilter === 'this_month') {
// $startDate = now()->startOfMonth();
// $endDate = now()->endOfMonth();
// $groupBy = 'week_of_month';
// } else {
// $startDate = now()->setTime(8, 0, 0);
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
// $groupBy = 'none'; // No grouping by hour
// }
// // Get fully scanned invoices
// if ($selectedInvoice === 'individual_material') {
// $allInvoiceNumbers = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->where('quantity', 1)
// ->groupBy('invoice_number')
// ->pluck('invoice_number');
// $fullyScannedInvoiceNumbers = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->where('quantity', 1)
// ->groupBy('invoice_number')
// ->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)")
// ->pluck('invoice_number');
// }
// else if($selectedInvoice === 'serial_invoice') {
// $allInvoiceNumbers = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->whereNull('quantity')
// ->pluck('invoice_number');
// $fullyScannedInvoiceNumbers = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->whereNull('quantity')
// ->groupBy('invoice_number')
// ->havingRaw("SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END) = COUNT(*)")
// ->pluck('invoice_number');
// }
// if ($selectedInvoice === 'bundle_material') {
// //Get all invoice numbers that contain bundle materials
// $allInvoiceNumbers = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->whereIn('invoice_number', function ($query) use ($selectedPlant) {
// $query->select('invoice_number')
// ->from('invoice_validations')
// ->where('plant_id', $selectedPlant)
// ->where('quantity', '>', 1);
// })
// ->groupBy('invoice_number')
// ->pluck('invoice_number');
// //Get fully scanned bundle invoices
// $fullyScannedBundle = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->where('quantity', '>', 1)
// ->groupBy('invoice_number')
// ->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)")
// ->pluck('invoice_number');
// //Get individual invoice numbers
// $individualInvoices = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->where('quantity', 1)
// ->groupBy('invoice_number')
// ->pluck('invoice_number');
// //Get fully scanned individual invoices
// $fullyScannedIndividual = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->where('quantity', 1)
// ->groupBy('invoice_number')
// ->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)")
// ->pluck('invoice_number');
// //Find common invoices between bundle and individual
// $commonInvoices = $allInvoiceNumbers->intersect($individualInvoices);
// //Identify incomplete common invoices
// $notScannedInvoicesForBundle = $commonInvoices->filter(function ($invoice) use ($fullyScannedIndividual, $fullyScannedBundle) {
// return !($fullyScannedIndividual->contains($invoice) && $fullyScannedBundle->contains($invoice));
// });
// //Final fully scanned = only those that are scanned in both individual & bundle
// $fullyScannedInvoiceNumbers = $fullyScannedBundle->filter(function ($invoice) use ($fullyScannedIndividual) {
// return $fullyScannedIndividual->contains($invoice);
// });
// //Exclude invoices that should be marked as "not scanned"
// $fullyScannedInvoiceNumbers = $fullyScannedInvoiceNumbers->diff($notScannedInvoicesForBundle);
// //Calculate not scanned invoices = all - fully scanned
// $notScannedInvoiceNumbers = $allInvoiceNumbers->diff($fullyScannedInvoiceNumbers);
// }
// // Not scanned = all - scanned
// $notScannedInvoiceNumbers = $allInvoiceNumbers->diff($fullyScannedInvoiceNumbers);
// //Get timestamps
// $scannedTimestamps = DB::table('invoice_validations')
// ->selectRaw("MAX(updated_at) AS final_time, invoice_number")
// ->where('plant_id', $selectedPlant)
// ->whereIn('invoice_number', $fullyScannedInvoiceNumbers)
// ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->pluck('final_time', 'invoice_number');
// $notScannedTimestamps = DB::table('invoice_validations')
// ->selectRaw("MIN(created_at) AS first_time, invoice_number")
// ->where('plant_id', $selectedPlant)
// ->whereIn('invoice_number', $notScannedInvoiceNumbers)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->pluck('first_time', 'invoice_number');
// // // Group timestamps
// // $scannedGrouped = [];
// // $notScannedGrouped = [];
// // foreach ($scannedTimestamps as $timestamp) {
// // $time = \Carbon\Carbon::parse($timestamp);
// // if ($time->lt($startDate) || $time->gt($endDate)) continue;
// // $key = match ($groupBy) {
// // 'hour' => (int) $time->format('G'),
// // 'day_of_week' => (int) $time->dayOfWeek,
// // 'week_of_month' => (int) ceil($time->day / 7),
// // };
// // $scannedGrouped[$key] = ($scannedGrouped[$key] ?? 0) + 1;
// // }
// // foreach ($notScannedTimestamps as $timestamp) {
// // $time = \Carbon\Carbon::parse($timestamp);
// // if ($time->lt($startDate) || $time->gt($endDate)) continue;
// // $key = match ($groupBy) {
// // 'hour' => (int) $time->format('G'),
// // 'day_of_week' => (int) $time->dayOfWeek,
// // 'week_of_month' => (int) ceil($time->day / 7),
// // };
// // $notScannedGrouped[$key] = ($notScannedGrouped[$key] ?? 0) + 1;
// // }
// // // Build x-axis labels and data arrays
// // if ($groupBy === 'hour') {
// // $shiftedKeys = array_merge(range(8, 23), range(0, 7));
// // $labels = array_map(fn($h) => date("g A", strtotime("$h:00")), $shiftedKeys);
// // $scannedData = array_map(fn($h) => $scannedGrouped[$h] ?? 0, $shiftedKeys);
// // $notScannedData = array_map(fn($h) => $notScannedGrouped[$h] ?? 0, $shiftedKeys);
// // } elseif ($groupBy === 'day_of_week') {
// // $labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
// // $scannedData = array_fill(0, 7, 0);
// // $notScannedData = array_fill(0, 7, 0);
// // foreach ($scannedGrouped as $k => $v) $scannedData[$k] = $v;
// // foreach ($notScannedGrouped as $k => $v) $notScannedData[$k] = $v;
// // // Shift so Monday starts first
// // $scannedData = [$scannedData[1], $scannedData[2], $scannedData[3], $scannedData[4], $scannedData[5], $scannedData[6], $scannedData[0]];
// // $notScannedData = [$notScannedData[1], $notScannedData[2], $notScannedData[3], $notScannedData[4], $notScannedData[5], $notScannedData[6], $notScannedData[0]];
// // } else {
// // $weeksCount = ceil($endDate->day / 7);
// // $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)";
// // }
// // $scannedData = array_fill(1, $weeksCount, 0);
// // $notScannedData = array_fill(1, $weeksCount, 0);
// // foreach ($scannedGrouped as $k => $v) $scannedData[$k] = $v;
// // foreach ($notScannedGrouped as $k => $v) $notScannedData[$k] = $v;
// // $scannedData = array_values($scannedData);
// // $notScannedData = array_values($notScannedData);
// // }
// // return [
// // 'datasets' => [
// // [
// // 'label' => 'Scanned Invoices',
// // 'data' => $scannedData,
// // 'borderColor' => 'rgba(75, 192, 192, 1)',
// // 'backgroundColor' => 'rgba(23, 211, 80, 0.7)',
// // 'fill' => false,
// // 'tension' => 0.3,
// // ],
// // [
// // 'label' => 'Not Scanned Invoices',
// // 'data' => $notScannedData,
// // 'borderColor' => 'rgba(255, 99, 132, 1)',
// // 'backgroundColor' => 'rgba(214, 105, 32, 0.9)',
// // 'fill' => false,
// // 'tension' => 0.3,
// // ],
// // ],
// // 'labels' => $labels,
// // ];
// // Group timestamps
// // Build x-axis labels and data arrays
// $scannedGrouped = [];
// $notScannedGrouped = [];
// foreach ($scannedTimestamps as $timestamp) {
// $time = \Carbon\Carbon::parse($timestamp);
// if ($time->lt($startDate) || $time->gt($endDate)) continue;
// $key = match ($groupBy) {
// 'hour' => (int) $time->format('G'),
// 'day_of_week' => (int) $time->dayOfWeek,
// 'week_of_month' => (int) ceil($time->day / 7),
// };
// $scannedGrouped[$key] = ($scannedGrouped[$key] ?? 0) + 1;
// }
// foreach ($notScannedTimestamps as $timestamp) {
// $time = \Carbon\Carbon::parse($timestamp);
// if ($time->lt($startDate) || $time->gt($endDate)) continue;
// $key = match ($groupBy) {
// 'hour' => (int) $time->format('G'),
// 'day_of_week' => (int) $time->dayOfWeek,
// 'week_of_month' => (int) ceil($time->day / 7),
// };
// $notScannedGrouped[$key] = ($notScannedGrouped[$key] ?? 0) + 1;
// }
// $labels = ['Imported Invoice', 'Completed Invoice']; // Custom labels
// return [
// 'datasets' => [
// [
// 'label' => 'Imported Invoices',
// 'data' => [count($allInvoiceNumbers), count($fullyScannedInvoiceNumbers)],
// 'backgroundColor' => 'rgba(75, 192, 192, 1)',
// 'fill' => false,
// ],
// [
// 'label' => 'Completed Invoices',
// 'data' => [0, count($fullyScannedInvoiceNumbers)],
// 'backgroundColor' => 'rgba(23, 211, 80, 1)',
// 'fill' => false,
// ],
// ],
// 'labels' => $labels,
// ];
// }
protected function getData(): array protected function getData(): array
{ {
$selectedPlant = session('selec_plant'); $selectedPlant = session('selec_plant');
$selectedInvoice = session('select_invoice'); $selectedInvoice = session('select_invoice');
$activeFilter = $this->filter; $activeFilter = $this->filter; // Assuming filter is passed and activeFilter can be 'yesterday', 'this_week', 'this_month'
if (!$selectedPlant || !$selectedInvoice) { if (!$selectedPlant || !$selectedInvoice) {
return [ return [
@@ -30,240 +311,195 @@ class InvoiceChart extends ChartWidget
]; ];
} }
// Define the date range based on the active filter
if ($activeFilter === 'yesterday') { if ($activeFilter === 'yesterday') {
$startDate = now()->subDay()->setTime(8, 0, 0); $startDate = now()->subDay()->setTime(8, 0, 0);
$endDate = now()->setTime(8, 0, 0); $endDate = now()->setTime(8, 0, 0);
$groupBy = 'hour'; $groupBy = 'none'; // No grouping by hour
} elseif ($activeFilter === 'this_week') { } elseif ($activeFilter === 'this_week') {
$startDate = now()->startOfWeek()->setTime(8, 0, 0); $startDate = now()->startOfWeek()->setTime(8, 0, 0);
$endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
$groupBy = 'day_of_week'; $groupBy = 'day_of_week';
} elseif ($activeFilter === 'this_month') { } elseif ($activeFilter === 'this_month') {
$startDate = now()->startOfMonth(); $startDate = now()->startOfMonth()->setTime(8, 0, 0);
$endDate = now()->endOfMonth(); $endDate = now()->endOfMonth()->setTime(8, 0, 0);
$groupBy = 'week_of_month'; $groupBy = 'week_of_month';
} else { } else {
$startDate = now()->setTime(8, 0, 0); $startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0); $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
$groupBy = 'hour'; $groupBy = 'none'; // No grouping by hour
} }
// Get fully scanned invoices // Get the counts for Imported Invoices (unique invoice numbers) and Completed Invoices
if ($selectedInvoice === 'individual_material') { if ($selectedInvoice === 'individual_material') {
$importedInvoicesCount = DB::table('invoice_validations')
$allInvoiceNumbers = \DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant) ->where('plant_id', $selectedPlant)
->where('quantity', 1) ->where('quantity', 1)
->groupBy('invoice_number') ->whereBetween('created_at', [$startDate, $endDate]) // Filter by date range
->pluck('invoice_number'); ->distinct('invoice_number') // Only distinct invoice numbers
->count('invoice_number');
$fullyScannedInvoiceNumbers = \DB::table('invoice_validations') $completedInvoicesCount = DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant) ->where('plant_id', $selectedPlant)
->where('quantity', 1) ->where('quantity', 1)
->groupBy('invoice_number') ->whereNotNull('serial_number') // assuming serial_number presence means completed
->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)") ->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
->pluck('invoice_number'); ->distinct('invoice_number') // Only distinct invoice numbers
} //elseif ($selectedInvoice === 'bundle_material') { ->count('invoice_number');
} elseif ($selectedInvoice === 'serial_invoice') {
// $allInvoiceNumbers = \DB::table('invoice_validations') $importedInvoicesCount = DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->havingRaw('MIN(quantity) > 1')
// ->groupBy('invoice_number')
// ->pluck('invoice_number');
// $fullyScannedInvoiceNumbers = \DB::table('invoice_validations')
// ->select('invoice_number')
// ->where('plant_id', $selectedPlant)
// ->havingRaw('MIN(quantity) > 1')
// ->groupBy('invoice_number')
// ->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)")
// ->pluck('invoice_number');
// }
else if($selectedInvoice === 'serial_invoice') {
$allInvoiceNumbers = \DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant) ->where('plant_id', $selectedPlant)
->whereNull('quantity') ->whereNull('quantity')
->pluck('invoice_number'); ->whereBetween('created_at', [$startDate, $endDate]) // Filter by date range
->distinct('invoice_number') // Only distinct invoice numbers
->count('invoice_number');
$fullyScannedInvoiceNumbers = \DB::table('invoice_validations') $completedInvoicesCount = DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant) ->where('plant_id', $selectedPlant)
->whereNull('quantity') ->whereNull('quantity')
->groupBy('invoice_number') ->where('scanned_status', 'Scanned') // assuming scanned_status means completed
->havingRaw("SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END) = COUNT(*)") ->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
->pluck('invoice_number'); ->distinct('invoice_number') // Only distinct invoice numbers
} ->count('invoice_number');
if ($selectedInvoice === 'bundle_material') { } elseif ($selectedInvoice === 'bundle_material') {
$importedInvoicesCount = DB::table('invoice_validations')
//Get all invoice numbers that contain bundle materials
$allInvoiceNumbers = DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant)
->whereIn('invoice_number', function ($query) use ($selectedPlant) {
$query->select('invoice_number')
->from('invoice_validations')
->where('plant_id', $selectedPlant)
->where('quantity', '>', 1);
})
->groupBy('invoice_number')
->pluck('invoice_number');
//Get fully scanned bundle invoices
$fullyScannedBundle = DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant) ->where('plant_id', $selectedPlant)
->where('quantity', '>', 1) ->where('quantity', '>', 1)
->groupBy('invoice_number') ->whereBetween('created_at', [$startDate, $endDate]) // Filter by date range
->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)") ->distinct('invoice_number') // Only distinct invoice numbers
->pluck('invoice_number'); ->count('invoice_number');
//Get individual invoice numbers $completedInvoicesCount = DB::table('invoice_validations')
$individualInvoices = DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant) ->where('plant_id', $selectedPlant)
->where('quantity', 1) ->where('quantity', '>', 1)
->groupBy('invoice_number') ->whereNotNull('serial_number') // assuming serial_number presence means completed
->pluck('invoice_number'); ->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
->distinct('invoice_number') // Only distinct invoice numbers
//Get fully scanned individual invoices ->count('invoice_number');
$fullyScannedIndividual = DB::table('invoice_validations')
->select('invoice_number')
->where('plant_id', $selectedPlant)
->where('quantity', 1)
->groupBy('invoice_number')
->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)")
->pluck('invoice_number');
//Find common invoices between bundle and individual
$commonInvoices = $allInvoiceNumbers->intersect($individualInvoices);
//Identify incomplete common invoices
$notScannedInvoicesForBundle = $commonInvoices->filter(function ($invoice) use ($fullyScannedIndividual, $fullyScannedBundle) {
return !($fullyScannedIndividual->contains($invoice) && $fullyScannedBundle->contains($invoice));
});
//Final fully scanned = only those that are scanned in both individual & bundle
$fullyScannedInvoiceNumbers = $fullyScannedBundle->filter(function ($invoice) use ($fullyScannedIndividual) {
return $fullyScannedIndividual->contains($invoice);
});
//Exclude invoices that should be marked as "not scanned"
$fullyScannedInvoiceNumbers = $fullyScannedInvoiceNumbers->diff($notScannedInvoicesForBundle);
//Calculate not scanned invoices = all - fully scanned
$notScannedInvoiceNumbers = $allInvoiceNumbers->diff($fullyScannedInvoiceNumbers);
} }
// Not scanned = all - scanned // Prepare the chart data
$notScannedInvoiceNumbers = $allInvoiceNumbers->diff($fullyScannedInvoiceNumbers); $labels = []; // Labels for each bar
$datasets = []; // Datasets for the chart
//Get timestamps if (in_array($activeFilter, ['yesterday', 'today'])) {
$scannedTimestamps = \DB::table('invoice_validations') $labels = ['Imported Invoice', 'Completed Invoice'];
->selectRaw("MAX(updated_at) AS final_time, invoice_number") $datasets = [[
->where('plant_id', $selectedPlant) 'label' => 'Invoices',
->whereIn('invoice_number', $fullyScannedInvoiceNumbers) 'data' => [$importedInvoicesCount, $completedInvoicesCount],
->whereBetween('updated_at', [$startDate, $endDate]) 'backgroundColor' => ['rgba(75, 192, 192, 1)', 'rgba(23, 211, 80, 1)'],
->groupBy('invoice_number') 'fill' => false,
->pluck('final_time', 'invoice_number'); ]];
}
elseif ($activeFilter === 'this_week') {
$daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
$importedInvoicesPerDay = array_fill(0, 7, 0);
$completedInvoicesPerDay = array_fill(0, 7, 0);
$notScannedTimestamps = \DB::table('invoice_validations') foreach ($daysOfWeek as $index => $day) {
->selectRaw("MIN(created_at) AS first_time, invoice_number") $dayStart = now()->startOfWeek()->addDays($index)->setTime(8, 0, 0);
->where('plant_id', $selectedPlant) $dayEnd = $dayStart->copy()->addDay()->setTime(8, 0, 0);
->whereIn('invoice_number', $notScannedInvoiceNumbers)
->whereBetween('created_at', [$startDate, $endDate])
->groupBy('invoice_number')
->pluck('first_time', 'invoice_number');
// Group timestamps $query = DB::table('invoice_validations')->where('plant_id', $selectedPlant);
$scannedGrouped = []; $completedQuery = DB::table('invoice_validations')->where('plant_id', $selectedPlant);
$notScannedGrouped = [];
foreach ($scannedTimestamps as $timestamp) { if ($selectedInvoice === 'individual_material') {
$time = \Carbon\Carbon::parse($timestamp); $query->where('quantity', 1)->whereBetween('created_at', [$dayStart, $dayEnd]);
if ($time->lt($startDate) || $time->gt($endDate)) continue; $completedQuery->where('quantity', 1)
->whereNotNull('serial_number')
$key = match ($groupBy) { ->whereBetween('updated_at', [$dayStart, $dayEnd]);
'hour' => (int) $time->format('G'), } elseif ($selectedInvoice === 'serial_invoice') {
'day_of_week' => (int) $time->dayOfWeek, $query->whereNull('quantity')->whereBetween('created_at', [$dayStart, $dayEnd]);
'week_of_month' => (int) ceil($time->day / 7), $completedQuery->whereNull('quantity')
}; ->where('scanned_status', 'Scanned')
->whereBetween('updated_at', [$dayStart, $dayEnd]);
$scannedGrouped[$key] = ($scannedGrouped[$key] ?? 0) + 1; } elseif ($selectedInvoice === 'bundle_material') {
$query->where('quantity', '>', 1)->whereBetween('created_at', [$dayStart, $dayEnd]);
$completedQuery->where('quantity', '>', 1)
->whereNotNull('serial_number')
->whereBetween('updated_at', [$dayStart, $dayEnd]);
} }
foreach ($notScannedTimestamps as $timestamp) { $importedInvoicesPerDay[$index] = $query->distinct('invoice_number')->count('invoice_number');
$time = \Carbon\Carbon::parse($timestamp); $completedInvoicesPerDay[$index] = $completedQuery->distinct('invoice_number')->count('invoice_number');
if ($time->lt($startDate) || $time->gt($endDate)) continue;
$key = match ($groupBy) {
'hour' => (int) $time->format('G'),
'day_of_week' => (int) $time->dayOfWeek,
'week_of_month' => (int) ceil($time->day / 7),
};
$notScannedGrouped[$key] = ($notScannedGrouped[$key] ?? 0) + 1;
} }
// Build x-axis labels and data arrays $labels = $daysOfWeek;
if ($groupBy === 'hour') { $datasets = [
$shiftedKeys = array_merge(range(8, 23), range(0, 7)); [
$labels = array_map(fn($h) => date("g A", strtotime("$h:00")), $shiftedKeys); 'label' => 'Imported Invoices',
$scannedData = array_map(fn($h) => $scannedGrouped[$h] ?? 0, $shiftedKeys); 'data' => $importedInvoicesPerDay,
$notScannedData = array_map(fn($h) => $notScannedGrouped[$h] ?? 0, $shiftedKeys); 'backgroundColor' => 'rgba(75, 192, 192, 1)',
} elseif ($groupBy === 'day_of_week') { ],
$labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; [
$scannedData = array_fill(0, 7, 0); 'label' => 'Completed Invoices',
$notScannedData = array_fill(0, 7, 0); 'data' => $completedInvoicesPerDay,
foreach ($scannedGrouped as $k => $v) $scannedData[$k] = $v; 'backgroundColor' => 'rgba(23, 211, 80, 1)',
foreach ($notScannedGrouped as $k => $v) $notScannedData[$k] = $v; ],
// Shift so Monday starts first ];
$scannedData = [$scannedData[1], $scannedData[2], $scannedData[3], $scannedData[4], $scannedData[5], $scannedData[6], $scannedData[0]]; }
$notScannedData = [$notScannedData[1], $notScannedData[2], $notScannedData[3], $notScannedData[4], $notScannedData[5], $notScannedData[6], $notScannedData[0]]; elseif ($activeFilter === 'this_month') {
} else { $startOfMonth = now()->startOfMonth();
$weeksCount = ceil($endDate->day / 7); $endOfMonth = now()->endOfMonth();
$monthName = $startOfMonth->format('M');
// Prepare weeks for the month
$labels = []; $labels = [];
for ($i = 1; $i <= $weeksCount; $i++) { $startDay = 1;
$weekStart = $startDate->copy()->addDays(($i - 1) * 7)->format('d M'); $totalDays = $endOfMonth->day;
$weekEnd = $startDate->copy()->addDays($i * 7 - 1)->min($endDate)->format('d M');
$labels[] = "Week $i ($weekStart - $weekEnd)"; while ($startDay <= $totalDays) {
$endDay = min($startDay + 6, $totalDays);
$labels[] = "{$monthName}({$startDay}-{$endDay})";
$startDay += 7;
} }
$scannedData = array_fill(1, $weeksCount, 0); $importedInvoicesPerWeek = array_fill(0, count($labels), 0);
$notScannedData = array_fill(1, $weeksCount, 0); $completedInvoicesPerWeek = array_fill(0, count($labels), 0);
foreach ($scannedGrouped as $k => $v) $scannedData[$k] = $v;
foreach ($notScannedGrouped as $k => $v) $notScannedData[$k] = $v; foreach ($labels as $index => $label) {
$scannedData = array_values($scannedData); $weekStart = now()->startOfMonth()->addDays(($index * 7))->setTime(8, 0, 0);
$notScannedData = array_values($notScannedData); $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');
} 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');
} }
return [ $importedInvoicesPerWeek[$index] = $queryImported->distinct('invoice_number')->count('invoice_number');
'datasets' => [ $completedInvoicesPerWeek[$index] = $queryCompleted->distinct('invoice_number')->count('invoice_number');
}
$datasets = [
[ [
'label' => 'Scanned Invoices', 'label' => 'Imported Invoices',
'data' => $scannedData, 'data' => $importedInvoicesPerWeek,
'borderColor' => 'rgba(75, 192, 192, 1)', 'backgroundColor' => 'rgba(75, 192, 192, 1)',
'backgroundColor' => 'rgba(23, 211, 80, 0.7)',
'fill' => false,
'tension' => 0.3,
], ],
[ [
'label' => 'Not Scanned Invoices', 'label' => 'Completed Invoices',
'data' => $notScannedData, 'data' => $completedInvoicesPerWeek,
'borderColor' => 'rgba(255, 99, 132, 1)', 'backgroundColor' => 'rgba(23, 211, 80, 1)',
'backgroundColor' => 'rgba(214, 105, 32, 0.9)',
'fill' => false,
'tension' => 0.3,
], ],
],
'labels' => $labels,
]; ];
} }
return [
'datasets' => $datasets,
'labels' => $labels,
];
}
protected function getType(): string protected function getType(): string
{ {