|
| 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 | +} |
0 commit comments