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'); $existingData->ld_additional_day = $usedOnleaveDaysTotal ?? 0; $existingData->save(); } } }