Refactor SendInvoiceDataReport command for improved readability and consistency
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Mail\InvoiceDataMail;
|
||||
use App\Models\AlertMailRule;
|
||||
use App\Models\InvoiceDataValidation;
|
||||
use App\Models\InvoiceOutValidation;
|
||||
use App\Models\Line;
|
||||
use App\Models\Plant;
|
||||
use App\Models\ProductionPlan;
|
||||
@@ -29,172 +32,167 @@ class SendInvoiceDataReport extends Command
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$scheduleType = $this->argument('schedule_type');
|
||||
$plantId = (int) $this->argument('plant');
|
||||
|
||||
|
||||
$mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceDataReport')
|
||||
$mailRules = AlertMailRule::where('module', 'InvoiceDataReport')
|
||||
->where('rule_name', 'InvoiceDataMail')
|
||||
->where('schedule_type', $scheduleType)
|
||||
->where('plant', $plantId)
|
||||
->get();
|
||||
|
||||
$plants = $plantId == 0
|
||||
? Plant::all()
|
||||
: Plant::where('id', $plantId)->get();
|
||||
$plants = ($plantId == 0) ? Plant::all() : Plant::where('id', $plantId)->get();
|
||||
|
||||
if ($plants->isEmpty()) {
|
||||
$this->error("No valid plant(s) found.");
|
||||
return;
|
||||
if ($plants->isEmpty()) {
|
||||
$this->error("No valid plant(s) found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// if (strtolower($scheduleType) == 'daily') {
|
||||
// $startDate = now()->subDay()->setTime(10, 0, 0);//8:00
|
||||
// $endDate = now()->setTime(10, 0, 0);//8
|
||||
// }
|
||||
|
||||
if (strtolower($scheduleType) == 'daily')
|
||||
{
|
||||
$firstRecord = InvoiceDataValidation::orderBy('document_date', 'asc')->first();
|
||||
$lastRecord = InvoiceDataValidation::orderBy('document_date', 'desc')->first();
|
||||
|
||||
if ($firstRecord && $lastRecord) {
|
||||
$startDate = \Carbon\Carbon::parse($firstRecord->document_date)->startOfDay();
|
||||
//$endDate = \Carbon\Carbon::parse($lastRecord->document_date)->endOfDay();
|
||||
$endDate = \Carbon\Carbon::parse($lastRecord->document_date)
|
||||
->addDay()
|
||||
->setTime(10, 0, 0);
|
||||
}
|
||||
|
||||
// if (strtolower($scheduleType) == 'daily') {
|
||||
// $startDate = now()->subDay()->setTime(10, 0, 0);//8:00
|
||||
// $endDate = now()->setTime(10, 0, 0);//8
|
||||
// }
|
||||
if (strtolower($scheduleType) == 'daily')
|
||||
else
|
||||
{
|
||||
$firstRecord = \App\Models\InvoiceDataValidation::orderBy('document_date', 'asc')->first();
|
||||
$lastRecord = \App\Models\InvoiceDataValidation::orderBy('document_date', 'desc')->first();
|
||||
|
||||
if ($firstRecord && $lastRecord) {
|
||||
$startDate = \Carbon\Carbon::parse($firstRecord->document_date)->startOfDay();
|
||||
//$endDate = \Carbon\Carbon::parse($lastRecord->document_date)->endOfDay();
|
||||
$endDate = \Carbon\Carbon::parse($lastRecord->document_date)
|
||||
->addDay()
|
||||
->setTime(10, 0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
$startDate = now()->startOfDay();
|
||||
$endDate = now()->endOfDay();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$startDate = now()->setTime(8, 0, 0);
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
$startDate = now()->startOfDay();
|
||||
$endDate = now()->endOfDay();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$startDate = now()->setTime(8, 0, 0);
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
}
|
||||
|
||||
//..
|
||||
//..
|
||||
|
||||
foreach ($plants as $plant)
|
||||
foreach ($plants as $plant)
|
||||
{
|
||||
$tableData = [];
|
||||
|
||||
// $distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer', 'WOS', ''];
|
||||
$distributions = InvoiceDataValidation::whereNotNull('distribution_channel_desc')
|
||||
->distinct()
|
||||
->pluck('distribution_channel_desc')
|
||||
->filter(fn($val) => trim($val) != '')
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
$distributions[] = '';
|
||||
|
||||
foreach ($distributions as $selectedDistribution)
|
||||
{
|
||||
$tableData = [];
|
||||
$invoices = 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')
|
||||
->get()
|
||||
->unique('document_number')
|
||||
->values();
|
||||
|
||||
// $distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer', 'WOS', ''];
|
||||
if ($invoices->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$distributions = \App\Models\InvoiceDataValidation::whereNotNull('distribution_channel_desc')
|
||||
->distinct()
|
||||
->pluck('distribution_channel_desc')
|
||||
->filter(fn($val) => trim($val) != '')
|
||||
->values()
|
||||
->toArray();
|
||||
// Filter invoices directly — exclude ones with '-' in document_number
|
||||
$invoices = $invoices->filter(function ($inv) {
|
||||
return !empty($inv->document_number) && !str_contains($inv->document_number, '-');
|
||||
});
|
||||
|
||||
$distributions[] = '';
|
||||
|
||||
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')
|
||||
->get()
|
||||
->unique('document_number')
|
||||
->values();
|
||||
|
||||
if ($invoices->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Filter invoices directly — exclude ones with '-' in document_number
|
||||
if (trim($selectedDistribution) == '' || $selectedDistribution == null) {
|
||||
$invoices = $invoices->filter(function ($inv) {
|
||||
return !empty($inv->document_number) && !str_contains($inv->document_number, '-');
|
||||
return str_starts_with($inv->document_number, '7');
|
||||
});
|
||||
}
|
||||
|
||||
if (trim($selectedDistribution) == '' || $selectedDistribution == null) {
|
||||
$invoices = $invoices->filter(function ($inv) {
|
||||
return str_starts_with($inv->document_number, '7');
|
||||
});
|
||||
}
|
||||
if ($invoices->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($invoices->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$invoiceNumbers = $invoices
|
||||
$invoiceNumbers = $invoices
|
||||
->pluck('document_number')
|
||||
->map(fn($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
|
||||
->toArray();
|
||||
|
||||
$wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
|
||||
->whereIn('qr_code', $invoiceNumbers)
|
||||
->whereBetween('scanned_at', [$startDate, $endDate])
|
||||
->distinct('qr_code')
|
||||
->pluck('qr_code')
|
||||
->map(fn($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
|
||||
->toArray();
|
||||
$wentOutInvoices = InvoiceOutValidation::where('plant_id', $plant->id)
|
||||
->whereIn('qr_code', $invoiceNumbers)
|
||||
->whereBetween('scanned_at', [$startDate, $endDate])
|
||||
->distinct('qr_code')
|
||||
->pluck('qr_code')
|
||||
->map(fn($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
|
||||
->toArray();
|
||||
|
||||
// if (!empty($wentOutInvoices)) {
|
||||
// $deletedValidations = \App\Models\InvoiceDataValidation::whereIn('document_number', $wentOutInvoices)
|
||||
// ->delete();
|
||||
// if (!empty($wentOutInvoices)) {
|
||||
// $deletedValidations = InvoiceDataValidation::whereIn('document_number', $wentOutInvoices)
|
||||
// ->delete();
|
||||
|
||||
// $deletedOuts = \App\Models\InvoiceOutValidation::whereIn('qr_code', $wentOutInvoices)
|
||||
// ->delete();
|
||||
// $deletedOuts = 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}).");
|
||||
// }
|
||||
// $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(strtoupper(trim($inv->document_number)), $wentOutInvoices);
|
||||
// });
|
||||
// $pendingInvoices = $invoices->filter(function ($inv) use ($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) {
|
||||
$doc = preg_replace('/\s+/', '', strtoupper((string) $inv->document_number));
|
||||
return !in_array($doc, $wentOutInvoices, true);
|
||||
});
|
||||
$pendingInvoices = $invoices->filter(function ($inv) use ($wentOutInvoices) {
|
||||
$doc = preg_replace('/\s+/', '', strtoupper((string) $inv->document_number));
|
||||
return !in_array($doc, $wentOutInvoices, true);
|
||||
});
|
||||
|
||||
|
||||
if ($pendingInvoices->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($pendingInvoices as $inv)
|
||||
{
|
||||
|
||||
$yesterday = now()->subDay()->toDateString();
|
||||
$today = now()->toDateString();
|
||||
|
||||
$documentDate = \Carbon\Carbon::parse($inv->document_date);
|
||||
|
||||
if (in_array($documentDate->toDateString(), [$today, $yesterday])) {
|
||||
$statusColor = 'status-pending-yellow';
|
||||
} else {
|
||||
$statusColor = 'status-pending-red';
|
||||
}
|
||||
|
||||
$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',
|
||||
'status_class' => $statusColor,
|
||||
];
|
||||
}
|
||||
if ($pendingInvoices->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tableData = collect($tableData)
|
||||
foreach ($pendingInvoices as $inv)
|
||||
{
|
||||
$yesterday = now()->subDay()->toDateString();
|
||||
$today = now()->toDateString();
|
||||
$documentDate = \Carbon\Carbon::parse($inv->document_date);
|
||||
|
||||
if (in_array($documentDate->toDateString(), [$today, $yesterday])) {
|
||||
$statusColor = 'status-pending-yellow';
|
||||
} else {
|
||||
$statusColor = 'status-pending-red';
|
||||
}
|
||||
|
||||
$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',
|
||||
'status_class' => $statusColor,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$tableData = collect($tableData)
|
||||
->sortBy('document_date')
|
||||
->values()
|
||||
->map(function ($item, $index) {
|
||||
@@ -203,42 +201,40 @@ class SendInvoiceDataReport extends Command
|
||||
})
|
||||
->toArray();
|
||||
|
||||
$mail = new InvoiceDataMail($scheduleType, $tableData);
|
||||
$contentVars = $mail->content()->with;
|
||||
$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'] ?? '');
|
||||
$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)
|
||||
{
|
||||
foreach ($mailRules as $rule)
|
||||
{
|
||||
$toEmails = collect(explode(',', $rule->email))
|
||||
->map(fn($e) => trim($e))
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
$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();
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
if (empty($toEmails)) {
|
||||
$this->warn("Skipping rule ID {$rule->id} — no valid To emails found.");
|
||||
continue;
|
||||
}
|
||||
\Mail::to($toEmails)->cc($ccEmails)->send($mail);
|
||||
|
||||
\Mail::to($toEmails)->cc($ccEmails)->send($mail);
|
||||
|
||||
$this->info("Mail sent for rule ID {$rule->id} → To: " . implode(', ', $toEmails) .
|
||||
($ccEmails ? " | CC: " . implode(', ', $ccEmails) : ''));
|
||||
$this->info("Mail sent for rule ID {$rule->id} → To: " . implode(', ', $toEmails) . ($ccEmails ? " | CC: " . implode(', ', $ccEmails) : ''));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user