Refactor invoice data handling in SendInvoiceDataReport to improve filtering and sorting of invoices

This commit is contained in:
dhanabalan
2025-11-07 22:35:58 +05:30
parent 713429b453
commit be2b02c1c9

View File

@@ -249,61 +249,79 @@ class SendInvoiceDataReport extends Command
$tableData = [];
$no = 1;
$distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer', 'WOS'];
$distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer', 'WOS', ''];
foreach ($distributions as $selectedDistribution)
{
$invoices = \App\Models\InvoiceDataValidation::where('plant_id', $plant->id)
->where('distribution_channel_desc', $selectedDistribution)
->whereBetween('document_date', [$startDate, $endDate])
->orderBy('document_date', 'asc')
->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
//->distinct('document_number')
->groupBy('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
->orderBy('document_date', 'asc')
// ->groupBy('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
->get();
if ($invoices->isEmpty()) {
continue;
}
// Filter invoices directly — exclude ones with '-' in document_number
$invoices = $invoices->filter(function ($inv) {
return !empty($inv->document_number) && !str_contains($inv->document_number, '-');
});
// For empty distribution, only take those starting with '7'
if (trim($selectedDistribution) == '' || $selectedDistribution == null) {
$invoices = $invoices->filter(function ($inv) {
return str_starts_with($inv->document_number, '7');
});
}
if ($invoices->isEmpty()) {
continue;
}
$invoiceNumbers = $invoices
->pluck('document_number')
->map(fn($n) => strtoupper(trim((string) $n)))
->toArray();
$wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
->whereIn('qr_code', $invoices->pluck('document_number')->toArray())
//->whereIn('qr_code', $invoices->pluck('document_number'))
//->whereIn('qr_code', $invoices->pluck('document_number')->toArray())
->whereIn('qr_code', $invoiceNumbers)
->whereBetween('scanned_at', [$startDate, $endDate])
->distinct('qr_code')
->pluck('qr_code')
->map(fn($n) => strtoupper(trim((string) $n)))
->toArray();
// if (!empty($wentOutInvoices)) {
// $deletedValidations = \App\Models\InvoiceDataValidation::whereIn('document_number', $wentOutInvoices)
// ->delete();
// $deletedOuts = \App\Models\InvoiceOutValidation::whereIn('qr_code', $wentOutInvoices)
// ->delete();
// $this->info("Deleted {$deletedValidations} from invoice_data_validations and {$deletedOuts} from invoice_out_validations for plant {$plant->name} ({$selectedDistribution}).");
// }
// $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
// return !in_array($inv->document_number, $wentOutInvoices);
// });
$pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
return !in_array($inv->document_number, $wentOutInvoices);
return !in_array(strtoupper(trim($inv->document_number)), $wentOutInvoices);
});
if ($pendingInvoices->isEmpty()) {
continue;
}
// foreach ($pendingInvoices as $inv) {
// $tableData[] = [
// 'no' => $no++,
// 'plant' => $plant->name,
// // 'distribution_type' => $selectedDistribution,
// // 'customer_code' => $inv->customer_code,
// 'document_number' => $inv->document_number,
// 'document_date' => $inv->document_date,
// 'customer_trade_name' => $inv->customer_trade_name,
// 'customer_location' => $inv->customer_location,
// 'status' => 'Pending',
// ];
// }
foreach ($pendingInvoices as $inv)
{
// $documentDate = \Carbon\Carbon::parse($inv->document_date);
// $now = \Carbon\Carbon::now();
//$endDate = now()->copy()->subDay()->setTime(10, 0, 0);
//$hoursDiff = $documentDate->diffInHours($now);
//$statusColor = $endDate <= $inv->document_date ? 'status-pending-yellow' : 'status-pending-red';
$yesterday = now()->subDay()->toDateString();
$today = now()->toDateString();
@@ -331,6 +349,11 @@ class SendInvoiceDataReport extends Command
}
}
$tableData = collect($tableData)
->sortBy('document_date')
->values()
->toArray();
$mail = new InvoiceDataMail($scheduleType, $tableData);
$contentVars = $mail->content()->with;