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,22 +32,18 @@ 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.");
|
||||
@@ -55,10 +54,11 @@ class SendInvoiceDataReport extends Command
|
||||
// $startDate = now()->subDay()->setTime(10, 0, 0);//8:00
|
||||
// $endDate = now()->setTime(10, 0, 0);//8
|
||||
// }
|
||||
|
||||
if (strtolower($scheduleType) == 'daily')
|
||||
{
|
||||
$firstRecord = \App\Models\InvoiceDataValidation::orderBy('document_date', 'asc')->first();
|
||||
$lastRecord = \App\Models\InvoiceDataValidation::orderBy('document_date', 'desc')->first();
|
||||
$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();
|
||||
@@ -73,7 +73,8 @@ class SendInvoiceDataReport extends Command
|
||||
$endDate = now()->endOfDay();
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
$startDate = now()->setTime(8, 0, 0);
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
}
|
||||
@@ -85,8 +86,7 @@ class SendInvoiceDataReport extends Command
|
||||
$tableData = [];
|
||||
|
||||
// $distributions = ['Direct Sale', 'Branch Sale', 'Internal Transfer', 'WOS', ''];
|
||||
|
||||
$distributions = \App\Models\InvoiceDataValidation::whereNotNull('distribution_channel_desc')
|
||||
$distributions = InvoiceDataValidation::whereNotNull('distribution_channel_desc')
|
||||
->distinct()
|
||||
->pluck('distribution_channel_desc')
|
||||
->filter(fn($val) => trim($val) != '')
|
||||
@@ -97,7 +97,7 @@ class SendInvoiceDataReport extends Command
|
||||
|
||||
foreach ($distributions as $selectedDistribution)
|
||||
{
|
||||
$invoices = \App\Models\InvoiceDataValidation::where('plant_id', $plant->id)
|
||||
$invoices = InvoiceDataValidation::where('plant_id', $plant->id)
|
||||
->where('distribution_channel_desc', $selectedDistribution)
|
||||
->whereBetween('document_date', [$startDate, $endDate])
|
||||
->orderBy('document_date', 'asc')
|
||||
@@ -130,7 +130,7 @@ class SendInvoiceDataReport extends Command
|
||||
->map(fn($n) => preg_replace('/\s+/', '', strtoupper((string) $n)))
|
||||
->toArray();
|
||||
|
||||
$wentOutInvoices = \App\Models\InvoiceOutValidation::where('plant_id', $plant->id)
|
||||
$wentOutInvoices = InvoiceOutValidation::where('plant_id', $plant->id)
|
||||
->whereIn('qr_code', $invoiceNumbers)
|
||||
->whereBetween('scanned_at', [$startDate, $endDate])
|
||||
->distinct('qr_code')
|
||||
@@ -139,10 +139,10 @@ class SendInvoiceDataReport extends Command
|
||||
->toArray();
|
||||
|
||||
// if (!empty($wentOutInvoices)) {
|
||||
// $deletedValidations = \App\Models\InvoiceDataValidation::whereIn('document_number', $wentOutInvoices)
|
||||
// $deletedValidations = InvoiceDataValidation::whereIn('document_number', $wentOutInvoices)
|
||||
// ->delete();
|
||||
|
||||
// $deletedOuts = \App\Models\InvoiceOutValidation::whereIn('qr_code', $wentOutInvoices)
|
||||
// $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}).");
|
||||
@@ -167,10 +167,8 @@ class SendInvoiceDataReport extends Command
|
||||
|
||||
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])) {
|
||||
@@ -215,7 +213,6 @@ class SendInvoiceDataReport extends Command
|
||||
|
||||
foreach ($mailRules as $rule)
|
||||
{
|
||||
|
||||
$toEmails = collect(explode(',', $rule->email))
|
||||
->map(fn($e) => trim($e))
|
||||
->filter()
|
||||
@@ -237,8 +234,7 @@ class SendInvoiceDataReport extends Command
|
||||
|
||||
\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