Enhance SendInvoiceDataReport to include 'WOS' distribution and improve email handling logic
This commit is contained in:
@@ -70,193 +70,251 @@ class SendInvoiceDataReport extends Command
|
|||||||
// ->filter()
|
// ->filter()
|
||||||
// ->toArray();
|
// ->toArray();
|
||||||
|
|
||||||
$plants = $plantId == 0
|
$plants = $plantId == 0
|
||||||
? Plant::all()
|
? Plant::all()
|
||||||
: Plant::where('id', $plantId)->get();
|
: Plant::where('id', $plantId)->get();
|
||||||
|
|
||||||
if ($plants->isEmpty()) {
|
if ($plants->isEmpty()) {
|
||||||
$this->error("No valid plant(s) found.");
|
$this->error("No valid plant(s) found.");
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (strtolower($scheduleType) == 'daily') {
|
|
||||||
$startDate = now()->subDay()->setTime(10, 0, 0);//8:00
|
|
||||||
$endDate = now()->setTime(10, 0, 0);//10
|
|
||||||
} else {
|
|
||||||
$startDate = now()->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 ($mailRules as $rule)
|
|
||||||
{
|
|
||||||
|
|
||||||
$toEmails = collect(explode(',', $rule->email))
|
|
||||||
->map(fn($e) => trim($e))
|
|
||||||
->filter()
|
|
||||||
->unique()
|
|
||||||
->values()
|
|
||||||
->toArray();
|
|
||||||
|
|
||||||
$ccEmails = collect(explode(',', $rule->cc_emails))
|
|
||||||
->map(fn($e) => trim($e))
|
|
||||||
->filter()
|
|
||||||
->unique()
|
|
||||||
->values()
|
|
||||||
->toArray();
|
|
||||||
|
|
||||||
if (empty($toEmails)) {
|
|
||||||
$this->warn("Skipping rule ID {$rule->id} — no valid To emails found.");
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$mail = new InvoiceDataMail($scheduleType, $tableData);
|
if (strtolower($scheduleType) == 'daily') {
|
||||||
|
$startDate = now()->subDay()->setTime(10, 0, 0);//8:00
|
||||||
foreach ($toEmails as $email) {
|
$endDate = now()->setTime(10, 0, 0);//8
|
||||||
\Mail::to($email)->cc($ccEmails)->send($mail);
|
} else {
|
||||||
|
$startDate = now()->setTime(8, 0, 0);
|
||||||
|
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info("Mail sent for rule ID {$rule->id} → To: " . implode(', ', $toEmails) .
|
// $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)
|
||||||
|
{
|
||||||
|
|
||||||
|
$tableData = [];
|
||||||
|
$no = 1;
|
||||||
|
$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'] ?? '');
|
||||||
|
|
||||||
|
foreach ($mailRules as $rule)
|
||||||
|
{
|
||||||
|
|
||||||
|
$toEmails = collect(explode(',', $rule->email))
|
||||||
|
->map(fn($e) => trim($e))
|
||||||
|
->filter()
|
||||||
|
->unique()
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
$ccEmails = collect(explode(',', $rule->cc_emails))
|
||||||
|
->map(fn($e) => trim($e))
|
||||||
|
->filter()
|
||||||
|
->unique()
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
if (empty($toEmails)) {
|
||||||
|
$this->warn("Skipping rule ID {$rule->id} — no valid To emails found.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
\Mail::to($toEmails)->cc($ccEmails)->send($mail);
|
||||||
|
|
||||||
|
$this->info("Mail sent for rule ID {$rule->id} → To: " . implode(', ', $toEmails) .
|
||||||
($ccEmails ? " | CC: " . implode(', ', $ccEmails) : ''));
|
($ccEmails ? " | CC: " . implode(', ', $ccEmails) : ''));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user