diff --git a/app/Console/Commands/Scheduler.php b/app/Console/Commands/Scheduler.php index 6d75a9a..e579fee 100644 --- a/app/Console/Commands/Scheduler.php +++ b/app/Console/Commands/Scheduler.php @@ -77,7 +77,7 @@ class Scheduler extends Command } } - // --- Invoice Validation Rules --- + // --- Invoice Validation Rules --- (Invoice Mail) $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation') ->where('rule_name', 'InvoiceMail') ->select('plant', 'schedule_type') @@ -104,7 +104,83 @@ class Scheduler extends Command } break; case 'Daily': - if (now()->format('H:i') == '07:59') { + if (now()->format('H:i') == '17:55') { + \Artisan::call('send:invoice-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + } + } + + //..SerialInvoice + + $invoiceSerialRules = AlertMailRule::where('module', 'InvoiceValidation') + ->where('rule_name', 'SerialInvoiceMail') + ->select('plant', 'schedule_type') + ->distinct() + ->get(); + + foreach ($invoiceSerialRules as $rule) { + + switch ($rule->schedule_type) { + case 'Live': + // Run every minute + \Artisan::call('send:invoice-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + break; + + case 'Hourly': + if (now()->minute == 0) { + \Artisan::call('send:invoice-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + case 'Daily': + if (now()->format('H:i') == '17:55') { + \Artisan::call('send:invoice-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + } + } + + //..MaterialInvoice + + $invoiceMaterialRules = AlertMailRule::where('module', 'InvoiceValidation') + ->where('rule_name', 'MaterialInvoiceMail') + ->select('plant', 'schedule_type') + ->distinct() + ->get(); + + foreach ($invoiceMaterialRules as $rule) { + + switch ($rule->schedule_type) { + case 'Live': + // Run every minute + \Artisan::call('send:invoice-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + break; + + case 'Hourly': + if (now()->minute == 0) { + \Artisan::call('send:invoice-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + case 'Daily': + if (now()->format('H:i') == '17:55') { \Artisan::call('send:invoice-report', [ 'schedule_type' => $rule->schedule_type, 'plant' => $rule->plant, diff --git a/app/Console/Commands/SendInvoiceReport.php b/app/Console/Commands/SendInvoiceReport.php index 74bf002..c44adc2 100644 --- a/app/Console/Commands/SendInvoiceReport.php +++ b/app/Console/Commands/SendInvoiceReport.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use App\Mail\test; +use App\Models\AlertMailRule; use App\Models\InvoiceValidation; use App\Models\Plant; use Illuminate\Console\Command; @@ -31,19 +32,31 @@ class SendInvoiceReport extends Command public function handle() { $schedule = $this->argument('schedule_type'); - // $scheduleType = $this->argument('scheduleType'); $plantIdArg = (int) $this->argument('plant'); - $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name'); + $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation') + ->where('rule_name', 'InvoiceMail') + ->where('plant', $plantIdArg) + ->get(); - // $startDate = now()->setTime(8, 0, 0); - // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + $serialRules = AlertMailRule::where('module', 'InvoiceValidation') + ->where('rule_name', 'SerialInvoiceMail') + ->where('plant', $plantIdArg) + ->get(); + + $materialRules = AlertMailRule::where('module', 'InvoiceValidation') + ->where('rule_name', 'MaterialInvoiceMail') + ->where('plant', $plantIdArg) + ->get(); + + // $this->info('Invoice Rules: ' . $invoiceRules->toJson()); + // $this->info('Serial Rules: ' . $serialRules->toJson()); + // $this->info('Material Rules: ' . $materialRules->toJson()); $serialTableData = []; $materialTableData = []; $bundleTableData = []; - // Get plant IDs: either one plant or all $plantIds = $plantIdArg == 0 ? InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id')->toArray() : [$plantIdArg]; @@ -251,11 +264,23 @@ class SendInvoiceReport extends Command // } // } - if ($mailRules->has('SerialInvoiceMail')) { + if ($serialRules->isNotEmpty()) { - foreach ($mailRules->get('SerialInvoiceMail') as $rule) { + foreach ($serialRules as $rule) { - if ($isPlantRun && $rule->plant != $plantIdArg && $rule->plant != 0) { + if ( + $isPlantRun && + $plantIdArg != 0 && + $rule->plant != 0 && + $rule->plant != $plantIdArg + ) { + continue; + } + + if ( + $plantIdArg == 0 && + $rule->plant != 0 + ) { continue; } @@ -273,12 +298,12 @@ class SendInvoiceReport extends Command } // Send to MaterialInvoiceMail recipients (material + bundle table) - if ($mailRules->has('MaterialInvoiceMail')) { + if ($materialRules->isNotEmpty()) { // $emails = $mailRules['MaterialInvoiceMail']->pluck('email')->unique()->toArray(); // foreach ($emails as $email) { // Mail::to($email)->send(new test([], $materialTableData, $bundleTableData, $schedule)); // } - foreach ($mailRules->get('MaterialInvoiceMail') as $rule) { + foreach ($materialRules as $rule) { $toEmails = collect(explode(',', $rule->email)) ->map(fn ($e) => trim($e)) @@ -334,17 +359,21 @@ class SendInvoiceReport extends Command } } - // Send to InvoiceMail recipients (all three tables) - if ($mailRules->has('InvoiceMail')) { + if ($invoiceRules->isNotEmpty()) { - $invoiceRules = $mailRules->get('InvoiceMail'); - $hasGlobalRule = $invoiceRules->contains('plant', 0); - - // foreach ($mailRules->get('InvoiceMail') as $rule) { foreach ($invoiceRules as $rule) { - if ($hasGlobalRule && $rule->plant != 0) { - $this->info("Skipping rule ID {$rule->id} (plant={$rule->plant}) — covered by global plant=0 rule."); + // During global run, only execute plant=0 rule + if ($plantIdArg == 0 && $rule->plant != 0) { + continue; + } + + // During plant run, only execute matching rule + if ( + $plantIdArg != 0 && + $rule->plant != 0 && + $rule->plant != $plantIdArg + ) { continue; }