year = $year ?? Carbon::now()->year; } /** * Execute the job. */ public function handle() { $users = User::get(); foreach ($users as $user) { $existingData = LeaveDays::where('ld_user_id', $user->id) ->where('ld_year', $this->year) ->where('ld_additional_day', ">", 0) ->first(); if (!$existingData) { continue; } // Lấy tổng ngày nghỉ phép 3 tháng đầu trong năm $usedOnleaveDaysTotal = Notes::join('categories', function ($join) { $join->on('notes.n_time_type', '=', 'categories.c_code') ->where('categories.c_type', 'TIME_TYPE'); }) ->where('n_user_id', $user->id) ->where('n_year', $this->year) ->where('n_month', "<=", 3) ->where('n_reason', 'ONLEAVE') ->sum('categories.c_value'); 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_additional_day = $usedOnleaveDaysTotal; } } else { $existingData->ld_additional_day = 0; } $existingData->save(); } } }