Menyelesaikan Menu KJPP

This commit is contained in:
2024-09-25 17:14:48 +07:00
parent 33ba88a276
commit 87a466385e
12 changed files with 360 additions and 70 deletions

View File

@@ -2,15 +2,19 @@
namespace Modules\Lpj\Http\Controllers;
use Throwable;
use Illuminate\Http\Request;
use Modules\Lpj\Models\KJPP;
use Illuminate\Http\Response;
use Modules\Lpj\Models\Branch;
use Modules\Location\Models\City;
use Modules\Lpj\Models\IjinUsaha;
use Modules\Lpj\Exports\KJPPExport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Location\Models\Village;
use Modules\Lpj\Models\JenisJaminan;
use Modules\Location\Models\District;
use Modules\Location\Models\Province;
use Illuminate\Support\Facades\Storage;
use Modules\Lpj\Http\Requests\KJPPRequest;
@@ -44,24 +48,33 @@ class KJPPController extends Controller
*/
public function store(KJPPRequest $request)
{
$validate = $request->validated();
$validated = $request->validated();
if ($validate) {
if ($validated) {
$file = $request->file('attachment');
$filename = $file ? time() . '.' . $file->getClientOriginalExtension() : 'default.pdf';
if ($file) {
$file->storeAs('uploads_pdf', $filename, 'public');
// Simpan file yang diunggah
$file->storeAs('public/uploads_pdf', $filename);
} else {
Storage::copy('/home/bagi/Downloads/default.pdf', 'public/uploads_pdf/' . $filename);
// Salin file default ke lokasi yang diinginkan
Storage::copy('public/test/default.pdf', 'public/uploads_pdf/' . $filename);
}
dd($validate);
KJPP::create($validate);
// 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', 'Ijin Usaha created successfully');
->with('success', 'KJPP created successfully');
} else {
return redirect()
->route('basicdata.kjpp.create')
->with('error', 'Validation failed');
}
}
@@ -70,7 +83,17 @@ class KJPPController extends Controller
*/
public function show($id)
{
return view('lpj::show');
$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'));
}
/**
@@ -78,15 +101,53 @@ class KJPPController extends Controller
*/
public function edit($id)
{
return view('lpj::edit');
$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(KJPP $request, $id)
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');
}
}
/**
@@ -94,7 +155,21 @@ class KJPPController extends Controller
*/
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)
@@ -161,6 +236,6 @@ class KJPPController extends Controller
public function export()
{
return Excel::download(new KJPPExport, 'currency.xlsx');
return Excel::download(new KJPPExport, 'kjpp.xlsx');
}
}