From 308f5a3a2ee1b550061e56ae0472ecd467f85290 Mon Sep 17 00:00:00 2001 From: Truong Vo <41848815+vmtruong301296@users.noreply.github.com> Date: Tue, 15 Apr 2025 10:34:54 +0700 Subject: [PATCH] =?UTF-8?q?[Ng=C3=A0y=20Ph=C3=A9p]=20Th=E1=BB=B1c=20hi?= =?UTF-8?q?=E1=BB=87n=20Job=20c=E1=BA=ADp=20nh=E1=BA=ADt=20ng=C3=A0y=20ph?= =?UTF-8?q?=C3=A9p=20b=E1=BA=A3ng=20m=E1=BB=97i=20th=C3=A1ng=20+=201=20ng?= =?UTF-8?q?=C3=A0y=20ph=C3=A9p=20cho=20m=E1=BB=97i=20user,=20th=E1=BB=9Di?= =?UTF-8?q?=20gian=2000:00=20ng=C3=A0y=201=20h=C3=A0ng=20th=C3=A1ng?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/AddMonthlyLeaveDaysCommand.php | 24 +++++++ .../Commands/InitializeLeaveDaysCommand.php | 5 +- BACKEND/app/Console/Kernel.php | 6 +- BACKEND/app/Jobs/AddMonthlyLeaveDays.php | 70 +++++++++++++++++++ 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 BACKEND/app/Console/Commands/AddMonthlyLeaveDaysCommand.php create mode 100644 BACKEND/app/Jobs/AddMonthlyLeaveDays.php 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(); + } + } + } + } +}