Skip to content

Commit 52c964f

Browse files
Add forms for Pulsa replacement process with validation and error handling
- Created `actions-choice-pulsa.blade.php` for selecting action type (confirmation or upload). - Developed `index-pulsa.blade.php` for user NIK input with error display. - Implemented `konfirmasi-pulsa.blade.php` for confirming phone number with validation. - Established `layout.blade.php` for consistent styling across forms. - Added `upload-pulsa.blade.php` for uploading proof of pulsa with detailed instructions and examples.
1 parent 5b6cdb1 commit 52c964f

33 files changed

+1798
-159
lines changed

app/Helpers/Helper.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,25 @@ public static function cekGanda($json, $key)
765765
return $cek->isNotEmpty();
766766
}
767767

768+
/**
769+
* Retrieves the pulsa limit (sbml) for a specific kegiatan based on the given jenis_pulsa_id.
770+
*
771+
* This method fetches the JenisPulsa record from cache, finds the entry with the specified ID,
772+
* and returns its 'sbml' value. If no matching record is found, it returns 0.
773+
*
774+
* @param int $jenis_pulsa_id The ID of the JenisPulsa to retrieve the limit for.
775+
* @return int The pulsa limit (sbml) for the specified kegiatan, or 0 if not found.
776+
*/
777+
public static function getLimitPulsaPerKegiatan($jenis_pulsa_id)
778+
{
779+
$limit = JenisPulsa::cache()
780+
->get('all')
781+
->where('id', $jenis_pulsa_id)
782+
->first();
783+
784+
return $limit ? $limit->sbml : 0;
785+
}
786+
768787
/**
769788
* Generate a document number.
770789
*
@@ -941,6 +960,31 @@ public static function getMitraById($id)
941960
return Mitra::cache()->get('all')->where('id', $id)->first();
942961
}
943962

963+
/**
964+
* Retrieve the ID of a Mitra by their NIK (Nomor Induk Kependudukan).
965+
*
966+
* This method fetches all Mitra records from the cache, filters them by the provided NIK,
967+
* and returns the ID of the first matching Mitra.
968+
*
969+
* @param string $nik The NIK of the Mitra to search for.
970+
* @return int|null The ID of the matching Mitra, or null if not found.
971+
*/
972+
public static function getMitraIdByNik($nik)
973+
{
974+
return optional(Mitra::cache()->get('all')->where('nik', $nik)->first())->id;
975+
}
976+
977+
/**
978+
* Get jenis pulsa based on ID.
979+
*
980+
* @param int $id The jenis pulsa ID
981+
* @return \App\Models\JenisPulsa|null
982+
*/
983+
public static function getJenisPulsaById($id)
984+
{
985+
return JenisPulsa::cache()->get('all')->where('id', $id)->first();
986+
}
987+
944988
/**
945989
* Get bank code data based on ID.
946990
*
@@ -2150,6 +2194,23 @@ public static function setOptionKepkaMitra($tahun)
21502194
return self::setOptions(KepkaMitra::cache()->get('all')->where('tahun', $tahun), 'id', 'nomor');
21512195
}
21522196

2197+
/**
2198+
* Generates options for Mitra based on the specified year.
2199+
*
2200+
* Retrieves the KepkaMitra ID for the given year, then fetches all Mitra records
2201+
* associated with that KepkaMitra ID. Returns the options array using the Mitra's
2202+
* 'id' as the key and 'nama' as the value.
2203+
*
2204+
* @param int|string $tahun The year to filter KepkaMitra records.
2205+
* @return array Options array with Mitra IDs as keys and names as values.
2206+
*/
2207+
public static function setOptionsMitra($tahun)
2208+
{
2209+
$kepkaMitraId = optional(KepkaMitra::cache()->get('all')->where('tahun', $tahun)->first())->id;
2210+
2211+
return self::setOptions(Mitra::cache()->get('all')->where('kepka_mitra_id', $kepkaMitraId), 'id', 'nama', '', 'nik');
2212+
}
2213+
21532214
/**
21542215
* Set options for Mata Anggaran based on the given DIPA ID and MAK.
21552216
*
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Helpers\Helper;
6+
use App\Models\DaftarPulsaMitra;
7+
use App\Models\Mitra;
8+
use App\Models\PulsaKegiatan;
9+
use Illuminate\Http\Request;
10+
use SweetAlert2\Laravel\Swal;
11+
12+
class PulsaController extends Controller
13+
{
14+
public function index()
15+
{
16+
$judul = PulsaKegiatan::getJudulByToken(request()->route('token'));
17+
$token = request()->route('token');
18+
19+
return view('index-pulsa', compact('judul', 'token'));
20+
}
21+
22+
public function verifikasi(Request $request)
23+
{
24+
$request->validate([
25+
'nik' => 'required|numeric|digits:16',
26+
]);
27+
28+
$token = $request->route('token');
29+
$nik = $request->input('nik');
30+
$mitraId = Helper::getMitraIdByNIK($nik);
31+
32+
$pulsaKegiatanId = PulsaKegiatan::getIdByToken($token);
33+
34+
if (DaftarPulsaMitra::where('pulsa_kegiatan_id', $pulsaKegiatanId)
35+
->where('mitra_id', $mitraId)
36+
->exists()) {
37+
session([
38+
'mitraId' => $mitraId,
39+
'pulsaKegiatanId' => $pulsaKegiatanId,
40+
]);
41+
42+
return redirect()->route('pulsa-actions', ['token' => $token]);
43+
} else {
44+
return redirect()->back()->withErrors(['NIK tidak terdaftar atau sudah diverifikasi.']);
45+
}
46+
}
47+
48+
public function actionsChoice(Request $request)
49+
{
50+
$mitra = Helper::getMitraById(session('mitraId'));
51+
$matchKegiatan = PulsaKegiatan::where('id', session('pulsaKegiatanId'))
52+
->where('token', request()->route('token'))
53+
->exists();
54+
$matchMitra = DaftarPulsaMitra::where('mitra_id', session('mitraId'))
55+
->where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
56+
->exists();
57+
if (! $mitra || ! $matchKegiatan || ! $matchMitra) {
58+
abort(404);
59+
}
60+
$judul = PulsaKegiatan::getJudulByToken(request()->route('token'));
61+
$token = request()->route('token');
62+
$nik = $mitra->nik;
63+
$nama = $mitra->nama;
64+
65+
return view('actions-choice-pulsa', compact('judul', 'token', 'nik', 'nama'));
66+
}
67+
68+
public function choice(Request $request)
69+
{
70+
$mitra = Helper::getMitraById(session('mitraId'));
71+
$matchKegiatan = PulsaKegiatan::where('id', session('pulsaKegiatanId'))
72+
->where('token', request()->route('token'))
73+
->exists();
74+
$matchMitra = DaftarPulsaMitra::where('mitra_id', session('mitraId'))
75+
->where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
76+
->exists();
77+
if (! $mitra || ! $matchKegiatan || ! $matchMitra) {
78+
abort(404);
79+
}
80+
$token = request()->route('token');
81+
if ($request->input('action-type') === 'konfirmasi') {
82+
return redirect()->route('pulsa-confirm', ['token' => $token]);
83+
}
84+
if ($request->input('action-type') === 'upload') {
85+
return redirect()->route('pulsa-upload', ['token' => $token]);
86+
}
87+
88+
}
89+
90+
public function confirm(Request $request)
91+
{
92+
$mitra = Helper::getMitraById(session('mitraId'));
93+
$matchKegiatan = PulsaKegiatan::where('id', session('pulsaKegiatanId'))
94+
->where('token', request()->route('token'))
95+
->exists();
96+
$matchMitra = DaftarPulsaMitra::where('mitra_id', session('mitraId'))
97+
->where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
98+
->exists();
99+
if (! $mitra || ! $matchKegiatan || ! $matchMitra) {
100+
abort(404);
101+
}
102+
$judul = PulsaKegiatan::getJudulByToken(request()->route('token'));
103+
$token = request()->route('token');
104+
$nik = $mitra->nik;
105+
$nama = $mitra->nama;
106+
$handphone = $mitra->no_pulsa;
107+
108+
return view('konfirmasi-pulsa', compact('judul', 'token', 'nik', 'nama', 'handphone'));
109+
110+
}
111+
112+
public function submitConfirm(Request $request)
113+
{
114+
$mitra = Helper::getMitraById(session('mitraId'));
115+
$matchKegiatan = PulsaKegiatan::where('id', session('pulsaKegiatanId'))
116+
->where('token', request()->route('token'))
117+
->exists();
118+
$matchMitra = DaftarPulsaMitra::where('mitra_id', session('mitraId'))
119+
->where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
120+
->exists();
121+
if (! $mitra || ! $matchKegiatan || ! $matchMitra) {
122+
abort(404);
123+
}
124+
$request->validate([
125+
'handphone' => 'required|regex:/^\+?[0-9]{10,15}$/',
126+
'confirm' => 'required|same:handphone',
127+
]);
128+
129+
$token = request()->route('token');
130+
$handphone = $request->input('handphone');
131+
$mitraModel = Mitra::find(session('mitraId'));
132+
$mitraModel->no_pulsa = $handphone;
133+
$updateMitra = $mitraModel->save();
134+
$updateDaftar = DaftarPulsaMitra::where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
135+
->where('mitra_id', session('mitraId'))
136+
->update(['confirmed' => true]);
137+
if ($updateMitra > 0 && $updateDaftar > 0) {
138+
Swal::success([
139+
'title' => 'Berhasil',
140+
'text' => 'Nomor handphone telah berhasil dikonfirmasi.',
141+
]);
142+
} else {
143+
Swal::error([
144+
'title' => 'Gagal',
145+
'text' => 'Gagal mengupdate nomor handphone.',
146+
]);
147+
}
148+
149+
return redirect()->route('pulsa-actions', ['token' => $token]);
150+
}
151+
152+
public function upload(Request $request)
153+
{
154+
$mitra = Helper::getMitraById(session('mitraId'));
155+
$matchKegiatan = PulsaKegiatan::where('id', session('pulsaKegiatanId'))
156+
->where('token', request()->route('token'))
157+
->exists();
158+
$matchMitra = DaftarPulsaMitra::where('mitra_id', session('mitraId'))
159+
->where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
160+
->exists();
161+
if (! $mitra || ! $matchKegiatan || ! $matchMitra) {
162+
abort(404);
163+
}
164+
$judul = PulsaKegiatan::getJudulByToken(request()->route('token'));
165+
$token = request()->route('token');
166+
$nik = $mitra->nik;
167+
$nama = $mitra->nama;
168+
$handphone = $mitra->no_pulsa;
169+
$nominal = DaftarPulsaMitra::getNominalByMitraIdAndKegiatanId(session('mitraId'), session('pulsaKegiatanId'));
170+
$uploaded = DaftarPulsaMitra::where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
171+
->where('mitra_id', session('mitraId'))
172+
->whereNotNull('file')
173+
->exists();
174+
175+
return view('upload-pulsa', compact('judul', 'token', 'nik', 'nama', 'handphone', 'nominal', 'uploaded'));
176+
177+
}
178+
179+
public function submitUpload(Request $request)
180+
{
181+
$mitra = Helper::getMitraById(session('mitraId'));
182+
$matchKegiatan = PulsaKegiatan::where('id', session('pulsaKegiatanId'))
183+
->where('token', request()->route('token'))
184+
->exists();
185+
$matchMitra = DaftarPulsaMitra::where('mitra_id', session('mitraId'))
186+
->where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
187+
->exists();
188+
if (! $mitra || ! $matchKegiatan || ! $matchMitra) {
189+
abort(404);
190+
}
191+
$request->validate([
192+
'attachment' => 'required|image|max:10240',
193+
]);
194+
$attachment = $request->file('attachment')->storeAs(
195+
date('Y'),
196+
session('pulsaKegiatanId').'-'.session('mitraId').'.'.$request->file('attachment')->getClientOriginalExtension(),
197+
'pulsa'
198+
);
199+
200+
$updateDaftar = DaftarPulsaMitra::where('pulsa_kegiatan_id', session('pulsaKegiatanId'))
201+
->where('mitra_id', session('mitraId'))
202+
->update(['file' => $attachment]);
203+
204+
if ($attachment && $updateDaftar > 0) {
205+
Swal::success([
206+
'title' => 'Berhasil',
207+
'text' => 'Lampiran berhasil diunggah.',
208+
]);
209+
} else {
210+
Swal::error([
211+
'title' => 'Gagal',
212+
'text' => 'Gagal mengunggah lampiran.',
213+
]);
214+
}
215+
216+
return redirect()->route('pulsa-actions', ['token' => request()->route('token')]);
217+
}
218+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use App\Models\PulsaKegiatan;
6+
use Closure;
7+
use Illuminate\Http\Request;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class ValidatePulsaToken
11+
{
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
16+
*/
17+
public function handle(Request $request, Closure $next): Response
18+
{
19+
$token = $request->route('token');
20+
21+
if (! $token || ! PulsaKegiatan::where('token', $token)->where('status', 'open')->exists()) {
22+
return abort(404);
23+
}
24+
25+
return $next($request);
26+
}
27+
}

app/Models/DaftarPulsaMitra.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
8+
class DaftarPulsaMitra extends Model
9+
{
10+
protected $fillable = ['mitra_id', 'pulsa_kegiatan_id'];
11+
12+
public function pulsaKegiatan(): BelongsTo
13+
{
14+
return $this->belongsTo(PulsaKegiatan::class, 'pulsa_kegiatan_id');
15+
}
16+
17+
public function mitra(): BelongsTo
18+
{
19+
return $this->belongsTo(Mitra::class);
20+
}
21+
22+
protected static function booted(): void
23+
{
24+
static::creating(function (DaftarPulsaMitra $pulsa) {
25+
$pulsa->confirmed = false;
26+
});
27+
}
28+
29+
public static function getNominalByMitraIdAndKegiatanId(int $mitraId, int $kegiatanId): ?int
30+
{
31+
return self::where('mitra_id', $mitraId)
32+
->where('pulsa_kegiatan_id', $kegiatanId)
33+
->value('nominal');
34+
}
35+
}

app/Models/Mitra.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public function daftarHonorMitra(): HasMany
3535
return $this->hasMany(DaftarHonorMitra::class);
3636
}
3737

38-
public function pulsaKegiatan(): HasMany
38+
public function DaftarPulsaMitra(): HasMany
3939
{
40-
return $this->hasMany(PulsaKegiatan::class);
40+
return $this->hasMany(DaftarPulsaMitra::class);
4141
}
4242
}

0 commit comments

Comments
 (0)