fix run cron job initialize leaveDays

This commit is contained in:
Nguyễn Hoàng Vĩ 2026-01-06 16:02:24 +07:00
parent d7b7a61511
commit 78066f624e
4 changed files with 60 additions and 18 deletions

View File

@ -24,9 +24,11 @@ class Kernel extends ConsoleKernel
// $schedule->command('daily:api-call') // $schedule->command('daily:api-call')
// ->dailyAt('18:00'); // ->dailyAt('18:00');
// Chạy command vào ngày 31/12 lúc 23:59:59 mỗi năm // Chạy command vào ngày 01/01 lúc 00:00 mỗi năm
$schedule->command('initialize:leavedays')->yearlyOn(12, 31, '23:59:59'); $schedule->command('initialize:leavedays')->yearlyOn(1, 1, '00:00');
$schedule->command('leave:deduct')->yearlyOn(3, 31, '23:59:59');
// 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 // Chạy buổi sáng lúc 12:00
$schedule->command('attendance:check S')->dailyAt('12:00'); $schedule->command('attendance:check S')->dailyAt('12:00');

View File

@ -3,6 +3,7 @@
namespace App\Jobs; namespace App\Jobs;
use App\Models\LeaveDays; use App\Models\LeaveDays;
use App\Models\Notes;
use App\Models\User; use App\Models\User;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
@ -11,6 +12,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Modules\Admin\app\Models\Category; use Modules\Admin\app\Models\Category;
use Illuminate\Support\Facades\DB;
class AddMonthlyLeaveDays implements ShouldQueue class AddMonthlyLeaveDays implements ShouldQueue
{ {
@ -46,15 +48,48 @@ class AddMonthlyLeaveDays implements ShouldQueue
->where('ld_year', $this->year) ->where('ld_year', $this->year)
->first(); ->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 // 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([ $leaveDay = new LeaveDays([
'ld_user_id' => $user->id, 'ld_user_id' => $user->id,
'ld_day_total' => $this->month, // Số ngày phép bằng tháng hiện tại 'ld_day_total' => $this->month, // Số ngày phép bằng tháng hiện tại
'ld_year' => $this->year, 'ld_year' => $this->year,
'ld_additional_day' => 0, 'ld_additional_day' => $ld_additional_day,
'ld_note' => 'Khởi tạo ngày phép đến tháng ' . $this->month, 'ld_note' => $ld_note,
'ld_special_leave_day' => 0, 'ld_special_leave_day' => 0,
]); ]);
$leaveDay->save(); $leaveDay->save();

View File

@ -55,10 +55,12 @@ class DeductLeaveDays implements ShouldQueue
if ($usedOnleaveDaysTotal) { if ($usedOnleaveDaysTotal) {
if ($existingData->ld_additional_day > $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"; $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; $existingData->ld_additional_day = $usedOnleaveDaysTotal;
} }
} else { } 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; $existingData->ld_additional_day = 0;
} }

View File

@ -42,16 +42,6 @@ class InitializeLeaveDays implements ShouldQueue
continue; 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 // Kiểm tra dữ liệu của user này trong năm trước
$previousYearData = LeaveDays::where('ld_user_id', $user->id) $previousYearData = LeaveDays::where('ld_user_id', $user->id)
->where('ld_year', $this->year - 1) ->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 // Tạo dữ liệu cho năm hiện tại
LeaveDays::insert([ LeaveDays::insert([
'ld_user_id' => $user->id, 'ld_user_id' => $user->id,