Esempio n. 1
0
 public function equalsubject(Request $request)
 {
     // for all_complete or part complete or all_refuse
     $complete = 0;
     $refuse = 0;
     $equal_subject;
     $order_state = '';
     foreach ($request->order_equal_subject_id as $key => $value) {
         $equal_subject = EqualSubject::with('order')->findOrFail($value);
         if ($request->equal_subject_id[$key] != '-1') {
             $equal_subject->equal_subject_id = $request->equal_subject_id[$key];
             $equal_subject->is_equal = 1;
             $complete = 1;
             // convert student_subject state to equal
             if ($equal_subject->save()) {
                 StudentSubject::where("subject_id", $equal_subject->equal_subject_id)->where('student_id', $equal_subject->order->student_id)->update(['state' => 'equal']);
             }
         } else {
             $equal_subject->is_equal = -1;
             $refuse = 1;
             $equal_subject->save();
         }
     }
     //convert order_equal state to (complete or part complete)
     $equal_order = Equal::findOrFail($equal_subject->equal_order_id);
     if ($complete == 0 && $refuse == 0) {
         // no subjetct sent
     } elseif ($complete == 1 && $refuse == 0) {
         $order_state = 'قبول كلى';
     } elseif ($complete == 0 && $refuse == 1) {
         $order_state = 'رفض كلى';
     } else {
         $order_state = 'قبول جزئي';
     }
     $equal_order->state = $order_state;
     $equal_order->save();
     //save in history
     $order_history = new Orderhistory();
     $order_history->ref_key = 'order_equals';
     $order_history->ref_value = $equal_order->id;
     $order_history->state = $order_state;
     //$order_history ->note =$request->note;
     $order_history->created_by = user()->id;
     $order_history->save();
     return redirect()->route('equal.show', $equal_order->id)->with('success', trans('orders::order.change_success'));
 }
 /**
  * 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);
             }
         }
     }
 }