validated(); if ($validated) { $file = $request->file('attachment'); $filename = $file ? time() . '.' . $file->getClientOriginalExtension() : 'default.pdf'; if ($file) { // Simpan file yang diunggah $file->storeAs('public/uploads_pdf', $filename); } else { // Salin file default ke lokasi yang diinginkan Storage::copy('public/test/default.pdf', 'public/uploads_pdf/' . $filename); } // Tambahkan nama file ke data yang divalidasi $validated['attachment'] = $filename; // Simpan data ke database KJPP::create($validated); return redirect() ->route('basicdata.kjpp.index') ->with('success', 'KJPP created successfully'); } else { return redirect() ->route('basicdata.kjpp.create') ->with('error', 'Validation failed'); } } /** * Show the specified resource. */ public function show($id) { $kjpp = KJPP::find($id); $ijin_usaha = IjinUsaha::where('code', $kjpp->nomor_ijin_usaha)->get(); $ijin_usahas = IjinUsaha::all(); $jenis_jaminan = JenisJaminan::all(); $branches = Branch::where('name', $kjpp->jenis_kantor)->get(); $provinces = Province::where('code', $kjpp->province_code)->get(); $cities = City::where('code', $kjpp->city_code)->get(); $districts = District::where('code', $kjpp->district_code)->get(); $villages = Village::where('code', $kjpp->village_code)->get(); // dd($branches); return view('lpj::kjpp.show', compact('jenis_jaminan', 'ijin_usahas', 'ijin_usaha', 'branches', 'kjpp', 'provinces', 'cities', 'districts', 'villages')); } /** * Show the form for editing the specified resource. */ public function edit($id) { $kjpp = KJPP::find($id); $branch = Branch::all(); $ijin_usaha = IjinUsaha::all(); $jenis_aset = JenisJaminan::all(); $provinces = Province::all(); $cities = City::where('province_code', $kjpp->province_code)->get(); $districts = District::where('city_code', $kjpp->city_code)->get(); $villages = Village::where('district_code', $kjpp->district_code)->get(); return view('lpj::kjpp.create', compact('kjpp', 'branch', 'ijin_usaha', 'jenis_aset', 'provinces', 'cities', 'districts', 'villages')); } /** * Update the specified resource in storage. */ public function update(KJPPRequest $request, $id) { $validated = $request->validated(); if ($validated) { $file = $request->file('attachment'); $filename = $file ? time() . '.' . $file->getClientOriginalExtension() : null; if ($file !== null) { // Jika ada file dari database maka hapus file yang lama ke file yang baru $kjpp = KJPP::find($id); // Storage::delete('public/uploads_pdf/' . $kjpp->attachment); // Simpan file yang diunggah $file->storeAs('public/uploads_pdf', $filename); $validated['attachment'] = $filename; } else { // Jika tidak ada file yang diunggah, gunakan file yang sudah ada atau file default $kjpp = KJPP::find($id); $validated['attachment'] = $kjpp->attachment ?? 'default.pdf'; } // Perbarui data di database KJPP::where('id', $id)->update($validated); return redirect() ->route('basicdata.kjpp.index') ->with('success', 'KJPP updated successfully'); } else { return redirect() ->route('basicdata.kjpp.edit', $id) ->with('error', 'Validation failed'); } } /** * Remove the specified resource from storage. */ public function destroy($id) { try { $kjpp = KJPP::find($id); // Jangan hapus file default.pdf if ($kjpp->attachment && $kjpp->attachment !== 'default.pdf') { Storage::delete('public/uploads_pdf/' . $kjpp->attachment); } // Hapus data dari database $kjpp->delete(); echo json_encode(['success' => true, 'message' => 'KJPP deleted successfully']); } catch (Throwable $e) { echo json_encode(['success' => false, 'message' => 'Failed to delete branch: ' . $e]); } } public function dataForDatatables(Request $request) { if (is_null($this->user) || !$this->user->can('kjpp.view')) { //abort(403, 'Sorry! You are not allowed to view users.'); } // Retrieve data from the database $query = KJPP::query(); // Apply search filter if provided if ($request->has('search') && !empty($request->get('search'))) { $search = $request->get('search'); $query->where(function ($q) use ($search) { $q->where('code', 'LIKE', "%$search%"); $q->orWhere('name', 'LIKE', "%$search%"); $q->orWhere('jenis_kantor', 'LIKE', "%$search%"); }); } // Apply sorting if provided if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) { $order = $request->get('sortOrder'); $column = $request->get('sortField'); $query->orderBy($column, $order); } // Get the total count of records $totalRecords = $query->count(); // Apply pagination if provided if ($request->has('page') && $request->has('size')) { $page = $request->get('page'); $size = $request->get('size'); $offset = ($page - 1) * $size; // Calculate the offset $query->skip($offset)->take($size); } // Get the filtered count of records $filteredRecords = $query->count(); // Get the data for the current page $data = $query->get(); // Calculate the page count $pageCount = ceil($totalRecords / $request->get('size')); // Calculate the current page number $currentPage = 0 + 1; // Return the response data as a JSON object return response()->json([ 'draw' => $request->get('draw'), 'recordsTotal' => $totalRecords, 'recordsFiltered' => $filteredRecords, 'pageCount' => $pageCount, 'page' => $currentPage, 'totalCount' => $totalRecords, 'data' => $data, ]); } public function export() { return Excel::download(new KJPPExport, 'kjpp.xlsx'); } }