feat(dokumen): tambahkan fitur hapus detail dokumen
- Menambahkan metode `clearDetail` pada `DokumenJaminanController` untuk menghapus detail dokumen. - Menghapus file terkait saat detail dihapus. - Menambahkan tombol untuk menghapus detail pada tampilan dokumen. - Mengimplementasikan konfirmasi penghapusan menggunakan SweetAlert. - Menambahkan rute baru untuk mengakses metode `clearDetail`.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -572,4 +573,40 @@
|
||||
$combinedLegalitas = array_merge($existingLegalitas, $newLegalitas);
|
||||
return response()->json($combinedLegalitas);
|
||||
}
|
||||
|
||||
public function clearDetail(Request $request)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$detailId = $request->input('detail_id');
|
||||
$detail = DetailDokumenJaminan::findOrFail($detailId);
|
||||
|
||||
// Delete associated files
|
||||
if ($detail->dokumen_jaminan) {
|
||||
$dokumen_jaminan = is_array(json_decode($detail->dokumen_jaminan))
|
||||
? json_decode($detail->dokumen_jaminan)
|
||||
: [$detail->dokumen_jaminan];
|
||||
|
||||
foreach ($dokumen_jaminan as $dokumen) {
|
||||
if (Storage::exists($dokumen)) {
|
||||
Storage::delete($dokumen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the detail record
|
||||
$detail->delete();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Detail berhasil dihapus']);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menghapus detail: ' . $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,7 +262,16 @@
|
||||
<label class="form-label max-w-56 font-bold">
|
||||
{{ $n + 1 }}. {{ $detail->jenisLegalitasJaminan->name }}
|
||||
</label>
|
||||
<input type="hidden" name="jenis_legalitas_jaminan_id[]" value=" {{ $detail->jenis_legalitas_jaminan_id }}">
|
||||
<input type="hidden" name="jenis_legalitas_jaminan_id[]" value="{{ $detail->jenis_legalitas_jaminan_id }}">
|
||||
<button type="button" class="btn btn-danger btn-sm" onclick="clearDetail({{ $detail->id }})">
|
||||
<i class="ki-duotone ki-trash-square fs-2">
|
||||
<span class="path1"></span>
|
||||
<span class="path2"></span>
|
||||
<span class="path3"></span>
|
||||
<span class="path4"></span>
|
||||
</i>
|
||||
Reset
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
@@ -713,5 +722,75 @@
|
||||
return `<input class="input" type="text" name="custom_field[${itemId}][${fieldName}]" value="${value}">`;
|
||||
}
|
||||
}
|
||||
|
||||
function clearDetail(detailId) {
|
||||
Swal.fire({
|
||||
title: 'Apakah Anda yakin?',
|
||||
text: "Anda akan menghapus detail ini!",
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Ya, yakin!',
|
||||
cancelButtonText: 'Batal'
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
Swal.fire({
|
||||
title: 'Apakah Anda yakin?',
|
||||
text: "Data yang telah di hapus tidak dapat di kembalikan",
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Ya, hapus!',
|
||||
cancelButtonText: 'Batal'
|
||||
}).then((result2) => {
|
||||
if (result2.isConfirmed) {
|
||||
// Hapus input fields
|
||||
$(`input[name="detail_dokumen_jaminan_id[]"][value="${detailId}"]`).closest('.grid.gap-5').remove();
|
||||
|
||||
// Kirim request AJAX untuk menghapus data dari database
|
||||
$.ajax({
|
||||
url: '{{ route("debitur.jaminan.clearDetail", $debitur->id ) }}',
|
||||
type: 'POST',
|
||||
data: {
|
||||
_token: '{{ csrf_token() }}',
|
||||
detail_id: detailId
|
||||
},
|
||||
success: function (response) {
|
||||
if (response.success) {
|
||||
Swal.fire({
|
||||
title: 'Berhasil!',
|
||||
text: 'Detail berhasil dihapus',
|
||||
icon: 'success',
|
||||
confirmButtonText: 'OK'
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
location.reload();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: 'Gagal!',
|
||||
text: 'Detail gagal dihapus',
|
||||
icon: 'error',
|
||||
confirmButtonText: 'OK'
|
||||
});
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
Swal.fire({
|
||||
title: 'Gagal!',
|
||||
text: 'Terjadi kesalahan saat menghapus detail',
|
||||
icon: 'error',
|
||||
confirmButtonText: 'OK'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
@endpush
|
||||
|
||||
@@ -349,6 +349,8 @@ Route::middleware(['auth'])->group(function () {
|
||||
Route::put('{jaminan}', [DokumenJaminanController::class, 'update'])->name('update');
|
||||
Route::post('store', [DokumenJaminanController::class, 'store'])->name('store');
|
||||
Route::delete('{jaminan}', [DokumenJaminanController::class, 'destroy'])->name('destroy');
|
||||
|
||||
Route::post('clear-detail', [DokumenJaminanController::class, 'clearDetail'])->name('clearDetail');
|
||||
});
|
||||
|
||||
Route::name('pemilik.')->prefix('{id}/pemilik')->group(function () {
|
||||
|
||||
Reference in New Issue
Block a user