Refactor invoice data handling in SendInvoiceDataReport to improve document number processing and remove commented-out code

This commit is contained in:
dhanabalan
2025-11-07 23:02:03 +05:30
parent e27504d46d
commit 3e93e924fc

View File

@@ -103,145 +103,6 @@ class SendInvoiceDataReport extends Command
$endDate = now()->copy()->addDay()->setTime(8, 0, 0); $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
} }
// $tableData = [];
// $no = 1;
// foreach ($plants as $plant) {
// $distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer'];
// foreach ($distributions as $selectedDistribution) {
// // Get invoice records
// $invoices = \App\Models\InvoiceDataValidation::where('plant_id', $plant->id)
// ->where('distribution_channel_desc', $selectedDistribution)
// ->whereBetween('document_date', [$startDate, $endDate])
// ->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
// ->distinct('document_number')
// ->get();
// if ($invoices->isEmpty()) {
// $tableData[] = [
// 'no' => $no++,
// 'plant' => $plant->name,
// 'distribution_type' => $selectedDistribution,
// 'customer_code' => '-',
// 'document_number' => '-',
// 'document_date' => '-',
// 'customer_trade_name' => '-',
// 'customer_location' => '-',
// 'status' => 'No Invoices',
// ];
// continue;
// }
// // Fetch which invoices actually went out
// $wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
// ->whereIn('qr_code', $invoices->pluck('document_number')->toArray())
// ->whereBetween('scanned_at', [$startDate, $endDate])
// ->distinct('qr_code')
// ->pluck('qr_code')
// ->toArray();
// foreach ($invoices as $inv) {
// $status = in_array($inv->document_number, $wentOutInvoices)
// ? 'Went Out'
// : 'Pending';
// $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' => $status,
// ];
// }
// }
// }
// $tableData = [];
// $no = 1;
// foreach ($plants as $plant)
// {
// $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])
// ->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
// ->distinct('document_number')
// ->get();
// if ($invoices->isEmpty()) {
// continue;
// }
// $wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
// ->whereIn('qr_code', $invoices->pluck('document_number')->toArray())
// ->whereBetween('scanned_at', [$startDate, $endDate])
// ->distinct('qr_code')
// ->pluck('qr_code')
// ->toArray();
// $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
// return !in_array($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',
// ];
// }
// }
// }
// $mail = new InvoiceDataMail($scheduleType, $tableData);
// $contentVars = $mail->content()->with;
// $this->info($contentVars['greeting'] ?? 'Invoice Data Report');
// $this->table(
// ['No', 'Plant', 'Document Number', 'Document Date', 'Trade Name', 'Location', 'Status'],//'Distribution Type', 'Customer Code',
// $tableData
// );
// $this->info($contentVars['wishes'] ?? '');
// Send Mail //
// if (!empty($emails)) {
// foreach ($emails as $email){
// \Mail::to($email)->cc($ccEmails)->send(new InvoiceDataMail($scheduleType, $tableData));
// }
// $this->info("Invoice data report sent to " . count($emails) . " recipient(s).");
// }
// if (!empty($toEmails)) {
// foreach ($toEmails as $email){
// \Mail::to($email)->cc($ccEmails)->send(new InvoiceDataMail($scheduleType, $tableData));
// }
// $this->info("Invoice data report sent to " . count($toEmails) . " recipient(s).");
// }
// else {
// $this->warn('No recipients found for InvoiceDataMail.');
// }
//.. //..
foreach ($plants as $plant) foreach ($plants as $plant)
@@ -258,8 +119,7 @@ class SendInvoiceDataReport extends Command
->whereBetween('document_date', [$startDate, $endDate]) ->whereBetween('document_date', [$startDate, $endDate])
->orderBy('document_date', 'asc') ->orderBy('document_date', 'asc')
->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location') ->select('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
//->distinct('document_number') ->distinct('document_number')
// ->groupBy('customer_code', 'document_number', 'document_date', 'customer_trade_name', 'customer_location')
->get(); ->get();
if ($invoices->isEmpty()) { if ($invoices->isEmpty()) {
@@ -285,7 +145,7 @@ class SendInvoiceDataReport extends Command
$invoiceNumbers = $invoices $invoiceNumbers = $invoices
->pluck('document_number') ->pluck('document_number')
->map(fn($n) => strtoupper(trim((string) $n))) ->map(fn($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
->toArray(); ->toArray();
$wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id) $wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
@@ -295,7 +155,7 @@ class SendInvoiceDataReport extends Command
->whereBetween('scanned_at', [$startDate, $endDate]) ->whereBetween('scanned_at', [$startDate, $endDate])
->distinct('qr_code') ->distinct('qr_code')
->pluck('qr_code') ->pluck('qr_code')
->map(fn($n) => strtoupper(trim((string) $n))) ->map(fn($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
->toArray(); ->toArray();
// if (!empty($wentOutInvoices)) { // if (!empty($wentOutInvoices)) {
@@ -311,8 +171,12 @@ class SendInvoiceDataReport extends Command
// $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) { // $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
// return !in_array($inv->document_number, $wentOutInvoices); // return !in_array($inv->document_number, $wentOutInvoices);
// }); // });
// $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
// return !in_array(strtoupper(trim($inv->document_number)), $wentOutInvoices);
// });
$pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) { $pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
return !in_array(strtoupper(trim($inv->document_number)), $wentOutInvoices); $doc = preg_replace('/\s+/', '', strtoupper((string) $inv->document_number));
return !in_array($doc, $wentOutInvoices, true);
}); });