/**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     $student = Student::find($this->student_id);
     $currentSemester = semester();
     $summerSemester = Semester::where('id', '>', $currentSemester->id)->where('order', 'summer')->orderBy('id', 'ASC')->first();
     // Log::info($student->id, $currentSemester->id, $summerSemester->id);
     if ($student && $currentSemester && $summerSemester) {
         $nextSemester = Semester::where('id', '>', $currentSemester->id)->where('order', '!=', 'summer')->orderBy('id', 'ASC')->first();
         if (!$nextSemester) {
             return false;
         }
         $current_department_id = $student->academystructure_department_id;
         if ($currentSemester->order != 'summer') {
             $nextDepartment = Department::where('parent_id', $student->academystructure_department_id)->first();
             if ($nextDepartment) {
                 $student->state = 'active';
                 $student->study_state = 'success';
                 $student->academystructure_department_id = $nextDepartment->id;
             }
         }
         $history = StudentHistory::where('student_id', $student->id)->orderBy('id', 'desc')->where('academycycle_semester_id', $currentSemester->id)->first();
         if ($history) {
             $history->study_state = 'success';
             $history->save();
         }
         if ($student->save()) {
             // get current semester subjects and make them fail or success for current semester
             $subjects = StudentSubject::selectRaw("subsub.id as subject_id,SUM(stugr.value) AS subject_points,student_subjects.state as subject_state,subsub.name AS subject_name, subsub.hour AS subject_hours")->join('subject_subjects AS subsub', function ($join) {
                 $join->on('subsub.id', '=', 'student_subjects.subject_id');
             })->where('student_subjects.student_id', $student->id)->leftJoin('student_grades AS stugr', function ($join) use($currentSemester, $student) {
                 $join->on('stugr.subject_id', '=', 'subsub.id')->where('stugr.semester_id', '=', $currentSemester->id)->where('stugr.student_id', '=', $student->id);
             })->whereIn('state', ['study'])->where('student_subjects.semester_id', '=', $currentSemester->id)->where('student_subjects.student_id', $student->id)->groupBy('subsub.id')->get();
             $failed_subjects_ids = [];
             $passed_subjects_ids = [];
             foreach ($subjects as $subject) {
                 if ($subject->subject_points < 50) {
                     $failed_subjects_ids[] = $subject->subject_id;
                 } elseif ($subject->subject_points >= 50) {
                     $passed_subjects_ids[] = $subject->subject_id;
                 }
             }
             StudentSubject::where('student_id', $student->id)->whereIn('subject_id', $failed_subjects_ids)->update(['state' => 'fail']);
             StudentSubject::where('student_id', $student->id)->whereIn('subject_id', $passed_subjects_ids)->update(['state' => 'success']);
             // end success and fail
             if ($currentSemester->order != 'summer' && $nextDepartment) {
                 $next_subject_ids = json_decode($nextDepartment->subject_ids, TRUE);
                 $equal_subject_ids = EqualSubject::whereHas('order', function ($w) use($student) {
                     $w->where('student_id', $student->id);
                 })->pluck('equal_subject_id')->toArray();
                 $next_subject_study_ids = array_diff($next_subject_ids, $equal_subject_ids);
                 $next_subject_equal_ids = array_intersect($next_subject_ids, $equal_subject_ids);
                 $student->subjects()->attach($next_subject_study_ids, ['semester_id' => $nextSemester->id, 'academystructure_department_id' => $nextDepartment->id, 'state' => 'study']);
                 $student->subjects()->attach($next_subject_equal_ids, ['semester_id' => $nextSemester->id, 'academystructure_department_id' => $nextDepartment->id, 'state' => 'equal']);
                 $data = ['state' => 'active', 'study_state' => 'active', 'academycycle_semester_id' => $nextSemester->id, 'academystructure_department_id' => $nextDepartment->id];
                 $student->histories()->create($data);
                 // create invoice for student
                 if ($next_subject_equal_ids) {
                     $item = FinancialInvoiceItem::where('slug', 'study_fee')->first();
                     $subjects_count = Subject::whereIn('id', $next_subject_equal_ids)->where('is_quran', 0)->count();
                     $data = ['student_id' => $student->id, 'type' => 'debit', 'item_id' => $item->id, 'pay_type' => NULL, 'semester_id' => $nextSemester->id, 'amount' => $subjects_count * $item->amount];
                     FinancialInvoice::create($data);
                 }
             }
             // move any failed subjects if less than 3 to summer semester
             $all_failed_subjects = StudentSubject::groupBy('subject_id')->where('state', 'fail')->pluck('subject_id')->toArray();
             $all_passed_subjects = StudentSubject::groupBy('subject_id')->where('state', 'success')->pluck('subject_id')->toArray();
             $failed_subjects = array_diff($all_failed_subjects, $all_passed_subjects);
             if (COUNT($failed_subjects) <= 3) {
                 $summerSemester = Semester::where('id', '>', $currentSemester->id)->where('order', 'summer')->orderBy('id', 'ASC')->first();
                 $student->subjects()->attach($failed_subjects, ['semester_id' => $summerSemester->id, 'academystructure_department_id' => $current_department_id, 'state' => 'study']);
                 $data = ['state' => 'active', 'study_state' => 'active', 'academycycle_semester_id' => $summerSemester->id, 'academystructure_department_id' => $current_department_id];
                 $student->histories()->create($data);
             }
         }
     }
 }