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; } $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) ->where('notes.n_user_id', $user->id) ->where('notes.n_reason', 'ONLEAVE') ->groupBy(DB::raw('notes.n_year')) ->first(); if ($totalLeaveDaysByMonth) { //Nếu ngày phép thừa năm trước chưa sử dụng hết => cập nhật lại ngày đó (Ngày tồn đọng - ngày sử dụng) if ($existingData->ld_additional_day > $totalLeaveDaysByMonth->leave_days) { LeaveDays::where('ld_year', $this->year) ->where('ld_user_id', $user->id) ->update([ 'ld_additional_day' => $totalLeaveDaysByMonth->leave_days, ]); } } else { //Nếu không sử dụng ngày nghỉ còn lại ở năm rồi thì xóa => theo luật ld LeaveDays::where('ld_year', $this->year) ->where('ld_user_id', $user->id) ->update([ 'ld_additional_day' => "0", ]); } } } }