fix(surveyor): optimasi save data inpeksi

This commit is contained in:
majid
2025-03-26 11:07:54 +07:00
parent f644002c79
commit fcbf2ba979
5 changed files with 328 additions and 158 deletions

View File

@@ -167,6 +167,23 @@ class SurveyorController extends Controller
public function store(Request $request)
{
$validatedData = $request->all();
$fotoTypes = [
'foto_gistaru',
'foto_bhumi',
'foto_argis_region',
'foto_tempat',
'foto_sentuh_tanahku',
'upload_gs'
];
// Hapus data foto dari $validatedData
foreach ($fotoTypes as $fotoType) {
if (isset($validatedData[$fotoType])) {
unset($validatedData[$fotoType]);
}
}
$result = $this->inspeksiService->storeInspeksi($validatedData, $request->input('type'), $request);
if ($result['success']) {
@@ -2028,9 +2045,9 @@ class SurveyorController extends Controller
public function dataForDatatables(Request $request)
{
if (is_null($this->user) || !$this->user->can('debitur.view')) {
// abort(403, 'Sorry! You are not allowed to view users.');
}
// if (is_null($this->user) || !$this->user->can('debitur.view')) {
// abort(403, 'Sorry! You are not allowed to view users.');
// }
$query = Permohonan::query();
$query = $query->orderBy('nomor_registrasi', 'desc');
@@ -2099,7 +2116,7 @@ class SurveyorController extends Controller
public function dataForDatatablesData(Request $request, $type)
{
if (is_null($this->user) || !$this->user->can('jenis_aset.view')) {
if (is_null(auth()->user()) || !$this->user->can('jenis_aset.view')) {
//abort(403, 'Sorry! You are not allowed to view users.');
}
@@ -2818,4 +2835,73 @@ class SurveyorController extends Controller
], 500);
}
}
public function uploadFileFoto(Request $request, $url)
{
// dd($request->all());
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))
->where('dokument_id', $request->input('dokument_id'))
->first();
$fotoTypes = [
'foto_gistaru',
'foto_bhumi',
'foto_argis_region',
'foto_tempat',
'foto_sentuh_tanahku',
'upload_gs'
];
if (!in_array($url, $fotoTypes)) {
return response()->json([
'success' => false,
'message' => 'Invalid key for upload'
], 400);
}
$existingData = $inspeksi ? json_decode($inspeksi->data_form, true) : [];
$existingData = $existingData ?? [];
$factData = $existingData;
if ($request->hasFile('file')) {
$file = $request->file('file');
$filePath = $file->store('uploads', 'public');
$uploadedPath = str_replace('public/', '', $filePath);
$factData[$url] = $uploadedPath;
$existingData = $factData;
if ($inspeksi) {
$inspeksi->data_form = json_encode($existingData);
$inspeksi->save();
} else {
Inspeksi::create([
'permohonan_id' => $request->input('permohonan_id'),
'dokument_id' => $request->input('document_id'),
'data_form' => json_encode($existingData),
]);
}
} else {
return response()->json([
'success' => false,
'message' => 'No file uploaded for the given key'
], 400);
}
return response()->json([
'success' => true,
'message' => 'Berhasil upload file foto',
'data' => [
'key' => $url,
'path' => $factData[$url] ?? null
]
]);
}
}

View File

@@ -10,44 +10,50 @@ class SaveFormInspesksiService
public function storeInspeksi(array $validatedData, string $type, Request $request)
{
try {
$processedData = $this->getActionSpecificRules($validatedData, $type, $request);
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))
->where('dokument_id', $request->input('dokument_id'))
->first();
if ($inspeksi) {
// Jika data sudah ada, merge dengan data yang baru
$existingData = json_decode($inspeksi->data_form, true) ?: [];
if (isset($existingData['signature']) && !isset($processedData['signature'])) {
$processedData['signature'] = $existingData['signature'];
}
$mergedData = $this->arrayMergeRecursive($existingData, $processedData);
// Update record
$inspeksi->update([
'data_form' => json_encode($mergedData),
'name' => $request->input('type')
]);
$responseData = $mergedData;
} else {
// Jika belum ada data, buat record baru
$inspeksi = Inspeksi::create([
$inspeksi = Inspeksi::firstOrNew(
[
'permohonan_id' => $request->input('permohonan_id'),
'dokument_id' => $request->input('dokument_id'),
'data_form' => json_encode($processedData),
'name' => $request->input('type')
]);
'dokument_id' => $request->input('dokument_id')
]
);
$responseData = $processedData;
$inspeksi->name = $request->input('type');
$processedData = $this->getActionSpecificRules($validatedData, $type, $request, $inspeksi);
// Merge data lama dengan data baru
$existingData = json_decode($inspeksi->data_form, true) ?: [];
$fotoTypes = [
'foto_gistaru',
'foto_bhumi',
'foto_argis_region',
'foto_tempat',
'foto_sentuh_tanahku',
'upload_gs'
];
foreach ($fotoTypes as $fotoType) {
if (isset($existingData[$fotoType])) {
$processedData[$fotoType] = $existingData[$fotoType];
}
}
$mergedData = $this->arrayMergeRecursive($existingData, $processedData);
if (isset($existingData['signature']) && !isset($processedData['signature'])) {
$mergedData['signature'] = $existingData['signature'];
}
$inspeksi->data_form = json_encode($mergedData);
$inspeksi->save();
return [
'success' => true,
'message' => 'Data berhasil disimpan',
];
} catch (\Exception $e) {
return [
'success' => false,
@@ -58,7 +64,7 @@ class SaveFormInspesksiService
}
private function getActionSpecificRules($data, $action, $request): array
private function getActionSpecificRules($data, $action, $request, $inspeksi): array
{
$allowedActions = [
'apartemen-kantor' => 'getUnitData',
@@ -95,7 +101,7 @@ class SaveFormInspesksiService
if (isset($allowedActions[$act])) {
$method = $allowedActions[$act];
$actionRules = $this->$method($data, $request);
$actionRules = $this->$method($data, $request, $inspeksi);
$rules = array_merge($rules, $actionRules);
// Cek apakah act memerlukan asset description rules
@@ -374,7 +380,7 @@ class SaveFormInspesksiService
}
private function getFactData($data, $request): array
private function getFactData($data, $request, $inspeksi): array
{
$factData = [
'fakta' => [
@@ -400,44 +406,16 @@ class SaveFormInspesksiService
];
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))->where('dokument_id', $request->input('dokument_id'))->first();
$fotoTypes = [
'foto_gistaru',
'foto_bhumi',
'foto_argis_region',
'foto_tempat',
'foto_sentuh_tanahku',
'upload_gs'
];
if ($inspeksi) {
$dataForm = json_decode($inspeksi->data_form, true);
foreach ($fotoTypes as $fotoType) {
// Jika ada file baru diupload
if ($request->hasFile($fotoType)) {
$factData[$fotoType] = $this->updateOrDeleteFile($dataForm, $request, $fotoType) ?: null;
} else {
$factData[$fotoType] = $dataForm[$fotoType] ?? null;
}
}
} else {
foreach ($fotoTypes as $fotoType) {
$factData[$fotoType] = $this->updateOrDeleteFile($data, $request, $fotoType) ?: null;
}
}
return $factData;
}
private function getRapData($data, $request): array
private function getRapData($data, $request, $inspeksi): array
{
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))
->where('dokument_id', $request->input('dokument_id'))
->first();
$dataForm = json_decode($inspeksi->data_form, true);
$perizinanData = isset($dataForm['perizinan']) ? $dataForm['perizinan'] : [];
@@ -966,14 +944,14 @@ class SaveFormInspesksiService
];
}
private function arrayMergeRecursive($arr1, $arr2)
private function arrayMergeRecursive(array $arr1, array $arr2): array
{
foreach ($arr2 as $key => $value) {
if ($key === 'signature' && isset($arr1['signature'])) {
// Jika key adalah signature, gabungkan secara spesifik
$arr1['signature'] = array_merge($arr1['signature'], $value);
// Gabungkan 'signature' secara spesifik
$arr1['signature'] = array_merge_recursive((array) $arr1['signature'], (array) $value);
} elseif (is_array($value) && isset($arr1[$key]) && is_array($arr1[$key])) {
// Rekursif untuk key lainnya
// Rekursif untuk elemen array
$arr1[$key] = $this->arrayMergeRecursive($arr1[$key], $value);
} else {
// Ganti nilai lama dengan nilai baru
@@ -981,12 +959,8 @@ class SaveFormInspesksiService
}
}
// Bersihkan key lama yang tidak ada di array baru
foreach ($arr1 as $key => $value) {
if (!array_key_exists($key, $arr2) && $key !== 'signature') {
unset($arr1[$key]);
}
}
// Hapus key lama yang tidak ada di array baru kecuali 'signature'
$arr1 = array_intersect_key($arr1, $arr2 + ['signature' => true]);
return $arr1;
}
@@ -1010,7 +984,7 @@ class SaveFormInspesksiService
throw new Exception("Invalid file upload for {$fileKey}");
}
} elseif (isset($data[$fileKey]) && $data[$fileKey]) {
return $data[$fileKey];
return $data[$fileKey] ?? null;
} else {
return null;
}

View File

@@ -125,7 +125,7 @@
<input id="inputGistaru" type="file" name="upload_gs"
class="file-input file-input-bordered w-full"
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="previewImage(this, 'upload-gs-preview')">
onchange="uploadFile(this, 'upload-gs-preview', 'upload_gs')">
<img id="upload-gs-preview"
src="{{ asset('storage/' . (isset($forminspeksi['upload_gs']) ? $forminspeksi['upload_gs'] : '')) }}"
@@ -146,7 +146,8 @@
<input id="inputGistaru" type="file" name="foto_sentuh_tanahku"
class="file-input file-input-bordered w-full"
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="previewImage(this, 'sentuh_tanahku-preview')">
onchange="uploadFile(this, 'sentuh_tanahku-preview', 'foto_sentuh_tanahku')"
>
<img id="sentuh_tanahku-preview"
src="{{ asset('storage/' . (isset($forminspeksi['foto_sentuh_tanahku']) ? $forminspeksi['foto_sentuh_tanahku'] : '')) }}"
@@ -169,7 +170,7 @@
<input id="inputGistaru" type="file" name="foto_gistaru"
class="file-input file-input-bordered w-full"
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="previewImage(this, 'gistaru-preview')">
onchange="uploadFile(this, 'gistaru-preview', 'foto_gistaru')">
<img id="gistaru-preview"
src="{{ asset('storage/' . (isset($forminspeksi['foto_gistaru']) ? $forminspeksi['foto_gistaru'] : '')) }}"
@@ -197,7 +198,7 @@
<input id="inputBhumi" type="file" name="foto_bhumi"
class="file-input file-input-bordered w-full "
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="previewImage(this, 'bhumi-preview')">
onchange="uploadFile(this, 'bhumi-preview', 'foto_bhumi')">
<img id="bhumi-preview"
src="{{ asset('storage/' . (isset($forminspeksi['foto_bhumi']) ? $forminspeksi['foto_bhumi'] : '')) }}"
alt="Foto Bhumi" class="mt-2 max-w-full h-auto"
@@ -210,97 +211,191 @@
</div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
<span class="form-label">Blad Tata Ruang Perdaerah</span>
</label>
<div class="input-group w-full flex gap-2">
<input class="name_rute" type="hidden" name="name_rute" value="rute">
<div class="w-full">
<input id="inputArgisRegion" type="file" name="foto_argis_region"
class="file-input file-input-bordered w-full"
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="previewImage(this, 'argis-region-preview')">
<img id="argis-region-preview"
src="{{ asset('storage/' . (isset($forminspeksi['foto_argis_region']) ? $forminspeksi['foto_argis_region'] : '')) }}"
alt="Foto Argis Region" class="mt-2 max-w-full h-auto"
style="{{ isset($forminspeksi['foto_argis_region']) ? '' : 'display: none;' }} max-width: 30rem;">
</div>
<
</div>
</div>
</div>
<!-- Upload Photo Button -->
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5" style="margin-top: 20px">
<label for="" class="form-label max-w-56 text-sm font-medium text-gray-700">Upload Peta</label>
<div class="w-full grid gap-5">
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
<span class="form-label">Blad Tata Ruang Perdaerah</span>
</label>
<div class="input-group w-full flex gap-2">
<input id="foto_tempat" type="file" name="foto_tempat"
class="file-input file-input-bordered w-full"
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="previewImage(this, 'foto_tempat-preview')">
<button type="button" id="btnCamera" class="btn btn-light" data-modal-toggle="#cameraModal">
<i class="ki-outline ki-abstract-33"></i> Camera
</button>
<input class="name_rute" type="hidden" name="name_rute" value="rute">
<div class="w-full">
<input id="inputArgisRegion" type="file" name="foto_argis_region"
class="file-input file-input-bordered w-full"
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="uploadFile(this, 'argis-region-preview', 'foto_argis_region')">
<img id="argis-region-preview"
src="{{ asset('storage/' . (isset($forminspeksi['foto_argis_region']) ? $forminspeksi['foto_argis_region'] : '')) }}"
alt="Foto Argis Region" class="mt-2 max-w-full h-auto"
style="{{ isset($forminspeksi['foto_argis_region']) ? '' : 'display: none;' }} max-width: 30rem;">
</div>
< </div>
</div>
@php
$fotoTempat = $forminspeksi['foto_tempat'] ?? null;
$fotoSrc = '';
if (is_array($fotoTempat)) {
$fotoSrc = asset('storage/' . $fotoTempat[0]);
} elseif (!empty($fotoTempat)) {
$fotoSrc = asset('storage/' . $fotoTempat);
}
@endphp
<img id="foto_tempat-preview" src="{{ $fotoSrc ?: '' }}"
alt="Foto Tempat" class="mt-2 max-w-full h-auto"
style="max-width: 30rem; {{ $fotoSrc ? '' : 'display: none;' }}">
</div>
</div>
<!-- Upload Photo Button -->
<!-- Notes Section -->
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5" style="margin-top: 20px">
<label for="" class="form-label max-w-56 text-sm font-medium text-gray-700">Upload
Peta</label>
<div class="w-full grid gap-5">
<div class="input-group w-full flex gap-2">
<input id="foto_tempat" type="file" name="foto_tempat"
class="file-input file-input-bordered w-full"
accept=".jpg,.jpeg,.png,.gif,.bmp,.tiff,.tif,.webp,.svg"
onchange="uploadFile(this, 'foto_tempat-preview', 'foto_tempat')">
<button type="button" id="btnCamera" class="btn btn-light"
data-modal-toggle="#cameraModal">
<i class="ki-outline ki-abstract-33"></i> Camera
</button>
</div>
@php
$fotoTempat = $forminspeksi['foto_tempat'] ?? null;
$fotoSrc = '';
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5" style="margin-top: 20px">
if (is_array($fotoTempat)) {
$fotoSrc = asset('storage/' . $fotoTempat[0]);
} elseif (!empty($fotoTempat)) {
$fotoSrc = asset('storage/' . $fotoTempat);
}
@endphp
<label class="form-label lg:form-label max-w-56 ">Catatan yang Perlu Diperhatikan
</label>
<div class="w-full">
<div id="keterangan-container" class="flex items-baseline flex-wrap gap-2.5 w-full">
@if (!empty($forminspeksi['fakta']['keterangan']) && is_array($forminspeksi['fakta']['keterangan']))
@foreach ($forminspeksi['fakta']['keterangan'] as $index => $item)
<img id="foto_tempat-preview" src="{{ $fotoSrc ?: '' }}" alt="Foto Tempat"
class="mt-2 max-w-full h-auto"
style="max-width: 30rem; {{ $fotoSrc ? '' : 'display: none;' }}">
</div>
</div>
<!-- Notes Section -->
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5" style="margin-top: 20px">
<label class="form-label lg:form-label max-w-56 ">Catatan yang Perlu Diperhatikan
</label>
<div class="w-full">
<div id="keterangan-container" class="flex items-baseline flex-wrap gap-2.5 w-full">
@if (!empty($forminspeksi['fakta']['keterangan']) && is_array($forminspeksi['fakta']['keterangan']))
@foreach ($forminspeksi['fakta']['keterangan'] as $index => $item)
<div class="keterangan flex items-center gap-2 mt-2 textarea-group w-full">
<textarea name="keterangan[]" class="textarea mt-2" placeholder="Masukkan catatan penting" rows="10">{{ old("keterangan.$index", $item) }}</textarea>
<button class="btn btn-danger btn-sm remove-btn" type="button"
style="display: none;">
<i class="ki-outline ki-trash"></i>
</button>
</div>
@endforeach
@else
<div class="keterangan flex items-center gap-2 mt-2 textarea-group w-full">
<textarea name="keterangan[]" class="textarea mt-2" placeholder="Masukkan catatan penting" rows="10">{{ old("keterangan.$index", $item) }}</textarea>
<textarea name="keterangan[]" class="textarea mt-2" placeholder="Masukkan catatan penting" rows="10"></textarea>
<button class="btn btn-danger btn-sm remove-btn" type="button"
style="display: none;">
<i class="ki-outline ki-trash"></i>
</button>
<em id="error-keterangan" class="alert text-danger text-sm"></em>
</div>
@endforeach
@else
<div class="keterangan flex items-center gap-2 mt-2 textarea-group w-full">
<textarea name="keterangan[]" class="textarea mt-2" placeholder="Masukkan catatan penting" rows="10"></textarea>
<button class="btn btn-danger btn-sm remove-btn" type="button" style="display: none;">
<i class="ki-outline ki-trash"></i>
</button>
<em id="error-keterangan" class="alert text-danger text-sm"></em>
</div>
@endif
@endif
</div>
<button type="button" onclick="addClonableItem('keterangan-container', 'keterangan')"
class="btn btn-primary btn-sm mt-5 ">
<i class="ki-outline ki-plus"></i>
</button>
</div>
<button type="button" onclick="addClonableItem('keterangan-container', 'keterangan')"
class="btn btn-primary btn-sm mt-5 ">
<i class="ki-outline ki-plus"></i>
</button>
</div>
</div>
</div>
</div>
@include('lpj::surveyor.components.modal-kamera')
@include('lpj::surveyor.components.modal-kamera')
@push('scripts')
@include('lpj::surveyor.js.camera-editor')
@endpush
@push('scripts')
<script stype="text/javascript">
function uploadFile(inputElement, previewElement, url) {
// Ambil file dari elemen input
const file = inputElement.files[0];
if (!file) {
Swal.fire({
icon: 'warning',
title: 'Tidak ada file yang dipilih.',
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 1500
});
return;
}
if (inputElement.files && file) {
var reader = new FileReader();
reader.onload = function(e) {
$('#' + previewElement).attr('src', e.target.result).show();
}
reader.readAsDataURL(inputElement.files[0]);
} else {
$('#' + previewElement).hide();
}
// Buat FormData untuk mengirim file
const formData = new FormData();
const dokument = "{{ request('dokument') }}";
const permohonan = "{{ $permohonan->id }}";
formData.append('file', file);
formData.append('dokument_id', dokument);
formData.append('permohonan_id', permohonan);
$.ajax({
url: '/surveyor/upload-file-foto/' + url,
type: 'POST',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
},
data: formData,
processData: false,
contentType: false,
success: function(response) {
if (response.success) {
// Tampilkan pesan sukses
Swal.fire({
icon: 'success',
title: response.message || 'File berhasil diunggah!',
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 1500
});
// Perbarui elemen preview
$(previewElement).attr('src', response.data.path).show();
$(inputElement).data('file-name', response.data.file_name);
} else {
// Tampilkan pesan kesalahan dari server
Swal.fire({
icon: 'error',
title: response.message || 'Gagal mengunggah file!',
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 1500
});
}
},
error: function(xhr, status, error) {
// Tampilkan pesan kesalahan
Swal.fire({
icon: 'error',
title: xhr.responseJSON?.message || 'Terjadi kesalahan saat mengunggah file.',
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 1500
});
console.error(`Error: ${error}`);
}
});
}
</script>
@include('lpj::surveyor.js.camera-editor')
@endpush

View File

@@ -517,6 +517,19 @@
showLoadingSwal('Mengirim data ke server...');
const form = document.querySelector('form');
const formData = new FormData(form);
const fotoFields = [
'foto_gistaru',
'foto_bhumi',
'foto_argis_region',
'foto_tempat',
'foto_sentuh_tanahku',
'upload_gs'
];
fotoFields.forEach((field) => {
formData.delete(field);
});
$.ajax({
url: '{{ route('surveyor.store') }}',
type: 'POST',

View File

@@ -599,6 +599,8 @@ Route::middleware(['auth'])->group(function () {
Route::put('store-proses-survey/{id}', [SurveyorController::class, 'storeProsesSurvey'])->name('storeProsesSurvey');
Route::post('save-edited-image/', [SurveyorController::class, 'saveEditedImage'])->name('saveEditedImage');
Route::post('upload-file-foto/{url}', [SurveyorController::class, 'uploadFileFoto'])->name('uploadFileFoto');
});
Route::name('penilai.')->prefix('penilai')->group(function () {