From 80806d0f1721053c0de6619225a74e13f0d0d925 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 29 Nov 2025 10:42:20 +0530 Subject: [PATCH] Added schedule method logic in app service provider --- app/Providers/AppServiceProvider.php | 108 +++++++++++++++++--- routes/console.php | 142 +++++++++++++-------------- 2 files changed, 163 insertions(+), 87 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 7075bdc..a5eff69 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,19 +2,19 @@ namespace App\Providers; +use App\Models\AlertMailRule; use App\Models\User; -use Filament\Facades\FilamentView; -use Illuminate\Support\Facades\Gate; -use Illuminate\Support\ServiceProvider; -use App\Policies\RolePolicy; use App\Policies\PermissionPolicy; -use Spatie\Permission\Models\Role; -use Spatie\Permission\Models\Permission; -use Filament\Support\Facades\FilamentAsset; +use App\Policies\RolePolicy; use Filament\Support\Assets\Js; +use Filament\Support\Facades\FilamentAsset; +use Illuminate\Console\Scheduling\Schedule; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\Vite; - +use Illuminate\Support\ServiceProvider; +use Spatie\Permission\Models\Permission; +use Spatie\Permission\Models\Role; // use Doctrine\DBAL\Types\Type; @@ -28,7 +28,6 @@ class AppServiceProvider extends ServiceProvider // } - /** * Bootstrap any application services. */ @@ -42,21 +41,21 @@ class AppServiceProvider extends ServiceProvider // }); Gate::before(function (User $user, string $ability) { - return $user->isSuperAdmin() ? true: null; + return $user->isSuperAdmin() ? true : null; }); // FilamentAsset::register([ // Js::make('chart-js-plugins', Vite::asset('resources/js/filament-chart-js-plugins.js'))->module(), // ]); FilamentAsset::register([ - Js::make('chart-js-plugins', Vite::asset('resources/js/filament-chart-js-plugins.js'))->module(), + Js::make('chart-js-plugins', Vite::asset('resources/js/filament-chart-js-plugins.js'))->module(), ]); - // if (env('APP_MODE') === 'admin') { - // FilamentAsset::register([ - // Js::make('chart-js-plugins', Vite::asset('resources/js/filament-chart-js-plugins.js'))->module(), - // ]); - // } + // if (env('APP_MODE') === 'admin') { + // FilamentAsset::register([ + // Js::make('chart-js-plugins', Vite::asset('resources/js/filament-chart-js-plugins.js'))->module(), + // ]); + // } // URL::forceScheme('https'); @@ -70,5 +69,82 @@ class AppServiceProvider extends ServiceProvider ini_set('max_execution_time', 300); // 300 seconds = 5 minutes ini_set('memory_limit', '512M'); // 512MB + $schedule = app(Schedule::class); + + $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; + + $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; + } + } + + // 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; + + $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; + } + } + + // 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; + + $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; + } + } } } diff --git a/routes/console.php b/routes/console.php index d809e28..b9300ac 100644 --- a/routes/console.php +++ b/routes/console.php @@ -13,88 +13,88 @@ Artisan::command('inspire', function () { // 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'); +// // $schedule->command('report:send-daily-production')->dailyAt('07:59'); - if (Schema::hasTable('alert_mail_rules')) { +// if (Schema::hasTable('alert_mail_rules')) { - // Production report scheduling - $productionRules = AlertMailRule::where('module', 'ProductionQuantities') - ->where('rule_name', 'ProductionMail') - ->select('plant', 'schedule_type') - ->distinct() - ->get(); +// // 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; +// } +// } +// } +// });