diff --git a/BACKEND/app/Console/Commands/AddMonthlyLeaveDaysCommand.php b/BACKEND/app/Console/Commands/AddMonthlyLeaveDaysCommand.php new file mode 100644 index 0000000..f691d15 --- /dev/null +++ b/BACKEND/app/Console/Commands/AddMonthlyLeaveDaysCommand.php @@ -0,0 +1,24 @@ +argument('month'); + $year = $this->argument('year'); + AddMonthlyLeaveDays::dispatch($month, $year); + } +} \ No newline at end of file diff --git a/BACKEND/app/Console/Commands/InitializeLeaveDaysCommand.php b/BACKEND/app/Console/Commands/InitializeLeaveDaysCommand.php index ad0a9e9..bf74a72 100644 --- a/BACKEND/app/Console/Commands/InitializeLeaveDaysCommand.php +++ b/BACKEND/app/Console/Commands/InitializeLeaveDaysCommand.php @@ -8,7 +8,7 @@ use App\Jobs\InitializeLeaveDays; class InitializeLeaveDaysCommand extends Command { protected $signature = 'initialize:leavedays {year?}'; - protected $description = 'Initialize leave days for users'; + protected $description = 'Cấp phép năm cho tất cả người dùng'; public function __construct() { @@ -18,6 +18,7 @@ class InitializeLeaveDaysCommand extends Command public function handle() { $year = $this->argument('year'); - InitializeLeaveDays::dispatch($year); + // Không sử dụng nữa, theo rule mới + // InitializeLeaveDays::dispatch($year); } } diff --git a/BACKEND/app/Console/Kernel.php b/BACKEND/app/Console/Kernel.php index 6d56e12..d8fa86b 100755 --- a/BACKEND/app/Console/Kernel.php +++ b/BACKEND/app/Console/Kernel.php @@ -3,6 +3,7 @@ namespace App\Console; use App\Jobs\DeductLeaveDays; +use App\Jobs\AddMonthlyLeaveDays; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -24,7 +25,7 @@ class Kernel extends ConsoleKernel // ->dailyAt('18:00'); // Chạy command vào ngày 31/12 lúc 23:59:59 mỗi năm - $schedule->command('initialize:leavedays')->yearlyOn(12, 31, '23:59:59'); + // $schedule->command('initialize:leavedays')->yearlyOn(12, 31, '23:59:59'); $schedule->command('leave:deduct')->yearlyOn(3, 31, '23:59:59'); // Chạy buổi sáng lúc 12:00 @@ -32,6 +33,9 @@ class Kernel extends ConsoleKernel // Chạy buổi chiều lúc 17:30 $schedule->command('attendance:check C')->dailyAt('17:30'); + + // Chạy vào 00:01 ngày đầu tiên của mỗi tháng + $schedule->command('add:monthly-leavedays')->monthlyOn(1, '00:01'); } /** diff --git a/BACKEND/app/Jobs/AddMonthlyLeaveDays.php b/BACKEND/app/Jobs/AddMonthlyLeaveDays.php new file mode 100644 index 0000000..3e933b7 --- /dev/null +++ b/BACKEND/app/Jobs/AddMonthlyLeaveDays.php @@ -0,0 +1,70 @@ +month = $month ?? Carbon::now()->month; + $this->year = $year ?? Carbon::now()->year; + } + + public function handle(): void + { + $users = User::get(); + + foreach ($users as $user) { + $leaveDay = LeaveDays::where('ld_user_id', $user->id) + ->where('ld_year', $this->year) + ->first(); + + if (!$leaveDay) { + // Nếu chưa có dữ liệu năm hiện tại, tạo mới + // Số ngày phép bằng với tháng hiện tại + $leaveDay = new LeaveDays([ + 'ld_user_id' => $user->id, + 'ld_day_total' => $this->month, // Số ngày phép bằng tháng hiện tại + 'ld_year' => $this->year, + 'ld_additional_day' => 0, + 'ld_note' => 'Khởi tạo ngày phép đến tháng ' . $this->month, + 'ld_special_leave_day' => 0, + ]); + $leaveDay->save(); + } else { + // Kiểm tra nếu số ngày phép hiện tại nhỏ hơn tháng hiện tại + if ($leaveDay->ld_day_total < $this->month) { + // Cập nhật số ngày phép bằng với tháng hiện tại + $oldDays = $leaveDay->ld_day_total; + $leaveDay->ld_day_total = $this->month; + + // Xử lý ghi chú + $newNote = "Cập nhật ngày phép đến tháng " . $this->month; + if (!empty($leaveDay->ld_note)) { + // Nếu đã có ghi chú, thêm ghi chú mới vào và xuống dòng + $leaveDay->ld_note = $leaveDay->ld_note . "\n" . $newNote; + } else { + // Nếu chưa có ghi chú, gán ghi chú mới + $leaveDay->ld_note = $newNote; + } + + $leaveDay->save(); + } + } + } + } +}