public function getUpload(Request $request) { $role = $this->cekRole(); $actifity = ''; $view = ''; $unit_id = $request->input('unit'); $sap_log = Saplog::where('status', 'A')->firstOrFail(); $array = ['asal_surat', 'actifity', 'jenis_dokumen', 'kode_jra', 'pr', 'po', 'cd', 'gr', 'file_pdf', 'unit_tujuan', 'lokasi_file', 'visibility']; if (\Gate::allows('upload-cek', 'admin')) { if ($request->input('unit') != null) { $unit = unit::where('id', $unit_id)->where('id', '!=', '1')->get(); } else { $unit = unit::where('id', '!=', '1')->get(); } $actifity = Actifity::all(); $unit_tujuan = unit::all(); $visibility = Visibility::all(); } else { $unit = unit::where('id', Auth::user()->personil->unit->id)->get(); $unit_tujuan = unit::all(); $actifity = Actifity::where('unit_id', Auth::user()->personil->unit->id)->get(); $visibility = Visibility::all(); } return view('dokumen/upload', compact('array', 'unit', 'visibility', 'actifity', 'unit_tujuan', 'sap_log')); }
public function postUploadExcel(Request $request) { $validator = \Validator::make($request->all(), ['format1' => 'required']); if ($validator->fails()) { \Session::flash('alert-warning', 'Upload File ZPC dengan format CSV'); return redirect()->back(); } $file = $request->file('format1'); if (Schema::hasTable('sap_')) { Schema::drop('sap_'); } $file_csv = fopen($file->getPathName(), 'r'); $header = fgetcsv($file_csv, ','); $header = $this->renameData($header); $header = $this->cekDuplicate($header); // dd($header); if (!Schema::hasTable('sap_')) { Schema::create('sap_', function (Blueprint $table) use($header) { $table->increments('id'); $rep = [' ', '.']; foreach ($header as $key => $value) { $val = str_replace($rep, '_', $value); $table->string(strtolower($val))->nullable(); } }); } $count = 0; while ($line = fgetcsv($file_csv, ',')) { $count++; for ($i = 0; $i < count($header); $i++) { if ($line[$i] === '') { $line[$i] = null; } $data[$header[$i]] = $line[$i]; } DB::table('sap_')->insert($data); } $storage1['lokasi_file'] = "file_zpc"; $storage['file_name'] = $file->getClientOriginalName(); $storage['jumlah_row'] = $count + 1; $storage['status'] = 'A'; $storage['created_by'] = Auth::user()->id; // $cek = Saplog::where('file_name', $storage['file_name'])->firstOrFail(); // if ($cek){ // $storage['file_name']= $storage['file_name'].'_rev1.csv'; // } Saplog::where('status', 'A')->update(array('status' => 'N')); Saplog::create($storage); Storage::disk('local')->put($storage1['lokasi_file'] . '/' . $storage['file_name'], File::get($file)); return redirect('hrga/view-upload-data'); }