diff --git a/BACKEND/app/Console/Kernel.php b/BACKEND/app/Console/Kernel.php index 3e9cf06..d97c98d 100755 --- a/BACKEND/app/Console/Kernel.php +++ b/BACKEND/app/Console/Kernel.php @@ -24,9 +24,11 @@ class Kernel extends ConsoleKernel // $schedule->command('daily:api-call') // ->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('leave:deduct')->yearlyOn(3, 31, '23:59:59'); + // Chạy command vào ngày 01/01 lúc 00:00 mỗi năm + $schedule->command('initialize:leavedays')->yearlyOn(1, 1, '00:00'); + + // Chạy command vào ngày 01/04 lúc 00:00 mỗi năm + $schedule->command('leave:deduct')->yearlyOn(4, 1, '00:00'); // Chạy buổi sáng lúc 12:00 $schedule->command('attendance:check S')->dailyAt('12:00'); diff --git a/BACKEND/app/Jobs/AddMonthlyLeaveDays.php b/BACKEND/app/Jobs/AddMonthlyLeaveDays.php index c63d683..ec8d482 100644 --- a/BACKEND/app/Jobs/AddMonthlyLeaveDays.php +++ b/BACKEND/app/Jobs/AddMonthlyLeaveDays.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Models\LeaveDays; +use App\Models\Notes; use App\Models\User; use Carbon\Carbon; use Illuminate\Bus\Queueable; @@ -11,6 +12,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Modules\Admin\app\Models\Category; +use Illuminate\Support\Facades\DB; class AddMonthlyLeaveDays implements ShouldQueue { @@ -46,15 +48,48 @@ class AddMonthlyLeaveDays implements ShouldQueue ->where('ld_year', $this->year) ->first(); - if (!$leaveDay && $this->month > 1) { + 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 + $previousYearData = LeaveDays::where('ld_user_id', $user->id) + ->where('ld_year', $this->year - 1) + ->first(); + $ld_additional_day = 0; + $ld_note = ''; + + if ($previousYearData) { + $ld_additional_day = $previousYearData->ld_day_total + $previousYearData->ld_additional_day + $previousYearData->ld_special_leave_day; + $totalLeaveDaysByMonth = Notes::join('categories', function ($join) { + $join->on('notes.n_time_type', '=', 'categories.c_code') + ->where('categories.c_type', 'TIME_TYPE'); + }) + ->select( + DB::raw('notes.n_user_id as n_user_id'), + DB::raw('notes.n_year as year'), + DB::raw('SUM(categories.c_value) as leave_days') + ) + ->where('notes.n_year', $this->year - 1) + ->where('notes.n_user_id', $user->id) + ->where('notes.n_reason', 'ONLEAVE') + ->groupBy(DB::raw('notes.n_year')) + ->first(); + if ($totalLeaveDaysByMonth) { + $ld_additional_day = $ld_additional_day - $totalLeaveDaysByMonth->leave_days; + if ($ld_additional_day < 0) { + $ld_additional_day = 0; + } + } + + if ($ld_additional_day > 0) { + $ld_note = "Cộng " . $ld_additional_day . " ngày phép tồn năm trước. \n"; + } + } + $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_additional_day' => $ld_additional_day, + 'ld_note' => $ld_note, 'ld_special_leave_day' => 0, ]); $leaveDay->save(); diff --git a/BACKEND/app/Jobs/DeductLeaveDays.php b/BACKEND/app/Jobs/DeductLeaveDays.php index 3dd165a..0b6afbe 100644 --- a/BACKEND/app/Jobs/DeductLeaveDays.php +++ b/BACKEND/app/Jobs/DeductLeaveDays.php @@ -55,10 +55,12 @@ class DeductLeaveDays implements ShouldQueue if ($usedOnleaveDaysTotal) { if ($existingData->ld_additional_day > $usedOnleaveDaysTotal) { $ld_note = "Trừ " . $existingData->ld_additional_day - $usedOnleaveDaysTotal . " ngày phép tồn năm trước. \n"; - $existingData->ld_note = $existingData->ld_note . "\n" . $ld_note; + $existingData->ld_note = $existingData->ld_note . $ld_note; $existingData->ld_additional_day = $usedOnleaveDaysTotal; } } else { + $ld_note = "Trừ " . $existingData->ld_additional_day . " ngày phép tồn năm trước. \n"; + $existingData->ld_note = $existingData->ld_note . $ld_note; $existingData->ld_additional_day = 0; } diff --git a/BACKEND/app/Jobs/InitializeLeaveDays.php b/BACKEND/app/Jobs/InitializeLeaveDays.php index ea79ed4..cb398e1 100644 --- a/BACKEND/app/Jobs/InitializeLeaveDays.php +++ b/BACKEND/app/Jobs/InitializeLeaveDays.php @@ -42,16 +42,6 @@ class InitializeLeaveDays implements ShouldQueue continue; } - // Kiểm tra xem dữ liệu của user này đã tồn tại cho năm hiện tại chưa - $existingData = LeaveDays::where('ld_user_id', $user->id) - ->where('ld_year', $this->year) - ->first(); - - if ($existingData) { - // Nếu dữ liệu đã tồn tại, bỏ qua user này - continue; - } - // Kiểm tra dữ liệu của user này trong năm trước $previousYearData = LeaveDays::where('ld_user_id', $user->id) ->where('ld_year', $this->year - 1) @@ -88,6 +78,19 @@ class InitializeLeaveDays implements ShouldQueue } } + // Kiểm tra xem dữ liệu của user này đã tồn tại cho năm hiện tại chưa + $existingData = LeaveDays::where('ld_user_id', $user->id) + ->where('ld_year', $this->year) + ->first(); + + if ($existingData) { + // Nếu dữ liệu đã tồn tại, update lại phép tồn + $existingData->ld_note = $ld_note; + $existingData->ld_additional_day = $ld_additional_day; + $existingData->save(); + continue; + } + // Tạo dữ liệu cho năm hiện tại LeaveDays::insert([ 'ld_user_id' => $user->id,