month = $month ?? Carbon::now()->month; $this->year = $year ?? Carbon::now()->year; } public function handle(): void { $users = User::get(); foreach ($users as $user) { // Nếu là nhân viên chưa chính thức, ko cộng phép if (!$user->is_permanent) { continue; } $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 { // Check có phải là nhân viên chính thức trong năm nay (Nhân viên mới) if ($user->permanent_date && $user->permanent_date !== '0000-00-00') { $permenantYear = Carbon::parse($user->permanent_date)->year; if ($permenantYear === $this->year) { $permanentCategory = Category::where('c_type', 'PERMANENT_ONLEAVE')->where('c_code', "PERMANENT")->first(); $permanentDefault = (int) $permanentCategory->c_value; // Ngày phép khi thành nv chính thức $permanentMonth = Carbon::parse($user->permanent_date)->month; if ($this->month > $leaveDay->ld_day_total - ($permanentDefault - $permanentMonth)) { $leaveDay->ld_day_total += self::ONLEAVE_PER_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(); } } } // Kiểm tra nếu số ngày phép hiện tại nhỏ hơn tháng hiện tại (Nhân viên cũ) if ($leaveDay->ld_day_total < $this->month) { // Cộng mỗi tháng 1 ngày phép cho nhân viên $leaveDay->ld_day_total += self::ONLEAVE_PER_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(); } } } } }