Refactor legalitas jaminan functionality
Updated route to point to new getLegalitasJaminan method in DokumenJaminanController. Enhanced logic to handle additional parameters and provide more contextual data, including existing document details. Refined Blade template to dynamically load legalitas jaminan fields, ensuring better user experience and maintainability.
This commit is contained in:
@@ -18,6 +18,8 @@
|
||||
use Modules\Lpj\Models\JenisJaminan;
|
||||
use Modules\Lpj\Models\JenisLegalitasJaminan;
|
||||
use Modules\Lpj\Models\PemilikJaminan;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
use ZipArchive;
|
||||
|
||||
class DokumenJaminanController extends Controller
|
||||
@@ -402,7 +404,7 @@
|
||||
return response()->download(storage_path('app/public/' . $document->dokumen_jaminan));
|
||||
}
|
||||
|
||||
public function legalitasJaminan($id, $jaminan)
|
||||
public function legalitasJaminan($id)
|
||||
{
|
||||
$jenisJaminan = JenisJaminan::find($id);
|
||||
$legalitasJaminan = $jenisJaminan->jenis_legalitas_jaminan_id;
|
||||
@@ -410,4 +412,60 @@
|
||||
$legalitas = JenisLegalitasJaminan::whereIn('code', json_decode($legalitasJaminan, true))->get();
|
||||
echo json_encode($legalitas);
|
||||
}
|
||||
|
||||
|
||||
public function getLegalitasJaminan($id = 10, $jenisJaminanId = 1) : JsonResponse
|
||||
{
|
||||
|
||||
$jenisJaminan = JenisJaminan::findOrFail($jenisJaminanId);
|
||||
$legalitasJaminan = $jenisJaminan->jenis_legalitas_jaminan_id;
|
||||
$newLegalitasJaminan = JenisLegalitasJaminan::whereIn('code', json_decode($legalitasJaminan, true))->get();
|
||||
|
||||
$existingLegalitas = [];
|
||||
$newLegalitas = [];
|
||||
|
||||
// Create a set of new jenis_legalitas_jaminan_ids for quick lookup
|
||||
$newLegalitasIds = $newLegalitasJaminan->pluck('id')->toArray();
|
||||
|
||||
if($id>0) {
|
||||
$document = DokumenJaminan::findOrFail($id);
|
||||
if ($document && $document->detail) {
|
||||
foreach ($document->detail as $detail) {
|
||||
// Only include existing legalitas if its id is in the new set
|
||||
if (in_array($detail->jenis_legalitas_jaminan_id, $newLegalitasIds)) {
|
||||
$existingLegalitas[] = [
|
||||
'id' => $detail->id,
|
||||
'jenis_legalitas_jaminan_id' => $detail->jenis_legalitas_jaminan_id,
|
||||
'name' => $detail->name,
|
||||
'dokumen_jaminan' => $detail->dokumen_jaminan,
|
||||
'custom_field' => $detail->jenisLegalitasJaminan->custom_field,
|
||||
'custom_field_type' => $detail->jenisLegalitasJaminan->custom_field_type,
|
||||
'details' => $detail->details,
|
||||
'keterangan' => $detail->keterangan,
|
||||
'is_existing' => true
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($newLegalitasJaminan as $legalitas) {
|
||||
if (!Collection::make($existingLegalitas)->contains('jenis_legalitas_jaminan_id', $legalitas->id)) {
|
||||
$newLegalitas[] = [
|
||||
'id' => null,
|
||||
'jenis_legalitas_jaminan_id' => $legalitas->id,
|
||||
'name' => $legalitas->name,
|
||||
'dokumen_jaminan' => null,
|
||||
'custom_field' => $legalitas->custom_field,
|
||||
'custom_field_type' => $legalitas->custom_field_type,
|
||||
'details' => null,
|
||||
'keterangan' => null,
|
||||
'is_existing' => false
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$combinedLegalitas = array_merge($existingLegalitas, $newLegalitas);
|
||||
return response()->json($combinedLegalitas);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="doctainer" class="grid gap-5">
|
||||
@if(isset($document->id))
|
||||
@php $n = 0; @endphp
|
||||
@foreach($document->detail as $detail)
|
||||
@@ -266,8 +266,7 @@
|
||||
</div>
|
||||
@php $n++; @endphp
|
||||
@endforeach
|
||||
@else
|
||||
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
@@ -445,11 +444,14 @@
|
||||
<script>
|
||||
function getLegalitasJaminan() {
|
||||
var legalitasJaminan = document.getElementById("jenis_jaminan_id").value;
|
||||
var url = "/basic-data/jenis-jaminan/legalitas/" + legalitasJaminan;
|
||||
var documentId = "{{ $document->id ?? "0" }}"; // Assuming you have access to the debitur ID in your Blade template
|
||||
var debiturId = "{{ $debitur->id }}";
|
||||
var url = `/basic-data/jenis-jaminan/legalitas/${documentId}/${legalitasJaminan}`;
|
||||
fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
"X-Requested-With": "XMLHttpRequest"
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
@@ -467,7 +469,8 @@
|
||||
<label class="form-label max-w-56 font-bold">
|
||||
${index + 1}. ${item.name}
|
||||
</label>
|
||||
<input type="hidden" name="jenis_legalitas_jaminan_id[]" value="${item.id}">
|
||||
<input type="hidden" name="jenis_legalitas_jaminan_id[]" value="${item.jenis_legalitas_jaminan_id}">
|
||||
${item.is_existing ? `<input type="hidden" name="detail_dokumen_jaminan_id[]" value="${item.id}">` : ''}
|
||||
</div>
|
||||
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
@@ -475,7 +478,7 @@
|
||||
Nama Dokumen
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<input class="input " type="text" id="name" name="name[]" value="" placeholder="Nama Dokumen">
|
||||
<input class="input" type="text" name="name[]" value="${item.name || ''}" placeholder="Nama Dokumen">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -485,44 +488,54 @@
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<input class="file-input" type="file" name="dokumen_jaminan[]" value="">
|
||||
${item.dokumen_jaminan ? `
|
||||
<a href="/debitur/${debiturId}/jaminan/download?dokumen=${item.id}" class="badge badge-sm badge-outline mt-2">
|
||||
${item.dokumen_jaminan.split('/').pop()}
|
||||
<i class="ki-filled ki-cloud-download"></i>
|
||||
</a>
|
||||
` : ''}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
${item.custom_field && item.custom_field.length > 0 ? `
|
||||
${item.custom_field ? `
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56 capitalize">
|
||||
${item.custom_field.replace(/_/g, " ")}
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
${item.custom_field_type === "text" ? `
|
||||
<input class="input" type="text" name="custom_field[][${item.custom_field}]">
|
||||
` : item.custom_field_type === "number" ? `
|
||||
<input class="input" type="number" name="custom_field[][${item.custom_field}]">
|
||||
` : item.custom_field_type === "date" ? `
|
||||
<input class="input" type="date" name="custom_field[][${item.custom_field}]">
|
||||
` : item.custom_field_type === "textarea" ? `
|
||||
<textarea class="textarea" rows="3" name="custom_field[][${item.custom_field}]"></textarea>
|
||||
` : `
|
||||
<input class="input" type="text" name="custom_field[][${item.custom_field}]">
|
||||
`}
|
||||
${getCustomFieldInput(item.custom_field_type, item.custom_field, item.details)}
|
||||
</div>
|
||||
</div>
|
||||
` : ""}
|
||||
` : ''}
|
||||
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56">
|
||||
Keterangan
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<textarea class="textarea" rows="3" type="number" name="keterangan[]"></textarea>
|
||||
<textarea class="textarea" rows="3" name="keterangan[]">${item.keterangan || ''}</textarea>
|
||||
</div>
|
||||
</div>`;
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Error:", error);
|
||||
// Handle the error here
|
||||
});
|
||||
.catch(error => console.error('Error:', error));
|
||||
}
|
||||
|
||||
function getCustomFieldInput(type, fieldName, value) {
|
||||
value = value ? JSON.parse(value)[fieldName] || '' : '';
|
||||
switch (type) {
|
||||
case "text":
|
||||
return `<input class="input" type="text" name="custom_field[][${fieldName}]" value="${value}">`;
|
||||
case "number":
|
||||
return `<input class="input" type="number" name="custom_field[][${fieldName}]" value="${value}">`;
|
||||
case "date":
|
||||
return `<input class="input" type="date" name="custom_field[][${fieldName}]" value="${value}">`;
|
||||
case "textarea":
|
||||
return `<textarea class="textarea" rows="3" name="custom_field[][${fieldName}]">${value}</textarea>`;
|
||||
default:
|
||||
return `<input class="input" type="text" name="custom_field[][${fieldName}]" value="${value}">`;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@endpush
|
||||
|
||||
@@ -66,7 +66,7 @@ Route::middleware(['auth'])->group(function () {
|
||||
Route::resource('jenis-legalitas-jaminan', JenisLegalitasJaminanController::class);
|
||||
|
||||
Route::name('jenis-jaminan.')->prefix('jenis-jaminan')->group(function () {
|
||||
Route::get('legalitas/{id}', [JenisJaminanController::class, 'legalitasJaminan'])->name('legalitas');
|
||||
Route::get('legalitas/{id}/{jenisJaminanId}', [DokumenJaminanController::class, 'getLegalitasJaminan'])->name('legalitas');
|
||||
Route::get('restore/{id}', [JenisJaminanController::class, 'restore'])->name('restore');
|
||||
Route::get('datatables', [JenisJaminanController::class, 'dataForDatatables'])->name('datatables');
|
||||
Route::get('export', [JenisJaminanController::class, 'export'])->name('export');
|
||||
|
||||
Reference in New Issue
Block a user