public function upload_save(Request $request)
 {
     $this->validate($request, ['excel' => 'required']);
     $inputFileName = $request->excel;
     try {
         $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
         $objReader = PHPExcel_IOFactory::createReader($inputFileType);
         $objPHPExcel = $objReader->load($inputFileName);
     } catch (Exception $e) {
         die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
     }
     $data_count = 0;
     $errors = 0;
     $semester = Semester::get_active_semester()->id;
     foreach ($objPHPExcel->getWorksheetIterator() as $sheet) {
         $highestRow = $sheet->getHighestRow();
         $highestColumn = $sheet->getHighestColumn();
         $start = false;
         for ($row = 1; $row <= $highestRow; $row++) {
             $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
             if (!$start) {
                 // Start check
                 if ($rowData[0][0] == "1" || $rowData[0][0] == "1.") {
                     $start = true;
                 }
             }
             if ($start) {
                 $id_siswa = null;
                 $data_count++;
                 if (!empty($rowData[0][1])) {
                     $siswa = Siswa::where('nis', $rowData[0][1])->first();
                     if (!$siswa) {
                         $errors++;
                         $id_siswa = null;
                         continue;
                     }
                     $id_siswa = $siswa->id;
                 }
                 if (!$id_siswa) {
                     continue;
                 }
                 $new = new NilaiSikap();
                 $created = null;
                 $check = NilaiSikap::where('id_siswa', $id_siswa)->where('id_semester', $semester);
                 $old = $check->first();
                 if ($old) {
                     $created = $old->created_at;
                     $check->delete();
                 }
                 $new->id_siswa = $id_siswa;
                 $new->id_semester = $semester;
                 $new->sikap = $rowData[0][3] ? $rowData[0][3] : null;
                 if ($created) {
                     $new->created_at = $created;
                 }
                 try {
                     $new->save();
                 } catch (\Illuminate\Database\QueryException $e) {
                     $errors++;
                 }
             }
         }
     }
     $message = "Upload file selesai. Terbaca ada {$data_count} data. ";
     $message .= $errors > 0 ? "Ada masalah dengan {$errors} data, dan tidak dapat dimasukkan ke dalam database." : "Semua data berhasil ditambahkan.";
     return redirect()->route('nilai.sikap')->with('message', $message);
 }
 public function make(Request $request)
 {
     $docObj = new PhpWord();
     $schDetails = Pengaturan::get_school_details();
     $printLocDate = $request->input('tempat_tanggal');
     $headmaster = Pengaturan::get_headmaster();
     $semester = Semester::get_active_semester();
     $siswa_to_print = [];
     $nis_array = [];
     if ($request->input('siswa') == 'kelas') {
         $siswa_to_print = Siswa::where('id_kelas', $request->input('kelas'))->get();
     } elseif ($request->input('siswa') == 'nis') {
         $nis_array = explode(' ', $request->input('nis'));
         foreach ($nis_array as $nis) {
             if (count($range = explode('-', $nis)) == 2) {
                 $range = Siswa::whereBetween('nis', $range)->get();
                 foreach ($range as $get) {
                     $siswa_to_print[] = $get;
                 }
             } elseif ($get = Siswa::where('nis', $nis)->first()) {
                 $siswa_to_print[] = $get;
             } else {
                 return back()->with('message', "NIS {$nis} tidak ditemukan.");
             }
         }
     } else {
         return back();
     }
     if (count($siswa_to_print) < 1) {
         return back()->with('message', "Tidak ada data untuk dicetak.");
     }
     $id_kelas = 0;
     $mapel_list = null;
     foreach ($siswa_to_print as $siswa) {
         if ($siswa->id_kelas == null) {
             continue;
         }
         if (!Semester::is_active_latest()) {
             $siswa->kelas_link->tingkat -= Semester::get_year_difference();
         }
         if ($request->input('cover')) {
             $docObj = $this->wordHalamanCover($docObj, $siswa);
             $docObj = $this->wordHalamanDataSekolah($docObj, $schDetails);
         }
         if ($request->input('bio')) {
             $docObj = $this->wordHalamanBiodata($docObj, $siswa, $printLocDate, $headmaster);
         }
         if ($request->input('nilai')) {
             if ($id_kelas != $siswa->id_kelas) {
                 $id_kelas = $siswa->id_kelas;
                 $mapel_list = NilaiAkhir::get_mapel_list($id_kelas);
             }
             $sikap = NilaiSikap::get_nilai($siswa->id);
             $nilai['sikap'] = $sikap ? $sikap->sikap : "";
             $nilai['mapel'] = NilaiAkhir::get_all_nilai($siswa, $mapel_list);
             $absensi = Absensi::get_absensi($siswa->id);
             $nilai['absensi'] = ['sakit' => $absensi ? $absensi->sakit : null, 'izin' => $absensi ? $absensi->izin : null, 'alpa' => $absensi ? $absensi->alpa : null];
             $nilai['prestasi'] = Prestasi::get_for_print($siswa->id, $semester->id);
             $nilai['ekskul'] = Ekskul::get_for_print($siswa->id, $semester->id);
             $nilai['pkl'] = Pkl::get_for_print($siswa->id, $semester->id);
             $docObj = $this->wordHalamanNilai($docObj, $schDetails, $siswa, $nilai, $headmaster, $printLocDate, $semester);
         }
     }
     $objWriter = IOFactory::createWriter($docObj, 'Word2007');
     $fn = date('Y-n-j_His');
     if ($request->input('siswa') == 'kelas') {
         $kelas = Kelas::find($request->input('kelas'));
         if (!Semester::is_active_latest()) {
             $kelas->tingkat -= Semester::get_year_difference();
         }
         $fn .= "_" . $kelas->name(false);
     }
     if ($request->input('siswa') == 'nis') {
         $fn .= "_" . $nis_array[0] . '-' . $nis_array[count($nis_array) - 1];
     }
     $objWriter->save("print/{$fn}.docx");
     return Response::download("print/{$fn}.docx", "{$fn}.docx", ['Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document']);
 }