|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use App\Models\Appointment; |
| 4 | +use App\Models\EmployeeInfo; |
4 | 5 | use Carbon\Carbon; |
5 | 6 | use Illuminate\Console\Scheduling\Schedule; |
6 | 7 | use Illuminate\Foundation\Inspiring; |
|
22 | 23 | })->purpose('Mark missed appointments as missed in the system automatically') |
23 | 24 | ->describe('This command checks for appointments that were scheduled in the past and updates their status to "missed" if they were not completed or canceled.'); |
24 | 25 |
|
| 26 | +Artisan::command('contracts:deactivate-expired-employees', function () { |
| 27 | + $now = Carbon::now(); |
| 28 | + $employeesWithExpiredContracts = EmployeeInfo::with('contracts') |
| 29 | + ->where('is_active', true) |
| 30 | + ->get() |
| 31 | + ->filter(function ($employee) use ($now) { |
| 32 | + if ($employee->active_contract) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + $latest = $employee->contracts->sortByDesc('end_date')->first(); |
| 37 | + |
| 38 | + return $latest && Carbon::parse($latest->end_date)->lt($now); |
| 39 | + }); |
| 40 | + |
| 41 | + $employeesWithExpiredContracts->each(fn ($employee) => $employee->update(['is_active' => false])); |
| 42 | + |
| 43 | + $this->info("Deactivated {$employeesWithExpiredContracts->count()} employees with expired contracts."); |
| 44 | +})->purpose('Deactivate employees with expired contracts automatically') |
| 45 | + ->describe('This command checks for employees whose contracts have expired and deactivates their accounts.'); |
| 46 | + |
25 | 47 | $schedule = app(Schedule::class); |
26 | 48 |
|
27 | 49 | /** Schedule missed appointments marking |
28 | 50 | * This command will run daily to mark missed appointments. |
29 | 51 | * If in development environment, you can run it more frequently for testing purposes. |
30 | 52 | */ |
31 | | -$schedule->command('appointments:mark-missed-appointments')->daily()->withoutOverlapping(); |
| 53 | +if (app()->environment('local', 'development')) { |
| 54 | + $schedule->command('appointments:mark-missed-appointments')->everyFiveMinutes()->withoutOverlapping(); |
| 55 | + $schedule->command('contracts:deactivate-expired-employees')->everyFiveMinutes()->withoutOverlapping(); |
| 56 | +} else { |
| 57 | + $schedule->command('appointments:mark-missed-appointments')->daily()->withoutOverlapping(); |
| 58 | + $schedule->command('contracts:deactivate-expired-employees')->daily()->withoutOverlapping(); |
| 59 | +} |
0 commit comments