Skip to content

Commit 645d6f0

Browse files
feat(ConsoleCommands): add command to deactivate employees with expired contracts
1 parent 5a58249 commit 645d6f0

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

routes/console.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use App\Models\Appointment;
4+
use App\Models\EmployeeInfo;
45
use Carbon\Carbon;
56
use Illuminate\Console\Scheduling\Schedule;
67
use Illuminate\Foundation\Inspiring;
@@ -22,10 +23,37 @@
2223
})->purpose('Mark missed appointments as missed in the system automatically')
2324
->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.');
2425

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+
2547
$schedule = app(Schedule::class);
2648

2749
/** Schedule missed appointments marking
2850
* This command will run daily to mark missed appointments.
2951
* If in development environment, you can run it more frequently for testing purposes.
3052
*/
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

Comments
 (0)