diff --git a/app/Console/Commands/Scheduler.php b/app/Console/Commands/Scheduler.php new file mode 100644 index 0000000..46f6407 --- /dev/null +++ b/app/Console/Commands/Scheduler.php @@ -0,0 +1,162 @@ +where('rule_name', 'ProductionMail') + ->select('plant', 'schedule_type') + ->distinct() + ->get(); + + foreach ($productionRules as $rule) { + switch ($rule->schedule_type) { + case 'Live': + // Run every minute + \Artisan::call('send:production-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + break; + case 'Hourly': + if (now()->minute == 0) { + \Artisan::call('send:production-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + case 'Daily': + if (now()->format('H:i') == '07:59') { + \Artisan::call('send:production-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + } + } + + + // --- Invoice Validation Rules --- + $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation') + ->select('plant', 'schedule_type') + ->distinct() + ->get(); + + foreach ($invoiceRules 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') == '07:59') { + \Artisan::call('send:invoice-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + } + } + + // --- Invoice Data Report Rules --- + $invoiceDataRules = AlertMailRule::where('module', 'InvoiceDataReport') + ->select('plant', 'schedule_type') + ->distinct() + ->get(); + + foreach ($invoiceDataRules as $rule) { + + switch ($rule->schedule_type) { + case 'Live': + // Run every minute + \Artisan::call('send:invoice-data-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + break; + case 'Hourly': + if (now()->minute == 0) { + \Artisan::call('send:invoice-data-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + case 'Daily': + if (now()->format('H:i') == '10:00') { + \Artisan::call('send:invoice-data-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + } + } + } + + + /** + * Helper to call Artisan commands with parameters. + */ + protected function callArtisanCommand($commandName, $rule) + { + \Artisan::call($commandName, [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + + $this->info("Executed {$commandName} for plant: {$rule->plant}"); + \Log::info("Executed {$commandName} for plant: {$rule->plant}"); + } +} diff --git a/routes/console.php b/routes/console.php index 5359745..3353679 100644 --- a/routes/console.php +++ b/routes/console.php @@ -2,8 +2,6 @@ use Illuminate\Foundation\Inspiring; use Illuminate\Support\Facades\Artisan; -use App\Models\AlertMailRule; -use Illuminate\Console\Scheduling\Schedule; @@ -11,87 +9,93 @@ Artisan::command('inspire', function () { $this->comment(Inspiring::quote()); })->purpose('Display an inspiring quote'); + +Artisan::command('auto:scheduler', function () { + $this->call('custom:scheduler'); +})->everyMinute(); + + // Schedule::command('send:invoice-report'); // Schedule::command('send:production-report'); -app()->booted(function () { - $schedule = app(Schedule::class); +// app()->booted(function () { +// $schedule = app(Schedule::class); - // $schedule->command('report:send-daily-production')->dailyAt('07:59'); - // Production report scheduling - $productionRules = AlertMailRule::where('module', 'ProductionQuantities') - ->where('rule_name', 'ProductionMail') - ->select('plant', 'schedule_type') - ->distinct() - ->get(); +// // $schedule->command('report:send-daily-production')->dailyAt('07:59'); +// // Production report scheduling +// $productionRules = AlertMailRule::where('module', 'ProductionQuantities') +// ->where('rule_name', 'ProductionMail') +// ->select('plant', 'schedule_type') +// ->distinct() +// ->get(); - foreach ($productionRules as $rule) { - $type = $rule->schedule_type; - $plantId = $rule->plant; +// foreach ($productionRules as $rule) { +// $type = $rule->schedule_type; +// $plantId = $rule->plant; - $command = $schedule->command('send:production-report', [$type, $plantId]); - // ->appendOutputTo(storage_path('logs/scheduler.log')); +// $command = $schedule->command('send:production-report', [$type, $plantId]); +// // ->appendOutputTo(storage_path('logs/scheduler.log')); - switch ($type) { - case 'Live': - $command->everyMinute(); - break; - case 'Hourly': - $command->hourly(); - break; - case 'Daily': - $command->dailyAt('07:59'); - break; - } - } +// switch ($type) { +// case 'Live': +// $command->everyMinute(); +// break; +// case 'Hourly': +// $command->hourly(); +// break; +// case 'Daily': +// $command->dailyAt('07:59'); +// break; +// } +// } - // Invoice report scheduling - $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation') - ->select('plant', 'schedule_type') - ->distinct() - ->get(); +// // Invoice report scheduling +// $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation') +// ->select('plant', 'schedule_type') +// ->distinct() +// ->get(); - foreach ($invoiceRules as $rule) { - $type = $rule->schedule_type; - $plantId = $rule->plant; +// foreach ($invoiceRules as $rule) { +// $type = $rule->schedule_type; +// $plantId = $rule->plant; - $command = $schedule->command('send:invoice-report', [$type, $plantId]); +// $command = $schedule->command('send:invoice-report', [$type, $plantId]); - switch ($type) { - case 'Live': - $command->everyMinute(); - break; - case 'Hourly': - $command->hourly(); - break; - case 'Daily': - $command->dailyAt('07:59'); - break; - } - } +// switch ($type) { +// case 'Live': +// $command->everyMinute(); +// break; +// case 'Hourly': +// $command->hourly(); +// break; +// case 'Daily': +// $command->dailyAt('07:59'); +// break; +// } +// } - // Invoice Data Report Scheduling - $invoiceDataRules = AlertMailRule::where('module', 'InvoiceDataReport') - ->select('plant', 'schedule_type') - ->distinct() - ->get(); +// // Invoice Data Report Scheduling +// $invoiceDataRules = AlertMailRule::where('module', 'InvoiceDataReport') +// ->select('plant', 'schedule_type') +// ->distinct() +// ->get(); - foreach ($invoiceDataRules as $rule) { - $type = $rule->schedule_type; - $plantId = $rule->plant; +// foreach ($invoiceDataRules as $rule) { +// $type = $rule->schedule_type; +// $plantId = $rule->plant; - $command = $schedule->command('send:invoice-data-report', [$type, $plantId]); +// $command = $schedule->command('send:invoice-data-report', [$type, $plantId]); - switch ($type) { - case 'Live': - $command->everyMinute(); - break; - case 'Hourly': - $command->hourly(); - break; - case 'Daily': - $command->dailyAt('10:00'); - break; - } - } -}); +// switch ($type) { +// case 'Live': +// $command->everyMinute(); +// break; +// case 'Hourly': +// $command->hourly(); +// break; +// case 'Daily': +// $command->dailyAt('10:00'); +// break; +// } +// } +// });