Ejemplo n.º 1
0
 /**
  * Handle the event.
  *
  * @param  RegistrationStepChanged  $event
  * @return void
  */
 public function handle(RegistrationStepChanged $event)
 {
     $registration = $event->registration;
     $semester = semester();
     $finish = $semester ? $semester->finish_at : date('Y-m-d');
     if ($semester->order != 'first') {
         $semester = Semester::where('order', 'first')->where('start_at', '>', $finish)->orderBy('start_at', 'ASC')->first();
     }
     if (empty($semester)) {
         return false;
     }
     $registration->load('step', 'student', 'contactcountry', 'files');
     if ($registration->step->enroll == 1 && !$registration->student) {
         $equation_subjects_ids = RegistrationEquationSubject::whereNotNull('subject_id')->where('status', 'accepted')->whereHas('equation', function ($w) use($registration) {
             $w->where('registration_id', $registration->id);
         })->pluck('subject_id')->toArray();
         $subject_ids = [];
         $i = 0;
         $department = 0;
         while (count(array_diff($subject_ids, $equation_subjects_ids)) == 0) {
             $department = Department::where('spec_id', $registration->academystructure_specialty_id)->where(function ($w) use($department) {
                 $w->orWhere('parent_id', !empty($department->id) ? $department->id : 0);
                 $w->orWhereNull('parent_id');
             })->first();
             $subject_ids = json_decode($department->subject_ids, TRUE);
             $i++;
             if ($i == 20) {
                 break;
             }
         }
         if (empty($subject_ids)) {
             return false;
         }
         $subjects = Subject::whereIn('id', $subject_ids)->get();
         $mobile = ($registration->contactcountry ? $registration->contactcountry->calling_code : "968") . $registration->contact_mobile;
         $data = ['username' => $registration->username, 'username_prefix' => $registration->enroll_code, 'password' => bcrypt($registration->contact_mobile), 'name' => $registration->fullname, 'email' => $registration->contact_email, 'mobile' => $mobile, 'state' => 'active', 'study_state' => 'active', 'national_id' => $registration->national_id, 'gender' => $registration->gender, 'registration_id' => $registration->id, 'academystructure_department_id' => $department->id, 'registration_type_id' => $registration->registration_type_id];
         if ($registration->files) {
             $photo = $registration->files->first(function ($key, $value) {
                 return $key == 'photo';
             });
             if ($photo) {
                 // $data['photo'] = asset($photo->file->url());
             }
         }
         Eloquent::unguard();
         $student = Student::create($data);
         $invoiceItem = FinancialInvoiceItem::where('slug', 'study_fee')->first();
         $study_fee = $registration->study_fee > 0 ? $registration->study_fee : $invoiceItem->amount;
         $total_amount = $subjects->sum(function ($s) use($equation_subjects_ids, $study_fee) {
             $amount = 0;
             if (!in_array($s->id, $equation_subjects_ids)) {
                 $amount = !$s->is_quran ? $s->hour * (int) $study_fee : $s->hour * ((int) $study_fee / 2);
             }
             return $amount;
         });
         if ($student) {
             if ($registration->debit && $invoiceItem) {
                 $invoiceData = ['ref_key' => 'registrations', 'ref_value' => $registration->id, 'student_id' => $student->id, 'amount' => $registration->debit, 'type' => 'credit', 'item_id' => $invoiceItem->id, 'pay_type' => 'epay', 'bank_code' => $registration->reason_code, 'transaction_wid' => $registration->transaction_uuid, 'note' => 'ايصال دفع مصاريف التسجيل.'];
                 FinancialInvoice::create($invoiceData);
                 // create debit invoice
                 $invoiceData = ['ref_key' => 'registrations', 'ref_value' => $registration->id, 'student_id' => $student->id, 'amount' => $total_amount, 'type' => 'debit', 'item_id' => $invoiceItem->id, 'pay_type' => 'epay', 'note' => 'فاتةورة التسجيل.'];
                 FinancialInvoice::create($invoiceData);
             }
             foreach ($subjects as $subject) {
                 $subject_state = in_array($subject->id, $equation_subjects_ids) ? 'equal' : 'study';
                 $subject_payed = in_array($subject->id, $equation_subjects_ids) ? 0 : 1;
                 if (!$registration->debit && $registration->step->make_payment) {
                     $subject_payed = 0;
                 }
                 $data = ['student_id' => $student->id, 'subject_id' => $subject->id, 'semester_id' => $semester->id, 'academystructure_department_id' => $department->id, 'state' => $subject_state, 'note' => '', 'payed' => $subject_payed];
                 $studentSubject = StudentSubject::create($data);
             }
             $historyData = ['student_id' => $student->id, 'state' => 'active', 'study_state' => 'active', 'academycycle_semester_id' => $semester->id, 'academystructure_department_id' => $department->id];
             StudentHistory::create($historyData);
             $this->createStudentEmail($student, $registration->contact_mobile);
             try {
                 if (!empty($photo)) {
                     $student->photo = $photo;
                     $student->save();
                 }
             } catch (\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException $e) {
             }
         }
         //\Log::info('turn to student '.$response);
     }
 }