diff --git a/app/Http/Controllers/HistoryPerusahaanController.php b/app/Http/Controllers/HistoryPerusahaanController.php new file mode 100644 index 0000000..f78f46a --- /dev/null +++ b/app/Http/Controllers/HistoryPerusahaanController.php @@ -0,0 +1,138 @@ +orderBy('created_at', 'desc')->get(); + + // Eager load companies with their relationships + $perusahaan = Perusahaan::with('kelurahan.kecamatan.kabupaten')->get() + ->map(function ($company) { + $company->Kabupaten = $company->kelurahan?->kecamatan?->kabupaten?->NamaKabupaten ?? '-'; + return $company; + }); + + + return Inertia::render('admin/history_perusahaan/index_history_perusahaan', [ + 'historyPerusahaan' => $historyPerusahaan, + 'perusahaan' => $perusahaan, + 'historyKegiatan' => HistoryKegiatan::all(), + 'kabupaten' => Kabupaten::all(), + 'kecamatan' => Kecamatan::all(), + 'kelurahan' => Kelurahan::all(), + ]); + } catch (\Exception $e) { + Log::error('Error fetching data: ' . $e->getMessage()); + return back()->with('error', 'Terjadi kesalahan saat memuat data.'); + } + } + + public function store(HistoryPerusahaanRequest $request) + { + try { + DB::beginTransaction(); + + $data = $request->validated(); + + if ($request->hasFile('DokumenHistory')) { + $fileName = time() . '_' . $request->file('DokumenHistory')->getClientOriginalName(); + // Misalnya, jika data memiliki PerusahaanId, gunakan untuk membuat folder + $folder = 'files/history_perusahaan/' . $data['PerusahaanId']; + $path = $request->file('DokumenHistory')->storeAs($folder, $fileName, 'public'); + $data['DokumenHistory'] = $path; + } + + HistoryPerusahaan::create($data); + + DB::commit(); + + return redirect() + ->route('admin.history_perusahaan.detail', $data['PerusahaanId']) + ->with('success', 'History Perusahaan berhasil ditambahkan'); + } catch (\Exception $e) { + DB::rollBack(); + Log::error('Error creating History Perusahaan: ' . $e->getMessage()); + return response()->json(['message' => 'Error: ' . $e->getMessage()], 500); + } + } + + + public function detail($perusahaanId) +{ + $perusahaan = Perusahaan::with('historyPerusahaan')->findOrFail($perusahaanId); + + $historyKegiatan = HistoryKegiatan::all(); + + $historyPerusahaan = HistoryPerusahaan::with('historyKegiatan')->where('PerusahaanId', $perusahaanId)->get(); + + return Inertia::render('admin/history_perusahaan/detail_history_perusahaan', [ + 'perusahaan' => $perusahaan, + 'historyKegiatan' => $historyKegiatan, + 'historyPerusahaan' => $historyPerusahaan, + ]); +} + + + +public function update(Request $request, $historyPerusahaanId) +{ + try { + DB::beginTransaction(); + + // Validasi input secara inline + $data = $request->validate([ + 'PerusahaanId' => 'required|integer', + 'NomorHistory' => 'required|string', + 'TanggalHistory' => 'required|date', + 'HistoryKegiatanId' => 'required|integer', + 'KeteranganHistory' => 'nullable|string', + 'DokumenHistory' => 'nullable|file|mimes:pdf', + ]); + + // Cari data HistoryPerusahaan berdasarkan primary key kustom + $history = HistoryPerusahaan::findOrFail($historyPerusahaanId); + + // Jika ada file baru yang diupload, proses upload dan set path file + if ($request->hasFile('DokumenHistory')) { + $fileName = time() . '_' . $request->file('DokumenHistory')->getClientOriginalName(); + $folder = 'files/history_perusahaan/' . $data['PerusahaanId']; + $path = $request->file('DokumenHistory')->storeAs($folder, $fileName, 'public'); + $data['DokumenHistory'] = $path; + } else { + // Jika tidak ada file baru, jangan mengubah nilai DokumenHistory + unset($data['DokumenHistory']); + } + + // Update record dengan data baru + $history->update($data); + + DB::commit(); + + return redirect() + ->route('admin.history_perusahaan.detail', $data['PerusahaanId']) + ->with('success', 'History Perusahaan berhasil diperbarui'); + } catch (\Exception $e) { + DB::rollBack(); + Log::error('Error updating History Perusahaan: ' . $e->getMessage()); + return response()->json(['message' => 'Error: ' . $e->getMessage()], 500); + } +} + + +} diff --git a/app/Http/Controllers/PerusahaanController.php b/app/Http/Controllers/PerusahaanController.php index 3ddf044..bd5da51 100644 --- a/app/Http/Controllers/PerusahaanController.php +++ b/app/Http/Controllers/PerusahaanController.php @@ -24,6 +24,7 @@ class PerusahaanController extends Controller { try { $perusahaan = Perusahaan::with('jenisKegiatan', 'kelurahan.kecamatan.kabupaten', 'verifikator', 'jenisDokIL')->get(); + return Inertia::render('admin/perusahaan/index_perusahaan', [ 'perusahaan' => $perusahaan, 'jenisKegiatan' => JenisKegiatan::all(), @@ -155,15 +156,46 @@ class PerusahaanController extends Controller ]); } - public function update(PerusahaanRequest $request, Perusahaan $perusahaan): JsonResponse - { - $perusahaan->update($request->validated()); + // public function update(PerusahaanRequest $request, Perusahaan $perusahaan): JsonResponse + // { + // $perusahaan->update($request->validated()); - return response()->json([ - 'status' => 'success', - 'message' => 'Data perusahaan berhasil diperbarui', - 'data' => $perusahaan - ]); + // return response()->json([ + // 'status' => 'success', + // 'message' => 'Data perusahaan berhasil diperbarui', + // 'data' => $perusahaan + // ]); + // } + + public function update(PerusahaanRequest $request, Perusahaan $perusahaan) + { + try { + DB::beginTransaction(); + + $data = $request->validated(); + + if (!$request->hasFile('ILDokumen')) { + unset($data['ILDokumen']); + } else { + if ($perusahaan->ILDokumen && Storage::disk('public')->exists($perusahaan->ILDokumen)) { + Storage::disk('public')->delete($perusahaan->SanksiFile); + } + + $fileName = time() . '_' . $request->file('ILDokumen')->getClientOriginalName(); + $path = $request->file('ILDokumen')->storeAs('files/il', $fileName, 'public'); + $data['ILDokumen'] = $path; + } + + $perusahaan->update($data); + + DB::commit(); + + return redirect()->route('admin.perusahaan.index')->with('success', 'Perusahaan berhasil diperbarui'); + } catch (\Exception $e) { + DB::rollBack(); + Log::error('Error updating perusahaan: ' . $e->getMessage()); + return response()->json(['message' => 'Error: ' . $e->getMessage()], 500); + } } public function destroy(Perusahaan $perusahaan): JsonResponse diff --git a/app/Http/Requests/HistoryPerusahaanRequest.php b/app/Http/Requests/HistoryPerusahaanRequest.php new file mode 100644 index 0000000..8447283 --- /dev/null +++ b/app/Http/Requests/HistoryPerusahaanRequest.php @@ -0,0 +1,33 @@ +|string> + */ + public function rules(): array + { + return [ + 'PerusahaanId' => 'required|integer', + 'HistoryKegiatanId' => 'required|integer', + 'TanggalHistory' => 'required|date', + 'NomorHistory' => 'nullable|string|max:100', + 'KeteranganHistory' => 'nullable|string', + 'DokumenHistory' => 'required|file|mimes:pdf', + ]; + } +} diff --git a/app/Models/HistoryPerusahaan.php b/app/Models/HistoryPerusahaan.php new file mode 100644 index 0000000..d8e024a --- /dev/null +++ b/app/Models/HistoryPerusahaan.php @@ -0,0 +1,34 @@ +belongsTo(Perusahaan::class, 'PerusahaanId', 'PerusahaanId'); + } + + public function historyKegiatan() + { + return $this->belongsTo(HistoryKegiatan::class, 'HistoryKegiatanId', 'HistoryKegiatanId'); + } + + public function kelurahan() + { + return $this->belongsTo(Kelurahan::class, 'KelurahanId', 'KelurahanId'); + } +} diff --git a/database/migrations/2025_03_03_115324_create_history_perusahaan.php b/database/migrations/2025_03_03_115324_create_history_perusahaan.php new file mode 100644 index 0000000..08afbce --- /dev/null +++ b/database/migrations/2025_03_03_115324_create_history_perusahaan.php @@ -0,0 +1,36 @@ +id('HistoryPerusahaanId'); + $table->unsignedInteger('PerusahaanId'); + $table->unsignedInteger('HistoryKegiatanId'); + $table->date('TanggalHistory')->comment('tanggal surat'); + $table->string('NomorHistory', 100)->nullable()->comment('nomor surat'); + $table->text('KeteranganHistory')->nullable(); + $table->string('DokumenHistory', 255); + $table->timestamps(); + + $table->foreign('PerusahaanId')->references('PerusahaanId')->on('Perusahaan')->onDelete('set null'); + $table->foreign('HistoryKegiatanId')->references('HistoryKegiatanId')->on('HistoryKegiatan')->onDelete('set null'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('HistoryPerusahaan'); + } +};