first(); $nextNumber = $lastKjpp ? intval(substr($lastKjpp->code, 1, 6)) + 1 : 1; $kjppNumber = 'K' . str_pad($nextNumber, 6, '0', STR_PAD_LEFT); // Combine KJPP number with branch code $fullKjppNumber = $kjppNumber; $emailKantor = []; $namaPimpinan = []; $nomorHpPimpinan = []; $namaPicReviewer = []; $nomorHpPicReviewer = []; $namaPicAdmin = []; $nomorHpPicAdmin = []; $namaPicMarketing = []; $nomorHpPicMarketing = []; return view('lpj::kjpp.create', compact('ijin_usaha', 'jenis_aset', 'provinces', 'fullKjppNumber', 'emailKantor', 'namaPimpinan', 'nomorHpPimpinan', 'namaPicReviewer', 'nomorHpPicReviewer', 'namaPicAdmin', 'nomorHpPicAdmin', 'namaPicMarketing', 'nomorHpPicMarketing')); } /** * Store a newly created resource in storage. */ public function store(KJPPRequest $request) { $validated = $request->validated(); $validated['email_kantor'] = json_encode($validated['email_kantor']); $validated['nama_pimpinan'] = json_encode($validated['nama_pimpinan']); $validated['nomor_hp_pimpinan'] = json_encode($validated['nomor_hp_pimpinan']); $validated['nama_pic_reviewer'] = json_encode($validated['nama_pic_reviewer']); $validated['nomor_hp_pic_reviewer'] = json_encode($validated['nomor_hp_pic_reviewer']); $validated['nama_pic_admin'] = json_encode($validated['nama_pic_admin']); $validated['nomor_hp_pic_admin'] = json_encode($validated['nomor_hp_pic_admin']); $validated['nama_pic_marketing'] = json_encode($validated['nama_pic_marketing']); $validated['nomor_hp_pic_marketing'] = json_encode($validated['nomor_hp_pic_marketing']); // Konversi array ke JSON untuk ijin_usaha_id $validated['ijin_usaha_id'] = json_encode($validated['ijin_usaha_id']); // Konversi jenis_aset_id ke JSON, dengan penanganan array kosong $validated['jenis_aset_id'] = $validated['jenis_aset_id'] ? json_encode($validated['jenis_aset_id']) : json_encode([]); // Handle file attachment if ($request->hasFile('attachment')) { $file = $request->file('attachment'); $filename = time() . '.' . $file->getClientOriginalExtension(); // Simpan file $file->storeAs('public/uploads_pdf', $filename); $validated['attachment'] = $filename; } else { // Gunakan file default jika tidak ada file yang diunggah $defaultFilename = 'default.pdf'; Storage::copy('public/test/default.pdf', 'public/uploads_pdf/' . $defaultFilename); $validated['attachment'] = $defaultFilename; } // Hapus baris dd() sebelum membuat record // dd($validated); try { // Buat record KJPP $kjpp = KJPP::create($validated); return redirect() ->route('basicdata.kjpp.index') ->with('success', 'KJPP created successfully'); } catch (\Exception $e) { // Tangani kesalahan jika ada return redirect() ->route('basicdata.kjpp.create') ->with('error', 'Failed to create KJPP: ' . $e->getMessage()); } } /** * 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(); $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(); $emailKantor = json_decode($kjpp->email_kantor); $namaPimpinan = json_decode($kjpp->nama_pimpinan); $nomorHpPimpinan = json_decode($kjpp->nomor_hp_pimpinan); $namaPicReviewer = json_decode($kjpp->nama_pic_reviewer); $nomorHpPicReviewer = json_decode($kjpp->nomor_hp_pic_reviewer); $namaPicAdmin = json_decode($kjpp->nama_pic_admin); $nomorHpPicAdmin = json_decode($kjpp->nomor_hp_pic_admin); $namaPicMarketing = json_decode($kjpp->nama_pic_marketing); $nomorHpPicMarketing = json_decode($kjpp->nomor_hp_pic_marketing); return view('lpj::kjpp.show', compact('jenis_jaminan', 'ijin_usahas', 'ijin_usaha', 'kjpp', 'provinces', 'cities', 'districts', 'villages', 'emailKantor', 'namaPimpinan', 'nomorHpPimpinan', 'namaPicReviewer', 'nomorHpPicReviewer', 'namaPicAdmin', 'nomorHpPicAdmin', 'namaPicMarketing', 'nomorHpPicMarketing')); } /** * Show the form for editing the specified resource. */ public function edit($id) { $kjpp = KJPP::find($id); $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(); $emailKantor = json_decode($kjpp->email_kantor); $namaPimpinan = json_decode($kjpp->nama_pimpinan); $nomorHpPimpinan = json_decode($kjpp->nomor_hp_pimpinan); $namaPicReviewer = json_decode($kjpp->nama_pic_reviewer); $nomorHpPicReviewer = json_decode($kjpp->nomor_hp_pic_reviewer); $namaPicAdmin = json_decode($kjpp->nama_pic_admin); $nomorHpPicAdmin = json_decode($kjpp->nomor_hp_pic_admin); $namaPicMarketing = json_decode($kjpp->nama_pic_marketing); $nomorHpPicMarketing = json_decode($kjpp->nomor_hp_pic_marketing); return view('lpj::kjpp.create', compact('kjpp', 'ijin_usaha', 'jenis_aset', 'provinces', 'cities', 'districts', 'villages', 'emailKantor', 'namaPimpinan', 'nomorHpPimpinan', 'namaPicReviewer', 'nomorHpPicReviewer', 'namaPicAdmin', 'nomorHpPicAdmin', 'namaPicMarketing', 'nomorHpPicMarketing')); } /** * 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); // Jika filenya ada default.pdf jangan dihapus if ($kjpp->attachment !== 'default.pdf') { 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'; } $validated['email_kantor'] = json_encode($validated['email_kantor']); $validated['nama_pimpinan'] = json_encode($validated['nama_pimpinan']); $validated['nomor_hp_pimpinan'] = json_encode($validated['nomor_hp_pimpinan']); $validated['nama_pic_reviewer'] = json_encode($validated['nama_pic_reviewer']); $validated['nomor_hp_pic_reviewer'] = json_encode($validated['nomor_hp_pic_reviewer']); $validated['nama_pic_admin'] = json_encode($validated['nama_pic_admin']); $validated['nomor_hp_pic_admin'] = json_encode($validated['nomor_hp_pic_admin']); $validated['nama_pic_marketing'] = json_encode($validated['nama_pic_marketing']); $validated['nomor_hp_pic_marketing'] = json_encode($validated['nomor_hp_pic_marketing']); $validated['ijin_usaha_id'] = json_encode($validated['ijin_usaha_id']); if (empty($validated['jenis_aset_id'])) { $validated['jenis_aset_id'] = json_encode([]); } else { $validated['jenis_aset_id'] = json_encode($validated['jenis_aset_id']); } // dd($validated); // 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 order by code ascending $query = KJPP::query()->orderBy('code', 'asc'); // 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'); } }