Commit 575363c3 by Alfiro Pratama

fix preg match usulan dana

parent c495e6f9
......@@ -75,7 +75,7 @@ class ProposalController extends Controller
->where('periode_id', $periode->periode_id)
->where('status', '1')
->first();
// dd($kelompok, $periode);
$cekproposal = Proposal::where('created_user', auth()->user()->id)
->where('periode_id', $periode->periode_id)
->first();
......@@ -122,6 +122,55 @@ class ProposalController extends Controller
//
$proposal = $request->except('_token');
// Normalisasi usulan_dana agar menerima berbagai format:
// - "5.000.000,00" -> 5000000
// - "5,000,000.00" -> 5000000
// - "5,000,000,00" -> 5000000
// - "5000000" -> 5000000
//
// Aturan:
// - Jika ada pemisah desimal (',' atau '.') di bagian paling kanan dan diikuti 1-2 digit,
// maka bagian setelah pemisah tersebut dibuang (anggap desimal).
// - Semua pemisah ribuan (',' atau '.') di bagian integer dibuang.
$rawUsulan = (string) ($proposal['usulan_dana'] ?? '');
$rawUsulan = trim($rawUsulan);
if ($rawUsulan === '') {
$usulanDana = 0;
} else {
// Buang spasi
$s = preg_replace('/\s+/', '', $rawUsulan);
// Cari separator paling kanan (',' atau '.')
$lastComma = strrpos($s, ',');
$lastDot = strrpos($s, '.');
$lastSepPos = -1;
$lastSepChar = null;
if ($lastComma !== false && $lastComma > $lastSepPos) {
$lastSepPos = $lastComma;
$lastSepChar = ',';
}
if ($lastDot !== false && $lastDot > $lastSepPos) {
$lastSepPos = $lastDot;
$lastSepChar = '.';
}
$intPart = $s;
if ($lastSepPos !== -1) {
$after = substr($s, $lastSepPos + 1);
// Jika setelah separator isinya 1-2 digit saja, anggap itu desimal dan buang
if ($after !== '' && preg_match('/^\d{1,2}$/', $after)) {
$intPart = substr($s, 0, $lastSepPos);
}
}
// Hapus semua pemisah ribuan yang tersisa, lalu ambil digit saja
$intPart = str_replace([',', '.'], '', $intPart);
$digits = preg_replace('/\D+/', '', $intPart);
$usulanDana = $digits === '' ? 0 : (int) $digits;
}
$this->validate($request,
[
'file' => 'required|mimes:pdf|max:5000',
......@@ -150,7 +199,7 @@ class ProposalController extends Controller
'judul' => $proposal['judul'],
'status' => '0',
'upload_dokumen' => $file_nama,
'usulan_dana' => $proposal['usulan_dana'],
'usulan_dana' => $usulanDana,
'date_upload' => now(),
'created_user' => Auth::user()->id
]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment