From dd8d4d700c5c839d7f5502fe3e61de12a4e1775a Mon Sep 17 00:00:00 2001 From: hafifierahman Date: Sun, 25 May 2025 23:01:13 +0800 Subject: [PATCH] backend validation for arrahnpay --- app/Http/Controllers/BayaranController.php | 84 ++++++++++++++++++++++ resources/views/includes/buffer.blade.php | 10 +++ resources/views/includes/home.blade.php | 11 +++ 3 files changed, 105 insertions(+) diff --git a/app/Http/Controllers/BayaranController.php b/app/Http/Controllers/BayaranController.php index 16f6803..0cb5e2a 100644 --- a/app/Http/Controllers/BayaranController.php +++ b/app/Http/Controllers/BayaranController.php @@ -86,6 +86,12 @@ class BayaranController extends Controller { ['email' => $customerEmail, 'notel' => $customerPhoneNumber, 'name' => $customerName, 'icno' => $customerIcno] = auth()->user()->toArray(); + #Validate the request + $error = $this->validateRequest($request); + if ($error) { + return redirect()->route('buffer')->with(['errorvalidate' => $error]); + } + $bayaranArrahn = array_filter($request->txtjenisbayaran, fn($x) => $x == 'arrahn'); $bayaranOthers = array_filter($request->txtjenisbayaran, fn($x) => $x != 'arrahn'); @@ -615,4 +621,82 @@ class BayaranController extends Controller return $exist_carts; } + + private function validateRequest($request) + { + $CallableHttp = $request->txtcompany == 'pkb' + ? Http::arrahn() + : Http::arrahn_koop(); + + if ( + !is_array($request->txtnorujukanhidden) || + !is_array($request->txtbayaranhidden) || + !is_array($request->txtkodcawhidden) || + count($request->txtnorujukanhidden) !== count($request->txtbayaranhidden) || + count($request->txtnorujukanhidden) !== count($request->txtkodcawhidden) + ) { + Log::error('Jumlah bayaran, norujukan dan kod cawangan tidak sepadan.', [ + 'txtnorujukanhidden' => $request->txtnorujukanhidden, + 'txtbayaranhidden' => $request->txtbayaranhidden, + 'txtkodcawhidden' => $request->txtkodcawhidden, + ]); + return 'Sila Pilih Sekali Lagi.'; + } + + foreach ($request->txtnorujukanhidden as $index => $x) { + $array_gadai = $CallableHttp->get('/api/gadai/norujukan/' . $request->txtkodcawhidden[$index] . '/' . $x)->json(); + + if (empty($array_gadai)) { + Log::error('norujukan tidak dijumpai.', [ + 'norujukan' => $x, + 'kodcaw' => $request->txtkodcawhidden[$index], + ]); + + return 'Sila Pilih Sekali lagi. Dan pastikan salah satu sahaja dipilih untuk ruangan PKB dan KOPKB.'; + } + + $array_gadai = $array_gadai[0]; + if (in_array($array_gadai['sttebus'], ['T', 'L'])) { + Log::error('Norujukan sudah ditebus.', [ + 'norujukan' => $x, + 'kodcaw' => $request->txtkodcawhidden[$index], + ]); + return 'Norujukan' . $x . ' sudah ditebus.'; + }else{ + $payment = (float) $request->txtbayaranhidden[$index]; + $remain_loan = (float) $array_gadai['bakipjm']; + $nm = (float) $array_gadai['nilaimarhun']; + $profit = 0; + + $jenisKeuntungan = $array_gadai['tagbaru'] == '1' ? 'onepercent' : 'asal'; + + $upahTawarruq = $CallableHttp + ->get('/api/upahtawarruq/' . $array_gadai['kodcaw'] . '/' . $array_gadai['norujukan'], ['jeniskeuntungan' => $jenisKeuntungan]) + ->json(); + + if ($jenisKeuntungan == 'onepercent') { + $profit = $upahTawarruq['keuntungan'] + $upahTawarruq['ujrah']; + } + + if ($jenisKeuntungan == 'asal') { + $profit = $upahTawarruq; + } + + $item = ($remain_loan - ($nm * 0.1) + $profit); + + if ($payment > $item) { + return 'Baki Pembiayaan tidak boleh kurang 10% daripada Nilai Marhun!'; + } else if ($payment < $profit && $profit !== 0.00) { + return 'Bayaran tidak boleh kurang daripada keuntungan semasa!'; + } + else if ($payment == 0.00 && $profit == 0.00) { + return 'Bayaran tidak boleh dalam nilai 0.00'; + } + + } + } + + return null; + } + } diff --git a/resources/views/includes/buffer.blade.php b/resources/views/includes/buffer.blade.php index fe6b34c..352f8ea 100644 --- a/resources/views/includes/buffer.blade.php +++ b/resources/views/includes/buffer.blade.php @@ -151,6 +151,16 @@ @enderror + @if (session('errorvalidate')) + + @endif + @if (\Session::has('error')) @enderror + @if (session('errorvalidate')) + + @endif + + @if (\Session::has('error'))