Updated this_month logic in invoice chart

This commit is contained in:
dhanabalan
2025-05-15 14:14:53 +05:30
parent db9185f7e0
commit 524d9a1960

View File

@@ -418,7 +418,8 @@ class InvoiceChart extends ChartWidget
]]; ]];
} }
elseif ($activeFilter == 'this_week') { elseif ($activeFilter == 'this_week')
{
$daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; $daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
$importedInvoicesPerDay = array_fill(0, 7, 0); $importedInvoicesPerDay = array_fill(0, 7, 0);
$completedInvoicesPerDay = array_fill(0, 7, 0); $completedInvoicesPerDay = array_fill(0, 7, 0);
@@ -488,52 +489,57 @@ class InvoiceChart extends ChartWidget
], ],
]; ];
} }
elseif ($activeFilter == 'this_month') {
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
$startOfMonth = now()->startOfMonth();
$endOfMonth = now()->endOfMonth();
$monthName = $startOfMonth->format('M'); $monthName = $startOfMonth->format('M');
// Prepare weeks for the month
$labels = []; $labels = [];
$startDay = 1; $importedInvoicesPerWeek = [];
$totalDays = $endOfMonth->day; $completedInvoicesPerWeek = [];
while ($startDay <= $totalDays) { $weekIndex = 0;
$endDay = min($startDay + 6, $totalDays); $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})"; $labels[] = "{$monthName}({$startDay}-{$endDay})";
$startDay += 7;
}
$importedInvoicesPerWeek = array_fill(0, count($labels), 0); $queryImported = DB::table('invoice_validations')
$completedInvoicesPerWeek = array_fill(0, count($labels), 0); ->where('plant_id', $selectedPlant)
->whereBetween('created_at', [$weekStart, $weekEnd]);
foreach ($labels as $index => $label) { $queryCompleted = DB::table('invoice_validations')
$weekStart = now()->startOfMonth()->addDays(($index * 7))->setTime(8, 0, 0); ->where('plant_id', $selectedPlant)
$weekEnd = $weekStart->copy()->addDays(6)->setTime(8, 0, 0); ->whereBetween('updated_at', [$weekStart, $weekEnd]);
$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') { if ($selectedInvoice == 'individual_material') {
$queryImported->where('quantity', 1); $queryImported->where('quantity', 1);
$queryCompleted->where('quantity', 1) $queryCompleted->where('quantity', 1)
->whereNotNull('serial_number') ->whereNotNull('serial_number')
->where('serial_number','!=', ''); ->where('serial_number', '!=', '');
} elseif ($selectedInvoice == 'serial_invoice') { } elseif ($selectedInvoice == 'serial_invoice') {
$queryImported->whereNull('quantity'); $queryImported->whereNull('quantity');
$queryCompleted->whereNull('quantity') $queryCompleted->whereNull('quantity')
->where('scanned_status', 'Scanned'); ->where('scanned_status', 'Scanned');
} elseif ($selectedInvoice == 'bundle_material') { } elseif ($selectedInvoice == 'bundle_material') {
$queryImported->where('quantity', '>', 1); $queryImported->where('quantity', '>', 1);
$queryCompleted->where('quantity', '>', 1) $queryCompleted->where('quantity', '>', 1)
->whereNotNull('serial_number') ->whereNotNull('serial_number')
->where('serial_number','!=', ''); ->where('serial_number', '!=', '');
} }
$importedInvoicesPerWeek[$index] = $queryImported->distinct('invoice_number')->count('invoice_number'); $importedInvoicesPerWeek[$weekIndex] = $queryImported->distinct('invoice_number')->count('invoice_number');
$completedInvoicesPerWeek[$index] = $queryCompleted->distinct('invoice_number')->count('invoice_number'); $completedInvoicesPerWeek[$weekIndex] = $queryCompleted->distinct('invoice_number')->count('invoice_number');
$weekStart = $weekEnd;
$weekIndex++;
} }
$datasets = [ $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 { else {
$labels = ['Imported Invoice', 'Completed Invoice']; $labels = ['Imported Invoice', 'Completed Invoice'];
$datasets = [[ $datasets = [[