public function approve($order_id) { $order = OrderChangeDepartment::findOrFail($order_id); $student_id = $order->student_id; $depid = $order->to; $current_dep = $order->from; $StudentHistory = StudentHistory::where('student_id', $student_id)->where('academycycle_semester_id', semester()->id)->update(['academystructure_department_id' => $depid]); $student = Student::with('subjects')->where('id', $student_id)->first(); $student->academystructure_department_id = $depid; $student->save(); $department = Department::findOrFail($current_dep); $current_subject_ids = json_decode($department->subject_ids, TRUE); $current_payed_subject_ids = StudentSubject::select('subject_id')->where('semester_id', semester()->id)->where('student_id', $student_id)->where('state', 'study')->where('payed', 1)->get(); $cuttent_invoice_amount = 0; if ($current_payed_subject_ids->count() > 0) { $cuttent_invoice_amount = Subject::select(DB::raw('sum(hour*amount) as total_amount'))->whereIn('id', $current_payed_subject_ids)->first(); } $new_department = Department::findOrFail($depid); $new_subject_ids = json_decode($new_department->subject_ids, TRUE); //if($current_dep == 8 or $current_dep == 13 or $current_dep =14 ){ //update department subjects StudentSubject::where('semester_id', semester()->id)->where('student_id', $student_id)->where('state', 'study')->whereIn('subject_id', $current_subject_ids)->update(['state' => 'drop']); foreach ($new_subject_ids as $new_subject_id) { $new_student_subject = new StudentSubject(); $new_student_subject->subject_id = $new_subject_id; $new_student_subject->student_id = $student_id; $new_student_subject->semester_id = semester()->id; $new_student_subject->academystructure_department_id = $depid; $new_student_subject->state = 'study'; $new_student_subject->save(); } if ($current_payed_subject_ids->count() > 0) { $new_payed_subject_ids = StudentSubject::select('subject_id')->where('semester_id', semester()->id)->where('student_id', $student_id)->where('state', 'study')->get(); $new_invoice_amount = Subject::select(DB::raw('sum(hour*amount) as total_amount'))->whereIn('id', $new_payed_subject_ids)->first(); if ($new_invoice_amount < $cuttent_invoice_amount) { StudentSubject::where('semester_id', semester()->id)->where('student_id', $student_id)->where('state', 'study')->update(['payed' => 1]); $invoiceData = ['ref_key' => 'Order_Change_AC_Departments', 'ref_value' => $order_id, 'student_id' => $student_id, 'amount' => $cuttent_invoice_amount->total_amount - $new_invoice_amount->total_amount, 'type' => 'credit', 'semester_id' => semester()->id, 'note' => 'فرق تغيير التخصص']; FinancialInvoice::create($invoiceData); } if ($new_invoice_amount > $cuttent_invoice_amount) { StudentSubject::where('semester_id', semester()->id)->where('student_id', $student_id)->where('state', 'study')->update(['payed' => 1]); } if ($new_invoice_amount > $cuttent_invoice_amount) { StudentSubject::where('semester_id', semester()->id)->where('student_id', $student_id)->where('state', 'study')->where('subject_id', '!=', 35)->update(['payed' => 1]); } } //updtae remain subjects (not in dep id) to be in same depid StudentSubject::where('semester_id', semester()->id)->where('student_id', $student_id)->where('state', 'study')->update(['academystructure_department_id' => $depid]); $order->state = 1; $order->save(); $msg = 'تم تغيير التخصص '; return redirect()->route('orders.change.department.index')->with('success', $msg); // }else{ // } }