From a5884d0f8c2b7112997e438b12859faf1ba1f67c Mon Sep 17 00:00:00 2001 From: SyaharaniSalsabilla Date: Wed, 22 Jul 2026 15:22:56 +0700 Subject: [PATCH] solve akses pasar --- .../Produktivitas/AksesPasarController.php | 27 ++++++++++++- .../petani/akses-pasar/index.blade.php | 39 +++++++++++++------ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Web/Produktivitas/AksesPasarController.php b/app/Http/Controllers/Web/Produktivitas/AksesPasarController.php index 6556cfd..6c14be4 100644 --- a/app/Http/Controllers/Web/Produktivitas/AksesPasarController.php +++ b/app/Http/Controllers/Web/Produktivitas/AksesPasarController.php @@ -25,9 +25,25 @@ class AksesPasarController extends Controller $aksesPasar = AksesPasar::where('petani_id', $petaniId)->first(); $hargaJualPanen = HargaJualPanen::where('petani_id', $petaniId)->get(); + $hargaJualRows = collect([ + 'Buah segar hasil petik', + 'Biji basah', + 'Biji kering/Biji Kupas', + 'Terfermentasi (Rp/kg)', + ])->map(function (string $bentuk, int $index) use ($hargaJualPanen) { + $existingData = $hargaJualPanen->firstWhere('bentuk_produk', $bentuk); + + return [ + 'index' => $index, + 'bentuk' => $bentuk, + 'harga_panen_terakhir' => $this->currencyForDisplay($existingData?->harga_panen_terakhir), + 'harga_tahun_lalu' => $this->currencyForDisplay($existingData?->harga_tahun_lalu), + 'harga_dua_tahun_lalu' => $this->currencyForDisplay($existingData?->harga_dua_tahun_lalu), + ]; + }); return view('produktivitas.petani.akses-pasar.index', array_merge( - compact('petani', 'aksesPasar', 'hargaJualPanen'), + compact('petani', 'aksesPasar', 'hargaJualPanen', 'hargaJualRows'), $this->aksesPasarOptions() )); } @@ -251,4 +267,11 @@ class AksesPasarController extends Controller $digits = preg_replace('/\D/', '', (string) $value); return $digits === '' ? null : $digits; } -} \ No newline at end of file + + private function currencyForDisplay(mixed $value): string + { + $digits = $this->currencyDigits($value); + + return is_null($digits) ? '' : number_format((int) $digits, 0, ',', '.'); + } +} diff --git a/resources/views/produktivitas/petani/akses-pasar/index.blade.php b/resources/views/produktivitas/petani/akses-pasar/index.blade.php index 12a97a8..9894264 100644 --- a/resources/views/produktivitas/petani/akses-pasar/index.blade.php +++ b/resources/views/produktivitas/petani/akses-pasar/index.blade.php @@ -93,34 +93,30 @@ - @php($listBentukProduk = ['Buah segar hasil petik', 'Biji basah', 'Biji kering/Biji Kupas', 'Terfermentasi (Rp/kg)']) - @php($hargaJualPanen = $hargaJualPanen ?? collect()) - - @foreach ($listBentukProduk as $index => $bentuk) - @php($existingData = $hargaJualPanen->firstWhere('bentuk_produk', $bentuk)) + @foreach ($hargaJualRows as $row) - - {{ $bentuk }}: + + {{ $row['bentuk'] }}:
Rp - + /kg
Rp - + /kg
Rp - + /kg
@@ -232,6 +228,16 @@ const praktikBelumPernahValue = String(@json($praktikBelumPernahValue)); const skorPengalamanBerhentiValues = @json($skorPengalamanBerhentiValues ?? [2,3]).map(Number); + function onlyDigits(value) { + return String(value || '').replace(/\D/g, ''); + } + + function formatRupiah(value) { + const digits = onlyDigits(value); + + return digits.replace(/\B(?=(\d{3})+(?!\d))/g, '.'); + } + function syncAlasanBerhenti() { const val = Number($('input[name="skor_pengalaman"]:checked').val()); const isBerhenti = skorPengalamanBerhentiValues.includes(val); @@ -256,10 +262,21 @@ $('input[name="skor_pengalaman"]').on('change', syncAlasanBerhenti); $('input[name="saluran_terdahulu[]"]').on('change', syncSaluranLainnya); $('input[name="praktik_fermentasi"]').on('change', syncPraktikFermentasi); + $(document).on('input', '.rupiah-input', function () { + const cursorAtEnd = this.selectionStart === this.value.length; + this.value = formatRupiah(this.value); + + if (cursorAtEnd) { + this.setSelectionRange(this.value.length, this.value.length); + } + }); + $('.rupiah-input').each(function () { + this.value = formatRupiah(this.value); + }); syncAlasanBerhenti(); syncSaluranLainnya(); syncPraktikFermentasi(); }); -@endpush \ No newline at end of file +@endpush