diff --git a/app/Http/Controllers/DokumenJaminanController.php b/app/Http/Controllers/DokumenJaminanController.php index b552df1..d7bd597 100644 --- a/app/Http/Controllers/DokumenJaminanController.php +++ b/app/Http/Controllers/DokumenJaminanController.php @@ -104,7 +104,7 @@ $file_name, ); $dokumenJaminan[] = 'jaminan/' . $debitur->id . '/' . $document->id . '/' . $file_name; - $dokumenNomor[] = $request->dokumen_nomor[$key][$index] ?? ''; + $dokumenNomor[] = $request->dokumen_nomor[$key][$index] ?? '-'; } } } @@ -249,7 +249,7 @@ 'jenis_legalitas_jaminan_id' => $value, 'name' => $request->name[$key], 'keterangan' => $request->keterangan[$key], - 'details' => isset($request->custom_field[$value]) ? json_encode($request->custom_field[$value]) : '' + 'details' => isset($request->custom_field[$key]) ? json_encode($request->custom_field[$key]) : '' ]; $dokumenJaminan = []; @@ -257,60 +257,75 @@ if (isset($request->dokumen_jaminan[$key]) && is_array($request->dokumen_jaminan[$key])) { foreach ($request->dokumen_jaminan[$key] as $index => $file) { - if ($file) { + if ($file instanceof \Illuminate\Http\UploadedFile) { + // Jika file baru diupload $file_name = $file->getClientOriginalName(); $file->storeAs( 'public/jaminan/' . $debitur->id . '/' . $document->id . '/', $file_name, ); $dokumenJaminan[] = 'jaminan/' . $debitur->id . '/' . $document->id . '/' . $file_name; - $dokumenNomor[] = $request->dokumen_nomor[$key][$index] ?? ''; + } elseif (is_string($file) && !empty($file)) { + // Jika file tidak diubah, gunakan path yang sudah ada + $dokumenJaminan[] = $file; + } else { + // Jika file kosong atau null, tambahkan placeholder atau skip + $dokumenJaminan[] = null; // atau skip dengan continue; } + + // Selalu update dokumen_nomor, baik file diubah atau tidak + $dokumenNomor[] = $request->dokumen_nomor[$key][$index] ?? '-'; } } + + + // Setelah loop, periksa apakah ada dokumen yang dihapus + $existingDetail = $existingDetails->get($request->detail_dokumen_jaminan_id[$key] ?? null); + + if ($existingDetail) { + $existingDokumen = json_decode($existingDetail->dokumen_jaminan, true) ?? []; + $existingNomor = json_decode($existingDetail->dokumen_nomor, true) ?? []; + + + + // Jika jumlah dokumen berkurang, berarti ada yang dihapus + if (count($existingDokumen) > count($dokumenJaminan)) { + $dokumenJaminan = $existingDokumen; + $dokumenNomor = $existingNomor; + foreach ($request->dokumen_jaminan[$key] as $index => $file) { + if ($file === null) { + // Hapus dokumen yang tidak ada lagi + unset($dokumenJaminan[$index]); + unset($dokumenNomor[$index]); + } elseif (is_string($file) && !empty($file)) { + // Update nomor dokumen untuk file yang tidak diubah + $dokumenNomor[$index] = $request->dokumen_nomor[$key][$index] ?? '-'; + } + } + // Reset array keys + $dokumenJaminan = array_values($dokumenJaminan); + $dokumenNomor = array_values($dokumenNomor); + } + } + + if (!empty($dokumenJaminan)) { $detailData['dokumen_jaminan'] = json_encode($dokumenJaminan); - $detailData['dokumen_nomor'] = json_encode($dokumenNomor); + $detailData['dokumen_nomor'] = json_encode($dokumenNomor); } if (isset($request->detail_dokumen_jaminan_id[$key])) { $detailId = $request->detail_dokumen_jaminan_id[$key]; $detailDocument = $existingDetails->get($detailId); - if ($detailDocument) { - // Merge new files with existing ones - if (!empty($dokumenJaminan)) { - $existingFiles = json_decode( - $detailDocument->dokumen_jaminan, - true, - ) ?: []; - $existingNomor = json_decode( - $detailDocument->dokumen_nomor, - true, - ) ?: []; - $mergedFiles = array_merge($existingFiles, $dokumenJaminan); - $mergedNomor = array_merge($existingNomor, $dokumenNomor); - $detailData['dokumen_jaminan'] = json_encode($mergedFiles); - $detailData['dokumen_nomor'] = json_encode($mergedNomor); - } - $detailDocument->update($detailData); - $existingDetails->forget($detailId); - } + $detailDocument->update($detailData); + } else { DetailDokumenJaminan::create($detailData); } } } - // Delete any remaining existing details that weren't updated - foreach ($existingDetails as $detail) { - $files = json_decode($detail->dokumen_jaminan, true) ?: []; - foreach ($files as $file) { - Storage::delete('public/' . $file); - } - $detail->delete(); - } - DB::commit(); return redirect()->route('debitur.jaminan.index', $id)->with( 'success', @@ -342,6 +357,7 @@ $jenisLegalitasJaminan = JenisLegalitasJaminan::all(); $_jenisJaminan = JenisJaminan::find($document->jenis_jaminan_id); + $legalitas = ''; if ($_jenisJaminan) { $legalitasJaminan = json_decode($_jenisJaminan->jenis_legalitas_jaminan_id, true); diff --git a/resources/views/component/detail-jaminan.blade.php b/resources/views/component/detail-jaminan.blade.php index 3c5b231..fd74707 100644 --- a/resources/views/component/detail-jaminan.blade.php +++ b/resources/views/component/detail-jaminan.blade.php @@ -272,73 +272,73 @@ @php $document = $dokumen; @endphp - @foreach($document->detail as $detail) - - - - - @if(isset($detail->dokumen_jaminan)) - @php - $dokumen_jaminan = is_array(json_decode($detail->dokumen_jaminan)) ? json_decode($detail->dokumen_jaminan) : [$detail->dokumen_jaminan]; - $dokumen_nomor = is_array(json_decode($detail->dokumen_nomor)) ? json_decode($detail->dokumen_nomor) : ($detail->dokumen_nomor ? [$detail->dokumen_nomor] : []); - @endphp - @foreach($dokumen_jaminan as $index => $dokumen) + @foreach($document->detail as $detail) - - - @if(isset($detail->details) && json_decode($detail->details)[$index]) - @foreach (json_decode($detail->details)[$index] as $key => $value) + @if(isset($detail->dokumen_jaminan)) + @php + $dokumen_jaminan = is_array(json_decode($detail->dokumen_jaminan)) ? json_decode($detail->dokumen_jaminan) : [$detail->dokumen_jaminan]; + $dokumen_nomor = is_array(json_decode($detail->dokumen_nomor)) ? json_decode($detail->dokumen_nomor) : ($detail->dokumen_nomor ? [$detail->dokumen_nomor] : []); + @endphp + @foreach($dokumen_jaminan as $index => $dokumen) + + @if(isset($detail->details) && isset(json_decode($detail->details)[$index])) + @foreach (json_decode($detail->details)[$index] as $key => $value) + + + + + @endforeach + @endif @endforeach @endif - @endforeach - @endif - - - - - @endforeach + + + + + @endforeach
- - {{ $loop->index+1 }}. {{ $detail->jenisLegalitasJaminan->name }} - -
- - {{ $loop->index+1 }}. Nomor : {{ $dokumen_nomor[$index] }} - - - @if(in_array(Auth::user()->roles[0]->name,['administrator','pemohon-eo'])) - - {{ basename($dokumen) }} - - - @endif - - Preview - + + + {{ $loop->index+1 }}. {{ $detail->jenisLegalitasJaminan->name }} +
- - {{ str_replace("_"," ",$key) ?? "" }} + {{ $loop->index+1 }}. Nomor : {{ $dokumen_nomor[$index] }} -

- {{ $value }} -

+ @if(in_array(Auth::user()->roles[0]->name,['administrator','pemohon-eo'])) + + {{ basename($dokumen) }} + + + @endif + + Preview +
+ + - {{ str_replace("_"," ",$key) ?? "" }} + + +

+ {{ $value }} +

+
- - - keterangan - - -

- {{ $detail->keterangan }} -

-
+ + - keterangan + + +

+ {{ $detail->keterangan }} +

+
diff --git a/resources/views/debitur/components/dokumen.blade.php b/resources/views/debitur/components/dokumen.blade.php index bb5d62a..91a9e03 100644 --- a/resources/views/debitur/components/dokumen.blade.php +++ b/resources/views/debitur/components/dokumen.blade.php @@ -255,7 +255,7 @@
@if(isset($document->id)) - @php $n = 0; @endphp + @php $n = 0; $p_index = 0; @endphp @foreach($document->detail as $detail)
@@ -282,129 +282,177 @@
-
+
-
-
- - - -
-
-
- @if(isset($detail->dokumen_jaminan)) - @if(isset($detail->dokumen_jaminan)) - @php - $dokumen_jaminan = is_array(json_decode($detail->dokumen_jaminan)) ? json_decode($detail->dokumen_jaminan) : [$detail->dokumen_jaminan]; - $dokumen_nomor = is_array(json_decode($detail->dokumen_nomor)) ? json_decode($detail->dokumen_nomor) : ($detail->dokumen_nomor ? [$detail->dokumen_nomor] : []); - @endphp -
- @foreach($dokumen_jaminan as $index => $dokumen) -
- @if(!empty($dokumen_nomor)) - Nomor Dokumen : {{ $dokumen_nomor[$index] }} + @php + $dokumen_jaminan = is_array(json_decode($detail->dokumen_jaminan)) ? json_decode($detail->dokumen_jaminan) : [$detail->dokumen_jaminan]; + $dokumen_nomor = is_array(json_decode($detail->dokumen_nomor)) ? json_decode($detail->dokumen_nomor) : ($detail->dokumen_nomor ? [$detail->dokumen_nomor] : []); + @endphp +
+ @foreach($dokumen_jaminan as $index => $dokumen) +
+
+ + + + + + {{ basename($dokumen) }} + + + + +
+ + @if($detail->details) + @if($detail->jenisLegalitasJaminan->custom_fields) + @foreach($detail->jenisLegalitasJaminan->custom_fields as $key) +
+ @php + $customField = getCustomField($key); + $fieldValue = json_decode($detail->details)[$index]->{$customField->name} ?? ''; + @endphp + +
+ @switch($customField->type) + @case('text') + + @break + @case('number') + + @break + @case('date') + + @break + @case('textarea') + + @break + @case('select') + + @break + @default + + @endswitch +
+
+ @endforeach @endif - - {{ basename($dokumen) }} - - + @else + @if($detail->jenisLegalitasJaminan->custom_fields) + @foreach($detail->jenisLegalitasJaminan->custom_fields as $key) +
+ @php + $customField = getCustomField($key); + @endphp + +
+ @switch($customField->type) + @case('text') + + @break + @case('number') + + @break + @case('date') + + @break + @case('textarea') + + @break + @case('select') + + @break + @default + + @endswitch +
+
+ @endforeach + @endif + @endif +
+ @php $p_index++; @endphp + @endforeach +
+
+ +
+ @else +
+
+ + + + +
+ @if($detail->jenisLegalitasJaminan->custom_fields) + @foreach($detail->jenisLegalitasJaminan->custom_fields as $key) +
+ @php + $customField = getCustomField($key); + @endphp + +
+ @switch($customField->type) + @case('text') + + @break + @case('number') + + @break + @case('date') + + @break + @case('textarea') + + @break + @case('select') + + @break + @default + + @endswitch +
@endforeach -
- @endif + @endif +
+
+ +
+ @php $p_index++; @endphp @endif
-
- @if($detail->details) - @if($detail->jenisLegalitasJaminan->custom_fields) - @foreach($detail->jenisLegalitasJaminan->custom_fields as $key) -
- @php - $customField = getCustomField($key); - $fieldValue = json_decode($detail->details)->{$customField->name} ?? ''; - @endphp - -
- @switch($customField->type) - @case('text') - - @break - @case('number') - - @break - @case('date') - - @break - @case('textarea') - - @break - @case('select') - - @break - @default - - @endswitch -
-
- @endforeach - @endif - @else - @if($detail->jenisLegalitasJaminan->custom_fields) - @foreach($detail->jenisLegalitasJaminan->custom_fields as $key) -
- @php - $customField = getCustomField($key); - @endphp - -
- @switch($customField->type) - @case('text') - - @break - @case('number') - - @break - @case('date') - - @break - @case('textarea') - - @break - @case('select') - - @break - @default - - @endswitch -
-
- @endforeach - @endif - @endif -
@php $n++; @endphp @endforeach + @if($legalitas) @foreach($legalitas as $item) -
- - -
- -
- -
- -
-
- -
- -
-
-
- - - -
-
-
-
-
- - @if($item->custom_fields) - @foreach($item->custom_fields as $field) - @php $custom_field = getCustomField($field) @endphp +
+
-
+ +
+
- @switch($custom_field->type) - @case('text') - - @break - @case('number') - - @break - @case('date') - - @break - @case('textarea') - - @break - @case('select') - - @break - @default - - @endswitch +
- @endforeach - @endif -
- -
- +
+ +
+
+
+ + +
+ + @if($item->custom_fields) + @foreach($item->custom_fields as $field) + @php $custom_field = getCustomField($field) @endphp +
+ +
+ @switch($custom_field->type) + @case('text') + + @break + @case('number') + + @break + @case('date') + + @break + @case('textarea') + + @break + @case('select') + + @break + @default + + @endswitch +
+
+ @endforeach + @endif +
+
+
+
+
+ +
+ +
+ +
+ +
- @php $n++; @endphp + @php $n++;$p_index++; @endphp @endforeach @endif + @endif
@@ -608,30 +663,31 @@ -
+
${item.dokumen_jaminan ? renderExistingFiles(item.dokumen_jaminan, debiturId, item.id, item.dokumen_nomor) : ""} -
-
+ +
- +
+ ${item.custom_fields && item.custom_fields.length > 0 ? item.custom_fields.map(field => ` +
+ +
+ ${getCustomFieldInput(index, field.type, field.name, item.details, item.jenis_legalitas_jaminan_id, 0)} +
+
+ `).join('') : ""}
- ${item.custom_fields && item.custom_fields.length > 0 ? item.custom_fields.map(field => ` -
- -
- ${getCustomFieldInput(index, field.type, field.name, item.details, item.jenis_legalitas_jaminan_id, 0)} -
-
- `).join('') : ""} - -
+
+ +
@@ -648,14 +704,23 @@ .catch(error => console.error("Error:", error)); } - function addFileInput(index) { + function addFileInput(index, pindex = null) { const documentContainer = document.getElementById(`document-container-${index}`); - const customFields = documentContainer.querySelectorAll('.custom-field'); - const container = documentContainer.querySelector('#document_container'); + let container = null; + const parentContainer = documentContainer.querySelector('#document_container'); + if (pindex !== null) { + + container = parentContainer.querySelector(`#document_container_${pindex}`); + } else { + container = documentContainer.querySelector('#document_container'); + } + + const customFields = container.querySelectorAll('.custom-field'); + // Get the current number of custom field sets const currentFieldSets = container.querySelectorAll('.custom-field-set').length; - const newFieldIndex = currentFieldSets+1; + const newFieldIndex = currentFieldSets + 1; const newInput = document.createElement("div"); newInput.className = "flex flex-col w-full gap-2 mb-4 custom-field-set"; @@ -677,23 +742,22 @@ newInput.innerHTML = `
-
-   -
- +
- ${customFieldsHtml} + ${customFieldsHtml} + `; - container.appendChild(newInput); + parentContainer.appendChild(newInput); } function removeFileInput(button) { - button.closest(".flex.flex-col.w-full.gap-2.mb-4").remove(); + button.closest(".custom-field-set").remove(); + } function renderExistingFiles(dokumenJaminan, debiturId, itemId, dokumenNomor) { diff --git a/resources/views/debitur/components/jaminan.blade.php b/resources/views/debitur/components/jaminan.blade.php index 28ff127..1af9f4b 100644 --- a/resources/views/debitur/components/jaminan.blade.php +++ b/resources/views/debitur/components/jaminan.blade.php @@ -110,7 +110,7 @@
@if(isset($detail->details)) - @if(json_decode($detail->details)[$index]) + @if(isset(json_decode($detail->details)[$index])) @foreach (json_decode($detail->details)[$index] as $key => $value)