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(); } } } } }